1GETIFADDRS(3)              Linux Programmer's Manual             GETIFADDRS(3)
2
3
4

NAME

6       getifaddrs, freeifaddrs - get interface addresses
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <ifaddrs.h>
11
12       int getifaddrs(struct ifaddrs **ifap);
13
14       void freeifaddrs(struct ifaddrs *ifa);
15

DESCRIPTION

17       The  getifaddrs() function creates a linked list of structures describ‐
18       ing the network interfaces of the local system, and stores the  address
19       of  the  first item of the list in *ifap.  The list consists of ifaddrs
20       structures, defined as follows:
21
22           struct ifaddrs {
23               struct ifaddrs  *ifa_next;    /* Next item in list */
24               char            *ifa_name;    /* Name of interface */
25               unsigned int     ifa_flags;   /* Flags from SIOCGIFFLAGS */
26               struct sockaddr *ifa_addr;    /* Address of interface */
27               struct sockaddr *ifa_netmask; /* Netmask of interface */
28               union {
29                   struct sockaddr *ifu_broadaddr;
30                                    /* Broadcast address of interface */
31                   struct sockaddr *ifu_dstaddr;
32                                    /* Point-to-point destination address */
33               } ifa_ifu;
34           #define              ifa_broadaddr ifa_ifu.ifu_broadaddr
35           #define              ifa_dstaddr   ifa_ifu.ifu_dstaddr
36               void            *ifa_data;    /* Address-specific data */
37           };
38
39       The ifa_next field contains a pointer to  the  next  structure  on  the
40       list, or NULL if this is the last item of the list.
41
42       The ifa_name points to the null-terminated interface name.
43
44       The  ifa_flags  field  contains the interface flags, as returned by the
45       SIOCGIFFLAGS ioctl(2) operation (see netdevice(7) for a list  of  these
46       flags).
47
48       The  ifa_addr  field  points  to  a  structure containing the interface
49       address.  (The sa_family subfield should be consulted to determine  the
50       format  of  the  address  structure.)   This  field  may contain a null
51       pointer.
52
53       The ifa_netmask field points to  a  structure  containing  the  netmask
54       associated  with  ifa_addr, if applicable for the address family.  This
55       field may contain a null pointer.
56
57       Depending on whether the bit IFF_BROADCAST or IFF_POINTOPOINT is set in
58       ifa_flags  (only  one  can be set at a time), either ifa_broadaddr will
59       contain the broadcast address associated with ifa_addr  (if  applicable
60       for  the  address  family)  or ifa_dstaddr will contain the destination
61       address of the point-to-point interface.
62
63       The ifa_data field points to a  buffer  containing  address-family-spe‐
64       cific  data;  this  field may be NULL if there is no such data for this
65       interface.
66
67       The data returned by getifaddrs() is dynamically allocated  and  should
68       be freed using freeifaddrs() when no longer needed.
69

RETURN VALUE

71       On  success,  getifaddrs()  returns zero; on error, -1 is returned, and
72       errno is set appropriately.
73

ERRORS

75       getifaddrs() may fail and set errno for any of the errors specified for
76       socket(2),  bind(2),  getsockname(2), recvmsg(2), sendto(2), malloc(3),
77       or realloc(3).
78

VERSIONS

80       The getifaddrs() function first appeared in glibc 2.3, but before glibc
81       2.3.3,  the  implementation supported only IPv4 addresses; IPv6 support
82       was added in glibc 2.3.3.  Support of address families other than  IPv4
83       is available only on kernels that support netlink.
84

ATTRIBUTES

86       For   an   explanation   of   the  terms  used  in  this  section,  see
87       attributes(7).
88
89       ┌────────────────────────────┬───────────────┬─────────┐
90Interface                   Attribute     Value   
91       ├────────────────────────────┼───────────────┼─────────┤
92getifaddrs(), freeifaddrs() │ Thread safety │ MT-Safe │
93       └────────────────────────────┴───────────────┴─────────┘
94

CONFORMING TO

96       Not in POSIX.1.  This function first appeared in BSDi and is present on
97       the  BSD  systems,  but  with  slightly different semantics documented—
98       returning one  entry  per  interface,  not  per  address.   This  means
99       ifa_addr  and other fields can actually be NULL if the interface has no
100       address, and no link-level address is returned if the interface has  an
101       IP address assigned.  Also, the way of choosing either ifa_broadaddr or
102       ifa_dstaddr differs on various systems.
103

NOTES

105       The addresses returned on Linux will  usually  be  the  IPv4  and  IPv6
106       addresses assigned to the interface, but also one AF_PACKET address per
107       interface containing lower-level details about the  interface  and  its
108       physical layer.  In this case, the ifa_data field may contain a pointer
109       to a struct rtnl_link_stats, defined in <linux/if_link.h> (in Linux 2.4
110       and  earlier, struct net_device_stats, defined in <linux/netdevice.h>),
111       which contains various interface attributes and statistics.
112

EXAMPLE

114       The program below demonstrates the use of getifaddrs(),  freeifaddrs(),
115       and  getnameinfo(3).   Here is what we see when running this program on
116       one system:
117
118           $ ./a.out
119           lo       AF_PACKET (17)
120                           tx_packets =        524; rx_packets =        524
121                           tx_bytes   =      38788; rx_bytes   =      38788
122           wlp3s0   AF_PACKET (17)
123                           tx_packets =     108391; rx_packets =     130245
124                           tx_bytes   =   30420659; rx_bytes   =   94230014
125           em1      AF_PACKET (17)
126                           tx_packets =          0; rx_packets =          0
127                           tx_bytes   =          0; rx_bytes   =          0
128           lo       AF_INET (2)
129                           address: <127.0.0.1>
130           wlp3s0   AF_INET (2)
131                           address: <192.168.235.137>
132           lo       AF_INET6 (10)
133                           address: <::1>
134           wlp3s0   AF_INET6 (10)
135                           address: <fe80::7ee9:d3ff:fef5:1a91%wlp3s0>
136
137   Program source
138
139       #define _GNU_SOURCE     /* To get defns of NI_MAXSERV and NI_MAXHOST */
140       #include <arpa/inet.h>
141       #include <sys/socket.h>
142       #include <netdb.h>
143       #include <ifaddrs.h>
144       #include <stdio.h>
145       #include <stdlib.h>
146       #include <unistd.h>
147       #include <linux/if_link.h>
148
149       int main(int argc, char *argv[])
150       {
151           struct ifaddrs *ifaddr, *ifa;
152           int family, s, n;
153           char host[NI_MAXHOST];
154
155           if (getifaddrs(&ifaddr) == -1) {
156               perror("getifaddrs");
157               exit(EXIT_FAILURE);
158           }
159
160           /* Walk through linked list, maintaining head pointer so we
161              can free list later */
162
163           for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
164               if (ifa->ifa_addr == NULL)
165                   continue;
166
167               family = ifa->ifa_addr->sa_family;
168
169               /* Display interface name and family (including symbolic
170                  form of the latter for the common families) */
171
172               printf("%-8s %s (%d)\n",
173                      ifa->ifa_name,
174                      (family == AF_PACKET) ? "AF_PACKET" :
175                      (family == AF_INET) ? "AF_INET" :
176                      (family == AF_INET6) ? "AF_INET6" : "???",
177                      family);
178
179               /* For an AF_INET* interface address, display the address */
180
181               if (family == AF_INET || family == AF_INET6) {
182                   s = getnameinfo(ifa->ifa_addr,
183                           (family == AF_INET) ? sizeof(struct sockaddr_in) :
184                                                 sizeof(struct sockaddr_in6),
185                           host, NI_MAXHOST,
186                           NULL, 0, NI_NUMERICHOST);
187                   if (s != 0) {
188                       printf("getnameinfo() failed: %s\n", gai_strerror(s));
189                       exit(EXIT_FAILURE);
190                   }
191
192                   printf("\t\taddress: <%s>\n", host);
193
194               } else if (family == AF_PACKET && ifa->ifa_data != NULL) {
195                   struct rtnl_link_stats *stats = ifa->ifa_data;
196
197                   printf("\t\ttx_packets = %10u; rx_packets = %10u\n"
198                          "\t\ttx_bytes   = %10u; rx_bytes   = %10u\n",
199                          stats->tx_packets, stats->rx_packets,
200                          stats->tx_bytes, stats->rx_bytes);
201               }
202           }
203
204           freeifaddrs(ifaddr);
205           exit(EXIT_SUCCESS);
206       }
207

SEE ALSO

209       bind(2), getsockname(2), socket(2), packet(7), ifconfig(8)
210

COLOPHON

212       This page is part of release 4.16 of the Linux  man-pages  project.   A
213       description  of  the project, information about reporting bugs, and the
214       latest    version    of    this    page,    can     be     found     at
215       https://www.kernel.org/doc/man-pages/.
216
217
218
219GNU                               2017-09-15                     GETIFADDRS(3)
Impressum