關於getifaddrs()和struct ifaddrs的說明

1,關於struct ifaddrs的說明: 

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 */ 
}; 

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,一個小例子: 
C代碼  收藏代碼
  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  
  10. main(int argc, char *argv[])  
  11. {  
  12.     struct ifaddrs *ifaddr, *ifa;  
  13.     int family, s;  
  14.     char host[NI_MAXHOST];  
  15.   
  16.     if (getifaddrs(&ifaddr) == -1)   
  17.     {  
  18.         perror("getifaddrs");  
  19.         exit(EXIT_FAILURE);  
  20.     }  
  21.   
  22.     /* Walk through linked list, maintaining head pointer so we 
  23.        can free list later */  
  24.   
  25.     for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)   
  26.     {  
  27.         if (ifa->ifa_addr == NULL)  
  28.             continue;  
  29.   
  30.         family = ifa->ifa_addr->sa_family;  
  31.   
  32.         /* Display interface name and family (including symbolic 
  33.            form of the latter for the common families) */  
  34.   
  35.         printf("%s  address family: %d%s\n",  
  36.                 ifa->ifa_name, family,  
  37.                 (family == AF_PACKET) ? " (AF_PACKET)" :  
  38.                 (family == AF_INET) ?   " (AF_INET)" :  
  39.                 (family == AF_INET6) ?  " (AF_INET6)" : "");  
  40.   
  41.         /* For an AF_INET* interface address, display the address */  
  42.   
  43.         if (family == AF_INET || family == AF_INET6)   
  44.         {  
  45.             s = getnameinfo(ifa->ifa_addr,  
  46.                     (family == AF_INET) ? sizeof(struct sockaddr_in) :  
  47.                                           sizeof(struct sockaddr_in6),  
  48.                     host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);  
  49.             if (s != 0)   
  50.             {  
  51.                 printf("getnameinfo() failed: %s\n", gai_strerror(s));  
  52.                 exit(EXIT_FAILURE);  
  53.             }  
  54.             printf("\taddress: <%s>\n", host);  
  55.         }  
  56.     }  
  57.   
  58.     freeifaddrs(ifaddr); //一定要釋放  
  59.     exit(EXIT_SUCCESS);  
  60. }  


輸出: 
引用

$ ./a.out 
lo      address family: 17 (AF_PACKET) 
eth0    address family: 17 (AF_PACKET) 
lo      address family: 2 (AF_INET) 
        address: <127.0.0.1> 
eth0    address family: 2 (AF_INET) 
        address: <10.1.1.4> 
lo      address family: 10 (AF_INET6) 
        address: <::1> 
eth0    address family: 10 (AF_INET6) 
                   address: <fe80::2d0:59ff:feda:eb51%eth0
發佈了13 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章