2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann.
5 * Copyright 2011 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
8 * A tiny tool to provide top-like netfilter connection tracking information.
10 * The Dark Lord has Nine. But we have One, mightier than they: the White
11 * Rider. He has passed through the fire and the abyss, and they shall
12 * fear him. We will go where he leads.
14 * -- The Lord of the Rings, Aragorn,
15 * Chapter 'The White Rider'.
28 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
29 #include <libnetfilter_conntrack/libnetfilter_conntrack_tcp.h>
31 #include <GeoIPCity.h>
32 #include <netinet/in.h>
45 #include "dissector_eth.h"
48 #define INCLUDE_UDP (1 << 0)
49 #define INCLUDE_TCP (1 << 1)
51 #ifndef ATTR_TIMESTAMP_START
52 # define ATTR_TIMESTAMP_START 63
54 #ifndef ATTR_TIMESTAMP_STOP
55 # define ATTR_TIMESTAMP_STOP 64
58 #define SCROLL_MAX 1000
63 struct flow_entry
*next
;
68 uint32_t ip4_src_addr
;
69 uint32_t ip4_dst_addr
;
70 uint32_t ip6_src_addr
[4];
71 uint32_t ip6_dst_addr
[4];
76 uint64_t counter_pkts
;
77 uint64_t counter_bytes
;
78 uint64_t timestamp_start
;
79 uint64_t timestamp_stop
;
80 char country_src
[128];
82 char rev_dns_src
[256];
83 char country_dst
[128];
85 char rev_dns_dst
[256];
92 struct flow_entry
*head
;
96 volatile sig_atomic_t sigint
= 0;
98 static int what
= INCLUDE_TCP
;
100 static int show_src
= 0;
102 static struct flow_list flow_list
;
104 static GeoIP
*gi_country
= NULL
;
105 static GeoIP
*gi_city
= NULL
;
107 static char *path_city_db
= NULL
, *path_country_db
= NULL
;
109 static const char *short_options
= "vhTULKs";
110 static const struct option long_options
[] = {
111 {"tcp", no_argument
, NULL
, 'T'},
112 {"udp", no_argument
, NULL
, 'U'},
113 {"show-src", no_argument
, NULL
, 's'},
114 {"city-db", required_argument
, NULL
, 'L'},
115 {"country-db", required_argument
, NULL
, 'K'},
116 {"version", no_argument
, NULL
, 'v'},
117 {"help", no_argument
, NULL
, 'h'},
121 const char *const l3proto2str
[AF_MAX
] = {
126 const char *const proto2str
[IPPROTO_MAX
] = {
127 [IPPROTO_TCP
] = "tcp",
128 [IPPROTO_UDP
] = "udp",
129 [IPPROTO_UDPLITE
] = "udplite",
130 [IPPROTO_ICMP
] = "icmp",
131 [IPPROTO_ICMPV6
] = "icmpv6",
132 [IPPROTO_SCTP
] = "sctp",
133 [IPPROTO_GRE
] = "gre",
134 [IPPROTO_DCCP
] = "dccp",
135 [IPPROTO_IGMP
] = "igmp",
136 [IPPROTO_IPIP
] = "ipip",
137 [IPPROTO_EGP
] = "egp",
138 [IPPROTO_PUP
] = "pup",
139 [IPPROTO_IDP
] = "idp",
140 [IPPROTO_RSVP
] = "rsvp",
141 [IPPROTO_IPV6
] = "ip6tun",
142 [IPPROTO_ESP
] = "esp",
144 [IPPROTO_PIM
] = "pim",
145 [IPPROTO_COMP
] = "comp",
148 const char *const state2str
[TCP_CONNTRACK_MAX
] = {
149 [TCP_CONNTRACK_NONE
] = "NOSTATE",
150 [TCP_CONNTRACK_SYN_SENT
] = "SYN_SENT",
151 [TCP_CONNTRACK_SYN_RECV
] = "SYN_RECV",
152 [TCP_CONNTRACK_ESTABLISHED
] = "ESTABLISHED",
153 [TCP_CONNTRACK_FIN_WAIT
] = "FIN_WAIT",
154 [TCP_CONNTRACK_CLOSE_WAIT
] = "CLOSE_WAIT",
155 [TCP_CONNTRACK_LAST_ACK
] = "LAST_ACK",
156 [TCP_CONNTRACK_TIME_WAIT
] = "TIME_WAIT",
157 [TCP_CONNTRACK_CLOSE
] = "CLOSE",
158 [TCP_CONNTRACK_SYN_SENT2
] = "SYN_SENT2",
161 const uint8_t states
[] = {
162 TCP_CONNTRACK_SYN_SENT
,
163 TCP_CONNTRACK_SYN_RECV
,
164 TCP_CONNTRACK_ESTABLISHED
,
165 TCP_CONNTRACK_FIN_WAIT
,
166 TCP_CONNTRACK_CLOSE_WAIT
,
167 TCP_CONNTRACK_LAST_ACK
,
168 TCP_CONNTRACK_TIME_WAIT
,
170 TCP_CONNTRACK_SYN_SENT2
,
174 static void signal_handler(int number
)
186 static void help(void)
188 printf("\n%s %s, top-like netfilter TCP/UDP flow tracking\n",
189 PROGNAME_STRING
, VERSION_STRING
);
190 puts("http://www.netsniff-ng.org\n\n"
191 "Usage: flowtop [options]\n"
193 " -T|--tcp Show only TCP flows (default)\n"
194 " -U|--udp Show only UDP flows\n"
195 " -s|--show-src Also show source, not only dest\n"
196 " --city-db <path> Specifiy path for geoip city database\n"
197 " --country-db <path> Specifiy path for geoip country database\n"
198 " -v|--version Print version\n"
199 " -h|--help Print this help\n\n"
204 " If netfilter is not running, you can activate it with i.e.:\n"
205 " iptables -A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT\n"
206 " iptables -A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT\n\n"
207 "Please report bugs to <bugs@netsniff-ng.org>\n"
208 "Copyright (C) 2011-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
209 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
210 "License: GNU GPL version 2.0\n"
211 "This is free software: you are free to change and redistribute it.\n"
212 "There is NO WARRANTY, to the extent permitted by law.\n");
216 static void version(void)
218 printf("\n%s %s, top-like netfilter TCP/UDP flow tracking\n",
219 PROGNAME_STRING
, VERSION_STRING
);
220 puts("http://www.netsniff-ng.org\n\n"
221 "Please report bugs to <bugs@netsniff-ng.org>\n"
222 "Copyright (C) 2011-2012 Daniel Borkmann <daniel@netsniff-ng.org>\n"
223 "Copyright (C) 2011-2012 Emmanuel Roullit <emmanuel@netsniff-ng.org>\n"
224 "License: GNU GPL version 2.0\n"
225 "This is free software: you are free to change and redistribute it.\n"
226 "There is NO WARRANTY, to the extent permitted by law.\n");
230 static void screen_init(WINDOW
**screen
)
232 (*screen
) = initscr();
235 keypad(stdscr
, TRUE
);
236 nodelay(*screen
, TRUE
);
241 static inline uint16_t get_port(uint16_t src
, uint16_t dst
)
248 /* XXX: Is there a better way to determine? */
249 if (src
< dst
&& src
< 1024) {
251 } else if (dst
< src
&& dst
< 1024) {
254 tmp1
= lookup_port_tcp(src
);
255 tmp2
= lookup_port_tcp(dst
);
258 } else if (!tmp1
&& tmp2
) {
269 static void screen_update(WINDOW
*screen
, struct flow_list
*fl
, int skip_lines
)
273 struct flow_entry
*n
;
276 maxy
= getmaxy(screen
);
279 init_pair(1, COLOR_RED
, COLOR_BLACK
);
280 init_pair(2, COLOR_BLUE
, COLOR_BLACK
);
281 init_pair(3, COLOR_YELLOW
, COLOR_BLACK
);
282 init_pair(4, COLOR_GREEN
, COLOR_BLACK
);
288 mvwprintw(screen
, 1, 2, "Kernel netfilter TCP/UDP flow statistics, [+%d]",
291 if (rcu_dereference(fl
->head
) == NULL
)
292 mvwprintw(screen
, line
, 2, "(No active sessions! Is netfilter running?)");
295 /* Yes, that's lame :-P */
296 for (i
= 0; i
< sizeof(states
); i
++) {
297 n
= rcu_dereference(fl
->head
);
299 while (n
&& maxy
> 0) {
302 if (n
->tcp_state
!= states
[i
] ||
303 (i
!= TCP_CONNTRACK_NONE
&&
304 n
->tcp_state
== TCP_CONNTRACK_NONE
) ||
306 get_port(n
->port_src
, n
->port_dst
) == 53) {
307 n
= rcu_dereference(n
->next
);
311 if (skip_lines
> 0) {
312 n
= rcu_dereference(n
->next
);
317 snprintf(tmp
, sizeof(tmp
), "%u/%s", n
->procnum
,
318 basename(n
->cmdline
));
319 tmp
[sizeof(tmp
) - 1] = 0;
321 mvwprintw(screen
, line
, 2, "[");
322 attron(COLOR_PAIR(3));
323 printw("%s", n
->procnum
> 0 ? tmp
: "bridged(?)");
324 attroff(COLOR_PAIR(3));
325 printw("]:%s:%s[", l3proto2str
[n
->l3_proto
],
326 proto2str
[n
->l4_proto
]);
327 attron(COLOR_PAIR(3));
328 printw("%s", state2str
[n
->tcp_state
]);
329 attroff(COLOR_PAIR(3));
332 if (n
->tcp_state
!= TCP_CONNTRACK_NONE
) {
333 printw("%s -> ", lookup_port_tcp(get_port(n
->port_src
,
336 printw("%s -> ", lookup_port_udp(get_port(n
->port_src
,
341 attron(COLOR_PAIR(1));
342 mvwprintw(screen
, ++line
, 8, "src: %s", n
->rev_dns_src
);
343 attroff(COLOR_PAIR(1));
344 printw(":%u (", ntohs(n
->port_src
));
345 attron(COLOR_PAIR(4));
346 printw("%s", (strlen(n
->country_src
) > 0 ?
347 n
->country_src
: "N/A"));
348 attroff(COLOR_PAIR(4));
349 printw(", %s) => ", (strlen(n
->city_src
) > 0 ?
350 n
->city_src
: "N/A"));
352 attron(COLOR_PAIR(2));
353 mvwprintw(screen
, ++line
, 8, "dst: %s", n
->rev_dns_dst
);
354 attroff(COLOR_PAIR(2));
355 printw(":%u (", ntohs(n
->port_dst
));
356 attron(COLOR_PAIR(4));
357 printw("%s", strlen(n
->country_dst
) > 0 ?
358 n
->country_dst
: "N/A");
359 attroff(COLOR_PAIR(4));
360 printw(", %s)", strlen(n
->city_dst
) > 0 ?
361 n
->city_dst
: "N/A");
365 n
= rcu_dereference(n
->next
);
375 static void screen_end(void)
380 static void presenter(void)
383 WINDOW
*screen
= NULL
;
385 dissector_init_ethernet(0);
386 screen_init(&screen
);
387 rcu_register_thread();
405 if (skip_lines
> SCROLL_MAX
)
406 skip_lines
= SCROLL_MAX
;
413 screen_update(screen
, &flow_list
, skip_lines
);
417 rcu_unregister_thread();
419 dissector_cleanup_ethernet();
422 static inline const char *make_n_a(const char *p
)
427 static void walk_process(char *process
, struct flow_entry
*n
)
434 if (snprintf(path
, sizeof(path
), "/proc/%s/fd", process
) == -1)
435 panic("giant process name! %s\n", process
);
441 while ((ent
= readdir(dir
))) {
444 if (snprintf(path
, sizeof(path
), "/proc/%s/fd/%s",
445 process
, ent
->d_name
) < 0)
447 if (stat(path
, &statbuf
) < 0)
449 if (S_ISSOCK(statbuf
.st_mode
) && n
->inode
== statbuf
.st_ino
) {
450 memset(n
->cmdline
, 0, sizeof(n
->cmdline
));
451 snprintf(path
, sizeof(path
), "/proc/%s/exe", process
);
452 rc
= readlink(path
, n
->cmdline
, sizeof(n
->cmdline
) - 1);
455 panic("readlink error: %s\n", strerror(errno
));
457 n
->procnum
= atoi(process
);
464 /* Derived from ifpromisc, Fred N. van Kempen, GPL v2.0 */
465 /* n->inode must be set */
466 static void walk_processes(struct flow_entry
*n
)
472 memset(n
->cmdline
, 0, sizeof(n
->cmdline
));
476 dir
= opendir("/proc");
478 panic("Cannot open /proc!\n");
480 while ((ent
= readdir(dir
)))
481 if (strspn(ent
->d_name
, "0123456789") == strlen(ent
->d_name
))
482 walk_process(ent
->d_name
, n
);
487 static int get_inode_from_local_port(int port
, const char *proto
, int ip6
)
494 memset(path
, 0, sizeof(path
));
495 snprintf(path
, sizeof(path
), "/proc/net/%s%s", proto
, ip6
? "6" : "");
496 proc
= fopen(path
, "r");
499 memset(buff
, 0, sizeof(buff
));
500 while (fgets(buff
, sizeof(buff
), proc
) != NULL
) {
501 int lport
= 0, inode
= 0;
502 buff
[sizeof(buff
) - 1] = 0;
503 if (sscanf(buff
, "%*u: %*X:%X %*X:%*X %*X %*X:%*X %*X:%*X "
504 "%*X %*u %*u %u", &lport
, &inode
) == 2) {
510 memset(buff
, 0, sizeof(buff
));
517 static void flow_entry_from_ct(struct flow_entry
*n
, struct nf_conntrack
*ct
)
519 n
->flow_id
= nfct_get_attr_u32(ct
, ATTR_ID
);
520 n
->use
= nfct_get_attr_u32(ct
, ATTR_USE
);
521 n
->status
= nfct_get_attr_u32(ct
, ATTR_STATUS
);
522 n
->l3_proto
= nfct_get_attr_u8(ct
, ATTR_ORIG_L3PROTO
);
523 n
->l4_proto
= nfct_get_attr_u8(ct
, ATTR_ORIG_L4PROTO
);
524 n
->ip4_src_addr
= nfct_get_attr_u32(ct
, ATTR_ORIG_IPV4_SRC
);
525 n
->ip4_dst_addr
= nfct_get_attr_u32(ct
, ATTR_ORIG_IPV4_DST
);
527 const uint8_t *ipv6_src
= nfct_get_attr(ct
, ATTR_ORIG_IPV6_SRC
);
529 memcpy(n
->ip6_src_addr
, ipv6_src
, sizeof(n
->ip6_src_addr
));
530 const uint8_t *ipv6_dst
= nfct_get_attr(ct
, ATTR_ORIG_IPV6_DST
);
532 memcpy(n
->ip6_dst_addr
, ipv6_dst
, sizeof(n
->ip6_dst_addr
));
534 n
->port_src
= nfct_get_attr_u16(ct
, ATTR_ORIG_PORT_SRC
);
535 n
->port_dst
= nfct_get_attr_u16(ct
, ATTR_ORIG_PORT_DST
);
536 n
->tcp_state
= nfct_get_attr_u8(ct
, ATTR_TCP_STATE
);
537 n
->tcp_flags
= nfct_get_attr_u8(ct
, ATTR_TCP_FLAGS_ORIG
);
538 n
->counter_pkts
= nfct_get_attr_u64(ct
, ATTR_ORIG_COUNTER_PACKETS
);
539 n
->counter_bytes
= nfct_get_attr_u64(ct
, ATTR_ORIG_COUNTER_BYTES
);
540 n
->timestamp_start
= nfct_get_attr_u64(ct
, ATTR_TIMESTAMP_START
);
541 n
->timestamp_stop
= nfct_get_attr_u64(ct
, ATTR_TIMESTAMP_STOP
);
544 n
->inode
= get_inode_from_local_port(ntohs(n
->port_src
),
545 proto2str
[n
->l4_proto
],
550 /* if this really runs on a router, we try it once and then let it be */
554 /* TODO: IP4 + IP6 */
555 static void flow_entry_get_extended(struct flow_entry
*n
)
557 struct sockaddr_in sa
;
558 struct hostent
*hent
;
559 GeoIPRecord
*gir_src
, *gir_dst
;
563 if (ntohs(n
->port_src
) == 53 || ntohs(n
->port_dst
) == 53)
566 memset(&sa
, 0, sizeof(sa
));
567 sa
.sin_family
= PF_INET
; //XXX: IPv4
568 sa
.sin_addr
.s_addr
= n
->ip4_src_addr
;
569 getnameinfo((struct sockaddr
*) &sa
, sizeof(sa
), n
->rev_dns_src
,
570 sizeof(n
->rev_dns_src
), NULL
, 0, NI_NUMERICHOST
);
572 hent
= gethostbyaddr(&sa
.sin_addr
, sizeof(sa
.sin_addr
), PF_INET
);
574 memset(n
->rev_dns_src
, 0, sizeof(n
->rev_dns_src
));
575 memcpy(n
->rev_dns_src
, hent
->h_name
,
576 min(sizeof(n
->rev_dns_src
), strlen(hent
->h_name
)));
579 gir_src
= GeoIP_record_by_ipnum(gi_city
, ntohl(n
->ip4_src_addr
));
581 const char *country
=
582 make_n_a(GeoIP_country_name_by_ipnum(gi_country
,
583 ntohl(n
->ip4_src_addr
)));
584 const char *city
= make_n_a(gir_src
->city
);
585 memcpy(n
->country_src
, country
,
586 min(sizeof(n
->country_src
), strlen(country
)));
587 memcpy(n
->city_src
, city
,
588 min(sizeof(n
->city_src
), strlen(city
)));
591 memset(&sa
, 0, sizeof(sa
));
592 sa
.sin_family
= PF_INET
; //XXX: IPv4
593 sa
.sin_addr
.s_addr
= n
->ip4_dst_addr
;
594 getnameinfo((struct sockaddr
*) &sa
, sizeof(sa
), n
->rev_dns_dst
,
595 sizeof(n
->rev_dns_dst
), NULL
, 0, NI_NUMERICHOST
);
597 hent
= gethostbyaddr(&sa
.sin_addr
, sizeof(sa
.sin_addr
), PF_INET
);
599 memset(n
->rev_dns_dst
, 0, sizeof(n
->rev_dns_dst
));
600 memcpy(n
->rev_dns_dst
, hent
->h_name
,
601 min(sizeof(n
->rev_dns_dst
), strlen(hent
->h_name
)));
604 gir_dst
= GeoIP_record_by_ipnum(gi_city
, ntohl(n
->ip4_dst_addr
));
606 const char *country
=
607 make_n_a(GeoIP_country_name_by_ipnum(gi_country
,
608 ntohl(n
->ip4_dst_addr
)));
609 const char *city
= make_n_a(gir_dst
->city
);
610 memcpy(n
->country_dst
, country
,
611 min(sizeof(n
->country_dst
), strlen(country
)));
612 memcpy(n
->city_dst
, city
,
613 min(sizeof(n
->city_dst
), strlen(city
)));
617 static void flow_list_init(struct flow_list
*fl
)
620 spinlock_init(&fl
->lock
);
623 static struct flow_entry
*__flow_list_find_by_id(struct flow_list
*fl
, uint32_t id
)
625 struct flow_entry
*n
= rcu_dereference(fl
->head
);
627 if (n
->flow_id
== id
)
629 n
= rcu_dereference(n
->next
);
634 static struct flow_entry
*__flow_list_find_prev_by_id(struct flow_list
*fl
, uint32_t id
)
636 struct flow_entry
*n
= rcu_dereference(fl
->head
);
637 if (n
->flow_id
== id
)
639 while (rcu_dereference(n
->next
) != NULL
) {
640 if (rcu_dereference(n
->next
)->flow_id
== id
)
642 n
= rcu_dereference(n
->next
);
647 static void flow_list_new_entry(struct flow_list
*fl
, struct nf_conntrack
*ct
)
649 struct flow_entry
*n
= xzmalloc(sizeof(*n
));
651 rcu_assign_pointer(n
->next
, fl
->head
);
652 rcu_assign_pointer(fl
->head
, n
);
653 flow_entry_from_ct(n
, ct
);
654 flow_entry_get_extended(n
);
657 static void flow_list_update_entry(struct flow_list
*fl
, struct nf_conntrack
*ct
)
660 uint32_t id
= nfct_get_attr_u32(ct
, ATTR_ID
);
661 struct flow_entry
*n
;
662 n
= __flow_list_find_by_id(fl
, id
);
664 n
= xzmalloc(sizeof(*n
));
666 rcu_assign_pointer(n
->next
, fl
->head
);
667 rcu_assign_pointer(fl
->head
, n
);
670 flow_entry_from_ct(n
, ct
);
672 flow_entry_get_extended(n
);
675 static void flow_list_destroy_entry(struct flow_list
*fl
, struct nf_conntrack
*ct
)
677 uint32_t id
= nfct_get_attr_u32(ct
, ATTR_ID
);
678 struct flow_entry
*n1
, *n2
;
680 n1
= __flow_list_find_by_id(fl
, id
);
682 n2
= __flow_list_find_prev_by_id(fl
, id
);
684 rcu_assign_pointer(n2
->next
, n1
->next
);
685 rcu_assign_pointer(n1
->next
, NULL
);
689 rcu_assign_pointer(fl
->head
, NULL
);
694 static void flow_list_destroy(struct flow_list
*fl
)
696 struct flow_entry
*n
;
698 while (fl
->head
!= NULL
) {
699 n
= rcu_dereference(fl
->head
->next
);
700 rcu_assign_pointer(fl
->head
->next
, NULL
);
702 rcu_assign_pointer(fl
->head
, n
);
706 spinlock_destroy(&fl
->lock
);
709 static int collector_cb(enum nf_conntrack_msg_type type
,
710 struct nf_conntrack
*ct
,
718 spinlock_lock(&flow_list
.lock
);
721 flow_list_new_entry(&flow_list
, ct
);
724 flow_list_update_entry(&flow_list
, ct
);
727 flow_list_destroy_entry(&flow_list
, ct
);
732 spinlock_unlock(&flow_list
.lock
);
734 return NFCT_CB_CONTINUE
;
737 static int dummy_cb(enum nf_conntrack_msg_type type
, struct nf_conntrack
*ct
,
743 static void *collector(void *null
)
746 u_int32_t family
= AF_INET
;
747 struct nfct_handle
*handle
;
748 struct nfct_filter
*filter
;
750 handle
= nfct_open(CONNTRACK
, NFCT_ALL_CT_GROUPS
);
752 panic("Cannot create a nfct handle!\n");
755 nfct_callback_register(handle
, NFCT_T_ALL
, dummy_cb
, NULL
);
756 nfct_query(handle
, NFCT_Q_DUMP
, &family
);
759 handle
= nfct_open(CONNTRACK
, NFCT_ALL_CT_GROUPS
);
761 panic("Cannot create a nfct handle!\n");
763 nfct_query(handle
, NFCT_Q_FLUSH
, &family
);
765 filter
= nfct_filter_create();
767 panic("Cannot create a nfct filter!\n");
768 if (what
& INCLUDE_UDP
)
769 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_UDP
);
770 if (what
& INCLUDE_TCP
)
771 nfct_filter_add_attr_u32(filter
, NFCT_FILTER_L4PROTO
, IPPROTO_TCP
);
773 struct nfct_filter_ipv4 filter_ipv4
= {
774 .addr
= ntohl(INADDR_LOOPBACK
),
778 nfct_filter_set_logic(filter
, NFCT_FILTER_SRC_IPV4
,
779 NFCT_FILTER_LOGIC_NEGATIVE
);
780 nfct_filter_add_attr(filter
, NFCT_FILTER_SRC_IPV4
, &filter_ipv4
);
782 struct nfct_filter_ipv6 filter_ipv6
= {
783 .addr
= { 0x0, 0x0, 0x0, 0x1 },
784 .mask
= { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
787 nfct_filter_set_logic(filter
, NFCT_FILTER_SRC_IPV6
,
788 NFCT_FILTER_LOGIC_NEGATIVE
);
789 nfct_filter_add_attr(filter
, NFCT_FILTER_SRC_IPV6
, &filter_ipv6
);
791 ret
= nfct_filter_attach(nfct_fd(handle
), filter
);
793 panic("Cannot attach filter to handle!\n");
795 nfct_filter_destroy(filter
);
798 gi_country
= GeoIP_open(path_country_db
, GEOIP_MMAP_CACHE
);
800 gi_country
= GeoIP_open_type(GEOIP_COUNTRY_EDITION
,
804 gi_city
= GeoIP_open(path_city_db
, GEOIP_MMAP_CACHE
);
806 gi_city
= GeoIP_open_type(GEOIP_CITY_EDITION_REV1
,
808 if (!gi_country
|| !gi_city
)
809 panic("Cannot open GeoIP database!\n");
811 GeoIP_set_charset(gi_country
, GEOIP_CHARSET_UTF8
);
812 GeoIP_set_charset(gi_city
, GEOIP_CHARSET_UTF8
);
814 flow_list_init(&flow_list
);
816 rcu_register_thread();
818 nfct_callback_register(handle
, NFCT_T_ALL
, collector_cb
, NULL
);
823 rcu_unregister_thread();
825 flow_list_destroy(&flow_list
);
827 GeoIP_delete(gi_city
);
828 GeoIP_delete(gi_country
);
835 xfree(path_country_db
);
840 int main(int argc
, char **argv
)
843 int ret
, c
, opt_index
, what_cmd
= 0;
845 check_for_root_maybe_die();
847 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
848 &opt_index
)) != EOF
) {
851 what_cmd
|= INCLUDE_TCP
;
854 what_cmd
|= INCLUDE_UDP
;
860 path_city_db
= xstrdup(optarg
);
863 path_country_db
= xstrdup(optarg
);
875 panic("Option -%c requires an argument!\n",
879 whine("Unknown option character "
880 "`0x%X\'!\n", optopt
);
893 register_signal(SIGINT
, signal_handler
);
894 register_signal(SIGHUP
, signal_handler
);
896 ret
= pthread_create(&tid
, NULL
, collector
, NULL
);
898 panic("Cannot create phthread!\n");