简体   繁体   English

如何在C程序中从Linux中的IPv6地址获取网络接口名称?

[英]How to get network interface name from IPv6 address in linux in a C program?

I want to get the network interface name (like ens33, ens37 etc) from an IPv6 address which belongs to that interface from a C program in Linux. 我想从属于Linux中C程序接口的IPv6地址获取网络接口名称(如ens33,ens37等)。 How to do that? 怎么做? Please help. 请帮忙。

You can get a list of network interfaces using getifaddrs ( Linux getting all network interface names ) 您可以使用getifaddrs 获取网络接口列表( Linux获取所有网络接口名称

below is from man page of getifaddrs 下面是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 */
       };

to do the reverse, just search the list. 反之,只需搜索列表即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM