Commit 24df8266 authored by chris's avatar chris

""

parent a0c6ae3a
This diff is collapsed.
......@@ -31,6 +31,12 @@ int history_pos = 0;
int history_len = 1;
pthread_mutex_t tick_mutex;
sig_atomic_t foad;
static void finish(int sig) {
foad = sig;
}
/* Only need ethernet and IP headers. */
......@@ -129,9 +135,8 @@ static void handle_packet(char* args, const struct pcap_pkthdr* pkthdr,const cha
}
}
/*
* packet capture thread
*/
/* packet_loop:
* Worker function for packet capture thread. */
void packet_loop(void* ptr) {
char errbuf[PCAP_ERRBUF_SIZE];
char* str = "ip";
......@@ -143,7 +148,8 @@ void packet_loop(void* ptr) {
pd = pcap_open_live(interface, CAPTURE_LENGTH, 1, 1000, errbuf);
if(pd == NULL) {
fprintf(stderr, "pcap_open_live(%s): %s\n", interface, errbuf);
exit(1);
foad = 1;
return;
}
if (filtercode) {
str = xmalloc(strlen(filtercode) + sizeof "() and ip");
......@@ -151,11 +157,13 @@ void packet_loop(void* ptr) {
}
if (pcap_compile(pd, &F, str, 1, 0) == -1) {
fprintf(stderr, "pcap_compile(%s): %s\n", str, pcap_geterr(pd));
exit(1);
foad = 1;
return;
}
if (pcap_setfilter(pd, &F) == -1) {
fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(pd));
exit(1);
foad = 1;
return;
}
if (filtercode)
xfree(str);
......@@ -164,21 +172,15 @@ void packet_loop(void* ptr) {
printf("end loop\n");
}
sig_atomic_t foad;
static void finish(int sig)
{
foad = sig;
}
/* usage:
* Print usage information. */
void usage(FILE *fp) {
fprintf(fp,
"iftop: display bandwidth usage on an interface by host\n"
"Options:\n"
"\n"
" -i interface listen on named interface (default: eth0)\n"
" -f filtercode code use filtercode code to select packets to count\n"
" -f filter code use filter code to select packets to count\n"
" (default: none, but only IP packets are counted)\n"
" -h display this message\n"
"\n"
......
......@@ -3,6 +3,7 @@
*
*/
#include <ctype.h>
#include <curses.h>
#include <string.h>
#include <math.h>
......@@ -75,28 +76,30 @@ static int get_bar_length(const int rate) {
static void draw_bar_scale(void) {
int i;
/* Draw bar graph scale on top of the window. */
mvhline(1, 0, 0, COLS);
mvhline(2, 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(1, x, ACS_BTEE);
mvaddch(2, x, ACS_BTEE);
if (x + strlen(p) >= COLS)
x = COLS - strlen(p);
mvaddstr(0, x, p);
mvaddstr(1, x, p);
}
mvaddch(1, 0, ACS_LLCORNER);
mvaddch(2, 0, ACS_LLCORNER);
}
static int dnsresolution = 1;
void ui_print() {
hash_node_type* n = NULL;
sorted_list_node* nn = NULL;
char hostname[HOSTNAME_LENGTH];
static char *line;
static int lcols;
int y = 2;
int y = 3;
sorted_list_type screen_list;
if (!line || lcols != COLS) {
......@@ -107,7 +110,18 @@ void ui_print() {
screen_list.compare = &screen_line_compare;
sorted_list_initialise(&screen_list);
erase();
clear();
//erase();
move(0, 0);
attron(A_REVERSE);
addstr(" Q ");
attroff(A_REVERSE);
addstr(" quit ");
attron(A_REVERSE);
addstr(" R ");
attroff(A_REVERSE);
addstr(dnsresolution ? " name resolution off "
: " name resolution on ");
draw_bar_scale();
while(hash_next_item(history, &n) == HASH_STATUS_OK) {
......@@ -145,7 +159,10 @@ void ui_print() {
L = sizeof hostname;
}
resolve(&screen_line->ap->src, hostname, L);
if (dnsresolution)
resolve(&screen_line->ap->src, hostname, L);
else
strcpy(hostname, inet_ntoa(screen_line->ap->src));
sprintf(line, "%-*s", L, hostname);
mvaddstr(y, x, line);
x += L;
......@@ -154,7 +171,10 @@ void ui_print() {
mvaddstr(y+1, x, " <= ");
x += 4;
resolve(&screen_line->ap->dst, hostname, L);
if (dnsresolution)
resolve(&screen_line->ap->dst, hostname, L);
else
strcpy(hostname, inet_ntoa(screen_line->ap->dst));
sprintf(line, "%-*s", L, hostname);
mvaddstr(y, x, line);
x += L;
......@@ -210,9 +230,16 @@ void ui_loop() {
pthread_mutex_init(&tick_wait_mutex, NULL);
pthread_cond_init(&tick_wait_cond, NULL);
while(foad == 0) {
if(getch() == 'q') {
foad = 1;
int i;
i = toupper(getch());
switch (i) {
case 'Q':
foad = 1;
break;
case 'R':
dnsresolution = !dnsresolution;
break;
}
tick();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment