【发布时间】:2013-06-15 17:24:39
【问题描述】:
对不起,如果这是一个蹩脚的问题。我是 tcpdump 和 pcap 的新手。我正在使用 pcap 静态库来开发和应用程序来侦听指定端口上的 TCP 数据。我建立了一个小型原型,它在嗅探通过端口 80(HTTP 的默认值)发送的 tcp 数据包时运行良好。但是,我想查看进出端口 5984 的 HTTP 数据包(这是 CouchDB 使用的默认端口)。由于某种原因,我的应用程序没有注意到/嗅探/看到此端口上的任何数据包。由于我不是经验丰富的网络开发人员,因此我可能缺少一些基本知识。
我不想在此处粘贴整个应用程序,但我可以添加查找问题所需的任何代码。请告诉我。
这是我的 pcap 过滤器表达式:
char filter_exp[] = "tcp port 5984";/* The filter expression */
过滤器编译并在 pcap 会话上设置没有问题。会话设置为以混杂模式运行。
//get a pcap session
//args device, # of packets to capture, promisc mode, timeout, err buff
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return(2);
//compile our filter
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);
}
//set the filter
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(2);
}
//begin sniffing packets. cnt -1: keep sniffing until err occurs
//last arg is optional. It can be used to pass additonal information to callback
pcap_loop(handle, -1, got_packet, NULL);
'got_packet' 是我的回调函数。这会使用相同的过滤器多次调用,但使用端口 80 代替 5984。
我尝试过使用 Curl:$ curl http://localhost:5984/test
只是为了它我已经尝试使用环回:$ curl http://127.0.0.1:5984/test
这些都没有被我的 pcap 应用程序注意到。但是,如果我将过滤器更改为侦听端口 80 并执行 $ curl http://www.google.com
我可以看到数据包通过。我忽略或不理解什么?
非常感谢!
-尼克
【问题讨论】:
-
这是在哪个操作系统上运行的?而且您知道与 127.0.0.1 建立连接所使用的设备与
curling google 时使用的设备不同? -
程序是否以管理员权限运行?
-
程序没有以 root 权限运行。 @chacham 好点……我会试试的。
-
@alk 我在 Mac OSx 10.8.3 上运行它,不,我不知道会使用不同的适配器。
标签: c++ c network-programming pcap tcpdump