gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:12 25 4
gpt4 key购买 nike

我正在尝试创建一个嗅探器,它使用 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_open_live()。负超时的行为是未定义的;您应该指定一个 值。该值以毫秒为单位; tcpdump 使用 1000,即 1 秒,而 Wireshark 使用 250,即 1/4 秒。

您还在默认设备上进行捕获,因为您正在使用 pcap_lookupdev()。这很少(如果有的话)环回设备,因此如果您在默认设备上捕获,如果您使用过滤器 host 127.0.0.1<,您将很少(如果有的话)看到任何流量。如果服务器和客户端与您运行程序的机器在同一台机器上运行,则需要在环回设备上进行捕获,该设备名为"lo" 在 Linux 上。如果服务器和客户端都在同一台机器上运行,那么您的流量将使用地址 127.0.0.1,您需要选择一个不同的地址。

关于客户端和服务器 Libpcap 之间的 Linux 嗅探器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36248512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com