какой то таймаут в работе pcap
написал такую простенькую программку, чтобы потом добавить её в программу, которая будет уведомление показывать на экране, если мой компьютер кто-то пингует. почему то приходится ждать определенного времени, прежде чем произойдет вывод пакета, хотя в документации сказано, чтобы этого не было, нужно использовать pcap_set_timeout.
#include <stdio.h>
#include <pcap/pcap.h>
static void read_packets (u_char *user, const struct pcap_pkthdr *h, const u_char *bytes) {
printf("%s: !\n", user);
fflush(stdout);
}
int main (int argc, char **argv) {
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp;
char dev[] = "wlp3s0";
bpf_u_int32 mask;
bpf_u_int32 net;
char filter_exp[] = "icmp";
if (pcap_lookupnet (dev, &net, &mask, errbuf) == -1) {
fprintf (stderr, "Can't get netmask for device %s\n", dev);
net = 0;
mask = 0;
}
pcap_t *handle = pcap_create (dev, errbuf);
pcap_activate (handle);
if (pcap_compile (handle, &fp, filter_exp, 0, net) == -1) {
fprintf (stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr (handle));
return (2);
}
if (pcap_setfilter (handle, &fp) == -1) {
fprintf (stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr (handle));
return (2);
}
if (pcap_set_timeout (handle, 1000) == -1) {
fprintf (stderr, "Couldn't set timeout: %s\n", pcap_geterr (handle));
}
pcap_loop (handle, 0, read_packets, "cf");
}
Ответы (1 шт):
pcap_set_timeout - set the packet buffer timeout for a not-yet-activated capture handle
Вольный перевод:
pcap_set_timeout - устанавливает таймаут пакетного буфера на ручке захвата, которая ещё не активирована
т.е. pcap_set_timeout() нужно вызывать до pcap_activate().
Ошибку было бы сразу видно, если корректно сравнивавать код возврата с нулём, а не с -1:
if (pcap_set_timeout (handle, 1000) < 0) {
/* pcap_geterr() -> can't perform operation on activated capture */
}