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 ad‐
49       dress.  (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  as‐
54       sociated  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 ad‐
61       dress 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 at‐
87       tributes(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—re‐
98       turning one entry per interface, not per address.  This means  ifa_addr
99       and  other fields can actually be NULL if the interface has no address,
100       and no link-level address is returned if the interface has  an  IP  ad‐
101       dress  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  ad‐
106       dresses  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

EXAMPLES

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;
152           int family, s;
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 (struct ifaddrs *ifa = ifaddr; ifa != NULL;
164                    ifa = ifa->ifa_next) {
165               if (ifa->ifa_addr == NULL)
166                   continue;
167
168               family = ifa->ifa_addr->sa_family;
169
170               /* Display interface name and family (including symbolic
171                  form of the latter for the common families) */
172
173               printf("%-8s %s (%d)\n",
174                      ifa->ifa_name,
175                      (family == AF_PACKET) ? "AF_PACKET" :
176                      (family == AF_INET) ? "AF_INET" :
177                      (family == AF_INET6) ? "AF_INET6" : "???",
178                      family);
179
180               /* For an AF_INET* interface address, display the address */
181
182               if (family == AF_INET || family == AF_INET6) {
183                   s = getnameinfo(ifa->ifa_addr,
184                           (family == AF_INET) ? sizeof(struct sockaddr_in) :
185                                                 sizeof(struct sockaddr_in6),
186                           host, NI_MAXHOST,
187                           NULL, 0, NI_NUMERICHOST);
188                   if (s != 0) {
189                       printf("getnameinfo() failed: %s\n", gai_strerror(s));
190                       exit(EXIT_FAILURE);
191                   }
192
193                   printf("\t\taddress: <%s>\n", host);
194
195               } else if (family == AF_PACKET && ifa->ifa_data != NULL) {
196                   struct rtnl_link_stats *stats = ifa->ifa_data;
197
198                   printf("\t\ttx_packets = %10u; rx_packets = %10u\n"
199                          "\t\ttx_bytes   = %10u; rx_bytes   = %10u\n",
200                          stats->tx_packets, stats->rx_packets,
201                          stats->tx_bytes, stats->rx_bytes);
202               }
203           }
204
205           freeifaddrs(ifaddr);
206           exit(EXIT_SUCCESS);
207       }
208

SEE ALSO

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

COLOPHON

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