什么是网络 ioctl 手册页?

发布于 2024-10-16 07:55:09 字数 172 浏览 3 评论 0原文

我想使用 ioctl SIOCGIFADDR 来确定 Linux 中接口的地址。我找到了一些网络参考资料来解释如何执行此操作,但似乎我的系统上已经有一些这方面的参考资料。是否有描述 SIOCGIFADDR 及其朋友的手册页(或其他内容)? man netdevice 很接近,但它没有相关的 ioctl。

I would like to use the ioctl SIOCGIFADDR to determine the address of an interface in Linux. I found some web references that explain how to do this, but it seems like there should be some reference for this already on my system. Is there a man page (or something else) that describes SIOCGIFADDR and friends? man netdevice is close, but it doesn't have the ioctl in question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ㄟ。诗瑗 2024-10-23 07:55:09

ioctl 似乎已被弃用。更好的方法(尽管更复杂)是使用 netlink。例如,请参阅这篇文章 如何通过 netlink 列出所有接口(对于 ioctl 版本,请参阅 该文章迷你系列的第 2 部分)。还有一些库可以更轻松地与 netlink 交互,例如参见 libnl libmnl。另请参阅这个问题

编辑: 这里是关于如何通过 netlink 获取所有接口的 IP 地址的示例

The ioctls seem to be deprecated. The better way (though way more complicated) is to use netlink. See for example this article on how to list all interfaces via netlink (for an ioctl version, see part 2 of that article mini-series). There are also libraries to interface with netlink more easily, see for example libnl and libmnl. Also see this SO question.

Edit: Here's an example on how to get the IP addresses of all interfaces via netlink.

埋葬我深情 2024-10-23 07:55:09

我在 Ubuntu 系统上找到的最接近的(通过 grep 手册页和头文件)是 man ioctl_list,它将调用列出为:

   0x00008915   SIOCGIFADDR         struct ifreq *           // I-O

The struct in question (ifreq< /code>)在 /usr/include/net/if.h 中声明,并附有解释每个字段含义的简短注释。

   /* Interface request structure used for socket ioctl's.  All interface
       ioctl's must have parameter definitions which begin with ifr_name.
       The remainder may be interface specific.  */

    struct ifreq
      {
    # define IFHWADDRLEN    6
    # define IFNAMSIZ       IF_NAMESIZE
        union
          {
            char ifrn_name[IFNAMSIZ];       /* Interface name, e.g. "en0".  */
          } ifr_ifrn;

        union
          {
            struct sockaddr ifru_addr;
            struct sockaddr ifru_dstaddr;
            struct sockaddr ifru_broadaddr;
            struct sockaddr ifru_netmask;
            struct sockaddr ifru_hwaddr;
            short int ifru_flags;
            int ifru_ivalue;
            int ifru_mtu;
            struct ifmap ifru_map;
            char ifru_slave[IFNAMSIZ];      /* Just fits the size */
            char ifru_newname[IFNAMSIZ];
            __caddr_t ifru_data;
          } ifr_ifru;
      };
    # define ifr_name       ifr_ifrn.ifrn_name      /* interface name       */
    # define ifr_hwaddr     ifr_ifru.ifru_hwaddr    /* MAC address          */
    # define ifr_addr       ifr_ifru.ifru_addr      /* address              */
    # define ifr_dstaddr    ifr_ifru.ifru_dstaddr   /* other end of p-p lnk */
    # define ifr_broadaddr  ifr_ifru.ifru_broadaddr /* broadcast address    */
    # define ifr_netmask    ifr_ifru.ifru_netmask   /* interface net mask   */
    # define ifr_flags      ifr_ifru.ifru_flags     /* flags                */
    # define ifr_metric     ifr_ifru.ifru_ivalue    /* metric               */
    # define ifr_mtu        ifr_ifru.ifru_mtu       /* mtu                  */
    # define ifr_map        ifr_ifru.ifru_map       /* device map           */
    # define ifr_slave      ifr_ifru.ifru_slave     /* slave device         */
    # define ifr_data       ifr_ifru.ifru_data      /* for use by interface */
    # define ifr_ifindex    ifr_ifru.ifru_ivalue    /* interface index      */
    # define ifr_bandwidth  ifr_ifru.ifru_ivalue    /* link bandwidth       */
    # define ifr_qlen       ifr_ifru.ifru_ivalue    /* queue length         */
    # define ifr_newname    ifr_ifru.ifru_newname   /* New name             */
    # define _IOT_ifreq     _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
    # define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
    # define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)

除了上述之外,我认为网络是您最好的选择。

The closest I've been able to find on my Ubuntu system (by grepping through man pages and header files) is man ioctl_list, which lists the call as:

   0x00008915   SIOCGIFADDR         struct ifreq *           // I-O

The struct in question (ifreq) is declared in /usr/include/net/if.h along with brief comments explaining the meaning of each field.

   /* Interface request structure used for socket ioctl's.  All interface
       ioctl's must have parameter definitions which begin with ifr_name.
       The remainder may be interface specific.  */

    struct ifreq
      {
    # define IFHWADDRLEN    6
    # define IFNAMSIZ       IF_NAMESIZE
        union
          {
            char ifrn_name[IFNAMSIZ];       /* Interface name, e.g. "en0".  */
          } ifr_ifrn;

        union
          {
            struct sockaddr ifru_addr;
            struct sockaddr ifru_dstaddr;
            struct sockaddr ifru_broadaddr;
            struct sockaddr ifru_netmask;
            struct sockaddr ifru_hwaddr;
            short int ifru_flags;
            int ifru_ivalue;
            int ifru_mtu;
            struct ifmap ifru_map;
            char ifru_slave[IFNAMSIZ];      /* Just fits the size */
            char ifru_newname[IFNAMSIZ];
            __caddr_t ifru_data;
          } ifr_ifru;
      };
    # define ifr_name       ifr_ifrn.ifrn_name      /* interface name       */
    # define ifr_hwaddr     ifr_ifru.ifru_hwaddr    /* MAC address          */
    # define ifr_addr       ifr_ifru.ifru_addr      /* address              */
    # define ifr_dstaddr    ifr_ifru.ifru_dstaddr   /* other end of p-p lnk */
    # define ifr_broadaddr  ifr_ifru.ifru_broadaddr /* broadcast address    */
    # define ifr_netmask    ifr_ifru.ifru_netmask   /* interface net mask   */
    # define ifr_flags      ifr_ifru.ifru_flags     /* flags                */
    # define ifr_metric     ifr_ifru.ifru_ivalue    /* metric               */
    # define ifr_mtu        ifr_ifru.ifru_mtu       /* mtu                  */
    # define ifr_map        ifr_ifru.ifru_map       /* device map           */
    # define ifr_slave      ifr_ifru.ifru_slave     /* slave device         */
    # define ifr_data       ifr_ifru.ifru_data      /* for use by interface */
    # define ifr_ifindex    ifr_ifru.ifru_ivalue    /* interface index      */
    # define ifr_bandwidth  ifr_ifru.ifru_ivalue    /* link bandwidth       */
    # define ifr_qlen       ifr_ifru.ifru_ivalue    /* queue length         */
    # define ifr_newname    ifr_ifru.ifru_newname   /* New name             */
    # define _IOT_ifreq     _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
    # define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
    # define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)

Other than the above, I sense that the Web is your best bet.

盗琴音 2024-10-23 07:55:09

netintro(4) 有一个最小的摘要。可惜 Linux 上不可用

netintro(4) has a minimal summary. Too bad its not available on linux

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文