diff options
Diffstat (limited to 'src/fnettrace/main.c')
-rw-r--r-- | src/fnettrace/main.c | 72 |
1 files changed, 63 insertions, 9 deletions
diff --git a/src/fnettrace/main.c b/src/fnettrace/main.c index d5772328c..331311ee8 100644 --- a/src/fnettrace/main.c +++ b/src/fnettrace/main.c | |||
@@ -26,7 +26,7 @@ static int arg_netfilter = 0; | |||
26 | static char *arg_log = NULL; | 26 | static char *arg_log = NULL; |
27 | 27 | ||
28 | typedef struct hnode_t { | 28 | typedef struct hnode_t { |
29 | struct hnode_t *hnext; // used for hash table | 29 | struct hnode_t *hnext; // used for hash table and unused linked list |
30 | struct hnode_t *dnext; // used to display stremas on the screen | 30 | struct hnode_t *dnext; // used to display stremas on the screen |
31 | uint32_t ip_src; | 31 | uint32_t ip_src; |
32 | uint32_t bytes; // number of bytes received in the last display interval | 32 | uint32_t bytes; // number of bytes received in the last display interval |
@@ -45,6 +45,36 @@ HNode *htable[HMAX] = {NULL}; | |||
45 | // display linked list | 45 | // display linked list |
46 | HNode *dlist = NULL; | 46 | HNode *dlist = NULL; |
47 | 47 | ||
48 | |||
49 | // speed up malloc/free | ||
50 | #define HNODE_MAX_MALLOC 16 | ||
51 | static HNode *hnode_unused = NULL; | ||
52 | static int hnode_malloc_cnt = 0; | ||
53 | HNode *hmalloc(void) { | ||
54 | if (hnode_unused == NULL) { | ||
55 | hnode_unused = malloc(sizeof(HNode) * HNODE_MAX_MALLOC); | ||
56 | if (!hnode_unused) | ||
57 | errExit("malloc"); | ||
58 | memset(hnode_unused, 0, sizeof(HNode) * HNODE_MAX_MALLOC); | ||
59 | HNode *ptr = hnode_unused; | ||
60 | int i; | ||
61 | for ( i = 1; i < HNODE_MAX_MALLOC; i++, ptr++) | ||
62 | ptr->hnext = hnode_unused + i; | ||
63 | } | ||
64 | |||
65 | HNode *rv = hnode_unused; | ||
66 | hnode_unused = hnode_unused->hnext; | ||
67 | return rv; | ||
68 | } | ||
69 | |||
70 | void hfree(HNode *ptr) { | ||
71 | assert(ptr); | ||
72 | memset(ptr, 0, sizeof(HNode)); | ||
73 | ptr->hnext = hnode_unused; | ||
74 | hnode_unused = ptr; | ||
75 | } | ||
76 | |||
77 | |||
48 | static void hnode_add(uint32_t ip_src, uint8_t protocol, uint16_t port_src, uint32_t bytes) { | 78 | static void hnode_add(uint32_t ip_src, uint8_t protocol, uint16_t port_src, uint32_t bytes) { |
49 | uint8_t h = hash(ip_src); | 79 | uint8_t h = hash(ip_src); |
50 | 80 | ||
@@ -65,9 +95,8 @@ static void hnode_add(uint32_t ip_src, uint8_t protocol, uint16_t port_src, uint | |||
65 | #ifdef DEBUG | 95 | #ifdef DEBUG |
66 | printf("malloc %d.%d.%d.%d\n", PRINT_IP(ip_src)); | 96 | printf("malloc %d.%d.%d.%d\n", PRINT_IP(ip_src)); |
67 | #endif | 97 | #endif |
68 | HNode *hnew = malloc(sizeof(HNode)); | 98 | HNode *hnew = hmalloc(); |
69 | if (!hnew) | 99 | assert(hnew); |
70 | errExit("malloc"); | ||
71 | hnew->hostname = NULL; | 100 | hnew->hostname = NULL; |
72 | hnew->ip_src = ip_src; | 101 | hnew->ip_src = ip_src; |
73 | hnew->port_src = port_src; | 102 | hnew->port_src = port_src; |
@@ -117,7 +146,7 @@ static void hnode_free(HNode *elem) { | |||
117 | htable[h] = elem->hnext; | 146 | htable[h] = elem->hnext; |
118 | else | 147 | else |
119 | prev->hnext = elem->hnext; | 148 | prev->hnext = elem->hnext; |
120 | free(elem); | 149 | hfree(elem); |
121 | } | 150 | } |
122 | 151 | ||
123 | #ifdef DEBUG | 152 | #ifdef DEBUG |
@@ -194,14 +223,15 @@ static unsigned adjust_bandwidth(unsigned bw) { | |||
194 | 223 | ||
195 | static void hnode_print(unsigned bw) { | 224 | static void hnode_print(unsigned bw) { |
196 | assert(!arg_netfilter); | 225 | assert(!arg_netfilter); |
197 | ansi_clrscr(); | 226 | bw = (bw < 1024 * DISPLAY_INTERVAL)? 1024 * DISPLAY_INTERVAL: bw; |
198 | |||
199 | #ifdef DEBUG | 227 | #ifdef DEBUG |
200 | printf("*********************\n"); | 228 | printf("*********************\n"); |
201 | debug_dlist(); | 229 | debug_dlist(); |
202 | printf("-----------------------------\n"); | 230 | printf("-----------------------------\n"); |
203 | debug_hnode(); | 231 | debug_hnode(); |
204 | printf("*********************\n"); | 232 | printf("*********************\n"); |
233 | #else | ||
234 | ansi_clrscr(); | ||
205 | #endif | 235 | #endif |
206 | 236 | ||
207 | // get terminal size | 237 | // get terminal size |
@@ -291,6 +321,17 @@ static void hnode_print(unsigned bw) { | |||
291 | ptr = next; | 321 | ptr = next; |
292 | } | 322 | } |
293 | 323 | ||
324 | #ifdef DEBUG | ||
325 | { | ||
326 | int cnt = 0; | ||
327 | HNode *ptr = hnode_unused; | ||
328 | while (ptr) { | ||
329 | cnt++; | ||
330 | ptr = ptr->hnext; | ||
331 | } | ||
332 | printf("hnode unused %d\n", cnt); | ||
333 | } | ||
334 | #endif | ||
294 | } | 335 | } |
295 | 336 | ||
296 | static void run_trace(void) { | 337 | static void run_trace(void) { |
@@ -343,9 +384,22 @@ static void run_trace(void) { | |||
343 | 384 | ||
344 | unsigned bytes = recvfrom(sock, buf, MAX_BUF_SIZE, 0, NULL, NULL); | 385 | unsigned bytes = recvfrom(sock, buf, MAX_BUF_SIZE, 0, NULL, NULL); |
345 | if (bytes >= 20) { // size of IP header | 386 | if (bytes >= 20) { // size of IP header |
346 | bw += bytes + 14; // assume a 14 byte Ethernet layer | 387 | #ifdef DEBUG |
388 | { | ||
389 | uint32_t ip_src; | ||
390 | memcpy(&ip_src, buf + 12, 4); | ||
391 | ip_src = ntohl(ip_src); | ||
392 | |||
393 | uint32_t ip_dst; | ||
394 | memcpy(&ip_dst, buf + 16, 4); | ||
395 | ip_dst = ntohl(ip_dst); | ||
396 | printf("%d.%d.%d.%d -> %d.%d.%d.%d, %u bytes\n", PRINT_IP(ip_src), PRINT_IP(ip_dst), bytes); | ||
397 | } | ||
398 | #endif | ||
347 | // filter out loopback traffic | 399 | // filter out loopback traffic |
348 | if (buf[12] != 127) { | 400 | if (buf[12] != 127 && buf[16] != 127) { |
401 | bw += bytes + 14; // assume a 14 byte Ethernet layer | ||
402 | |||
349 | uint32_t ip_src; | 403 | uint32_t ip_src; |
350 | memcpy(&ip_src, buf + 12, 4); | 404 | memcpy(&ip_src, buf + 12, 4); |
351 | ip_src = ntohl(ip_src); | 405 | ip_src = ntohl(ip_src); |