我在程序中使用libpcap和libevent。libevent版本1.4和2.0之间的差异如何影响libpcap事件?
相关源代码包括:
const u_int16_t RELAY_PORT = 8000;
pcap_t *create_pcap(const void *dev, pcap_style_t style)
{
pcap_t *handle; /* Session handle */
struct bpf_program fp; /* The compiled filter */
bpf_u_int32 mask; /* The netmask */
bpf_u_int32 net; /* The IP subnet*/
const struct pcap_pkthdr* pcap_header; /* A pointer to pcap_pkthdr structure */
const u_char *pcap_packet; /* The captured packet */
char interface[20];
strcpy(interface, dev);
/* Find the properties for the network interface */
if (pcap_lookupnet(interface, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Pcap counldn't get netmask for device %s: %s\n", interface, errbuf);
net = 0;
mask = 0;
}
handle = pcap_open_live(interface, BUFSIZ, 0, 0, errbuf);
if (handle == NULL) {
fprintf(stderr, "Pcap open live capture failure: %s\n", errbuf);
exit(1);
}
sprintf(filter_exp, "tcp[tcpflags] & (tcp-syn|tcp-ack) == (tcp-syn|tcp-ack) && src port %d || dst port %d", RELAY_PORT, RELAY_PORT);
/* Compile and apply the filter */
if (pcap_compile(handle, &fp, filter_exp, 0, mask) == -1) {
fprintf(stderr, "Pcap parse filter failure: %s\n", pcap_geterr(handle));
exit(1);
}
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Pcap couldn't install filter: %s\n", pcap_geterr(handle));
exit(1);
}
if(style == NONBLOCKING){
if(pcap_setnonblock(handle, 1, errbuf) == -1){
fprintf(stderr, "Pcap set non-blocking fails: %s\n", errbuf);
exit(1);
}
}
return handle;
}
//////////////////////////////////////////////////
void on_capture(int pcapfd, short op, void *arg)
{
int res;
printf("on capture \n");
pcap_t *handle;
handle = (pcap_t *)arg;
fqueue_t* pkt_queue;
/* put all packets in the buffer into the packet FIFO queue
* and then process these packets
* */
pkt_queue = init_fqueue();
res = pcap_dispatch(handle, -1, collect_pkt, (u_char *)pkt_queue);
printf("pcap_dispatch() returns %d\n", res);
if(!res) return;
process_packet(pkt_queue);
}
//////////////////
int pcapfd;
pcap_t *pcap_handle;
struct event pcap_ev;
pcap_handle = create_pcap("eth0", NONBLOCKING);
pcapfd = pcap_get_selectable_fd(pcap_handle);
if(pcapfd<0){
perror("pcap_get_selectable_fd() failed!\n");
exit(1);
}
if (setnonblock(pcapfd) == -1) return -1;
base = event_init();
event_set(&pcap_ev, pcapfd, EV_READ|EV_PERSIST, on_capture, pcap_handle);
event_base_set(base, &pcap_ev);
if(event_add(&pcap_ev, NULL) == -1){
perror("event_add() failed for pcap_ev!\n");
exit(-1);
}
event_base_dispatch(base);
---------------------------------------------
我还寄存器上event_base两个TCP事件
然后我在主机A和主机B运行程序数据包发送给A(on_accept和on_recv。) ,同时我使用tcpdump捕获A上的数据包(tcpdump -i eth0 port 8000)
为了比较,我有两台笔记本电脑充当A,我试图在这两台笔记本电脑上编译并运行程序,一个是Fedora(fedora版本18),一个是Ubuntu(Ubu NTU 14.04.2 LTS)
ubuntu: Linux 3.13.0-61-generic
fedora: Linux 3.11.10-100-fc18.x86_64
在Ubuntu事件按以下顺序
on capture
pcap_dispatch() returns 0
on capture
pcap_dispatch() returns 0
on accept
on recv
但奇怪的是,pcap_dispatch返回0两次被调用。我的期望是,当触发on_capture事件时,pcap_dispatch将在触发on_accept事件之前捕获TCP SYN数据包(TCP数据包在切换到TCP堆栈之前在NIC上捕获)。但我不知道为什么on_capture事件被调用两次,pcap_dispatch()返回0.
在Fedora上,程序按预期工作,pcap_dispatch()可以在on_accept事件之前第一次调用它时捕获数据包。
我使用ldd
来检查每台笔记本电脑上该程序的库。
的Fedora:
$ldd relay
linux-vdso.so.1 => (0x00007fff1d1ad000)
libevent-1.4.so.2 => /lib/libevent-1.4.so.2 (0x00007faca467d000)
libpcap.so.1 => /lib64/libpcap.so.1 (0x00000035b4a00000)
libc.so.6 => /lib64/libc.so.6 (0x00000035b0a00000)
libnsl.so.1 => /lib64/libnsl.so.1 (0x00000035cea00000)
librt.so.1 => /lib64/librt.so.1 (0x00000035b1a00000)
libresolv.so.2 => /lib64/libresolv.so.2 (0x00000035b2e00000)
/lib64/ld-linux-x86-64.so.2 (0x00000035b0200000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00000035b1600000)
Ubuntu的:
$ ldd relay
linux-vdso.so.1 => (0x00007ffd08bc5000)
libevent-2.0.so.5 => /usr/lib/x86_64-linux-gnu/libevent-2.0.so.5 (0x00007eff35f81000)
libpcap.so.0.8 => /usr/lib/x86_64-linux-gnu/libpcap.so.0.8 (0x00007eff35d43000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007eff3597e000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007eff35760000)
/lib64/ld-linux-x86-64.so.2 (0x00007eff361c5000)
确实,无论是libpcap的和libevent的版本是不同的。 我的程序在ubuntu上运行时有什么潜在的问题?如何解决ubuntu上的意外问题? 谢谢!
我不明白。如果我将超时设置为1/10秒(或1/100,无论如何),这意味着如果没有数据包到达,pcap事件仍然会频繁触发,这是不可取的,不是吗?此外,timeout = -1是什么意思? – lily
我读了你的答案和这篇文章https://github.com/jvinet/knock/issues/5。所以基本上有两个原因,为什么不应该使用timeout = 0:1)它可能增加CPU的负担2)“足够的数据包到达”可能意味着“足够的数据包填满缓冲区”在Linux上,这需要很长的时间时间。但在我看来1)和2)彼此冲突。不是吗?有什么其他原因,超时= 0不应该使用? – lily
“pcap事件仍然会被频繁触发,这是不可取的,不是吗?”鉴于我提到的内核错误,是的,它*是可取的;请参阅https://github.com/the-tcpdump-group/libpcap/issues/335#issuecomment-30280794(并且相信我这一次 - 这真的会发生什么,正如我在修复该错误时发现的那样;如果您没有认为这个解释是有道理的,你错过了一些东西)。 –