Added regexp based screen filtering.
This commit is contained in:
8
CHANGES
8
CHANGES
@@ -1,6 +1,14 @@
|
||||
Change log for iftop
|
||||
$Id$
|
||||
|
||||
0.10
|
||||
|
||||
User selectable sort criteria
|
||||
On-the-fly filter code changes
|
||||
Shell escape
|
||||
Alternative resolver back-ends
|
||||
Improved totals display
|
||||
|
||||
0.9 22/10/02
|
||||
|
||||
Now works on FreeBSD
|
||||
|
||||
4
Makefile
4
Makefile
@@ -70,9 +70,9 @@ LDLIBS += -lpcap -lcurses -lm
|
||||
|
||||
|
||||
SRCS = iftop.c addr_hash.c hash.c ns_hash.c resolver.c ui.c util.c sorted_list.c\
|
||||
options.c serv_hash.c threadprof.c edline.c
|
||||
options.c serv_hash.c threadprof.c edline.c screenfilter.c
|
||||
HDRS = addr_hash.h hash.h iftop.h ns_hash.h resolver.h sorted_list.h ui.h options.h sll.h\
|
||||
serv_hash.h threadprof.h ether.h ip.h tcp.h
|
||||
serv_hash.h threadprof.h ether.h ip.h tcp.h screenfilter.h
|
||||
TXTS = README CHANGES INSTALL TODO iftop.8 COPYING
|
||||
SPECFILE = iftop.spec iftop.spec.in
|
||||
|
||||
|
||||
@@ -96,6 +96,7 @@ static void set_defaults() {
|
||||
options.showhelp = 0;
|
||||
options.bandwidth_in_bytes = 0;
|
||||
options.sort = OPTION_SORT_DIV2;
|
||||
options.screenfilter = NULL;
|
||||
}
|
||||
|
||||
static void die(char *msg) {
|
||||
|
||||
@@ -49,6 +49,8 @@ typedef struct {
|
||||
int bandwidth_in_bytes;
|
||||
option_sort_t sort;
|
||||
|
||||
char* screenfilter;
|
||||
|
||||
} options_t;
|
||||
|
||||
#endif /* __OPTIONS_H_ */
|
||||
|
||||
56
screenfilter.c
Normal file
56
screenfilter.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* screenfilter.c:
|
||||
*
|
||||
* Copyright (c) 2002 DecisionSoft Ltd.
|
||||
* Paul Warren (pdw) Fri Oct 25 10:21:00 2002
|
||||
*
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <regex.h>
|
||||
#include <stdio.h>
|
||||
#include "iftop.h"
|
||||
#include "options.h"
|
||||
|
||||
static const char rcsid[] = "$Id$";
|
||||
|
||||
extern options_t options ;
|
||||
|
||||
regex_t preg;
|
||||
|
||||
int screen_filter_set(char* s) {
|
||||
int r;
|
||||
|
||||
if(options.screenfilter != NULL) {
|
||||
xfree(options.screenfilter);
|
||||
options.screenfilter = NULL;
|
||||
regfree(&preg);
|
||||
}
|
||||
|
||||
r = regcomp(&preg, s, 0);
|
||||
|
||||
if(r == 0) {
|
||||
options.screenfilter = s;
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
xfree(s);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int screen_filter_match(char *s) {
|
||||
int r;
|
||||
if(options.screenfilter == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
r = regexec(&preg, s, 0, NULL, 0);
|
||||
if(r == 0) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
16
screenfilter.h
Normal file
16
screenfilter.h
Normal file
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* screenfilter.h:
|
||||
*
|
||||
* Copyright (c) 2002 DecisionSoft Ltd.
|
||||
* Paul Warren (pdw) Fri Oct 25 10:25:50 2002
|
||||
*
|
||||
* RCS: $Id$
|
||||
*/
|
||||
|
||||
#ifndef __SCREENFILTER_H_ /* include guard */
|
||||
#define __SCREENFILTER_H_
|
||||
|
||||
int screen_filter_set(char* s);
|
||||
int screen_filter_match(char* s);
|
||||
|
||||
#endif /* __SCREENFILTER_H_ */
|
||||
39
ui.c
39
ui.c
@@ -22,6 +22,7 @@
|
||||
#include "resolver.h"
|
||||
#include "sorted_list.h"
|
||||
#include "options.h"
|
||||
#include "screenfilter.h"
|
||||
|
||||
#define HOSTNAME_LENGTH 256
|
||||
|
||||
@@ -36,9 +37,9 @@
|
||||
" s - toggle show source host h - toggle this help display\n"\
|
||||
" d - toggle show destination host b - toggle bar graph display\n"\
|
||||
" f - edit filter code\n"\
|
||||
"Port display: ! - shell command\n"\
|
||||
" R - toggle service resolution q - quit\n"\
|
||||
" S - toggle show source port\n"\
|
||||
"Port display: l - set screen filter\n"\
|
||||
" R - toggle service resolution ! - shell command\n"\
|
||||
" S - toggle show source port q - quit\n"\
|
||||
" D - toggle show destination port\n"\
|
||||
" p - toggle port display\n"\
|
||||
"\n"\
|
||||
@@ -450,7 +451,7 @@ void sprint_host(char * line, struct in_addr* addr, unsigned int port, unsigned
|
||||
|
||||
void ui_print() {
|
||||
sorted_list_node* nn = NULL;
|
||||
char hostname[HOSTNAME_LENGTH];
|
||||
char host1[HOSTNAME_LENGTH], host2[HOSTNAME_LENGTH];
|
||||
static char *line;
|
||||
static int lcols;
|
||||
int y = 0;
|
||||
@@ -482,17 +483,23 @@ void ui_print() {
|
||||
|
||||
while((y < LINES - 5) && ((nn = sorted_list_next_item(&screen_list, nn)) != NULL)) {
|
||||
int x = 0, L;
|
||||
|
||||
|
||||
host_pair_line* screen_line = (host_pair_line*)nn->data;
|
||||
|
||||
if(y < LINES - 5) {
|
||||
L = (COLS - 8 * HISTORY_DIVISIONS - 4) / 2;
|
||||
if(L > sizeof hostname) {
|
||||
L = sizeof hostname;
|
||||
if(L > HOSTNAME_LENGTH) {
|
||||
L = HOSTNAME_LENGTH;
|
||||
}
|
||||
|
||||
sprint_host(line, &(screen_line->ap.src), screen_line->ap.src_port, screen_line->ap.protocol, L);
|
||||
sprint_host(host1, &(screen_line->ap.src), screen_line->ap.src_port, screen_line->ap.protocol, L);
|
||||
sprint_host(host2, &(screen_line->ap.dst), screen_line->ap.dst_port, screen_line->ap.protocol, L);
|
||||
if(!screen_filter_match(host1) && !screen_filter_match(host2)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
mvaddstr(y, x, line);
|
||||
mvaddstr(y, x, host1);
|
||||
x += L;
|
||||
|
||||
mvaddstr(y, x, " => ");
|
||||
@@ -500,9 +507,8 @@ void ui_print() {
|
||||
|
||||
x += 4;
|
||||
|
||||
sprint_host(line, &(screen_line->ap.dst), screen_line->ap.dst_port, screen_line->ap.protocol, L);
|
||||
|
||||
mvaddstr(y, x, line);
|
||||
mvaddstr(y, x, host2);
|
||||
|
||||
draw_line_totals(y, screen_line);
|
||||
|
||||
@@ -763,7 +769,7 @@ void ui_loop() {
|
||||
case 'f': {
|
||||
char *s;
|
||||
dontshowdisplay = 1;
|
||||
if ((s = edline(0, "Filter", options.filtercode))) {
|
||||
if ((s = edline(0, "Net filter", options.filtercode))) {
|
||||
char *m;
|
||||
if (!(m = set_filter_code(s))) {
|
||||
xfree(options.filtercode);
|
||||
@@ -784,6 +790,17 @@ void ui_loop() {
|
||||
dontshowdisplay = 0;
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
char *s;
|
||||
dontshowdisplay = 1;
|
||||
if ((s = edline(0, "Screen filter", options.screenfilter))) {
|
||||
if(!screen_filter_set(s)) {
|
||||
showhelp("Invalid regexp");
|
||||
}
|
||||
}
|
||||
dontshowdisplay = 0;
|
||||
break;
|
||||
}
|
||||
case '!': {
|
||||
char *s;
|
||||
dontshowdisplay = 1;
|
||||
|
||||
Reference in New Issue
Block a user