41 char errbuf[PCAP_ERRBUF_SIZE];
43 pcap_if_t *alldevsp = NULL;
44 bpf_u_int32 netmask, network;
45 struct bpf_program filter_prog;
53 g_message (
"no free pcap");
59 if (pcap_findalldevs (&alldevsp, errbuf) < 0)
60 g_message (
"Error for pcap_findalldevs(): %s", errbuf);
62 iface = alldevsp->name;
65 ret = pcap_open_live (iface, 1500, 0, 1, errbuf);
68 g_message (
"%s", errbuf);
70 pcap_freealldevs (alldevsp);
74 if (pcap_lookupnet (iface, &network, &netmask, errbuf) < 0)
76 g_message (
"pcap_lookupnet failed: %s", errbuf);
78 pcap_freealldevs (alldevsp);
83 if (pcap_compile (ret, &filter_prog, filter, 1, netmask) < 0)
86 snprintf (buffer,
sizeof (buffer),
"pcap_compile: Filter \"%s\"", filter);
89 pcap_freealldevs (alldevsp);
94 if (pcap_setnonblock (ret, 1, NULL) == -1)
97 g_message (
"call to pcap_setnonblock failed, some plugins/scripts will"
98 " hang/freeze. Upgrade your version of libcap!");
101 if (pcap_setfilter (ret, &filter_prog) < 0)
104 if (alldevsp != NULL)
105 pcap_freealldevs (alldevsp);
106 pcap_freecode (&filter_prog);
111 pcap_freecode (&filter_prog);
112 if (alldevsp != NULL)
113 pcap_freealldevs (alldevsp);
122 struct pcap_pkthdr head;
125 gettimeofday (&timeout, NULL);
126 timeout.tv_sec += tv->tv_sec;
127 timeout.tv_usec += tv->tv_usec;
128 while (timeout.tv_usec >= 1000000)
131 timeout.tv_usec -= 1000000;
136 p = (u_char *) pcap_next (
pcaps[bpf], &head);
137 *caplen = head.caplen;
140 gettimeofday (&now, NULL);
143 !((now.tv_sec > timeout.tv_sec)
144 || (now.tv_sec == timeout.tv_sec && now.tv_usec >= timeout.tv_usec)));