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 2017-09-15 "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" );
46 .BI "void freeifaddrs(struct ifaddrs *" "ifa" );
51 function creates a linked list of structures describing
52 the network interfaces of the local system,
53 and stores the address of the first item of the list in
57 structures, defined as follows:
62 struct ifaddrs *ifa_next; /* Next item in list */
63 char *ifa_name; /* Name of interface */
64 unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
65 struct sockaddr *ifa_addr; /* Address of interface */
66 struct sockaddr *ifa_netmask; /* Netmask of interface */
68 struct sockaddr *ifu_broadaddr;
69 /* Broadcast address of interface */
70 struct sockaddr *ifu_dstaddr;
71 /* Point-to-point destination address */
73 #define ifa_broadaddr ifa_ifu.ifu_broadaddr
74 #define ifa_dstaddr ifa_ifu.ifu_dstaddr
75 void *ifa_data; /* Address-specific data */
82 field contains a pointer to the next structure on the list,
83 or NULL if this is the last item of the list.
87 points to the null-terminated interface name.
90 .\" indicates the maximum length of this field.
94 field contains the interface flags, as returned by the
99 for a list of these flags).
103 field points to a structure containing the interface address.
106 subfield should be consulted to determine the format of the
108 This field may contain a null pointer.
112 field points to a structure containing the netmask associated with
114 if applicable for the address family.
115 This field may contain a null pointer.
117 Depending on whether the bit
123 (only one can be set at a time),
126 will contain the broadcast address associated with
128 (if applicable for the address family) or
130 will contain the destination address of the point-to-point interface.
134 field points to a buffer containing address-family-specific data;
135 this field may be NULL if there is no such data for this interface.
139 is dynamically allocated and should be freed using
141 when no longer needed.
146 on error, \-1 is returned, and
148 is set appropriately.
153 for any of the errors specified for
165 function first appeared in glibc 2.3, but before glibc 2.3.3,
166 the implementation supported only IPv4 addresses;
167 IPv6 support was added in glibc 2.3.3.
168 Support of address families other than IPv4 is available only
169 on kernels that support netlink.
171 For an explanation of the terms used in this section, see
177 Interface Attribute Value
181 T} Thread safety MT-Safe
186 This function first appeared in BSDi and is
187 present on the BSD systems, but with slightly different
188 semantics documented\(emreturning one entry per interface,
192 and other fields can actually be NULL if the interface has no address,
193 and no link-level address is returned if the interface has an IP address
195 Also, the way of choosing either
199 differs on various systems.
200 .\" , but the BSD-derived documentation generally
201 .\" appears to be confused and obsolete on this point.
202 .\" i.e., commonly it still says one of them will be NULL, even if
203 .\" the ifa_ifu union is already present
205 The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
206 assigned to the interface, but also one
208 address per interface containing lower-level details about the interface
209 and its physical layer.
212 field may contain a pointer to a
213 .IR "struct rtnl_link_stats" ,
215 .IR <linux/if_link.h>
216 (in Linux 2.4 and earlier,
217 .IR "struct net_device_stats" ,
219 .IR <linux/netdevice.h> ),
220 which contains various interface attributes and statistics.
222 The program below demonstrates the use of
227 Here is what we see when running this program on one system:
233 tx_packets = 524; rx_packets = 524
234 tx_bytes = 38788; rx_bytes = 38788
235 wlp3s0 AF_PACKET (17)
236 tx_packets = 108391; rx_packets = 130245
237 tx_bytes = 30420659; rx_bytes = 94230014
239 tx_packets = 0; rx_packets = 0
240 tx_bytes = 0; rx_bytes = 0
244 address: <192.168.235.137>
248 address: <fe80::7ee9:d3ff:fef5:1a91%wlp3s0>
254 #define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
255 #include <arpa/inet.h>
256 #include <sys/socket.h>
262 #include <linux/if_link.h>
264 int main(int argc, char *argv[])
266 struct ifaddrs *ifaddr, *ifa;
268 char host[NI_MAXHOST];
270 if (getifaddrs(&ifaddr) == \-1) {
271 perror("getifaddrs");
275 /* Walk through linked list, maintaining head pointer so we
276 can free list later */
278 for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa\->ifa_next, n++) {
279 if (ifa\->ifa_addr == NULL)
282 family = ifa\->ifa_addr\->sa_family;
284 /* Display interface name and family (including symbolic
285 form of the latter for the common families) */
287 printf("%\-8s %s (%d)\en",
289 (family == AF_PACKET) ? "AF_PACKET" :
290 (family == AF_INET) ? "AF_INET" :
291 (family == AF_INET6) ? "AF_INET6" : "???",
294 /* For an AF_INET* interface address, display the address */
296 if (family == AF_INET || family == AF_INET6) {
297 s = getnameinfo(ifa\->ifa_addr,
298 (family == AF_INET) ? sizeof(struct sockaddr_in) :
299 sizeof(struct sockaddr_in6),
301 NULL, 0, NI_NUMERICHOST);
303 printf("getnameinfo() failed: %s\en", gai_strerror(s));
307 printf("\et\etaddress: <%s>\en", host);
309 } else if (family == AF_PACKET && ifa\->ifa_data != NULL) {
310 struct rtnl_link_stats *stats = ifa\->ifa_data;
312 printf("\et\ettx_packets = %10u; rx_packets = %10u\en"
313 "\et\ettx_bytes = %10u; rx_bytes = %10u\en",
314 stats\->tx_packets, stats\->rx_packets,
315 stats\->tx_bytes, stats\->rx_bytes);