I have a program that sends a set of TCP SYN packets to a host (using the source sockets) and uses libpcap
(with a filter) to receive responses. I am trying to implement this in an asynchronous I / O structure, but it seems that some answers are missing in libpcap
(namely, the first series packets when less than 100 microseconds
between TCP SYN and response are required). The pcap knob is configured as follows:
pcap_t* pcap = pcap_open_live(NULL, -1, false, -1, errorBuffer); pcap_setnonblock(pcap, true, errorBuffer);
Then I add a filter (contained in the expressionExpression line):
struct bpf_program filter; pcap_compile(pcap, &filter, filterExpression.c_str(), false, 0); pcap_setfilter(pcap, &filter); pcap_freecode(&filter);
And in the loop after sending each package, I use select to find out if I can read from libpcap:
int pcapFd = pcap_get_selectable_fd(pcap); fd_set fdRead; FD_ZERO(&fdRead); FD_SET(pcapFd, &fdRead); select(pcapFd + 1, &fdRead, NULL, NULL, &selectTimeout);
And read it:
if (FD_ISSET(pcapFd, &fdRead)) { struct pcap_pkthdr* pktHeader; const u_char* pktData; if (pcap_next_ex(pcap, &pktHeader, &pktData) > 0) {
As I said, some of the packages are skipped (getting into the "receive nothing"). I know that these packages exist because I can synchronize them synchronously (using tcpdump
or the pcap_loop
stream). Did I miss some details here? Or is this a problem with libpcap
?