2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.2.68.3 2004/03/06 08:14:59 marka Exp $ */
21 * Obtain the list of network interfaces using the getifaddrs(3) library.
26 #define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'G')
27 #define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
29 struct isc_interfaceiter {
30 unsigned int magic; /* Magic number. */
32 void *buf; /* (unused) */
33 unsigned int bufsize; /* (always 0) */
34 struct ifaddrs *ifaddrs; /* List of ifaddrs */
35 struct ifaddrs *pos; /* Ptr to current ifaddr */
36 isc_interface_t current; /* Current interface data. */
37 isc_result_t result; /* Last result code. */
41 isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
42 isc_interfaceiter_t *iter;
44 char strbuf[ISC_STRERRORSIZE];
46 REQUIRE(mctx != NULL);
47 REQUIRE(iterp != NULL);
48 REQUIRE(*iterp == NULL);
50 iter = isc_mem_get(mctx, sizeof(*iter));
52 return (ISC_R_NOMEMORY);
59 if (getifaddrs(&iter->ifaddrs) < 0) {
60 isc__strerror(errno, strbuf, sizeof(strbuf));
61 UNEXPECTED_ERROR(__FILE__, __LINE__,
62 isc_msgcat_get(isc_msgcat,
63 ISC_MSGSET_IFITERGETIFADDRS,
66 "addresses: getifaddrs: %s"),
68 result = ISC_R_UNEXPECTED;
73 * A newly created iterator has an undefined position
74 * until isc_interfaceiter_first() is called.
77 iter->result = ISC_R_FAILURE;
79 iter->magic = IFITER_MAGIC;
81 return (ISC_R_SUCCESS);
84 if (iter->ifaddrs != NULL) /* just in case */
85 freeifaddrs(iter->ifaddrs);
86 isc_mem_put(mctx, iter, sizeof(*iter));
91 * Get information about the current interface to iter->current.
92 * If successful, return ISC_R_SUCCESS.
93 * If the interface has an unsupported address family,
94 * return ISC_R_IGNORE.
98 internal_current(isc_interfaceiter_t *iter) {
101 unsigned int namelen;
103 REQUIRE(VALID_IFITER(iter));
108 INSIST(ifa->ifa_name != NULL);
109 INSIST(ifa->ifa_addr != NULL);
111 family = ifa->ifa_addr->sa_family;
112 if (family != AF_INET && family != AF_INET6)
113 return (ISC_R_IGNORE);
115 memset(&iter->current, 0, sizeof(iter->current));
117 namelen = strlen(ifa->ifa_name);
118 if (namelen > sizeof(iter->current.name) - 1)
119 namelen = sizeof(iter->current.name) - 1;
121 memset(iter->current.name, 0, sizeof(iter->current.name));
122 memcpy(iter->current.name, ifa->ifa_name, namelen);
124 iter->current.flags = 0;
126 if ((ifa->ifa_flags & IFF_UP) != 0)
127 iter->current.flags |= INTERFACE_F_UP;
129 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
130 iter->current.flags |= INTERFACE_F_POINTTOPOINT;
132 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
133 iter->current.flags |= INTERFACE_F_LOOPBACK;
135 iter->current.af = family;
137 get_addr(family, &iter->current.address, ifa->ifa_addr, ifa->ifa_name);
139 if (ifa->ifa_netmask != NULL)
140 get_addr(family, &iter->current.netmask, ifa->ifa_netmask,
143 if (ifa->ifa_dstaddr != NULL &&
144 (iter->current.flags & IFF_POINTOPOINT) != 0)
145 get_addr(family, &iter->current.dstaddress, ifa->ifa_dstaddr,
148 return (ISC_R_SUCCESS);
152 * Step the iterator to the next interface. Unlike
153 * isc_interfaceiter_next(), this may leave the iterator
154 * positioned on an interface that will ultimately
155 * be ignored. Return ISC_R_NOMORE if there are no more
156 * interfaces, otherwise ISC_R_SUCCESS.
159 internal_next(isc_interfaceiter_t *iter) {
160 iter->pos = iter->pos->ifa_next;
162 if (iter->pos == NULL)
163 return (ISC_R_NOMORE);
165 return (ISC_R_SUCCESS);
169 internal_destroy(isc_interfaceiter_t *iter) {
171 freeifaddrs(iter->ifaddrs);
172 iter->ifaddrs = NULL;
176 void internal_first(isc_interfaceiter_t *iter) {
177 iter->pos = iter->ifaddrs;