26 #include <gvm/base/logging.h> 29 #define NUM_CLIENTS 128 35 #define G_LOG_DOMAIN "lib misc" 43 char *msg = pcap_geterr (p);
44 g_message (
"%s : %s",
prefix, msg);
54 char errbuf[PCAP_ERRBUF_SIZE];
56 bpf_u_int32 netmask, network;
57 struct bpf_program filter_prog;
65 g_message (
"no free pcap");
70 iface = pcap_lookupdev (errbuf);
72 ret = pcap_open_live (iface, 1500, 0, 1, errbuf);
75 g_message (
"%s", errbuf);
79 if (pcap_lookupnet (iface, &network, &netmask, 0) < 0)
81 g_message (
"pcap_lookupnet failed");
86 if (pcap_compile (ret, &filter_prog, filter, 1, netmask) < 0)
93 if (pcap_setnonblock (ret, 1, NULL) == -1)
96 g_message (
"call to pcap_setnonblock failed, some plugins/scripts will" 97 " hang/freeze. Upgrade your version of libcap!");
100 if (pcap_setfilter (ret, &filter_prog) < 0)
107 pcap_freecode (&filter_prog);
115 struct pcap_pkthdr head;
118 gettimeofday (&timeout, NULL);
119 timeout.tv_sec += tv->tv_sec;
120 timeout.tv_usec += tv->tv_usec;
121 while (timeout.tv_usec >= 1000000)
124 timeout.tv_usec -= 1000000;
129 p = (u_char *) pcap_next (
pcaps[bpf], &head);
130 *caplen = head.caplen;
133 gettimeofday (&now, NULL);
136 !((now.tv_sec > timeout.tv_sec)
137 || (now.tv_sec == timeout.tv_sec && now.tv_usec >= timeout.tv_usec)));
145 struct timeval tv = {0, 100000};
153 return pcap_datalink (
pcaps[bpf]);
159 pcap_close (
pcaps[bpf]);
int bpf_datalink(int bpf)
int bpf_open_live(char *iface, char *filter)
struct timeval timeval(unsigned long val)
u_char * bpf_next(int bpf, int *caplen)
void print_pcap_error(pcap_t *p, char *prefix)
u_char * bpf_next_tv(int bpf, int *caplen, struct timeval *tv)
static void prefix(int n, int i)
static pcap_t * pcaps[NUM_CLIENTS]