异步的libpcap:丢包? [英] Asynchronous libpcap: losing packets?
问题描述
我有发送一套TCP SYN数据包的到主机(使用原始套接字)中的程序并使用 libpcap的
(有过滤器),得到的反应。我想在异步I / O框架来实现这一点,但似乎的libpcap
缺少一些响应(即一个系列的第一个数据包时,它需要小于 100微秒
的TCP SYN和响应)之间。该PCAP手柄的设置是这样的:
I have a program that sends a set of TCP SYN packets to a host (using raw sockets) and uses libpcap
(with a filter) to obtain the responses. I'm trying to implement this in an asynchronous I/O framework, but it seems that libpcap
is missing some of the responses (namely the first packets of a series when it takes less than 100 microseconds
between the TCP SYN and the response). The pcap handle is setup like this:
pcap_t* pcap = pcap_open_live(NULL, -1, false, -1, errorBuffer);
pcap_setnonblock(pcap, true, errorBuffer);
然后我添加过滤器(包含在filterEx pression字符串):
Then I add a filter (contained on the filterExpression string):
struct bpf_program filter;
pcap_compile(pcap, &filter, filterExpression.c_str(), false, 0);
pcap_setfilter(pcap, &filter);
pcap_freecode(&filter);
和上一个循环,发送每个数据包后,我用select知道,如果我可以从libpcap的读取:
And on a loop, after sending each packet, I use select to know 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);
和阅读:
if (FD_ISSET(pcapFd, &fdRead)) {
struct pcap_pkthdr* pktHeader;
const u_char* pktData;
if (pcap_next_ex(pcap, &pktHeader, &pktData) > 0) {
// Process received response.
}
else {
// Nothing to receive (or error).
}
}
正如我以前说过,某些数据包会丢失(落入没有领取其他人)。我知道这些数据包都是存在的,因为我可以(使用同步方式捕捉它们的tcpdump
或正在运行的线程 pcap_loop
)。我失去了一些细节吗?或者,这是一个问题,的libpcap
?
As I said before, some of the packets are missed (falling into the "nothing to receive" else). I know these packets are there, because I can capture them on a synchronous fashion (using tcpdump
or a thread running pcap_loop
). Am I missing some detail here? Or is this an issue with libpcap
?
推荐答案
这似乎是使用libpcap的Linux下的内存映射的问题。请参阅我的其他问题了解详情。
This seem to be an issue with libpcap using memory mapping under Linux. Please see my other question for details.
这篇关于异步的libpcap:丢包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!