diff options
Diffstat (limited to 'gg_sniff/pcap.c')
-rw-r--r-- | gg_sniff/pcap.c | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/gg_sniff/pcap.c b/gg_sniff/pcap.c index b13a486..4a2d6fb 100644 --- a/gg_sniff/pcap.c +++ b/gg_sniff/pcap.c @@ -4,20 +4,43 @@ #define PCAP_COUNT 20 #define PCAP_TO 300 -static pcap_t * -ggsniff_pcap_init(void) +static pcap_t *my_pcap_open_live(const char *, int, int, int, + char *, u_int, u_int) + +static pcap_t *_pcap; +static struct event *_pcap_ev; + +int +ggsniff_pcap_init(struct event_base *ev_base) { + char errbuf[PCAP_ERRBUF_SIZE]; + struct bpf_program bprog; + int pcap_fd; + pcap = my_pcap_open_live(PCAP_INTERFACE, PCAP_SNAPLEN, 1, PCAP_TO, errbuf, -1, 0); if (pcap == NULL) - fatal("capture: pcap_open_live failed on interface %s\n" - "with snaplen %d : %s", - PCAP_INTERFACE, PCAP_SNAPLEN, errbuf); + gg_log_fatal("capture: pcap_open_live failed on interface %s\n" + "with snaplen %d : %s", + PCAP_INTERFACE, PCAP_SNAPLEN, errbuf); if (pcap_compile(pcap, &bprog, PCAP_FILTER, 0, 0) < 0) - fatal("capture: pcap_compile failed with filter %s : %s", - PCAP_FILTER, pcap_geterr(pcap)); + gg_log_fatal("capture: pcap_compile failed with filter %s : %s", + PCAP_FILTER, pcap_geterr(pcap)); if (pcap_setfilter(pcap, &bprog) < 0) - fatal("capture: pcap_setfilter failed : %s", - pcap_geterr(pcap)); + gg_log_fatal("capture: pcap_setfilter failed : %s", + pcap_geterr(pcap)); + pcap_fd = pcap_fileno(_pcap); + + _pcap_ev = event_new(ev_base, pcap_fd, EV_READ|EV_PERSIST, cb_pcap, NULL); + event_add(_pcap_ev, NULL); + + return 1; +} + +void +ggsniff_pcap_shutdown(void) +{ + event_del(_pcap_ev); + pcap_close(_pcap); } /* |