2 .\" Copyright (c) 2008 Petr Baudis <pasky@suse.cz>
3 .\" and copyright (c) 2009, Linux Foundation, written by Michael Kerrisk
4 .\" <mtk.manpages@gmail.com>
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
12 .\" 2008-12-08 Petr Baudis <pasky@suse.cz>
13 .\" Rewrite the BSD manpage in the Linux man pages style and account
14 .\" for glibc specificities, provide an example.
15 .\" 2009-01-14 mtk, many edits and changes, rewrote example program.
17 .TH getifaddrs 3 (date) "Linux man-pages (unreleased)"
19 getifaddrs, freeifaddrs \- get interface addresses
22 .RI ( libc ", " \-lc )
25 .B #include <sys/types.h>
26 .B #include <ifaddrs.h>
28 .BI "int getifaddrs(struct ifaddrs **" "ifap" );
29 .BI "void freeifaddrs(struct ifaddrs *" "ifa" );
34 function creates a linked list of structures describing
35 the network interfaces of the local system,
36 and stores the address of the first item of the list in
40 structures, defined as follows:
45 struct ifaddrs *ifa_next; /* Next item in list */
46 char *ifa_name; /* Name of interface */
47 unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
48 struct sockaddr *ifa_addr; /* Address of interface */
49 struct sockaddr *ifa_netmask; /* Netmask of interface */
51 struct sockaddr *ifu_broadaddr;
52 /* Broadcast address of interface */
53 struct sockaddr *ifu_dstaddr;
54 /* Point\-to\-point destination address */
56 #define ifa_broadaddr ifa_ifu.ifu_broadaddr
57 #define ifa_dstaddr ifa_ifu.ifu_dstaddr
58 void *ifa_data; /* Address\-specific data */
65 field contains a pointer to the next structure on the list,
66 or NULL if this is the last item of the list.
70 points to the null-terminated interface name.
73 .\" indicates the maximum length of this field.
77 field contains the interface flags, as returned by the
82 for a list of these flags).
86 field points to a structure containing the interface address.
89 subfield should be consulted to determine the format of the
91 This field may contain a null pointer.
95 field points to a structure containing the netmask associated with
97 if applicable for the address family.
98 This field may contain a null pointer.
100 Depending on whether the bit
106 (only one can be set at a time),
109 will contain the broadcast address associated with
111 (if applicable for the address family) or
113 will contain the destination address of the point-to-point interface.
117 field points to a buffer containing address-family-specific data;
118 this field may be NULL if there is no such data for this interface.
122 is dynamically allocated and should be freed using
124 when no longer needed.
129 on error, \-1 is returned, and
131 is set to indicate the error.
136 for any of the errors specified for
146 For an explanation of the terms used in this section, see
152 Interface Attribute Value
158 T} Thread safety MT-Safe
164 This function first appeared in BSDi and is
165 present on the BSD systems, but with slightly different
166 semantics documented\[em]returning one entry per interface,
170 and other fields can actually be NULL if the interface has no address,
171 and no link-level address is returned if the interface has an IP address
173 Also, the way of choosing either
177 differs on various systems.
178 .\" , but the BSD-derived documentation generally
179 .\" appears to be confused and obsolete on this point.
180 .\" i.e., commonly it still says one of them will be NULL, even if
181 .\" the ifa_ifu union is already present
184 first appeared in glibc 2.3, but before glibc 2.3.3,
185 the implementation supported only IPv4 addresses;
186 IPv6 support was added in glibc 2.3.3.
187 Support of address families other than IPv4 is available only
188 on kernels that support netlink.
190 The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
191 assigned to the interface, but also one
193 address per interface containing lower-level details about the interface
194 and its physical layer.
197 field may contain a pointer to a
198 .IR "struct rtnl_link_stats" ,
201 (in Linux 2.4 and earlier,
202 .IR "struct net_device_stats" ,
204 .IR <linux/netdevice.h> ),
205 which contains various interface attributes and statistics.
207 The program below demonstrates the use of
212 Here is what we see when running this program on one system:
218 tx_packets = 524; rx_packets = 524
219 tx_bytes = 38788; rx_bytes = 38788
220 wlp3s0 AF_PACKET (17)
221 tx_packets = 108391; rx_packets = 130245
222 tx_bytes = 30420659; rx_bytes = 94230014
224 tx_packets = 0; rx_packets = 0
225 tx_bytes = 0; rx_bytes = 0
229 address: <192.168.235.137>
233 address: <fe80::7ee9:d3ff:fef5:1a91%wlp3s0>
239 #define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
240 #include <arpa/inet.h>
241 #include <sys/socket.h>
247 #include <linux/if_link.h>
249 int main(int argc, char *argv[])
251 struct ifaddrs *ifaddr;
253 char host[NI_MAXHOST];
255 if (getifaddrs(&ifaddr) == \-1) {
256 perror("getifaddrs");
260 /* Walk through linked list, maintaining head pointer so we
261 can free list later. */
263 for (struct ifaddrs *ifa = ifaddr; ifa != NULL;
264 ifa = ifa\->ifa_next) {
265 if (ifa\->ifa_addr == NULL)
268 family = ifa\->ifa_addr\->sa_family;
270 /* Display interface name and family (including symbolic
271 form of the latter for the common families). */
273 printf("%\-8s %s (%d)\en",
275 (family == AF_PACKET) ? "AF_PACKET" :
276 (family == AF_INET) ? "AF_INET" :
277 (family == AF_INET6) ? "AF_INET6" : "???",
280 /* For an AF_INET* interface address, display the address. */
282 if (family == AF_INET || family == AF_INET6) {
283 s = getnameinfo(ifa\->ifa_addr,
284 (family == AF_INET) ? sizeof(struct sockaddr_in) :
285 sizeof(struct sockaddr_in6),
287 NULL, 0, NI_NUMERICHOST);
289 printf("getnameinfo() failed: %s\en", gai_strerror(s));
293 printf("\et\etaddress: <%s>\en", host);
295 } else if (family == AF_PACKET && ifa\->ifa_data != NULL) {
296 struct rtnl_link_stats *stats = ifa\->ifa_data;
298 printf("\et\ettx_packets = %10u; rx_packets = %10u\en"
299 "\et\ettx_bytes = %10u; rx_bytes = %10u\en",
300 stats\->tx_packets, stats\->rx_packets,
301 stats\->tx_bytes, stats\->rx_bytes);