Linux   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了通过Linux中的SIOCGIFCONF轮询接口名称大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在尝试轮询网络设备名称.我从各种片段拼凑出来, > http://unixhelp.ed.ac.uk/CGI/man-cgi?netdevice+7 > http://lists.apple.com/archives/Unix-porting/2002/Apr/msg00134.html > http://ubuntuforums.org/showthread.php?t=1421487 但我
我正在尝试轮询网络设备名称.我从各种片段拼凑出来,

> http://unixhelp.ed.ac.uk/CGI/man-cgi?netdevice+7
> http://lists.apple.com/archives/Unix-porting/2002/Apr/msg00134.html
> http://ubuntuforums.org/showthread.php?t=1421487

但我的输出只是胡言乱语.

#include <stdio.h>
#include <stdlib.h>
#include <net/route.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>

#define BUFLEN 1024
#define SEQ 9999

int main (int argc,const char* argv[])
{
  // File descriptor for socket
  int socketfd;
  struct ifconf conf;
  struct ifreq req[10];
  struct ifreq *ifr;

  printf("opening socket...");
  socketfd = socket(AF_ROUTE,SOCK_RAW,0);
  if (socketfd >= 0) {
    printf(" OK\n");
    conf.ifc_len = sizeof(req);
    conf.ifc_buf = (__caddr_t) req;
    ioctl(socketfd,SIOCGIFCONF,&conf);

    printf("Discovering interfaces...\n");
    int i;
    for (i=0; i<conf.ifc_len/sizeof(req[0]); i++) {
      ifr = &conf.ifc_req[i];
      printf("%d. %s\n",i+1,req[i].ifr_name);
    }
  }
  else {
    printf("Failed!\n");
  }
  return 0;
}

输出

opening socket... OK
Discovering interfaces...
?u???}??Gh???
2. p?9}?
3.
4. v?=?n??u?`?y??]g?<?~?v??
5.
6.
7.
8. ?v?T?
9. ?|?mw??j??v??h??|??v?T00~??v?$?|??|?@
10. T00~??v?$?|??|?@

我尝试逐个输出ifr_name数组的每个char,看看它们是否为空终止但是没有太大变化.我的程序的每次迭代输出不同的东西,所以这让我觉得我引用了一些错误的东西.有人能为我提供一些有关我可能做错的见解吗?

解决方法

有关如何获取Linux上的接口列表,请参阅 http://git.netfilter.org/cgi-bin/gitweb.cgi?p=libmnl.git;a=blob;f=examples/rtnl/rtnl-link-dump.c;hb=HEAD. AF_ROUTE是一些BSD的东西,并且在Linux上不鼓励使用ioctl,因为它有明显的局限性(例如在单个接口上传送多个地址).

大佬总结

以上是大佬教程为你收集整理的通过Linux中的SIOCGIFCONF轮询接口名称全部内容,希望文章能够帮你解决通过Linux中的SIOCGIFCONF轮询接口名称所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: