Made display changes more responsive.

This commit is contained in:
pdw
2002-04-01 21:22:20 +00:00
parent bb3619b3fc
commit 57afc62a3a
4 changed files with 70 additions and 48 deletions

2
README
View File

@@ -3,6 +3,8 @@ $Id$
Read the INSTALL file, manual page and source code for more information. Read the INSTALL file, manual page and source code for more information.
iftop must be run as root.
RedHat 7.2 users: RedHat 7.2 users:
There is a bug in the version of ncurses distibuted with RedHat 7.2 that There is a bug in the version of ncurses distibuted with RedHat 7.2 that
will cause iftop to segfault. The RPM in RedHat's Rawhide distribution will cause iftop to segfault. The RPM in RedHat's Rawhide distribution

4
TODO
View File

@@ -12,10 +12,6 @@ $Id$
* Aggregate traffic by source host / dest host rather than just by host pair. * Aggregate traffic by source host / dest host rather than just by host pair.
* Cummulative byte counters.
* Configurable sort criteria. * Configurable sort criteria.
* Configurable refresh rates. * Configurable refresh rates.
* Ability to turn bar graphs on and off.

10
iftop.c
View File

@@ -87,7 +87,7 @@ void history_rotate() {
} }
void tick() { void tick(int print) {
time_t t; time_t t;
pthread_mutex_lock(&tick_mutex); pthread_mutex_lock(&tick_mutex);
@@ -95,10 +95,14 @@ void tick() {
t = time(NULL); t = time(NULL);
if(t - last_timestamp >= RESOLUTION) { if(t - last_timestamp >= RESOLUTION) {
//printf("TICKING\n"); //printf("TICKING\n");
analyse_data();
ui_print(); ui_print();
history_rotate(); history_rotate();
last_timestamp = t; last_timestamp = t;
} }
else if(print) {
ui_print();
}
pthread_mutex_unlock(&tick_mutex); pthread_mutex_unlock(&tick_mutex);
} }
@@ -115,7 +119,7 @@ static void handle_packet(char* args, const struct pcap_pkthdr* pkthdr,const cha
int direction = 0; /* incoming */ int direction = 0; /* incoming */
eptr = (struct ether_header*)packet; eptr = (struct ether_header*)packet;
tick(); tick(0);
if(ntohs(eptr->ether_type) == ETHERTYPE_IP) { if(ntohs(eptr->ether_type) == ETHERTYPE_IP) {
struct ip* iptr; struct ip* iptr;
@@ -285,6 +289,8 @@ int main(int argc, char **argv) {
init_history(); init_history();
ui_init();
pthread_create(&thread, NULL, (void*)&packet_loop, NULL); pthread_create(&thread, NULL, (void*)&packet_loop, NULL);
ui_loop(); ui_loop();

102
ui.c
View File

@@ -41,6 +41,10 @@ extern options_t options ;
void ui_finish(); void ui_finish();
sorted_list_type screen_list;
host_pair_line totals;
int peaksent, peakrecv, peaktotal;
int screen_line_compare(void* a, void* b) { int screen_line_compare(void* a, void* b) {
int i; int i;
host_pair_line* aa = (host_pair_line*)a; host_pair_line* aa = (host_pair_line*)a;
@@ -149,43 +153,28 @@ void draw_totals(host_pair_line* totals) {
extern history_type history_totals; extern history_type history_totals;
void screen_list_init() {
void ui_print() {
hash_node_type* n = NULL;
sorted_list_node* nn = NULL;
char hostname[HOSTNAME_LENGTH];
static char *line;
static int lcols;
int peaksent = 0, peakrecv = 0, peaktotal = 0;
int y = 1;
int i;
sorted_list_type screen_list;
host_pair_line totals;
if (!line || lcols != COLS) {
xfree(line);
line = calloc(COLS + 1, 1);
}
screen_list.compare = &screen_line_compare; screen_list.compare = &screen_line_compare;
sorted_list_initialise(&screen_list); sorted_list_initialise(&screen_list);
}
clear(); void screen_data_clear() {
//erase(); sorted_list_node* nn = NULL;
move(0, 0); peaksent = peakrecv = peaktotal = 0;
attron(A_REVERSE); while((nn = sorted_list_next_item(&screen_list, nn)) != NULL) {
addstr(" Q "); free(nn->data);
attroff(A_REVERSE); }
addstr(" quit "); sorted_list_destroy(&screen_list);
attron(A_REVERSE); }
addstr(" R ");
attroff(A_REVERSE); void analyse_data() {
addstr(options.dnsresolution ? " name resolution off " hash_node_type* n = NULL;
: " name resolution on "); int i;
draw_bar_scale(&y);
memset(&totals, 0, sizeof totals); memset(&totals, 0, sizeof totals);
screen_data_clear();
while(hash_next_item(history, &n) == HASH_STATUS_OK) { while(hash_next_item(history, &n) == HASH_STATUS_OK) {
history_type* d = (history_type*)n->rec; history_type* d = (history_type*)n->rec;
host_pair_line* screen_line; host_pair_line* screen_line;
@@ -235,6 +224,36 @@ void ui_print() {
} }
} }
}
void ui_print() {
sorted_list_node* nn = NULL;
char hostname[HOSTNAME_LENGTH];
static char *line;
static int lcols;
int y = 1;
if (!line || lcols != COLS) {
xfree(line);
line = calloc(COLS + 1, 1);
}
clear();
//erase();
move(0, 0);
attron(A_REVERSE);
addstr(" Q ");
attroff(A_REVERSE);
addstr(" quit ");
attron(A_REVERSE);
addstr(" R ");
attroff(A_REVERSE);
addstr(options.dnsresolution ? " name resolution off "
: " name resolution on ");
draw_bar_scale(&y);
/* Screen layout: we have 2 * HISTORY_DIVISIONS 6-character wide history /* Screen layout: we have 2 * HISTORY_DIVISIONS 6-character wide history
* items, and so can use COLS - 12 * HISTORY_DIVISIONS to print the two * items, and so can use COLS - 12 * HISTORY_DIVISIONS to print the two
@@ -274,7 +293,6 @@ void ui_print() {
} }
y += 2; y += 2;
free(screen_line);
} }
@@ -316,14 +334,9 @@ void ui_print() {
refresh(); refresh();
sorted_list_destroy(&screen_list);
} }
void ui_loop() { void ui_init() {
pthread_mutex_t tick_wait_mutex;
pthread_cond_t tick_wait_cond;
extern sig_atomic_t foad;
(void) initscr(); /* initialize the curses library */ (void) initscr(); /* initialize the curses library */
keypad(stdscr, TRUE); /* enable keyboard mapping */ keypad(stdscr, TRUE); /* enable keyboard mapping */
(void) nonl(); /* tell curses not to do NL->CR/NL on output */ (void) nonl(); /* tell curses not to do NL->CR/NL on output */
@@ -332,9 +345,12 @@ void ui_loop() {
halfdelay(2); halfdelay(2);
erase(); erase();
pthread_mutex_init(&tick_wait_mutex, NULL); screen_list_init();
pthread_cond_init(&tick_wait_cond, NULL); }
void ui_loop() {
extern sig_atomic_t foad;
while(foad == 0) { while(foad == 0) {
int i; int i;
i = toupper(getch()); i = toupper(getch());
@@ -345,13 +361,15 @@ void ui_loop() {
case 'R': case 'R':
options.dnsresolution = !options.dnsresolution; options.dnsresolution = !options.dnsresolution;
tick(1);
break; break;
case 'B': case 'B':
options.showbars = !options.showbars; options.showbars = !options.showbars;
tick(1);
break; break;
} }
tick(); tick(0);
} }
} }