From 36700cef9252382d139a96bfda9a9e0f8e1b5ef1 Mon Sep 17 00:00:00 2001 From: pdw <> Date: Fri, 25 Oct 2002 09:59:02 +0000 Subject: [PATCH] Added regexp based screen filtering. --- CHANGES | 8 ++++++++ Makefile | 4 ++-- options.c | 1 + options.h | 2 ++ screenfilter.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ screenfilter.h | 16 +++++++++++++++ ui.c | 39 +++++++++++++++++++++++++---------- 7 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 screenfilter.c create mode 100644 screenfilter.h diff --git a/CHANGES b/CHANGES index deb12a2..5e62d6b 100644 --- a/CHANGES +++ b/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 diff --git a/Makefile b/Makefile index 56290cb..39cbddb 100644 --- a/Makefile +++ b/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 diff --git a/options.c b/options.c index 7aaef08..922f077 100644 --- a/options.c +++ b/options.c @@ -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) { diff --git a/options.h b/options.h index b52911f..31934d2 100644 --- a/options.h +++ b/options.h @@ -49,6 +49,8 @@ typedef struct { int bandwidth_in_bytes; option_sort_t sort; + char* screenfilter; + } options_t; #endif /* __OPTIONS_H_ */ diff --git a/screenfilter.c b/screenfilter.c new file mode 100644 index 0000000..93b0465 --- /dev/null +++ b/screenfilter.c @@ -0,0 +1,56 @@ +/* + * screenfilter.c: + * + * Copyright (c) 2002 DecisionSoft Ltd. + * Paul Warren (pdw) Fri Oct 25 10:21:00 2002 + * + */ + +#include +#include +#include +#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; + } +} + diff --git a/screenfilter.h b/screenfilter.h new file mode 100644 index 0000000..c0c3192 --- /dev/null +++ b/screenfilter.h @@ -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_ */ diff --git a/ui.c b/ui.c index 8209abf..bed0f78 100644 --- a/ui.c +++ b/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;