问题描述
我正在尝试使用 net-tools
ifconfig
源代码在我的应用中获取网络接口信息。我只保留了与以太网/WLAN IP 接口有关的部分。
作为初始测试,我将 main()
的 ipconfig.c
替换为:
int main(int argc,char **argv)
{
//FirsT RUN <--------------------- RUNS OK,PRINTS THE INFO
/* Create a channel to the NET kernel. */
if ((skfd = sockets_open(0)) < 0) {
perror("socket");
exit(1);
}
int err = if_print("eth0");
if (err<0) exit(1);
err = if_print("wlan0");
if (err<0) exit(1);
(void) close(skfd);
//SECOND RUN <------------------ FAILS WITH
//ERROR "eth0: error fetching interface information: Bad file descriptor"
if ((skfd = sockets_open(0)) < 0) {
perror("socket");
exit(1);
}
err = if_print("eth0");
if (err<0) exit(1);
err = if_print("wlan0");
if (err<0) exit(1);
(void) close(skfd);
exit(err < 0);
}
其中 if_print()
函数简化为:
static int if_print(char *ifname)
{
int res;
struct interface *ife;
ife = lookup_interface(ifname);
if (!ife) { return -1; }
res = do_if_fetch(ife);
if (res >= 0)
ife_print(ife);
return res;
}
错误打印在 do_if_fetch
中,并在 if_fetch()
中由 ioctl()
调用中的一个(可能是第一个)触发。
int do_if_fetch(struct interface *ife)
{
if (if_fetch(ife) < 0) {
const char *errmsg;
if (errno == ENODEV) {
/* Give better error message for this case. */
errmsg = _("Device not found");
} else {
errmsg = strerror(errno);
}
fprintf(stderr,_("%s: error fetching interface information: %s\n"),ife->name,errmsg);
return -1;
}
return 0;
}
发生了什么?为什么它只在第一次工作?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)