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 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
11 .\" 2008-12-08 Petr Baudis <pasky@suse.cz>
12 .\" Rewrite the BSD manpage in the Linux man pages style and account
13 .\" for glibc specificities, provide an example.
14 .\" 2009-01-14 mtk, many edits and changes, rewrote example program.
16 .TH getifaddrs 3 (date) "Linux man-pages (unreleased)"
18 getifaddrs, freeifaddrs \- get interface addresses
21 .RI ( libc ", " \-lc )
24 .B #include <sys/types.h>
25 .B #include <ifaddrs.h>
27 .BI "int getifaddrs(struct ifaddrs **" "ifap" );
28 .BI "void freeifaddrs(struct ifaddrs *" "ifa" );
33 function creates a linked list of structures describing
34 the network interfaces of the local system,
35 and stores the address of the first item of the list in
39 structures, defined as follows:
44 struct ifaddrs *ifa_next; /* Next item in list */
45 char *ifa_name; /* Name of interface */
46 unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
47 struct sockaddr *ifa_addr; /* Address of interface */
48 struct sockaddr *ifa_netmask; /* Netmask of interface */
50 struct sockaddr *ifu_broadaddr;
51 /* Broadcast address of interface */
52 struct sockaddr *ifu_dstaddr;
53 /* Point\-to\-point destination address */
55 #define ifa_broadaddr ifa_ifu.ifu_broadaddr
56 #define ifa_dstaddr ifa_ifu.ifu_dstaddr
57 void *ifa_data; /* Address\-specific data */
64 field contains a pointer to the next structure on the list,
65 or NULL if this is the last item of the list.
69 points to the null-terminated interface name.
72 .\" indicates the maximum length of this field.
76 field contains the interface flags, as returned by the
81 for a list of these flags).
85 field points to a structure containing the interface address.
88 subfield should be consulted to determine the format of the
90 This field may contain a null pointer.
94 field points to a structure containing the netmask associated with
96 if applicable for the address family.
97 This field may contain a null pointer.
99 Depending on whether the bit
105 (only one can be set at a time),
108 will contain the broadcast address associated with
110 (if applicable for the address family) or
112 will contain the destination address of the point-to-point interface.
116 field points to a buffer containing address-family-specific data;
117 this field may be NULL if there is no such data for this interface.
121 is dynamically allocated and should be freed using
123 when no longer needed.
128 on error, \-1 is returned, and
130 is set to indicate the error.
135 for any of the errors specified for
147 function first appeared in glibc 2.3, but before glibc 2.3.3,
148 the implementation supported only IPv4 addresses;
149 IPv6 support was added in glibc 2.3.3.
150 Support of address families other than IPv4 is available only
151 on kernels that support netlink.
153 For an explanation of the terms used in this section, see
161 Interface Attribute Value
165 T} Thread safety MT-Safe
172 This function first appeared in BSDi and is
173 present on the BSD systems, but with slightly different
174 semantics documented\(emreturning one entry per interface,
178 and other fields can actually be NULL if the interface has no address,
179 and no link-level address is returned if the interface has an IP address
181 Also, the way of choosing either
185 differs on various systems.
186 .\" , but the BSD-derived documentation generally
187 .\" appears to be confused and obsolete on this point.
188 .\" i.e., commonly it still says one of them will be NULL, even if
189 .\" the ifa_ifu union is already present
191 The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
192 assigned to the interface, but also one
194 address per interface containing lower-level details about the interface
195 and its physical layer.
198 field may contain a pointer to a
199 .IR "struct rtnl_link_stats" ,
202 (in Linux 2.4 and earlier,
203 .IR "struct net_device_stats" ,
205 .IR <linux/netdevice.h> ),
206 which contains various interface attributes and statistics.
208 The program below demonstrates the use of
213 Here is what we see when running this program on one system:
219 tx_packets = 524; rx_packets = 524
220 tx_bytes = 38788; rx_bytes = 38788
221 wlp3s0 AF_PACKET (17)
222 tx_packets = 108391; rx_packets = 130245
223 tx_bytes = 30420659; rx_bytes = 94230014
225 tx_packets = 0; rx_packets = 0
226 tx_bytes = 0; rx_bytes = 0
230 address: <192.168.235.137>
234 address: <fe80::7ee9:d3ff:fef5:1a91%wlp3s0>
240 #define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
241 #include <arpa/inet.h>
242 #include <sys/socket.h>
248 #include <linux/if_link.h>
250 int main(int argc, char *argv[])
252 struct ifaddrs *ifaddr;
254 char host[NI_MAXHOST];
256 if (getifaddrs(&ifaddr) == \-1) {
257 perror("getifaddrs");
261 /* Walk through linked list, maintaining head pointer so we
262 can free list later. */
264 for (struct ifaddrs *ifa = ifaddr; ifa != NULL;
265 ifa = ifa\->ifa_next) {
266 if (ifa\->ifa_addr == NULL)
269 family = ifa\->ifa_addr\->sa_family;
271 /* Display interface name and family (including symbolic
272 form of the latter for the common families). */
274 printf("%\-8s %s (%d)\en",
276 (family == AF_PACKET) ? "AF_PACKET" :
277 (family == AF_INET) ? "AF_INET" :
278 (family == AF_INET6) ? "AF_INET6" : "???",
281 /* For an AF_INET* interface address, display the address. */
283 if (family == AF_INET || family == AF_INET6) {
284 s = getnameinfo(ifa\->ifa_addr,
285 (family == AF_INET) ? sizeof(struct sockaddr_in) :
286 sizeof(struct sockaddr_in6),
288 NULL, 0, NI_NUMERICHOST);
290 printf("getnameinfo() failed: %s\en", gai_strerror(s));
294 printf("\et\etaddress: <%s>\en", host);
296 } else if (family == AF_PACKET && ifa\->ifa_data != NULL) {
297 struct rtnl_link_stats *stats = ifa\->ifa_data;
299 printf("\et\ettx_packets = %10u; rx_packets = %10u\en"
300 "\et\ettx_bytes = %10u; rx_bytes = %10u\en",
301 stats\->tx_packets, stats\->rx_packets,
302 stats\->tx_bytes, stats\->rx_bytes);