1GETIFADDRS(3) Linux Programmer's Manual GETIFADDRS(3)
2
3
4
6 getifaddrs, freeifaddrs - get interface addresses
7
9 #include <sys/types.h>
10 #include <ifaddrs.h>
11
12 int getifaddrs(struct ifaddrs **ifap);
13 void freeifaddrs(struct ifaddrs *ifa);
14
16 The getifaddrs() function creates a linked list of structures describ‐
17 ing the network interfaces of the local system, and stores the address
18 of the first item of the list in *ifap. The list consists of ifaddrs
19 structures, defined as follows:
20
21 struct ifaddrs {
22 struct ifaddrs *ifa_next; /* Next item in list */
23 char *ifa_name; /* Name of interface */
24 unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
25 struct sockaddr *ifa_addr; /* Address of interface */
26 struct sockaddr *ifa_netmask; /* Netmask of interface */
27 union {
28 struct sockaddr *ifu_broadaddr;
29 /* Broadcast address of interface */
30 struct sockaddr *ifu_dstaddr;
31 /* Point-to-point destination address */
32 } ifa_ifu;
33 #define ifa_broadaddr ifa_ifu.ifu_broadaddr
34 #define ifa_dstaddr ifa_ifu.ifu_dstaddr
35 void *ifa_data; /* Address-specific data */
36 };
37
38 The ifa_next field contains a pointer to the next structure on the
39 list, or NULL if this is the last item of the list.
40
41 The ifa_name points to the null-terminated interface name.
42
43 The ifa_flags field contains the interface flags, as returned by the
44 SIOCGIFFLAGS ioctl(2) operation (see netdevice(7) for a list of these
45 flags).
46
47 The ifa_addr field points to a structure containing the interface ad‐
48 dress. (The sa_family subfield should be consulted to determine the
49 format of the address structure.) This field may contain a null
50 pointer.
51
52 The ifa_netmask field points to a structure containing the netmask as‐
53 sociated with ifa_addr, if applicable for the address family. This
54 field may contain a null pointer.
55
56 Depending on whether the bit IFF_BROADCAST or IFF_POINTOPOINT is set in
57 ifa_flags (only one can be set at a time), either ifa_broadaddr will
58 contain the broadcast address associated with ifa_addr (if applicable
59 for the address family) or ifa_dstaddr will contain the destination ad‐
60 dress of the point-to-point interface.
61
62 The ifa_data field points to a buffer containing address-family-spe‐
63 cific data; this field may be NULL if there is no such data for this
64 interface.
65
66 The data returned by getifaddrs() is dynamically allocated and should
67 be freed using freeifaddrs() when no longer needed.
68
70 On success, getifaddrs() returns zero; on error, -1 is returned, and
71 errno is set to indicate the error.
72
74 getifaddrs() may fail and set errno for any of the errors specified for
75 socket(2), bind(2), getsockname(2), recvmsg(2), sendto(2), malloc(3),
76 or realloc(3).
77
79 The getifaddrs() function first appeared in glibc 2.3, but before glibc
80 2.3.3, the implementation supported only IPv4 addresses; IPv6 support
81 was added in glibc 2.3.3. Support of address families other than IPv4
82 is available only on kernels that support netlink.
83
85 For an explanation of the terms used in this section, see at‐
86 tributes(7).
87
88 ┌────────────────────────────────────────────┬───────────────┬─────────┐
89 │Interface │ Attribute │ Value │
90 ├────────────────────────────────────────────┼───────────────┼─────────┤
91 │getifaddrs(), freeifaddrs() │ Thread safety │ MT-Safe │
92 └────────────────────────────────────────────┴───────────────┴─────────┘
93
95 Not in POSIX.1. This function first appeared in BSDi and is present on
96 the BSD systems, but with slightly different semantics documented—re‐
97 turning one entry per interface, not per address. This means ifa_addr
98 and other fields can actually be NULL if the interface has no address,
99 and no link-level address is returned if the interface has an IP ad‐
100 dress assigned. Also, the way of choosing either ifa_broadaddr or
101 ifa_dstaddr differs on various systems.
102
104 The addresses returned on Linux will usually be the IPv4 and IPv6 ad‐
105 dresses assigned to the interface, but also one AF_PACKET address per
106 interface containing lower-level details about the interface and its
107 physical layer. In this case, the ifa_data field may contain a pointer
108 to a struct rtnl_link_stats, defined in <linux/if_link.h> (in Linux 2.4
109 and earlier, struct net_device_stats, defined in <linux/netdevice.h>),
110 which contains various interface attributes and statistics.
111
113 The program below demonstrates the use of getifaddrs(), freeifaddrs(),
114 and getnameinfo(3). Here is what we see when running this program on
115 one system:
116
117 $ ./a.out
118 lo AF_PACKET (17)
119 tx_packets = 524; rx_packets = 524
120 tx_bytes = 38788; rx_bytes = 38788
121 wlp3s0 AF_PACKET (17)
122 tx_packets = 108391; rx_packets = 130245
123 tx_bytes = 30420659; rx_bytes = 94230014
124 em1 AF_PACKET (17)
125 tx_packets = 0; rx_packets = 0
126 tx_bytes = 0; rx_bytes = 0
127 lo AF_INET (2)
128 address: <127.0.0.1>
129 wlp3s0 AF_INET (2)
130 address: <192.168.235.137>
131 lo AF_INET6 (10)
132 address: <::1>
133 wlp3s0 AF_INET6 (10)
134 address: <fe80::7ee9:d3ff:fef5:1a91%wlp3s0>
135
136 Program source
137
138 #define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
139 #include <arpa/inet.h>
140 #include <sys/socket.h>
141 #include <netdb.h>
142 #include <ifaddrs.h>
143 #include <stdio.h>
144 #include <stdlib.h>
145 #include <unistd.h>
146 #include <linux/if_link.h>
147
148 int main(int argc, char *argv[])
149 {
150 struct ifaddrs *ifaddr;
151 int family, s;
152 char host[NI_MAXHOST];
153
154 if (getifaddrs(&ifaddr) == -1) {
155 perror("getifaddrs");
156 exit(EXIT_FAILURE);
157 }
158
159 /* Walk through linked list, maintaining head pointer so we
160 can free list later. */
161
162 for (struct ifaddrs *ifa = ifaddr; ifa != NULL;
163 ifa = ifa->ifa_next) {
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
209 bind(2), getsockname(2), socket(2), packet(7), ifconfig(8)
210
212 This page is part of release 5.13 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 2021-03-22 GETIFADDRS(3)