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 2009-01-14 "GNU" "Linux Programmer's Manual"
34 .\" FIXME Make a link for freeifaddrs.3
35 .\" FIXME Check other changes in Petr's mail
37 getifaddrs, freeifaddrs \- get interface addresses
40 .B #include <sys/types.h>
41 .B #include <ifaddrs.h>
43 .BI "int getifaddrs(struct ifaddrs **" "ifap" );
45 .BI "void freeifaddrs(struct ifaddrs *" "ifa" );
50 function creates a linked list of structures describing
51 the network interfaces of the local system,
52 and stores the address of the first item of the list in
56 structures, defined as follows:
61 struct ifaddrs *ifa_next; /* Next item in list */
62 char *ifa_name; /* Name of interface */
63 unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
64 struct sockaddr *ifa_addr; /* Address of interface */
65 struct sockaddr *ifa_netmask; /* Netmask of interface */
67 struct sockaddr *ifu_broadaddr;
68 /* Broadcast address of interface */
69 struct sockaddr *ifu_dstaddr;
70 /* Point-to-point destination address */
72 #define ifa_broadaddr ifa_ifu.ifu_broadaddr
73 #define ifa_dstaddr ifa_ifu.ifu_dstaddr
74 void *ifa_data; /* Address-specific data */
81 field contains a pointer to the next structure on the list,
82 or NULL if this is the last item of the list.
86 points to the null-terminated interface name.
89 .\" indicates the maximum length of this field.
93 field contains the interface flags, as returned by the
98 for a list of these flags).
102 field points to a structure containing the interface address.
105 sub-field should be consulted to determine the format of the
110 field points to a structure containing the netmask associated with
112 if applicable for the address family.
114 Depending on whether the bit
120 (only one can be set at a time),
123 will contain the broadcast address associated with
125 (if applicable for the address family) or
127 will contain the destination address of the point-to-point interface.
131 field points to a buffer containing address-family-specific data;
132 this field may be NULL if there is no such data for this interface.
136 is dynamically allocated and should be freed using
138 when no longer needed.
143 on error, -1 is returned, and
145 is set appropriately.
150 for any of the errors specified for
153 .\" FIXME Petr, I added getsockname() and recvmsg(); do you agree?
163 function first appeared in glibc 2.3, but before glibc 2.3.3,
164 the implementation only supported IPv4 addresses;
165 IPv6 support was added in glibc 2.3.3.
168 This function first appeared in BSDi and is
169 present on the BSD systems, but with slightly different
170 semantics documented\(emreturning one entry per interface,
174 and other fields can actually be NULL if the interface has no address,
175 and no link-level address is returned if the interface has an IP address
177 Also, the way of choosing either
181 differs on various systems.
182 .\" , but the BSD-derived documentation generally
183 .\" appears to be confused and obsolete on this point.
184 .\" i.e., commonly it still says one of them will be NULL, even if
185 .\" the ifa_ifu union is already present
187 The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
188 assigned to the interface, but also one
190 address per interface containing lower-level details about the interface
191 and its physical layer.
194 field may contain a pointer to a
195 .IR "struct net_device_stats" ,
197 .IR <linux/netdevice.h> ,
198 which contains various interface attributes and statistics.
200 The program below demonstrates the use of
205 Here is what we see when running this program on one system:
210 lo address family: 17 (AF_PACKET)
211 eth0 address family: 17 (AF_PACKET)
212 lo address family: 2 (AF_INET)
214 eth0 address family: 2 (AF_INET)
216 lo address family: 10 (AF_INET6)
218 eth0 address family: 10 (AF_INET6)
219 address: <fe80::2d0:59ff:feda:eb51%eth0>
225 #include <arpa/inet.h>
226 #include <sys/socket.h>
234 main(int argc, char *argv[])
236 struct ifaddrs *ifaddr;
237 struct net_device_stats *ndsp;
239 char host[NI_MAXHOST];
241 if (getifaddrs(&ifaddr) == \-1) {
242 perror("getifaddrs");
246 while (ifaddr != NULL) { /* Walk through linked list */
247 family = ifaddr\->ifa_addr\->sa_family;
249 /* Display interface name and family (including symbolic
250 form of the latter for the common families) */
252 printf("%\-6s address family: %d%s\\n",
253 ifaddr\->ifa_name, family,
254 (family == AF_PACKET) ? " (AF_PACKET)" :
255 (family == AF_INET) ? " (AF_INET)" :
256 (family == AF_INET6) ? " (AF_INET6)" : "");
258 /* For an AF_INET* interface address, display the address */
260 if (family == AF_INET || family == AF_INET6) {
261 s = getnameinfo(ifaddr\->ifa_addr,
262 (family == AF_INET) ? sizeof(struct sockaddr_in) :
263 sizeof(struct sockaddr_in6),
264 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
266 printf("getnameinfo() failed: %s\\n", gai_strerror(s));
269 printf("\\taddress: <%s>\\n", host);
272 ifaddr = ifaddr\->ifa_next;