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 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" Redistribution and use in source and binary forms, with or without
25 .\" modification, are permitted provided that the following conditions
28 .\" 2008-12-08 Petr Baudis <pasky@suse.cz>
29 .\" Rewrite the BSD manpage in the Linux man pages style and account
30 .\" for glibc specificities, provide an example.
31 .\" 2009-01-14 mtk, many edits and changes, rewrote example program.
33 .TH GETIFADDRS 3 2010-10-06 "GNU" "Linux Programmer's Manual"
35 getifaddrs, freeifaddrs \- get interface addresses
38 .B #include <sys/types.h>
39 .B #include <ifaddrs.h>
41 .BI "int getifaddrs(struct ifaddrs **" "ifap" );
43 .BI "void freeifaddrs(struct ifaddrs *" "ifa" );
48 function creates a linked list of structures describing
49 the network interfaces of the local system,
50 and stores the address of the first item of the list in
54 structures, defined as follows:
59 struct ifaddrs *ifa_next; /* Next item in list */
60 char *ifa_name; /* Name of interface */
61 unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
62 struct sockaddr *ifa_addr; /* Address of interface */
63 struct sockaddr *ifa_netmask; /* Netmask of interface */
65 struct sockaddr *ifu_broadaddr;
66 /* Broadcast address of interface */
67 struct sockaddr *ifu_dstaddr;
68 /* Point-to-point destination address */
70 #define ifa_broadaddr ifa_ifu.ifu_broadaddr
71 #define ifa_dstaddr ifa_ifu.ifu_dstaddr
72 void *ifa_data; /* Address-specific data */
79 field contains a pointer to the next structure on the list,
80 or NULL if this is the last item of the list.
84 points to the null-terminated interface name.
87 .\" indicates the maximum length of this field.
91 field contains the interface flags, as returned by the
96 for a list of these flags).
100 field points to a structure containing the interface address.
103 subfield should be consulted to determine the format of the
108 field points to a structure containing the netmask associated with
110 if applicable for the address family.
112 Depending on whether the bit
118 (only one can be set at a time),
121 will contain the broadcast address associated with
123 (if applicable for the address family) or
125 will contain the destination address of the point-to-point interface.
129 field points to a buffer containing address-family-specific data;
130 this field may be NULL if there is no such data for this interface.
134 is dynamically allocated and should be freed using
136 when no longer needed.
141 on error, \-1 is returned, and
143 is set appropriately.
148 for any of the errors specified for
160 function first appeared in glibc 2.3, but before glibc 2.3.3,
161 the implementation only supported IPv4 addresses;
162 IPv6 support was added in glibc 2.3.3.
163 Support of address families other than IPv4 is only available
164 on kernels that support netlink.
167 This function first appeared in BSDi and is
168 present on the BSD systems, but with slightly different
169 semantics documented\(emreturning one entry per interface,
173 and other fields can actually be NULL if the interface has no address,
174 and no link-level address is returned if the interface has an IP address
176 Also, the way of choosing either
180 differs on various systems.
181 .\" , but the BSD-derived documentation generally
182 .\" appears to be confused and obsolete on this point.
183 .\" i.e., commonly it still says one of them will be NULL, even if
184 .\" the ifa_ifu union is already present
186 The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
187 assigned to the interface, but also one
189 address per interface containing lower-level details about the interface
190 and its physical layer.
193 field may contain a pointer to a
194 .IR "struct net_device_stats" ,
196 .IR <linux/netdevice.h> ,
197 which contains various interface attributes and statistics.
199 The program below demonstrates the use of
204 Here is what we see when running this program on one system:
209 lo address family: 17 (AF_PACKET)
210 eth0 address family: 17 (AF_PACKET)
211 lo address family: 2 (AF_INET)
213 eth0 address family: 2 (AF_INET)
215 lo address family: 10 (AF_INET6)
217 eth0 address family: 10 (AF_INET6)
218 address: <fe80::2d0:59ff:feda:eb51%eth0>
224 #include <arpa/inet.h>
225 #include <sys/socket.h>
233 main(int argc, char *argv[])
235 struct ifaddrs *ifaddr, *ifa;
237 char host[NI_MAXHOST];
239 if (getifaddrs(&ifaddr) == \-1) {
240 perror("getifaddrs");
244 /* Walk through linked list, maintaining head pointer so we
245 can free list later */
247 for (ifa = ifaddr; ifa != NULL; ifa = ifa\->ifa_next) {
248 if (ifa\->ifa_addr == NULL)
251 family = ifa\->ifa_addr\->sa_family;
253 /* Display interface name and family (including symbolic
254 form of the latter for the common families) */
256 printf("%s\t address family: %d%s\\n",
257 ifa\->ifa_name, family,
258 (family == AF_PACKET) ? " (AF_PACKET)" :
259 (family == AF_INET) ? " (AF_INET)" :
260 (family == AF_INET6) ? " (AF_INET6)" : "");
262 /* For an AF_INET* interface address, display the address */
264 if (family == AF_INET || family == AF_INET6) {
265 s = getnameinfo(ifa\->ifa_addr,
266 (family == AF_INET) ? sizeof(struct sockaddr_in) :
267 sizeof(struct sockaddr_in6),
268 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
270 printf("getnameinfo() failed: %s\\n", gai_strerror(s));
273 printf("\\taddress: <%s>\\n", host);