【问题标题】:How to get network interface name from IPv6 address in linux in a C program?如何在 C 程序中从 linux 中的 IPv6 地址获取网络接口名称?
【发布时间】:2014-12-04 13:49:19
【问题描述】:

我想从 Linux 中的 C 程序中属于该接口的 IPv6 地址获取网络接口名称(如 ens33、ens37 等)。怎么做?请帮忙。

【问题讨论】:

    标签: c linux networking ipv6


    【解决方案1】:

    您可以使用getifaddrs (Linux getting all network interface names) 获取网络接口列表

    以下来自getifaddrs的手册页

       The  getifaddrs() function creates a linked list of structures describing the network interfaces of the local system, and stores the address
       of the first item of the list in *ifap.  The list consists of ifaddrs structures, defined as follows:
    
           struct ifaddrs {
               struct ifaddrs  *ifa_next;    /* Next item in list */
               char            *ifa_name;    /* Name of interface */
               unsigned int     ifa_flags;   /* Flags from SIOCGIFFLAGS */
               struct sockaddr *ifa_addr;    /* Address of interface */
               struct sockaddr *ifa_netmask; /* Netmask of interface */
               union {
                   struct sockaddr *ifu_broadaddr;
                                    /* Broadcast address of interface */
                   struct sockaddr *ifu_dstaddr;
                                    /* Point-to-point destination address */
               } ifa_ifu;
           #define              ifa_broadaddr ifa_ifu.ifu_broadaddr
           #define              ifa_dstaddr   ifa_ifu.ifu_dstaddr
               void            *ifa_data;    /* Address-specific data */
           };
    

    反之,只需搜索列表。

    【讨论】:

      猜你喜欢
      • 2013-06-21
      • 2017-01-25
      • 1970-01-01
      • 2011-06-15
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多