aboutsummaryrefslogtreecommitdiffstats
path: root/src/fnettrace/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fnettrace/main.c')
-rw-r--r--src/fnettrace/main.c49
1 files changed, 14 insertions, 35 deletions
diff --git a/src/fnettrace/main.c b/src/fnettrace/main.c
index 352d61bbd..d5772328c 100644
--- a/src/fnettrace/main.c
+++ b/src/fnettrace/main.c
@@ -29,7 +29,6 @@ typedef struct hnode_t {
29 struct hnode_t *hnext; // used for hash table 29 struct hnode_t *hnext; // used for hash table
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 ip_dst;
33 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
34 uint16_t port_src; 33 uint16_t port_src;
35 uint8_t protocol; 34 uint8_t protocol;
@@ -46,7 +45,7 @@ HNode *htable[HMAX] = {NULL};
46// display linked list 45// display linked list
47HNode *dlist = NULL; 46HNode *dlist = NULL;
48 47
49static void hnode_add(uint32_t ip_src, uint32_t ip_dst, uint8_t protocol, uint16_t port_src, uint32_t bytes) { 48static void hnode_add(uint32_t ip_src, uint8_t protocol, uint16_t port_src, uint32_t bytes) {
50 uint8_t h = hash(ip_src); 49 uint8_t h = hash(ip_src);
51 50
52 // find 51 // find
@@ -55,7 +54,7 @@ static void hnode_add(uint32_t ip_src, uint32_t ip_dst, uint8_t protocol, uint16
55 while (ptr) { 54 while (ptr) {
56 if (ptr->ip_src == ip_src) { 55 if (ptr->ip_src == ip_src) {
57 ip_instance++; 56 ip_instance++;
58 if (ptr->ip_dst == ip_dst && ptr->port_src == port_src && ptr->protocol == protocol) { 57 if (ptr->port_src == port_src && ptr->protocol == protocol) {
59 ptr->bytes += bytes; 58 ptr->bytes += bytes;
60 return; 59 return;
61 } 60 }
@@ -71,7 +70,6 @@ static void hnode_add(uint32_t ip_src, uint32_t ip_dst, uint8_t protocol, uint16
71 errExit("malloc"); 70 errExit("malloc");
72 hnew->hostname = NULL; 71 hnew->hostname = NULL;
73 hnew->ip_src = ip_src; 72 hnew->ip_src = ip_src;
74 hnew->ip_dst = ip_dst;
75 hnew->port_src = port_src; 73 hnew->port_src = port_src;
76 hnew->protocol = protocol; 74 hnew->protocol = protocol;
77 hnew->hnext = NULL; 75 hnew->hnext = NULL;
@@ -119,8 +117,6 @@ static void hnode_free(HNode *elem) {
119 htable[h] = elem->hnext; 117 htable[h] = elem->hnext;
120 else 118 else
121 prev->hnext = elem->hnext; 119 prev->hnext = elem->hnext;
122 if (elem->hostname)
123 free(elem->hostname);
124 free(elem); 120 free(elem);
125} 121}
126 122
@@ -245,18 +241,12 @@ static void hnode_print(unsigned bw) {
245 else 241 else
246 snprintf(bytes, 11, "%u B/s ", (unsigned) (ptr->bytes / DISPLAY_INTERVAL)); 242 snprintf(bytes, 11, "%u B/s ", (unsigned) (ptr->bytes / DISPLAY_INTERVAL));
247 243
248 char *hostname = ptr->hostname; 244 if (!ptr->hostname)
249 if (!hostname) 245 ptr->hostname = radix_longest_prefix_match(ptr->ip_src);
250 hostname = radix_find_last(ptr->ip_src); 246 if (!ptr->hostname)
251 if (!hostname) 247 ptr->hostname = retrieve_hostname(ptr->ip_src);
252 hostname = retrieve_hostname(ptr->ip_src); 248 if (!ptr->hostname)
253 if (!hostname) 249 ptr->hostname = " ";
254 hostname = " ";
255 else {
256 ptr->hostname = strdup(hostname);
257 if (!ptr->hostname)
258 errExit("strdup");
259 }
260 250
261 unsigned bwunit = bw / DISPLAY_BW_UNITS; 251 unsigned bwunit = bw / DISPLAY_BW_UNITS;
262 char *bwline; 252 char *bwline;
@@ -274,13 +264,13 @@ static void hnode_print(unsigned bw) {
274 protocol = "(UDP)"; 264 protocol = "(UDP)";
275/* 265/*
276 else (ptr->port_src == 443) 266 else (ptr->port_src == 443)
277 protocol = "SSL"; 267 protocol = "TLS";
278 else if (ptr->port_src == 53) 268 else if (ptr->port_src == 53)
279 protocol = "DNS"; 269 protocol = "DNS";
280*/ 270*/
281 271
282 len = snprintf(line, LINE_MAX, "%10s %s %d.%d.%d.%d:%u%s %s\n", 272 len = snprintf(line, LINE_MAX, "%10s %s %d.%d.%d.%d:%u%s %s\n",
283 bytes, bwline, PRINT_IP(ptr->ip_src), ptr->port_src, protocol, hostname); 273 bytes, bwline, PRINT_IP(ptr->ip_src), ptr->port_src, protocol, ptr->hostname);
284 adjust_line(line, len, cols); 274 adjust_line(line, len, cols);
285 printf("%s", line); 275 printf("%s", line);
286 276
@@ -360,16 +350,12 @@ static void run_trace(void) {
360 memcpy(&ip_src, buf + 12, 4); 350 memcpy(&ip_src, buf + 12, 4);
361 ip_src = ntohl(ip_src); 351 ip_src = ntohl(ip_src);
362 352
363 uint32_t ip_dst;
364 memcpy(&ip_dst, buf + 16, 4);
365 ip_dst = ntohl(ip_dst);
366
367 uint8_t hlen = (buf[0] & 0x0f) * 4; 353 uint8_t hlen = (buf[0] & 0x0f) * 4;
368 uint16_t port_src; 354 uint16_t port_src;
369 memcpy(&port_src, buf + hlen, 2); 355 memcpy(&port_src, buf + hlen, 2);
370 port_src = ntohs(port_src); 356 port_src = ntohs(port_src);
371 357
372 hnode_add(ip_src, ip_dst, buf[9], port_src, bytes + 14); 358 hnode_add(ip_src, buf[9], port_src, bytes + 14);
373 } 359 }
374 } 360 }
375 } 361 }
@@ -537,7 +523,6 @@ void logprintf(char* fmt, ...) {
537static void usage(void) { 523static void usage(void) {
538 printf("Usage: fnetlock [OPTIONS]\n"); 524 printf("Usage: fnetlock [OPTIONS]\n");
539 printf("Options:\n"); 525 printf("Options:\n");
540 printf(" --build=filename - compact list of addresses\n");
541 printf(" --help, -? - this help screen\n"); 526 printf(" --help, -? - this help screen\n");
542 printf(" --log=filename - netlocker logfile\n"); 527 printf(" --log=filename - netlocker logfile\n");
543 printf(" --netfilter - build the firewall rules and commit them.\n"); 528 printf(" --netfilter - build the firewall rules and commit them.\n");
@@ -552,21 +537,15 @@ int main(int argc, char **argv) {
552 radix_add(0x09000000, 0xff000000, "IBM"); 537 radix_add(0x09000000, 0xff000000, "IBM");
553 radix_add(0x09090909, 0xffffffff, "Quad9 DNS"); 538 radix_add(0x09090909, 0xffffffff, "Quad9 DNS");
554 radix_add(0x09000000, 0xff000000, "IBM"); 539 radix_add(0x09000000, 0xff000000, "IBM");
555 radix_print();
556 printf("This test should print \"IBM, Quad9 DNS, IBM\"\n"); 540 printf("This test should print \"IBM, Quad9 DNS, IBM\"\n");
557 char *name = radix_find_first(0x09090909); 541 char *name = radix_longest_prefix_match(0x09040404);
558 printf("%s, ", name); 542 printf("%s, ", name);
559 name = radix_find_last(0x09090909); 543 name = radix_longest_prefix_match(0x09090909);
560 printf("%s, ", name); 544 printf("%s, ", name);
561 name = radix_find_last(0x09322209); 545 name = radix_longest_prefix_match(0x09322209);
562 printf("%s\n", name); 546 printf("%s\n", name);
563#endif 547#endif
564 548
565 if (argc == 2 && strncmp(argv[1], "--build=", 8) == 0) {
566 build_list(argv[1] + 8);
567 return 0;
568 }
569
570 if (getuid() != 0) { 549 if (getuid() != 0) {
571 fprintf(stderr, "Error: you need to be root to run this program\n"); 550 fprintf(stderr, "Error: you need to be root to run this program\n");
572 return 1; 551 return 1;