23 #include "addr_hash.h"
24 #include "serv_hash.h"
27 #include "sorted_list.h"
29 #include "screenfilter.h"
31 #include "ui_common.h"
35 #define HELP_MESSAGE \
36 "Host display: General:\n"\
37 " n - toggle DNS host resolution P - pause display\n"\
38 " s - toggle show source host h - toggle this help display\n"\
39 " d - toggle show destination host b - toggle bar graph display\n"\
40 " t - cycle line display mode B - cycle bar graph average\n"\
41 " T - toggle cumulative line totals\n"\
42 "Port display: j/k - scroll display\n"\
43 " N - toggle service resolution f - edit filter code\n"\
44 " S - toggle show source port l - set screen filter\n"\
45 " D - toggle show destination port L - lin/log scales\n"\
46 " p - toggle port display ! - shell command\n"\
49 " 1/2/3 - sort by 1st/2nd/3rd column\n"\
50 " < - sort by source name\n"\
51 " > - sort by dest name\n"\
52 " o - freeze current order\n"\
54 "iftop, version " PACKAGE_VERSION
57 extern hash_type* history;
58 extern int history_pos;
59 extern int history_len;
61 extern options_t options ;
65 #define HELP_MSG_SIZE 80
67 int persistenthelp = 0;
69 char helpmsg[HELP_MSG_SIZE];
70 int dontshowdisplay = 0;
72 /* Barchart scales. */
76 { 64000, 10 }, /* 64 kbit/s */
79 { 1000000, 10 }, /* 1 Mbit/s */
82 { 1000000000, 100 } /* 1 Gbit/s */
84 static int rateidx = 0, wantbiggerrate;
86 static int rateidx_init = 0;
88 static int get_bar_interval(float bandwidth) {
90 if(bandwidth > 100000000) {
96 static float get_max_bandwidth() {
98 if(options.max_bandwidth > 0) {
99 max = options.max_bandwidth;
102 max = scale[rateidx].max;
108 static int get_bar_length(const int rate) {
112 if (rate > scale[rateidx].max) {
115 while(rate > scale[rateidx_init++].max) {
117 rateidx = rateidx_init;
120 if(options.log_scale) {
121 l = log(rate) / log(get_max_bandwidth());
124 l = rate / get_max_bandwidth();
129 static void draw_bar_scale(int* y) {
132 max = get_max_bandwidth();
133 interval = get_bar_interval(max);
134 if(options.showbars) {
136 /* Draw bar graph scale on top of the window. */
139 mvhline(*y + 1, 0, 0, COLS);
142 if(options.log_scale) {
151 /* for (i = 1.25; i * 8 <= max; i *= interval) { */
155 /* This 1024 vs 1000 stuff is just plain evil */
156 readable_size(i, s, sizeof s, options.log_scale ? 1000 : 1024, options.bandwidth_in_bytes);
157 p = s + strspn(s, " ");
158 x = get_bar_length(i * 8);
159 mvaddch(*y + 1, x, ACS_BTEE);
160 if (x + strlen(p) >= COLS)
161 x = COLS - strlen(p);
164 if(options.log_scale) {
171 mvaddch(*y + 1, 0, ACS_LLCORNER);
175 mvhline(*y, 0, 0, COLS);
180 void draw_line_total(float sent, float recv, int y, int x, option_linedisplay_t linedisplay, int bytes) {
183 switch(linedisplay) {
184 case OPTION_LINEDISPLAY_TWO_LINE:
185 draw_line_total(sent, recv, y, x, OPTION_LINEDISPLAY_ONE_LINE_SENT, bytes);
186 draw_line_total(sent, recv, y+1, x, OPTION_LINEDISPLAY_ONE_LINE_RECV, bytes);
188 case OPTION_LINEDISPLAY_ONE_LINE_SENT:
191 case OPTION_LINEDISPLAY_ONE_LINE_RECV:
194 case OPTION_LINEDISPLAY_ONE_LINE_BOTH:
198 if(linedisplay != OPTION_LINEDISPLAY_TWO_LINE) {
199 readable_size(n, buf, 10, 1024, bytes);
204 void draw_bar(float n, int y) {
206 mvchgat(y, 0, -1, A_NORMAL, 0, NULL);
207 L = get_bar_length(8 * n);
209 mvchgat(y, 0, L + 1, A_REVERSE, 0, NULL);
212 void draw_line_totals(int y, host_pair_line* line, option_linedisplay_t linedisplay) {
214 int x = (COLS - 8 * HISTORY_DIVISIONS);
216 for(j = 0; j < HISTORY_DIVISIONS; j++) {
217 draw_line_total(line->sent[j], line->recv[j], y, x, linedisplay, options.bandwidth_in_bytes);
221 if(options.showbars) {
222 switch(linedisplay) {
223 case OPTION_LINEDISPLAY_TWO_LINE:
224 draw_bar(line->sent[options.bar_interval],y);
225 draw_bar(line->recv[options.bar_interval],y+1);
227 case OPTION_LINEDISPLAY_ONE_LINE_SENT:
228 draw_bar(line->sent[options.bar_interval],y);
230 case OPTION_LINEDISPLAY_ONE_LINE_RECV:
231 draw_bar(line->recv[options.bar_interval],y);
233 case OPTION_LINEDISPLAY_ONE_LINE_BOTH:
234 draw_bar(line->recv[options.bar_interval] + line->sent[options.bar_interval],y);
240 void draw_totals(host_pair_line* totals) {
245 int x = (COLS - 8 * HISTORY_DIVISIONS);
247 draw_line_totals(y, totals, OPTION_LINEDISPLAY_TWO_LINE);
249 for(j = 0; j < HISTORY_DIVISIONS; j++) {
250 readable_size((totals->sent[j] + totals->recv[j]) , buf, 10, 1024, options.bandwidth_in_bytes);
256 extern history_type history_totals;
260 sorted_list_node* nn = NULL;
261 char host1[HOSTNAME_LENGTH], host2[HOSTNAME_LENGTH];
269 if (!line || lcols != COLS) {
271 line = calloc(COLS + 1, 1);
275 * erase() is faster than clear(). Dunno why we switched to
276 * clear() -pdw 24/10/02
282 if(options.showhelp) {
283 mvaddstr(y,0,HELP_MESSAGE);
288 while(i < options.screen_offset && ((nn = sorted_list_next_item(&screen_list, nn)) != NULL)) {
292 /* Screen layout: we have 2 * HISTORY_DIVISIONS 6-character wide history
293 * items, and so can use COLS - 12 * HISTORY_DIVISIONS to print the two
296 if(i == 0 || nn != NULL) {
297 while((y < LINES - 5) && ((nn = sorted_list_next_item(&screen_list, nn)) != NULL)) {
301 host_pair_line* screen_line = (host_pair_line*)nn->data;
304 L = (COLS - 8 * HISTORY_DIVISIONS - 4) / 2;
305 if(options.show_totals) {
308 if(L > HOSTNAME_LENGTH) {
312 sprint_host(host1, screen_line->ap.af,
313 &(screen_line->ap.src6),
314 screen_line->ap.src_port,
315 screen_line->ap.protocol, L, options.aggregate_src);
316 sprint_host(host2, screen_line->ap.af,
317 &(screen_line->ap.dst6),
318 screen_line->ap.dst_port,
319 screen_line->ap.protocol, L, options.aggregate_dest);
321 if(!screen_filter_match(host1) && !screen_filter_match(host2)) {
325 mvaddstr(y, x, host1);
328 switch(options.linedisplay) {
329 case OPTION_LINEDISPLAY_TWO_LINE:
330 mvaddstr(y, x, " => ");
331 mvaddstr(y+1, x, " <= ");
333 case OPTION_LINEDISPLAY_ONE_LINE_BOTH:
334 mvaddstr(y, x, "<=> ");
336 case OPTION_LINEDISPLAY_ONE_LINE_SENT:
337 mvaddstr(y, x, " => ");
339 case OPTION_LINEDISPLAY_ONE_LINE_RECV:
340 mvaddstr(y, x, " <= ");
347 mvaddstr(y, x, host2);
349 if(options.show_totals) {
350 draw_line_total(screen_line->total_sent, screen_line->total_recv, y, COLS - 8 * (HISTORY_DIVISIONS + 1), options.linedisplay, 1);
353 draw_line_totals(y, screen_line, options.linedisplay);
356 if(options.linedisplay == OPTION_LINEDISPLAY_TWO_LINE) {
369 mvhline(y-1, 0, 0, COLS);
371 mvaddstr(y, 0, "TX: ");
372 mvaddstr(y+1, 0, "RX: ");
373 mvaddstr(y+2, 0, "TOTAL: ");
375 /* Cummulative totals */
376 mvaddstr(y, 16, "cum: ");
378 readable_size(history_totals.total_sent, line, 10, 1024, 1);
379 mvaddstr(y, 22, line);
381 readable_size(history_totals.total_recv, line, 10, 1024, 1);
382 mvaddstr(y+1, 22, line);
384 readable_size(history_totals.total_recv + history_totals.total_sent, line, 10, 1024, 1);
385 mvaddstr(y+2, 22, line);
388 mvaddstr(y, 32, "peak: ");
390 readable_size(peaksent / RESOLUTION, line, 10, 1024, options.bandwidth_in_bytes);
391 mvaddstr(y, 39, line);
393 readable_size(peakrecv / RESOLUTION, line, 10, 1024, options.bandwidth_in_bytes);
394 mvaddstr(y+1, 39, line);
396 readable_size(peaktotal / RESOLUTION, line, 10, 1024, options.bandwidth_in_bytes);
397 mvaddstr(y+2, 39, line);
399 mvaddstr(y, COLS - 8 * HISTORY_DIVISIONS - 8, "rates:");
401 draw_totals(&totals);
406 mvaddstr(0, 1, helpmsg);
407 mvaddstr(0, 1 + strlen(helpmsg), " ");
408 mvchgat(0, 0, strlen(helpmsg) + 2, A_REVERSE, 0, NULL);
410 move(LINES - 1, COLS - 1);
414 /* Bar chart auto scale */
415 if (wantbiggerrate && options.max_bandwidth == 0) {
421 void ui_tick(int print) {
425 else if(showhelphint && (time(NULL) - helptimer > HELP_TIME) && !persistenthelp) {
431 void ui_curses_init() {
432 (void) initscr(); /* initialize the curses library */
433 keypad(stdscr, TRUE); /* enable keyboard mapping */
434 (void) nonl(); /* tell curses not to do NL->CR/NL on output */
435 (void) cbreak(); /* take input chars one at a time, no wait for \n */
436 (void) noecho(); /* don't echo input */
437 (void) curs_set(0); /* hide blinking cursor in ui */
441 void showhelp(const char * s) {
442 strncpy(helpmsg, s, HELP_MSG_SIZE);
444 helptimer = time(NULL);
456 screen_hash = addr_hash_create();
458 service_hash = serv_hash_create();
459 serv_hash_initialise(service_hash);
461 snprintf(msg,20,"Listening on %s",options.interface);
468 void showportstatus() {
469 if(options.showports == OPTION_PORTS_ON) {
470 showhelp("Port display ON");
472 else if(options.showports == OPTION_PORTS_OFF) {
473 showhelp("Port display OFF");
475 else if(options.showports == OPTION_PORTS_DEST) {
476 showhelp("Port display DEST");
478 else if(options.showports == OPTION_PORTS_SRC) {
479 showhelp("Port display SOURCE");
486 char *edline(int linenum, const char *prompt, const char *initial);
488 char *set_filter_code(const char *filter);
490 extern sig_atomic_t foad;
501 if(options.dnsresolution) {
502 options.dnsresolution = 0;
503 showhelp("DNS resolution off");
506 options.dnsresolution = 1;
507 showhelp("DNS resolution on");
513 if(options.portresolution) {
514 options.portresolution = 0;
515 showhelp("Port resolution off");
518 options.portresolution = 1;
519 showhelp("Port resolution on");
526 options.showhelp = !options.showhelp;
531 if(options.showbars) {
532 options.showbars = 0;
533 showhelp("Bars off");
536 options.showbars = 1;
543 options.bar_interval = (options.bar_interval + 1) % 3;
544 if(options.bar_interval == 0) {
545 showhelp("Bars show 2s average");
547 else if(options.bar_interval == 1) {
548 showhelp("Bars show 10s average");
551 showhelp("Bars show 40s average");
556 if(options.aggregate_src) {
557 options.aggregate_src = 0;
558 showhelp("Show source host");
561 options.aggregate_src = 1;
562 showhelp("Hide source host");
566 if(options.aggregate_dest) {
567 options.aggregate_dest = 0;
568 showhelp("Show dest host");
571 options.aggregate_dest = 1;
572 showhelp("Hide dest host");
576 /* Show source ports */
577 if(options.showports == OPTION_PORTS_OFF) {
578 options.showports = OPTION_PORTS_SRC;
580 else if(options.showports == OPTION_PORTS_DEST) {
581 options.showports = OPTION_PORTS_ON;
583 else if(options.showports == OPTION_PORTS_ON) {
584 options.showports = OPTION_PORTS_DEST;
587 options.showports = OPTION_PORTS_OFF;
592 /* Show dest ports */
593 if(options.showports == OPTION_PORTS_OFF) {
594 options.showports = OPTION_PORTS_DEST;
596 else if(options.showports == OPTION_PORTS_SRC) {
597 options.showports = OPTION_PORTS_ON;
599 else if(options.showports == OPTION_PORTS_ON) {
600 options.showports = OPTION_PORTS_SRC;
603 options.showports = OPTION_PORTS_OFF;
609 (options.showports == OPTION_PORTS_OFF)
613 // Don't tick here, otherwise we get a bogus display
618 showhelp("Display unpaused");
622 showhelp("Display paused");
627 if(options.freezeorder) {
628 options.freezeorder = 0;
629 showhelp("Order unfrozen");
632 options.freezeorder = 1;
633 showhelp("Order frozen");
638 options.sort = OPTION_SORT_DIV1;
639 showhelp("Sort by col 1");
642 options.sort = OPTION_SORT_DIV2;
643 showhelp("Sort by col 2");
646 options.sort = OPTION_SORT_DIV3;
647 showhelp("Sort by col 3");
650 options.sort = OPTION_SORT_SRC;
651 showhelp("Sort by source");
654 options.sort = OPTION_SORT_DEST;
655 showhelp("Sort by dest");
658 options.screen_offset++;
662 if(options.screen_offset > 0) {
663 options.screen_offset--;
668 options.linedisplay = (options.linedisplay + 1) % 4;
669 switch(options.linedisplay) {
670 case OPTION_LINEDISPLAY_TWO_LINE:
671 showhelp("Two lines per host");
673 case OPTION_LINEDISPLAY_ONE_LINE_SENT:
674 showhelp("Sent traffic only");
676 case OPTION_LINEDISPLAY_ONE_LINE_RECV:
677 showhelp("Received traffic only");
679 case OPTION_LINEDISPLAY_ONE_LINE_BOTH:
680 showhelp("One line per host");
688 if ((s = edline(0, "Net filter", options.filtercode))) {
690 if (s[strspn(s, " \t")] == 0) {
691 /* Empty filter; set to NULL. */
695 if (!(m = set_filter_code(s))) {
696 xfree(options.filtercode);
697 options.filtercode = s;
698 /* -lpcap will write junk to stderr; we do our best to
700 move(COLS - 1, LINES - 1);
702 showhelp("Installed new filter");
716 if ((s = edline(0, "Screen filter", options.screenfilter))) {
717 if(!screen_filter_set(s)) {
718 showhelp("Invalid regexp");
724 showhelp("Sorry, screen filters not supported on this platform")
729 #ifdef ALLOW_SUBSHELL
732 if ((s = edline(0, "Command", "")) && s[strspn(s, " \t")]) {
739 if (i == -1 || (i == 127 && errno != 0)) {
740 fprintf(stderr, "system: %s: %s\n", s, strerror(errno));
744 fprintf(stderr, "%s: exited with code %d\n", s, WEXITSTATUS(i));
745 else if (WIFSIGNALED(i))
746 fprintf(stderr, "%s: killed by signal %d\n", s, WTERMSIG(i));
751 fprintf(stderr, "Press any key....");
752 while (getch() == ERR);
759 showhelp("Sorry, subshells have been disabled.");
764 options.show_totals = !options.show_totals;
765 if(options.show_totals) {
766 showhelp("Show cumulative totals");
769 showhelp("Hide cumulative totals");
774 options.log_scale = !options.log_scale;
775 showhelp(options.log_scale ? "Logarithmic scale" : "Linear scale");
785 showhelp("Press H or ? for help");