Linxu的struct ifaddrs 与getifaddrs()函数

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

1,关于struct ifaddrs的说明:

 

[cpp] view plaincopy
 
  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中的实例代码:

 

[cpp] view plaincopy
 
    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\n",  
    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\n", gai_strerror(s));  
    47.                 exit(EXIT_FAILURE);  
    48.             }  
    49.             printf("\taddress: <%s>\n", host);  
    50.         }  
    51.     }  
    52.   
    53.     freeifaddrs(ifaddr);  
    54.     exit(EXIT_SUCCESS);  
    55. }  

posted on 2015-08-06 15:17  月未央  阅读(1978)  评论(0编辑  收藏  举报

导航