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
163 This function first appeared in BSDi and is
164 present on the BSD systems, but with slightly different
165 semantics documented\[em]returning one entry per interface,
169 and other fields can actually be NULL if the interface has no address,
170 and no link-level address is returned if the interface has an IP address
172 Also, the way of choosing either
176 differs on various systems.
177 .\" , but the BSD-derived documentation generally
178 .\" appears to be confused and obsolete on this point.
179 .\" i.e., commonly it still says one of them will be NULL, even if
180 .\" the ifa_ifu union is already present
183 first appeared in glibc 2.3, but before glibc 2.3.3,
184 the implementation supported only IPv4 addresses;
185 IPv6 support was added in glibc 2.3.3.
186 Support of address families other than IPv4 is available only
187 on kernels that support netlink.
189 The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
190 assigned to the interface, but also one
192 address per interface containing lower-level details about the interface
193 and its physical layer.
196 field may contain a pointer to a
197 .IR "struct rtnl_link_stats" ,
200 (in Linux 2.4 and earlier,
201 .IR "struct net_device_stats" ,
203 .IR <linux/netdevice.h> ),
204 which contains various interface attributes and statistics.
206 The program below demonstrates the use of
211 Here is what we see when running this program on one system:
217 tx_packets = 524; rx_packets = 524
218 tx_bytes = 38788; rx_bytes = 38788
219 wlp3s0 AF_PACKET (17)
220 tx_packets = 108391; rx_packets = 130245
221 tx_bytes = 30420659; rx_bytes = 94230014
223 tx_packets = 0; rx_packets = 0
224 tx_bytes = 0; rx_bytes = 0
228 address: <192.168.235.137>
232 address: <fe80::7ee9:d3ff:fef5:1a91%wlp3s0>
238 #define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */
239 #include <arpa/inet.h>
240 #include <sys/socket.h>
246 #include <linux/if_link.h>
248 int main(int argc, char *argv[])
250 struct ifaddrs *ifaddr;
252 char host[NI_MAXHOST];
254 if (getifaddrs(&ifaddr) == \-1) {
255 perror("getifaddrs");
259 /* Walk through linked list, maintaining head pointer so we
260 can free list later. */
262 for (struct ifaddrs *ifa = ifaddr; ifa != NULL;
263 ifa = ifa\->ifa_next) {
264 if (ifa\->ifa_addr == NULL)
267 family = ifa\->ifa_addr\->sa_family;
269 /* Display interface name and family (including symbolic
270 form of the latter for the common families). */
272 printf("%\-8s %s (%d)\en",
274 (family == AF_PACKET) ? "AF_PACKET" :
275 (family == AF_INET) ? "AF_INET" :
276 (family == AF_INET6) ? "AF_INET6" : "???",
279 /* For an AF_INET* interface address, display the address. */
281 if (family == AF_INET || family == AF_INET6) {
282 s = getnameinfo(ifa\->ifa_addr,
283 (family == AF_INET) ? sizeof(struct sockaddr_in) :
284 sizeof(struct sockaddr_in6),
286 NULL, 0, NI_NUMERICHOST);
288 printf("getnameinfo() failed: %s\en", gai_strerror(s));
292 printf("\et\etaddress: <%s>\en", host);
294 } else if (family == AF_PACKET && ifa\->ifa_data != NULL) {
295 struct rtnl_link_stats *stats = ifa\->ifa_data;
297 printf("\et\ettx_packets = %10u; rx_packets = %10u\en"
298 "\et\ettx_bytes = %10u; rx_bytes = %10u\en",
299 stats\->tx_packets, stats\->rx_packets,
300 stats\->tx_bytes, stats\->rx_bytes);