C - 用来显示所有接口的pcap_findalldevs卡在了一个无限循环中。

问题描述 投票:0回答:1

我在C语言中使用pcap库工作,我试图创建一个函数来打印所有的接口。

为此,我使用了pcap_findalldevs函数,但是当我试图在for循环中打印名称和描述时,打印会一直持续下去,并且只打印名称 "any "和描述 "Pseudo-device that captures on all interfaces"。

的代码。

char errbuff[PCAP_ERRBUF_SIZE];
pcap_if_t *interface_list;

if(pcap_findalldevs(&interface_list, errbuff) == PCAP_ERROR){
    fprintf(stderr, "Could not list all interfaces: %s", errbuff);
    return ERR;
}

pcap_if_t *interface;
for(interface = interface_list; interface != NULL; interface = interface_list->next){
    printf("Name: %s (%s)\n",
           interface->name, interface->description);
}

pcap_freealldevs(interface_list);

是我忽略了一个错误 还是完全错误?

c loops interface pcap
1个回答
0
投票

在您的 "for "指令中,您使用了 "interface_list "而不是 "interface "变量来访问 "next "字段。

正确的用法应该是 "for(interface = interface_list;interface != NULL;interface = ... 界面->next){"

© www.soinside.com 2019 - 2024. All rights reserved.