1GETIFADDRS(3) Linux Programmer's Manual GETIFADDRS(3)
2
3
4
6 getifaddrs, freeifaddrs - get interface addresses
7
9 #include <sys/types.h>
10 #include <ifaddrs.h>
11
12 int getifaddrs(struct ifaddrs **ifap);
13
14 void freeifaddrs(struct ifaddrs *ifa);
15
17 The getifaddrs() function creates a linked list of structures describ‐
18 ing the network interfaces of the local system, and stores the address
19 of the first item of the list in *ifap. The list consists of ifaddrs
20 structures, defined as follows:
21
22 struct ifaddrs {
23 struct ifaddrs *ifa_next; /* Next item in list */
24 char *ifa_name; /* Name of interface */
25 unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
26 struct sockaddr *ifa_addr; /* Address of interface */
27 struct sockaddr *ifa_netmask; /* Netmask of interface */
28 union {
29 struct sockaddr *ifu_broadaddr;
30 /* Broadcast address of interface */
31 struct sockaddr *ifu_dstaddr;
32 /* Point-to-point destination address */
33 } ifa_ifu;
34 #define ifa_broadaddr ifa_ifu.ifu_broadaddr
35 #define ifa_dstaddr ifa_ifu.ifu_dstaddr
36 void *ifa_data; /* Address-specific data */
37 };
38
39 The ifa_next field contains a pointer to the next structure on the
40 list, or NULL if this is the last item of the list.
41
42 The ifa_name points to the null-terminated interface name.
43
44 The ifa_flags field contains the interface flags, as returned by the
45 SIOCGIFFLAGS ioctl(2) operation (see netdevice(7) for a list of these
46 flags).
47
48 The ifa_addr field points to a structure containing the interface
49 address. (The sa_family subfield should be consulted to determine the
50 format of the address structure.) This field may contain a NULL
51 pointer.
52
53 The ifa_netmask field points to a structure containing the netmask
54 associated with ifa_addr, if applicable for the address family. This
55 field may contain a NULL pointer.
56
57 Depending on whether the bit IFF_BROADCAST or IFF_POINTOPOINT is set in
58 ifa_flags (only one can be set at a time), either ifa_broadaddr will
59 contain the broadcast address associated with ifa_addr (if applicable
60 for the address family) or ifa_dstaddr will contain the destination
61 address of the point-to-point interface.
62
63 The ifa_data field points to a buffer containing address-family-spe‐
64 cific data; this field may be NULL if there is no such data for this
65 interface.
66
67 The data returned by getifaddrs() is dynamically allocated and should
68 be freed using freeifaddrs() when no longer needed.
69
71 On success, getifaddrs() returns zero; on error, -1 is returned, and
72 errno is set appropriately.
73
75 getifaddrs() may fail and set errno for any of the errors specified for
76 socket(2), bind(2), getsockname(2), recvmsg(2), sendto(2), malloc(3),
77 or realloc(3).
78
80 The getifaddrs() function first appeared in glibc 2.3, but before glibc
81 2.3.3, the implementation supported only IPv4 addresses; IPv6 support
82 was added in glibc 2.3.3. Support of address families other than IPv4
83 is available only on kernels that support netlink.
84
86 Not in POSIX.1-2001. This function first appeared in BSDi and is
87 present on the BSD systems, but with slightly different semantics docu‐
88 mented—returning one entry per interface, not per address. This means
89 ifa_addr and other fields can actually be NULL if the interface has no
90 address, and no link-level address is returned if the interface has an
91 IP address assigned. Also, the way of choosing either ifa_broadaddr or
92 ifa_dstaddr differs on various systems.
93
95 The addresses returned on Linux will usually be the IPv4 and IPv6
96 addresses assigned to the interface, but also one AF_PACKET address per
97 interface containing lower-level details about the interface and its
98 physical layer. In this case, the ifa_data field may contain a pointer
99 to a struct rtnl_link_stats, defined in <linux/if_link.h> (in Linux 2.4
100 and earlier, struct net_device_stats, defined in <linux/netdevice.h>),
101 which contains various interface attributes and statistics.
102
104 The program below demonstrates the use of getifaddrs(), freeifaddrs(),
105 and getnameinfo(3). Here is what we see when running this program on
106 one system:
107
108 $ ./a.out
109 lo address family: 17 (AF_PACKET)
110 eth0 address family: 17 (AF_PACKET)
111 lo address family: 2 (AF_INET)
112 address: <127.0.0.1>
113 eth0 address family: 2 (AF_INET)
114 address: <10.1.1.4>
115 lo address family: 10 (AF_INET6)
116 address: <::1>
117 eth0 address family: 10 (AF_INET6)
118 address: <fe80::2d0:59ff:feda:eb51%eth0>
119
120 Program source
121
122 #include <arpa/inet.h>
123 #include <sys/socket.h>
124 #include <netdb.h>
125 #include <ifaddrs.h>
126 #include <stdio.h>
127 #include <stdlib.h>
128 #include <unistd.h>
129
130 int
131 main(int argc, char *argv[])
132 {
133 struct ifaddrs *ifaddr, *ifa;
134 int family, s;
135 char host[NI_MAXHOST];
136
137 if (getifaddrs(&ifaddr) == -1) {
138 perror("getifaddrs");
139 exit(EXIT_FAILURE);
140 }
141
142 /* Walk through linked list, maintaining head pointer so we
143 can free list later */
144
145 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
146 if (ifa->ifa_addr == NULL)
147 continue;
148
149 family = ifa->ifa_addr->sa_family;
150
151 /* Display interface name and family (including symbolic
152 form of the latter for the common families) */
153
154 printf("%s address family: %d%s\n",
155 ifa->ifa_name, family,
156 (family == AF_PACKET) ? " (AF_PACKET)" :
157 (family == AF_INET) ? " (AF_INET)" :
158 (family == AF_INET6) ? " (AF_INET6)" : "");
159
160 /* For an AF_INET* interface address, display the address */
161
162 if (family == AF_INET || family == AF_INET6) {
163 s = getnameinfo(ifa->ifa_addr,
164 (family == AF_INET) ? sizeof(struct sockaddr_in) :
165 sizeof(struct sockaddr_in6),
166 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
167 if (s != 0) {
168 printf("getnameinfo() failed: %s\n", gai_strerror(s));
169 exit(EXIT_FAILURE);
170 }
171 printf("\taddress: <%s>\n", host);
172 }
173 }
174
175 freeifaddrs(ifaddr);
176 exit(EXIT_SUCCESS);
177 }
178
180 bind(2), getsockname(2), socket(2), packet(7), ifconfig(8)
181
183 This page is part of release 3.53 of the Linux man-pages project. A
184 description of the project, and information about reporting bugs, can
185 be found at http://www.kernel.org/doc/man-pages/.
186
187
188
189GNU 2012-11-11 GETIFADDRS(3)