Linxu的struct ifaddrs 与getifaddrs()函数

摘要:
/*接口名称*/unsigned int ifa_ flags;/*SIOCGIFFLAGS中的标志*/struct sockaddr*ifa_addr;/*接口地址*/struct sockaddr*ifa_ netmask;

来自man函数手册中的介绍:

1,关于struct ifaddrs的说明:

  1. struct ifaddrs  
  2. {  
  3.     struct ifaddrs  *ifa_next;    /* Next item in list */  
  4.     char            *ifa_name;    /* Name of interface */  
  5.     unsigned int     ifa_flags;   /* Flags from SIOCGIFFLAGS */  
  6.     struct sockaddr *ifa_addr;    /* Address of interface */  
  7.     struct sockaddr *ifa_netmask; /* Netmask of interface */  
  8.     union  
  9.     {  
  10.         struct sockaddr *ifu_broadaddr; /* Broadcast address of interface */  
  11.         struct sockaddr *ifu_dstaddr; /* Point-to-point destination address */  
  12.     } ifa_ifu;  
  13.     #define              ifa_broadaddr ifa_ifu.ifu_broadaddr  
  14.     #define              ifa_dstaddr   ifa_ifu.ifu_dstaddr  
  15.     void            *ifa_data;    /* Address-specific data */  
  16. };   


2,关于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:

       The ifa_next field contains a pointer to the next structure on the list, or
       NULL if this is the last item of the list.

       The ifa_name points to the null-terminated interface name.

       The ifa_flags field contains the interface flags

       The ifa_addr field points to a structure containing the interface address.

       The ifa_netmask field points to a structure containing the netmask associated with ifa_addr, if applicable for the address family.

       Depending on whether the bit IFF_BROADCAST or IFF_POINTOPOINT is set in ifa_flags (only one can be set at a time), either ifa_broadaddr will contain the broadcast address associated with ifa_addr (if applicable for the address family) or ifa_dstaddr will contain the destination address of the point-to-point interface.

       The ifa_data field points to a buffer containing address-family-specific data;this field may be NULL if there is no such data for this interface.

返回值:
On success, getifaddrs() returns zero; on error, -1 is returned, and errno is set appropriately.

3,注意:
       The data returned by getifaddrs() is dynamically allocated and should be freed using freeifaddrs() when no longer needed.

4,  man中的实例代码:

    1. #include <arpa/inet.h>  
    2. #include <sys/socket.h>  
    3. #include <netdb.h>  
    4. #include <ifaddrs.h>  
    5. #include <stdio.h>  
    6. #include <stdlib.h>  
    7. #include <unistd.h>  
    8.   
    9. int main(int argc, char *argv[])  
    10. {  
    11.     struct ifaddrs *ifaddr, *ifa;  
    12.     int family, s;  
    13.     char host[NI_MAXHOST];  
    14.   
    15.     if (getifaddrs(&ifaddr) == -1) {  
    16.         perror("getifaddrs");  
    17.         exit(EXIT_FAILURE);  
    18.     }  
    19.   
    20.     /* Walk through linked list, maintaining head pointer so we 
    21.      *               can free list later */  
    22.   
    23.     for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {  
    24.         if (ifa->ifa_addr == NULL)  
    25.             continue;  
    26.   
    27.         family = ifa->ifa_addr->sa_family;  
    28.   
    29.         /* Display interface name and family (including symbolic 
    30.          *                   form of the latter for the common families) */  
    31.   
    32.         printf("%s  address family: %d%s ",  
    33.                 ifa->ifa_name, family,  
    34.                 (family == AF_PACKET) ? " (AF_PACKET)" :  
    35.                 (family == AF_INET) ?   " (AF_INET)" :  
    36.                 (family == AF_INET6) ?  " (AF_INET6)" : "");  
    37.   
    38.         /* For an AF_INET* interface address, display the address */  
    39.   
    40.         if (family == AF_INET || family == AF_INET6) {  
    41.             s = getnameinfo(ifa->ifa_addr,  
    42.                     (family == AF_INET) ? sizeof(struct sockaddr_in) :  
    43.                     sizeof(struct sockaddr_in6),  
    44.                     host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);  
    45.             if (s != 0) {  
    46.                 printf("getnameinfo() failed: %s ", gai_strerror(s));  
    47.                 exit(EXIT_FAILURE);  
    48.             }  
    49.             printf(" address: <%s> ", host);  
    50.         }  
    51.     }  
    52.   
    53.     freeifaddrs(ifaddr);  
    54.     exit(EXIT_SUCCESS);  
    55. }  

免责声明:文章转载自《Linxu的struct ifaddrs 与getifaddrs()函数》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇glibcxx升级perl 中的my和全局变量下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SQL Server数据库管理员必备:DBCC命令

一、了解DBCC   DBCC(Database consistenecy checker,简称dbcc) 是一个实用命令集,用来检查数据库的逻辑一致性及物理一致性。   数据库控制台命令语句可分为以下类别:   维护: 对数据库、索引或文件组进行维护的任务。   杂项: 杂项任务,如启用跟踪标志或从内存中删除 DLL。   信息: 收集并显示各种类型...

根据class显示或隐藏多个div

引用一下jquery,然后function放head中 function test(){ $(".1").css("display","none"); //隐藏class为1 的div //控制显示的话 $(".1").css("display","block"); //$(".2").css("display","none"); 隐藏clas...

Haskell学习笔记

…高阶函数 map :: (a->b) ->[a] ->[b],将函数f依次应用于序列[a],得到新的序列[b]。 filter :: (a->bool)->[a]->[a],利用函数f过滤序列[a]。 这两个函数都可用list comprehension来实现,不过在某些情况下更简洁。利用这两个函数和CF组合可以...

idea修改maven默认配置不生效

1.问题现象 我的idea版本是2019.2的,通过File->Other Setting->Setting for New Projects修改maven的默认配置后,新建项目maven默认配置不生效 2.解决 打开C:Users用户.IntelliJIdea2019.2configoptions下的project.default.xml,添...

Android color颜色-色号总结

code时经常会用到颜色,然而对于像我这样的对于颜色不是很敏感的同学来说,就很痛苦了。 我想要某种颜色,但是又说不出来具体是哪种;这边总结了一下color种类以及色号。 <?xml version="1.0" encoding="utf-8"?> <resources> <color name="white"...

scan chain的原理和实现——5.UDTP

UDTP(user defined test point) 指示DFTC在设计中用户指定的位置插入控制点和观察点 1.为什么要使用UDTP? 修复无法控制的clock和/或asynch pins;  增加设计的测试覆盖率;  减少pattern数量 2.UDTP的类型 ①Force force_0、force_1、force_01、force_z0、for...