gpt4 book ai didi

c - pcap_loop 只是等待

转载 作者:行者123 更新时间:2023-11-30 19:21:13 24 4
gpt4 key购买 nike

我正在尝试使用 C 中的 pcap 进行一些嗅探,如 here 所解释的那样我的问题是 pcap_loop 绝对不捕获任何数据包和/或不执行任何操作,我的回调函数从未被调用。我的猜测是超时值,但即使我将其设置为 20(毫秒),也没有任何变化。希望这只是一个我看不到的简单错误,但我会让你们尝试解决它,因为它已经让我的大脑太困惑了!!

谢谢

日光

编辑:我选择 wlan0 作为接口(interface),它适用于链接中给出的程序我的主要:

int main(int argc, char* argv[]) {

// interface & err buff
char *dev, errbuff[PCAP_ERRBUF_SIZE];
int i = 0,inum= -1;
// it filters out only packet from this machin
char filter_exp[] = "ip host localhost";
struct bpf_program fp; /* compiled filter program (expression) */
/* typedef, no need for struct ... */
bpf_u_int32 mask;
bpf_u_int32 net;

int num_packets = 10;

// 1.0+ API pcap version
pcap_if_t * alldevs;
pcap_if_t * pdev;
pcap_t * handle;
// 1st argument interface
if(argc == 2) {
dev = argv[1];
printf("Chosen interface : %s\n",dev);
}



//+1.0 api version
if(pcap_findalldevs(&alldevs,errbuff)){
fprintf(stderr,"findalldev failed to retrieve interface\n %s",errbuff);
return(2);
}
if(alldevs == NULL){
fprintf(stderr,"Retrieved interface is null\n");
return(2);
}
// select all interfaces
for(pdev = alldevs; pdev != NULL;pdev = pdev->next) {
printf("Device %d : ",++i);
print_pcap_if_t(pdev);
//print_pcap_addr(pdev->addresses);

}
printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);
if(inum < 1 || inum > i){
fprintf(stderr,"Device %d not in list.\n",i);
return(2);
}
/* Jump to the selected adapter */
for(pdev=alldevs, i=0; i< inum - 1 ;pdev=pdev->next, i++);
printf("\n-------------------------------------------------\n");

//printf("Chosen device : %s",pdev->name);
//print_pcap_if_t(pdev);

/* activate device */
printf("activating\n");
handle = pcap_open_live(pdev->name,SNAP_LEN,1,1000,errbuff);
if(handle == NULL){
fprintf(stderr,"Could not open device for sniffing");
return(2);
}

/* compile filter */
if(pcap_compile(handle,&fp,filter_exp,0,net) == -1) {
fprintf(stderr,"Could not compile filtering rules");
return(EXIT_FAILURE);
}

/* apply filter */
if(pcap_setfilter(handle,&fp) == -1) {
fprintf(stderr,"Could not set filtering rules");
return(EXIT_FAILURE);
}

printf("Waiting for packets to come in your hands");
fflush(stdout);

pcap_loop(handle,num_packets,got_packet,NULL);

pcap_freecode(&fp);
pcap_close(handle);

pcap_freealldevs(alldevs);
return(0);

}

最佳答案

ip host localhost

“localhost”是IP地址127.0.0.1的名称;它不是您的计算机在 Internet 上的 IP 地址,它是一个特殊的 IP 地址,用于从您的计算机向自身发送 IPv4 数据包(例如,如果您想测试 FTP 服务器,则为“ftp localhost”)通过从计算机上的命令提示符连接到它来在您的计算机上)。

与其他主机之间的流量将不会来自 127.0.0.1,也不会发送至 127.0.0.1。

例如,如果您的计算机的 IP 地址为 10.0.1.2,请尝试“ip host 10.0.1.2”。

关于c - pcap_loop 只是等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20363141/

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