阻止pcap和多个I/O`select` [英] blocking pcap and multiple I/O `select `
问题描述
我想使用pcap捕获数据包 然后将捕获的数据包发送到另一台主机 我的源代码片段如下:
I want to use pcap to capture packets and then send the captured packets to another host my source code snippets are like:
for(;;){
pcap_packet = pcap_next(pcap_handler, &pcap_header);
if(pcap_packet !=NULL)
printf("capture a packet with length of %d\n", pcap_header.len);
// send the packet as payload to the sender, excluding the Ethernet Header
n = send(sd_proxy, pcap_packet+ETHERNET_HDR_LEN, pcap_header.len-ETHERNET_HDR_LEN, 0);
if(n<0){
shutdown(connfd, SHUT_RDWR);
close(connfd);
break;
} new
}
所以基本上,我希望程序被pcap_next阻止,一旦
套接字sd_proxy与其他主机建立TCP连接,
如果其他主机启动了活动的TCP关闭,我想检测到这一点
主动关闭.理想情况下,我应该使用select
和n=recv(rd_fd, ...)
如果其他主机启动主动关闭,则select
会注意到rd_fd
出现问题,然后我看是否为'n = 0'.
但是使用pcap时,select无法与pcap配合使用
so basically, I want program to be blocked by the pcap_next, once a
the socket sd_proxy establishes a TCP connection with the other host,
if the other host initiate an active TCP close, I want to detect this
active close. Ideally, I should use a select
and a n=recv(rd_fd, ...)
if the other host initiates a active close, select
will notice there is something with rd_fd
and then I see whether 'n=0' or not.
but with pcap, select can't cooperate with pcap
那么如何完成我的任务? 谢谢!
so how to finish my task? thanks!
推荐答案
select无法与pcap合作
select can't cooperate with pcap
为什么不呢?您是否尝试过在pcap_handler
上使用pcap_get_selectable_fd()
并将结果文件描述符添加到您在select()
调用中设置的文件描述符中?
Why not? Have you tried using pcap_get_selectable_fd()
on pcap_handler
and adding the resulting file descriptor to the file descriptor set in your select()
call?
您正在Linux上工作,因此应该可以正常工作.
You're working on Linux, so that should Just Work.
(如果您正在使用BPF的系统(例如* BSD或OS X)上运行,则可能会遇到问题,至少在这些OS的较旧版本中;各种* BSD都有其select-with-BPF修复了一段时间;在OS X上,我在Lion中修复了它,因此在Snow Leopard和更早版本上可能会出现问题.)
(If you were running on a system that uses BPF, such as *BSD or OS X, you might have problems with this, at least with older versions of those OSes; various *BSDs have had their select-with-BPF bugs fixed for a while; on OS X, I fixed it in Lion, so it could have problems on Snow Leopard and earlier.)
这篇关于阻止pcap和多个I/O`select`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!