Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
客户端和服务器Libpcap之间的Linux嗅探器_Linux_Libpcap_Sniffer - Fatal编程技术网

客户端和服务器Libpcap之间的Linux嗅探器

客户端和服务器Libpcap之间的Linux嗅探器,linux,libpcap,sniffer,Linux,Libpcap,Sniffer,我正在尝试创建一个嗅探器,它使用inet地址127.0.0.1(环回地址)读取从服务器发送到客户端的文本。即使客户端从服务器接收到数据,程序也会保持暂停 嗅探器代码: int main(int argc,char **argv) { char *dev; char errbuf[PCAP_ERRBUF_SIZE]; pcap_t* descr; bpf_u_int32 maskp; /* subnet mask *

我正在尝试创建一个嗅探器,它使用inet地址127.0.0.1(环回地址)读取从服务器发送到客户端的文本。即使客户端从服务器接收到数据,程序也会保持暂停

嗅探器代码:

int main(int argc,char **argv)
{ 
    char *dev; 
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t* descr;
    bpf_u_int32 maskp;          /* subnet mask               */
    bpf_u_int32 netp;           /* ip*/
    struct bpf_program fp;      /* hold compiled program            */
    char *filter = "host 127.0.0.1";
    //char *filter = "port 5000";

    dev = pcap_lookupdev(errbuf);
    if(dev == NULL)
    { printf("%s\n",errbuf); exit(1); }
                printf("call success");

    /* ask pcap for the network address and mask of the device */
    pcap_lookupnet(dev,&netp,&maskp,errbuf);

    descr = pcap_open_live(dev,BUFSIZ,1,-1,errbuf);
    if(descr == NULL)
    { printf("pcap_open_live(): %s\n",errbuf); exit(1); }

                    /* Lets try and compile the program.. non-optimized */
    if(pcap_compile(descr,&fp,filter,0,netp) == -1)
    { fprintf(stderr,"Error calling pcap_compile\n"); exit(1); }

    /* set the compiled program as the filter */
    if(pcap_setfilter(descr,&fp) == -1)
    { fprintf(stderr,"Error setting filter\n"); exit(1); }


    pcap_loop(descr,2,callback,NULL);

    fprintf(stdout,"\nfinished\n");
    return 0;
}
您正在将-1作为to_ms参数传递给
pcap\u open\u live()
。负超时的行为未定义;您应该指定一个正值。该值以毫秒为单位;tcpdump使用1000,即1秒,而Wireshark使用250,即1/4秒


当您使用
pcap\u lookupdev()
时,您也在默认设备上进行捕获。这很少(如果有的话)是环回设备,因此如果您在默认设备上捕获,那么如果您使用过滤器
主机127.0.0.1
,您将很少(如果有的话)看到任何流量。如果服务器和客户端都在与运行程序的机器相同的机器上运行,则需要在环回设备上进行捕获,该设备在Linux上名为
“lo”
。如果服务器和客户端不在同一台计算机上运行,则您的通信量将不会使用地址127.0.0.1,您需要选择不同的地址。

服务器和客户端正在使用端口5000。我会看到的。但是,对环回地址“char filter=“127.0.0.1”使用筛选器的方法正确吗?还是需要按端口5000进行筛选?