Tcpdump - pcap -不能嗅探端口5984上的数据包

Tcpdump - pcap - Cannot sniff packets on port 5984

本文关键字:5984 数据包 pcap 不能 嗅探 Tcpdump      更新时间:2023-10-16

抱歉,如果这是一个蹩脚的问题。我是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: %sn", dev, errbuf);
        return(2);
     //compile our filter
    if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
        fprintf(stderr, "Couldn't parse filter %s: %sn", filter_exp, pcap_geterr(handle));
        return(2);
    }
    //set the filter
    if (pcap_setfilter(handle, &fp) == -1) {
        fprintf(stderr, "Couldn't install filter %s: %sn", 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

我可以看到数据包通过。我忽略或不理解的是什么?

非常感谢!

尼克

如果数据包从您的Mac发送到相同的Mac—例如,如果您正在与"localhost"或127.0.0.1(这是同一件事—"localhost"解析为127.0.0.1)通信,则捕获lo0,而不是en0en1。到127.0.0.1的流量不会在任何真正的网络上发送,它会在内部环路返回到您的机器,因此您必须查看"环回"网络和"环回"接口。

(类似的答案适用于其他UN* x,除了在Linux上,环回接口只称为lo,而不是lo0。在Windows上没有对应的,在某些版本的UN*X上,例如Solaris 10和更早的版本,您无法捕获环回接口。