2 * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: ifiter_getifaddrs.c,v 1.13 2009/09/24 23:48:13 tbox Exp $ */
22 * Obtain the list of network interfaces using the getifaddrs(3) library.
28 #define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'G')
30 #define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
33 static isc_boolean_t seenv6 = ISC_FALSE;
36 /*% Iterator structure */
37 struct isc_interfaceiter {
38 unsigned int magic; /*%< Magic number. */
40 void *buf; /*%< (unused) */
41 unsigned int bufsize; /*%< (always 0) */
42 struct ifaddrs *ifaddrs; /*%< List of ifaddrs */
43 struct ifaddrs *pos; /*%< Ptr to current ifaddr */
44 isc_interface_t current; /*%< Current interface data. */
45 isc_result_t result; /*%< Last result code. */
48 char entry[ISC_IF_INET6_SZ];
54 isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
55 isc_interfaceiter_t *iter;
57 char strbuf[ISC_STRERRORSIZE];
60 REQUIRE(mctx != NULL);
61 REQUIRE(iterp != NULL);
62 REQUIRE(*iterp == NULL);
64 iter = isc_mem_get(mctx, sizeof(*iter));
66 return (ISC_R_NOMEMORY);
74 * Only open "/proc/net/if_inet6" if we have never seen a IPv6
75 * address returned by getifaddrs().
78 iter->proc = fopen("/proc/net/if_inet6", "r");
79 if (iter->proc == NULL) {
80 isc__strerror(errno, strbuf, sizeof(strbuf));
81 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
82 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
83 "failed to open /proc/net/if_inet6");
87 iter->valid = ISC_R_FAILURE;
90 /* If interrupted, try again */
91 for (trys = 0; trys < 3; trys++) {
92 if ((ret = getifaddrs(&iter->ifaddrs)) >= 0)
98 isc__strerror(errno, strbuf, sizeof(strbuf));
99 UNEXPECTED_ERROR(__FILE__, __LINE__,
100 "getting interface addresses: %s: %s",
101 isc_msgcat_get(isc_msgcat,
102 ISC_MSGSET_IFITERGETIFADDRS,
106 result = ISC_R_UNEXPECTED;
111 * A newly created iterator has an undefined position
112 * until isc_interfaceiter_first() is called.
115 iter->result = ISC_R_FAILURE;
117 iter->magic = IFITER_MAGIC;
119 return (ISC_R_SUCCESS);
123 if (iter->proc != NULL)
126 if (iter->ifaddrs != NULL) /* just in case */
127 freeifaddrs(iter->ifaddrs);
128 isc_mem_put(mctx, iter, sizeof(*iter));
133 * Get information about the current interface to iter->current.
134 * If successful, return ISC_R_SUCCESS.
135 * If the interface has an unsupported address family,
136 * return ISC_R_IGNORE.
140 internal_current(isc_interfaceiter_t *iter) {
143 unsigned int namelen;
145 REQUIRE(VALID_IFITER(iter));
152 * burnicki: iter->pos is usually never NULL here (anymore?),
153 * so linux_if_inet6_current(iter) is never called here.
154 * However, that routine would check (under Linux), if the
155 * interface is in a tentative state, e.g. if there's no link
156 * yet but an IPv6 address has already be assigned.
158 if (iter->pos == NULL)
159 return (linux_if_inet6_current(iter));
163 INSIST(ifa->ifa_name != NULL);
169 * burnicki: if the interface is not running then
170 * it may be in a tentative state. See above.
172 if ((ifa->ifa_flags & IFF_RUNNING) == 0)
173 return (ISC_R_IGNORE);
176 if (ifa->ifa_addr == NULL)
177 return (ISC_R_IGNORE);
179 family = ifa->ifa_addr->sa_family;
180 if (family != AF_INET && family != AF_INET6)
181 return (ISC_R_IGNORE);
184 if (family == AF_INET6)
188 memset(&iter->current, 0, sizeof(iter->current));
190 namelen = strlen(ifa->ifa_name);
191 if (namelen > sizeof(iter->current.name) - 1)
192 namelen = sizeof(iter->current.name) - 1;
194 memset(iter->current.name, 0, sizeof(iter->current.name));
195 memcpy(iter->current.name, ifa->ifa_name, namelen);
197 iter->current.flags = 0;
199 if ((ifa->ifa_flags & IFF_UP) != 0)
200 iter->current.flags |= INTERFACE_F_UP;
202 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
203 iter->current.flags |= INTERFACE_F_POINTTOPOINT;
205 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
206 iter->current.flags |= INTERFACE_F_LOOPBACK;
208 if ((ifa->ifa_flags & IFF_BROADCAST) != 0)
209 iter->current.flags |= INTERFACE_F_BROADCAST;
212 if ((ifa->ifa_flags & IFF_MULTICAST) != 0)
213 iter->current.flags |= INTERFACE_F_MULTICAST;
216 iter->current.af = family;
218 get_addr(family, &iter->current.address, ifa->ifa_addr, ifa->ifa_name);
220 if (ifa->ifa_netmask != NULL)
221 get_addr(family, &iter->current.netmask, ifa->ifa_netmask,
224 if (ifa->ifa_dstaddr != NULL &&
225 (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
226 get_addr(family, &iter->current.dstaddress, ifa->ifa_dstaddr,
229 if (ifa->ifa_broadaddr != NULL &&
230 (iter->current.flags & INTERFACE_F_BROADCAST) != 0)
231 get_addr(family, &iter->current.broadcast, ifa->ifa_broadaddr,
234 #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
235 iter->current.ifindex = if_nametoindex(iter->current.name);
237 return (ISC_R_SUCCESS);
241 * Step the iterator to the next interface. Unlike
242 * isc_interfaceiter_next(), this may leave the iterator
243 * positioned on an interface that will ultimately
244 * be ignored. Return ISC_R_NOMORE if there are no more
245 * interfaces, otherwise ISC_R_SUCCESS.
248 internal_next(isc_interfaceiter_t *iter) {
250 if (iter->pos != NULL)
251 iter->pos = iter->pos->ifa_next;
252 if (iter->pos == NULL) {
255 return (linux_if_inet6_next(iter));
257 return (ISC_R_NOMORE);
260 return (ISC_R_SUCCESS);
264 internal_destroy(isc_interfaceiter_t *iter) {
267 if (iter->proc != NULL)
272 freeifaddrs(iter->ifaddrs);
273 iter->ifaddrs = NULL;
277 void internal_first(isc_interfaceiter_t *iter) {
280 linux_if_inet6_first(iter);
282 iter->pos = iter->ifaddrs;