Fixed dependency handling.

Added cumulative totals.
Added option to turn bars on/off.
This commit is contained in:
pdw
2002-04-01 19:46:20 +00:00
parent a248b6046e
commit bb3619b3fc
6 changed files with 73 additions and 59 deletions

View File

@@ -27,7 +27,7 @@ MANDIR = man
#MANDIR = share/man # FHS-ish #MANDIR = share/man # FHS-ish
# You shouldn't need to change anything below this point. # You shouldn't need to change anything below this point.
VERSION = 0.1 VERSION = 0.2
CFLAGS += -g -Wall "-DIFTOP_VERSION=\"$(VERSION)\"" CFLAGS += -g -Wall "-DIFTOP_VERSION=\"$(VERSION)\""
LDFLAGS += -g LDFLAGS += -g
LDLIBS += -lpcap -lpthread -lcurses -lm LDLIBS += -lpcap -lpthread -lcurses -lm
@@ -39,9 +39,7 @@ TXTS = README CHANGES INSTALL TODO iftop.8 COPYING
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)
# If you do not have makedepend, you will need to remove references to depend iftop: $(OBJS) Makefile
# and nodepend below.
iftop: depend $(OBJS) Makefile
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS) $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)
install: iftop install: iftop
@@ -57,23 +55,21 @@ uninstall:
clean: nodepend clean: nodepend
rm -f *~ *.o core iftop rm -f *~ *.o core iftop
tarball: nodepend $(SRCS) $(HDRS) $(TXTS) tarball: depend $(SRCS) $(HDRS) $(TXTS)
mkdir iftop-$(VERSION) mkdir iftop-$(VERSION)
set -e ; for i in Makefile $(SRCS) $(HDRS) $(TXTS) ; do cp $$i iftop-$(VERSION)/$$i ; done set -e ; for i in Makefile depend $(SRCS) $(HDRS) $(TXTS) ; do cp $$i iftop-$(VERSION)/$$i ; done
tar cvf - iftop-$(VERSION) | gzip --best > iftop-$(VERSION).tar.gz tar cvf - iftop-$(VERSION) | gzip --best > iftop-$(VERSION).tar.gz
rm -rf iftop-$(VERSION) rm -rf iftop-$(VERSION)
tags : tags :
etags *.c *.h etags *.c *.h
depend: depend: $(SRCS)
makedepend -- $(CFLAGS) -- $(SRCS) $(CPP) -MM $(SRCS) > depend
touch depend
nodepend: nodepend:
makedepend -- --
rm -f depend rm -f depend
include depend
# DO NOT DELETE

11
iftop.c
View File

@@ -121,6 +121,7 @@ static void handle_packet(char* args, const struct pcap_pkthdr* pkthdr,const cha
struct ip* iptr; struct ip* iptr;
history_type* ht; history_type* ht;
addr_pair ap; addr_pair ap;
int len;
iptr = (struct ip*)(packet + sizeof(struct ether_header)); /* alignment? */ iptr = (struct ip*)(packet + sizeof(struct ether_header)); /* alignment? */
if(options.netfilter == 0) { if(options.netfilter == 0) {
@@ -181,21 +182,27 @@ static void handle_packet(char* args, const struct pcap_pkthdr* pkthdr,const cha
hash_insert(history, &ap, ht); hash_insert(history, &ap, ht);
} }
len = ntohs(iptr->ip_len);
/* Update record */ /* Update record */
ht->last_write = history_pos; ht->last_write = history_pos;
if(iptr->ip_src.s_addr == ap.src.s_addr) { if(iptr->ip_src.s_addr == ap.src.s_addr) {
ht->sent[history_pos] += ntohs(iptr->ip_len); ht->sent[history_pos] += len;
ht->total_sent += len;
} }
else { else {
ht->recv[history_pos] += ntohs(iptr->ip_len); ht->recv[history_pos] += len;
ht->total_recv += len;
} }
if(direction == 0) { if(direction == 0) {
/* incoming */ /* incoming */
history_totals.recv[history_pos] += ntohs(iptr->ip_len); history_totals.recv[history_pos] += ntohs(iptr->ip_len);
history_totals.total_recv += len;
} }
else { else {
history_totals.sent[history_pos] += ntohs(iptr->ip_len); history_totals.sent[history_pos] += ntohs(iptr->ip_len);
history_totals.total_sent += len;
} }
} }

View File

@@ -13,6 +13,8 @@
typedef struct { typedef struct {
long recv[HISTORY_LENGTH]; long recv[HISTORY_LENGTH];
long sent[HISTORY_LENGTH]; long sent[HISTORY_LENGTH];
long total_sent;
long total_recv;
int last_write; int last_write;
} history_type; } history_type;

View File

@@ -24,6 +24,7 @@ void set_defaults() {
inet_aton("255.255.255.0", &options.netfiltermask); inet_aton("255.255.255.0", &options.netfiltermask);
options.dnsresolution = 1; options.dnsresolution = 1;
options.promiscuous = 0; options.promiscuous = 0;
options.showbars = 1;
} }
void die(char *msg) { void die(char *msg) {

View File

@@ -23,6 +23,7 @@ typedef struct {
struct in_addr netfiltermask; struct in_addr netfiltermask;
int dnsresolution; int dnsresolution;
int promiscuous; int promiscuous;
int showbars;
} options_t; } options_t;

97
ui.c
View File

@@ -78,23 +78,29 @@ static int get_bar_length(const int rate) {
return (l * COLS); return (l * COLS);
} }
static void draw_bar_scale(void) { static void draw_bar_scale(int* y) {
int i; int i;
int y = 1; if(options.showbars) {
/* Draw bar graph scale on top of the window. */ /* Draw bar graph scale on top of the window. */
mvhline(y + 1, 0, 0, COLS); mvhline(*y + 1, 0, 0, COLS);
for (i = min_rate; i <= max_rate; i *= 10) { for (i = min_rate; i <= max_rate; i *= 10) {
char s[40], *p; char s[40], *p;
int x; int x;
readable_size(i, s, sizeof s, 1000); readable_size(i, s, sizeof s, 1000);
p = s + strspn(s, " "); p = s + strspn(s, " ");
x = get_bar_length(i); x = get_bar_length(i);
mvaddch(y + 1, x, ACS_BTEE); mvaddch(*y + 1, x, ACS_BTEE);
if (x + strlen(p) >= COLS) if (x + strlen(p) >= COLS)
x = COLS - strlen(p); x = COLS - strlen(p);
mvaddstr(y, x, p); mvaddstr(*y, x, p);
}
mvaddch(*y + 1, 0, ACS_LLCORNER);
*y += 2;
}
else {
mvhline(*y, 0, 0, COLS);
*y += 1;
} }
mvaddch(y + 1, 0, ACS_LLCORNER);
} }
int history_length(const int d) { int history_length(const int d) {
@@ -119,16 +125,18 @@ void draw_line_totals(int y, host_pair_line* line) {
x += 8; x += 8;
} }
t = history_length(BARGRAPH_INTERVAL); if(options.showbars) {
mvchgat(y, 0, -1, A_NORMAL, 0, NULL); t = history_length(BARGRAPH_INTERVAL);
L = get_bar_length(8 * line->sent[BARGRAPH_INTERVAL] / t); mvchgat(y, 0, -1, A_NORMAL, 0, NULL);
if (L > 0) L = get_bar_length(8 * line->sent[BARGRAPH_INTERVAL] / t);
mvchgat(y, 0, L + 1, A_REVERSE, 0, NULL); if (L > 0)
mvchgat(y, 0, L + 1, A_REVERSE, 0, NULL);
mvchgat(y+1, 0, -1, A_NORMAL, 0, NULL); mvchgat(y+1, 0, -1, A_NORMAL, 0, NULL);
L = get_bar_length(8 * line->recv[BARGRAPH_INTERVAL] / t); L = get_bar_length(8 * line->recv[BARGRAPH_INTERVAL] / t);
if (L > 0) if (L > 0)
mvchgat(y+1, 0, L + 1, A_REVERSE, 0, NULL); mvchgat(y+1, 0, L + 1, A_REVERSE, 0, NULL);
}
} }
void draw_totals(host_pair_line* totals) { void draw_totals(host_pair_line* totals) {
@@ -149,7 +157,7 @@ void ui_print() {
static char *line; static char *line;
static int lcols; static int lcols;
int peaksent = 0, peakrecv = 0, peaktotal = 0; int peaksent = 0, peakrecv = 0, peaktotal = 0;
int y = 0; int y = 1;
int i; int i;
sorted_list_type screen_list; sorted_list_type screen_list;
host_pair_line totals; host_pair_line totals;
@@ -174,7 +182,7 @@ void ui_print() {
attroff(A_REVERSE); attroff(A_REVERSE);
addstr(options.dnsresolution ? " name resolution off " addstr(options.dnsresolution ? " name resolution off "
: " name resolution on "); : " name resolution on ");
draw_bar_scale(); draw_bar_scale(&y);
memset(&totals, 0, sizeof totals); memset(&totals, 0, sizeof totals);
@@ -227,8 +235,6 @@ void ui_print() {
} }
} }
y = 3;
/* 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
@@ -239,7 +245,6 @@ void ui_print() {
host_pair_line* screen_line = (host_pair_line*)nn->data; host_pair_line* screen_line = (host_pair_line*)nn->data;
if(y < LINES - 4) { if(y < LINES - 4) {
int t;
L = (COLS - 8 * HISTORY_DIVISIONS - 4) / 2; L = (COLS - 8 * HISTORY_DIVISIONS - 4) / 2;
if(L > sizeof hostname) { if(L > sizeof hostname) {
@@ -267,17 +272,6 @@ void ui_print() {
draw_line_totals(y, screen_line); draw_line_totals(y, screen_line);
/* Do some sort of primitive bar graph thing. */
t = history_length(BARGRAPH_INTERVAL);
mvchgat(y, 0, -1, A_NORMAL, 0, NULL);
L = get_bar_length(8 * screen_line->sent[BARGRAPH_INTERVAL] / t);
if (L > 0)
mvchgat(y, 0, L + 1, A_REVERSE, 0, NULL);
mvchgat(y+1, 0, -1, A_NORMAL, 0, NULL);
L = get_bar_length(8 * screen_line->recv[BARGRAPH_INTERVAL] / t);
if (L > 0)
mvchgat(y+1, 0, L + 1, A_REVERSE, 0, NULL);
} }
y += 2; y += 2;
free(screen_line); free(screen_line);
@@ -298,16 +292,25 @@ void ui_print() {
readable_size((peakrecv + peaksent) * 8 / RESOLUTION, line, 10, 1024); readable_size((peakrecv + peaksent) * 8 / RESOLUTION, line, 10, 1024);
mvaddstr(y+1, 8, line); mvaddstr(y+1, 8, line);
mvaddstr(y, 17, "sent: peak: "); mvaddstr(y, 18, "TX: ");
mvaddstr(y+1, 17, "recv: "); mvaddstr(y+1, 18, "RX: ");
readable_size(history_totals.total_sent / RESOLUTION, line, 10, 1024);
mvaddstr(y, 22, line);
readable_size(history_totals.total_recv / RESOLUTION, line, 10, 1024);
mvaddstr(y+1, 22, line);
mvaddstr(y, 33, "peaks: ");
/* mvaddstr(y+1, 24, "recv: "); */
readable_size(peaksent * 8 / RESOLUTION, line, 10, 1024); readable_size(peaksent * 8 / RESOLUTION, line, 10, 1024);
mvaddstr(y, 30, line); mvaddstr(y, 39, line);
readable_size(peakrecv * 8 / RESOLUTION, line, 10, 1024); readable_size(peakrecv * 8 / RESOLUTION, line, 10, 1024);
mvaddstr(y+1, 30, line); mvaddstr(y+1, 39, line);
mvaddstr(y, COLS - 8 * HISTORY_DIVISIONS - 10, "totals:"); mvaddstr(y, COLS - 8 * HISTORY_DIVISIONS - 8, "totals:");
draw_totals(&totals); draw_totals(&totals);
@@ -343,6 +346,10 @@ void ui_loop() {
case 'R': case 'R':
options.dnsresolution = !options.dnsresolution; options.dnsresolution = !options.dnsresolution;
break; break;
case 'B':
options.showbars = !options.showbars;
break;
} }
tick(); tick();
} }