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 .\"*******************************************************************
35 .\" This file was generated with po4a. Translate the source file.
37 .\"*******************************************************************
38 .TH GETIFADDRS 3 2010\-10\-06 GNU "Linux Programmer's Manual"
40 getifaddrs, freeifaddrs \- get interface addresses
43 \fB#include <sys/types.h>\fP
44 \fB#include <ifaddrs.h>\fP
46 \fBint getifaddrs(struct ifaddrs **\fP\fIifap\fP\fB);\fP
48 \fBvoid freeifaddrs(struct ifaddrs *\fP\fIifa\fP\fB);\fP
51 The \fBgetifaddrs\fP() function creates a linked list of structures describing
52 the network interfaces of the local system, and stores the address of the
53 first item of the list in \fI*ifap\fP. The list consists of \fIifaddrs\fP
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 */
77 The \fIifa_next\fP field contains a pointer to the next structure on the list,
78 or NULL if this is the last item of the list.
82 .\" indicates the maximum length of this field.
83 The \fIifa_name\fP points to the null\-terminated interface name.
85 The \fIifa_flags\fP field contains the interface flags, as returned by the
86 \fBSIOCGIFFLAGS\fP \fBioctl\fP(2) operation (see \fBnetdevice\fP(7) for a list of
89 The \fIifa_addr\fP field points to a structure containing the interface
90 address. (The \fIsa_family\fP subfield should be consulted to determine the
91 format of the address structure.)
93 The \fIifa_netmask\fP field points to a structure containing the netmask
94 associated with \fIifa_addr\fP, if applicable for the address family.
96 Depending on whether the bit \fBIFF_BROADCAST\fP or \fBIFF_POINTOPOINT\fP is set
97 in \fIifa_flags\fP (only one can be set at a time), either \fIifa_broadaddr\fP
98 will contain the broadcast address associated with \fIifa_addr\fP (if
99 applicable for the address family) or \fIifa_dstaddr\fP will contain the
100 destination address of the point\-to\-point interface.
102 The \fIifa_data\fP field points to a buffer containing address\-family\-specific
103 data; this field may be NULL if there is no such data for this interface.
105 The data returned by \fBgetifaddrs\fP() is dynamically allocated and should be
106 freed using \fBfreeifaddrs\fP() when no longer needed.
108 On success, \fBgetifaddrs\fP() returns zero; on error, \-1 is returned, and
109 \fIerrno\fP is set appropriately.
111 \fBgetifaddrs\fP() may fail and set \fIerrno\fP for any of the errors specified
112 for \fBsocket\fP(2), \fBbind\fP(2), \fBgetsockname\fP(2), \fBrecvmsg\fP(2),
113 \fBsendto\fP(2), \fBmalloc\fP(3), or \fBrealloc\fP(3).
115 The \fBgetifaddrs\fP() function first appeared in glibc 2.3, but before glibc
116 2.3.3, the implementation only supported IPv4 addresses; IPv6 support was
117 added in glibc 2.3.3. Support of address families other than IPv4 is only
118 available on kernels that support netlink.
120 .\" , but the BSD-derived documentation generally
121 .\" appears to be confused and obsolete on this point.
122 .\" i.e., commonly it still says one of them will be NULL, even if
123 .\" the ifa_ifu union is already present
124 Not in POSIX.1\-2001. This function first appeared in BSDi and is present on
125 the BSD systems, but with slightly different semantics
126 documented\(emreturning one entry per interface, not per address. This
127 means \fIifa_addr\fP and other fields can actually be NULL if the interface has
128 no address, and no link\-level address is returned if the interface has an IP
129 address assigned. Also, the way of choosing either \fIifa_broadaddr\fP or
130 \fIifa_dstaddr\fP differs on various systems.
132 The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
133 assigned to the interface, but also one \fBAF_PACKET\fP address per interface
134 containing lower\-level details about the interface and its physical layer.
135 In this case, the \fIifa_data\fP field may contain a pointer to a \fIstruct
136 net_device_stats\fP, defined in \fI<linux/netdevice.h>\fP, which contains
137 various interface attributes and statistics.
139 The program below demonstrates the use of \fBgetifaddrs\fP(), \fBfreeifaddrs\fP(),
140 and \fBgetnameinfo\fP(3). Here is what we see when running this program on one
146 lo address family: 17 (AF_PACKET)
147 eth0 address family: 17 (AF_PACKET)
148 lo address family: 2 (AF_INET)
150 eth0 address family: 2 (AF_INET)
152 lo address family: 10 (AF_INET6)
154 eth0 address family: 10 (AF_INET6)
155 address: <fe80::2d0:59ff:feda:eb51%eth0>
161 #include <arpa/inet.h>
162 #include <sys/socket.h>
170 main(int argc, char *argv[])
172 struct ifaddrs *ifaddr, *ifa;
174 char host[NI_MAXHOST];
176 if (getifaddrs(&ifaddr) == \-1) {
177 perror("getifaddrs");
181 /* Walk through linked list, maintaining head pointer so we
182 can free list later */
184 for (ifa = ifaddr; ifa != NULL; ifa = ifa\->ifa_next) {
185 if (ifa\->ifa_addr == NULL)
188 family = ifa\->ifa_addr\->sa_family;
190 /* Display interface name and family (including symbolic
191 form of the latter for the common families) */
193 printf("%s\t address family: %d%s\en",
194 ifa\->ifa_name, family,
195 (family == AF_PACKET) ? " (AF_PACKET)" :
196 (family == AF_INET) ? " (AF_INET)" :
197 (family == AF_INET6) ? " (AF_INET6)" : "");
199 /* For an AF_INET* interface address, display the address */
201 if (family == AF_INET || family == AF_INET6) {
202 s = getnameinfo(ifa\->ifa_addr,
203 (family == AF_INET) ? sizeof(struct sockaddr_in) :
204 sizeof(struct sockaddr_in6),
205 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
207 printf("getnameinfo() failed: %s\en", gai_strerror(s));
210 printf("\etaddress: <%s>\en", host);
219 \fBbind\fP(2), \fBgetsockname\fP(2), \fBsocket\fP(2), \fBpacket\fP(7), \fBifconfig\fP(8)