1 .\" Copyright (c) 2008 Petr Baudis <pasky@suse.cz>
2 .\" and copyright (c) 2009, Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" Redistribution and use in source and binary forms, with or without
28 .\" modification, are permitted provided that the following conditions
31 .\" 2008-12-08 Petr Baudis <pasky@suse.cz>
32 .\" Rewrite the BSD manpage in the Linux man pages style and account
33 .\" for glibc specificities, provide an example.
34 .\" 2009-01-14 mtk, many edits and changes, rewrote example program.
36 .TH GETIFADDRS 3 2021-03-22 "GNU" "Linux Programmer's Manual"
38 getifaddrs, freeifaddrs \- get interface addresses
41 .B #include <sys/types.h>
42 .B #include <ifaddrs.h>
44 .BI "int getifaddrs(struct ifaddrs **" "ifap" );
45 .BI "void freeifaddrs(struct ifaddrs *" "ifa" );
50 function creates a linked list of structures describing
51 the network interfaces of the local system,
52 and stores the address of the first item of the list in
56 structures, defined as follows:
61 struct ifaddrs *ifa_next; /* Next item in list */
62 char *ifa_name; /* Name of interface */
63 unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
64 struct sockaddr *ifa_addr; /* Address of interface */
65 struct sockaddr *ifa_netmask; /* Netmask of interface */
67 struct sockaddr *ifu_broadaddr;
68 /* Broadcast address of interface */
69 struct sockaddr *ifu_dstaddr;
70 /* Point\-to\-point destination address */
72 #define ifa_broadaddr ifa_ifu.ifu_broadaddr
73 #define ifa_dstaddr ifa_ifu.ifu_dstaddr
74 void *ifa_data; /* Address\-specific data */
81 field contains a pointer to the next structure on the list,
82 or NULL if this is the last item of the list.
86 points to the null-terminated interface name.
89 .\" indicates the maximum length of this field.
93 field contains the interface flags, as returned by the
98 for a list of these flags).
102 field points to a structure containing the interface address.
105 subfield should be consulted to determine the format of the
107 This field may contain a null pointer.
111 field points to a structure containing the netmask associated with
113 if applicable for the address family.
114 This field may contain a null pointer.
116 Depending on whether the bit
122 (only one can be set at a time),
125 will contain the broadcast address associated with
127 (if applicable for the address family) or
129 will contain the destination address of the point-to-point interface.
133 field points to a buffer containing address-family-specific data;
134 this field may be NULL if there is no such data for this interface.
138 is dynamically allocated and should be freed using
140 when no longer needed.
145 on error, \-1 is returned, and
147 is set to indicate the error.
152 for any of the errors specified for
164 function first appeared in glibc 2.3, but before glibc 2.3.3,
165 the implementation supported only IPv4 addresses;
166 IPv6 support was added in glibc 2.3.3.
167 Support of address families other than IPv4 is available only
168 on kernels that support netlink.
170 For an explanation of the terms used in this section, see
178 Interface Attribute Value
182 T} Thread safety MT-Safe
189 This function first appeared in BSDi and is
190 present on the BSD systems, but with slightly different
191 semantics documented\(emreturning one entry per interface,
195 and other fields can actually be NULL if the interface has no address,
196 and no link-level address is returned if the interface has an IP address
198 Also, the way of choosing either
202 differs on various systems.
203 .\" , but the BSD-derived documentation generally
204 .\" appears to be confused and obsolete on this point.
205 .\" i.e., commonly it still says one of them will be NULL, even if
206 .\" the ifa_ifu union is already present
208 The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
209 assigned to the interface, but also one
211 address per interface containing lower-level details about the interface
212 and its physical layer.
215 field may contain a pointer to a
216 .IR "struct rtnl_link_stats" ,
218 .IR <linux/if_link.h>
219 (in Linux 2.4 and earlier,
220 .IR "struct net_device_stats" ,
222 .IR <linux/netdevice.h> ),
223 which contains various interface attributes and statistics.
225 The program below demonstrates the use of
230 Here is what we see when running this program on one system:
236 tx_packets = 524; rx_packets = 524
237 tx_bytes = 38788; rx_bytes = 38788
238 wlp3s0 AF_PACKET (17)
239 tx_packets = 108391; rx_packets = 130245
240 tx_bytes = 30420659; rx_bytes = 94230014
242 tx_packets = 0; rx_packets = 0
243 tx_bytes = 0; rx_bytes = 0
247 address: <192.168.235.137>
251 address: <fe80::7ee9:d3ff:fef5:1a91%wlp3s0>
257 #define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
258 #include <arpa/inet.h>
259 #include <sys/socket.h>
265 #include <linux/if_link.h>
267 int main(int argc, char *argv[])
269 struct ifaddrs *ifaddr;
271 char host[NI_MAXHOST];
273 if (getifaddrs(&ifaddr) == \-1) {
274 perror("getifaddrs");
278 /* Walk through linked list, maintaining head pointer so we
279 can free list later. */
281 for (struct ifaddrs *ifa = ifaddr; ifa != NULL;
282 ifa = ifa\->ifa_next) {
283 if (ifa\->ifa_addr == NULL)
286 family = ifa\->ifa_addr\->sa_family;
288 /* Display interface name and family (including symbolic
289 form of the latter for the common families). */
291 printf("%\-8s %s (%d)\en",
293 (family == AF_PACKET) ? "AF_PACKET" :
294 (family == AF_INET) ? "AF_INET" :
295 (family == AF_INET6) ? "AF_INET6" : "???",
298 /* For an AF_INET* interface address, display the address. */
300 if (family == AF_INET || family == AF_INET6) {
301 s = getnameinfo(ifa\->ifa_addr,
302 (family == AF_INET) ? sizeof(struct sockaddr_in) :
303 sizeof(struct sockaddr_in6),
305 NULL, 0, NI_NUMERICHOST);
307 printf("getnameinfo() failed: %s\en", gai_strerror(s));
311 printf("\et\etaddress: <%s>\en", host);
313 } else if (family == AF_PACKET && ifa\->ifa_data != NULL) {
314 struct rtnl_link_stats *stats = ifa\->ifa_data;
316 printf("\et\ettx_packets = %10u; rx_packets = %10u\en"
317 "\et\ettx_bytes = %10u; rx_bytes = %10u\en",
318 stats\->tx_packets, stats\->rx_packets,
319 stats\->tx_bytes, stats\->rx_bytes);