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 2014-02-26 "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.
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" ,
201 .IR <linux/if_link.h>
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, *ifa;
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 (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa\->ifa_next, n++) {
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)\\n",
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\\n", gai_strerror(s));
293 printf("\\t\\taddress: <%s>\\n", host);
295 } else if (family == AF_PACKET && ifa\->ifa_data != NULL) {
296 struct rtnl_link_stats *stats = ifa\->ifa_data;
298 printf("\\t\\ttx_packets = %10u; rx_packets = %10u\\n"
299 "\\t\\ttx_bytes = %10u; rx_bytes = %10u\\n",
300 stats\->tx_packets, stats\->rx_packets,
301 stats\->tx_bytes, stats\->rx_bytes);