Fixed dependency handling.
Added cumulative totals. Added option to turn bars on/off.
This commit is contained in:
20
Makefile
20
Makefile
@@ -27,7 +27,7 @@ MANDIR = man
|
||||
#MANDIR = share/man # FHS-ish
|
||||
|
||||
# You shouldn't need to change anything below this point.
|
||||
VERSION = 0.1
|
||||
VERSION = 0.2
|
||||
CFLAGS += -g -Wall "-DIFTOP_VERSION=\"$(VERSION)\""
|
||||
LDFLAGS += -g
|
||||
LDLIBS += -lpcap -lpthread -lcurses -lm
|
||||
@@ -39,9 +39,7 @@ TXTS = README CHANGES INSTALL TODO iftop.8 COPYING
|
||||
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
# If you do not have makedepend, you will need to remove references to depend
|
||||
# and nodepend below.
|
||||
iftop: depend $(OBJS) Makefile
|
||||
iftop: $(OBJS) Makefile
|
||||
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)
|
||||
|
||||
install: iftop
|
||||
@@ -57,23 +55,21 @@ uninstall:
|
||||
clean: nodepend
|
||||
rm -f *~ *.o core iftop
|
||||
|
||||
tarball: nodepend $(SRCS) $(HDRS) $(TXTS)
|
||||
tarball: depend $(SRCS) $(HDRS) $(TXTS)
|
||||
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
|
||||
rm -rf iftop-$(VERSION)
|
||||
|
||||
tags :
|
||||
etags *.c *.h
|
||||
|
||||
depend:
|
||||
makedepend -- $(CFLAGS) -- $(SRCS)
|
||||
touch depend
|
||||
depend: $(SRCS)
|
||||
$(CPP) -MM $(SRCS) > depend
|
||||
|
||||
nodepend:
|
||||
makedepend -- --
|
||||
rm -f depend
|
||||
|
||||
include depend
|
||||
|
||||
# DO NOT DELETE
|
||||
|
||||
|
||||
|
||||
11
iftop.c
11
iftop.c
@@ -121,6 +121,7 @@ static void handle_packet(char* args, const struct pcap_pkthdr* pkthdr,const cha
|
||||
struct ip* iptr;
|
||||
history_type* ht;
|
||||
addr_pair ap;
|
||||
int len;
|
||||
|
||||
iptr = (struct ip*)(packet + sizeof(struct ether_header)); /* alignment? */
|
||||
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);
|
||||
}
|
||||
|
||||
len = ntohs(iptr->ip_len);
|
||||
|
||||
/* Update record */
|
||||
ht->last_write = history_pos;
|
||||
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 {
|
||||
ht->recv[history_pos] += ntohs(iptr->ip_len);
|
||||
ht->recv[history_pos] += len;
|
||||
ht->total_recv += len;
|
||||
}
|
||||
|
||||
if(direction == 0) {
|
||||
/* incoming */
|
||||
history_totals.recv[history_pos] += ntohs(iptr->ip_len);
|
||||
history_totals.total_recv += len;
|
||||
}
|
||||
else {
|
||||
history_totals.sent[history_pos] += ntohs(iptr->ip_len);
|
||||
history_totals.total_sent += len;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
2
iftop.h
2
iftop.h
@@ -13,6 +13,8 @@
|
||||
typedef struct {
|
||||
long recv[HISTORY_LENGTH];
|
||||
long sent[HISTORY_LENGTH];
|
||||
long total_sent;
|
||||
long total_recv;
|
||||
int last_write;
|
||||
} history_type;
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ void set_defaults() {
|
||||
inet_aton("255.255.255.0", &options.netfiltermask);
|
||||
options.dnsresolution = 1;
|
||||
options.promiscuous = 0;
|
||||
options.showbars = 1;
|
||||
}
|
||||
|
||||
void die(char *msg) {
|
||||
|
||||
@@ -23,6 +23,7 @@ typedef struct {
|
||||
struct in_addr netfiltermask;
|
||||
int dnsresolution;
|
||||
int promiscuous;
|
||||
int showbars;
|
||||
|
||||
} options_t;
|
||||
|
||||
|
||||
97
ui.c
97
ui.c
@@ -78,23 +78,29 @@ static int get_bar_length(const int rate) {
|
||||
return (l * COLS);
|
||||
}
|
||||
|
||||
static void draw_bar_scale(void) {
|
||||
static void draw_bar_scale(int* y) {
|
||||
int i;
|
||||
int y = 1;
|
||||
/* Draw bar graph scale on top of the window. */
|
||||
mvhline(y + 1, 0, 0, COLS);
|
||||
for (i = min_rate; i <= max_rate; i *= 10) {
|
||||
char s[40], *p;
|
||||
int x;
|
||||
readable_size(i, s, sizeof s, 1000);
|
||||
p = s + strspn(s, " ");
|
||||
x = get_bar_length(i);
|
||||
mvaddch(y + 1, x, ACS_BTEE);
|
||||
if (x + strlen(p) >= COLS)
|
||||
x = COLS - strlen(p);
|
||||
mvaddstr(y, x, p);
|
||||
if(options.showbars) {
|
||||
/* Draw bar graph scale on top of the window. */
|
||||
mvhline(*y + 1, 0, 0, COLS);
|
||||
for (i = min_rate; i <= max_rate; i *= 10) {
|
||||
char s[40], *p;
|
||||
int x;
|
||||
readable_size(i, s, sizeof s, 1000);
|
||||
p = s + strspn(s, " ");
|
||||
x = get_bar_length(i);
|
||||
mvaddch(*y + 1, x, ACS_BTEE);
|
||||
if (x + strlen(p) >= COLS)
|
||||
x = COLS - strlen(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) {
|
||||
@@ -119,16 +125,18 @@ void draw_line_totals(int y, host_pair_line* line) {
|
||||
x += 8;
|
||||
}
|
||||
|
||||
t = history_length(BARGRAPH_INTERVAL);
|
||||
mvchgat(y, 0, -1, A_NORMAL, 0, NULL);
|
||||
L = get_bar_length(8 * line->sent[BARGRAPH_INTERVAL] / t);
|
||||
if (L > 0)
|
||||
mvchgat(y, 0, L + 1, A_REVERSE, 0, NULL);
|
||||
if(options.showbars) {
|
||||
t = history_length(BARGRAPH_INTERVAL);
|
||||
mvchgat(y, 0, -1, A_NORMAL, 0, NULL);
|
||||
L = get_bar_length(8 * 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 * line->recv[BARGRAPH_INTERVAL] / t);
|
||||
if (L > 0)
|
||||
mvchgat(y+1, 0, L + 1, A_REVERSE, 0, NULL);
|
||||
mvchgat(y+1, 0, -1, A_NORMAL, 0, NULL);
|
||||
L = get_bar_length(8 * line->recv[BARGRAPH_INTERVAL] / t);
|
||||
if (L > 0)
|
||||
mvchgat(y+1, 0, L + 1, A_REVERSE, 0, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void draw_totals(host_pair_line* totals) {
|
||||
@@ -149,7 +157,7 @@ void ui_print() {
|
||||
static char *line;
|
||||
static int lcols;
|
||||
int peaksent = 0, peakrecv = 0, peaktotal = 0;
|
||||
int y = 0;
|
||||
int y = 1;
|
||||
int i;
|
||||
sorted_list_type screen_list;
|
||||
host_pair_line totals;
|
||||
@@ -174,7 +182,7 @@ void ui_print() {
|
||||
attroff(A_REVERSE);
|
||||
addstr(options.dnsresolution ? " name resolution off "
|
||||
: " name resolution on ");
|
||||
draw_bar_scale();
|
||||
draw_bar_scale(&y);
|
||||
|
||||
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
|
||||
* 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;
|
||||
|
||||
if(y < LINES - 4) {
|
||||
int t;
|
||||
|
||||
L = (COLS - 8 * HISTORY_DIVISIONS - 4) / 2;
|
||||
if(L > sizeof hostname) {
|
||||
@@ -267,17 +272,6 @@ void ui_print() {
|
||||
|
||||
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;
|
||||
free(screen_line);
|
||||
@@ -298,16 +292,25 @@ void ui_print() {
|
||||
readable_size((peakrecv + peaksent) * 8 / RESOLUTION, line, 10, 1024);
|
||||
mvaddstr(y+1, 8, line);
|
||||
|
||||
mvaddstr(y, 17, "sent: peak: ");
|
||||
mvaddstr(y+1, 17, "recv: ");
|
||||
mvaddstr(y, 18, "TX: ");
|
||||
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);
|
||||
mvaddstr(y, 30, line);
|
||||
mvaddstr(y, 39, line);
|
||||
|
||||
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);
|
||||
|
||||
@@ -343,6 +346,10 @@ void ui_loop() {
|
||||
case 'R':
|
||||
options.dnsresolution = !options.dnsresolution;
|
||||
break;
|
||||
|
||||
case 'B':
|
||||
options.showbars = !options.showbars;
|
||||
break;
|
||||
}
|
||||
tick();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user