Detect FPU by checking CPUID features.
[dragonfly.git] / contrib / bind-9.5.2 / lib / isc / unix / interfaceiter.c
blob359e363a983ad4c37dc2c4d18147fd8e50e19ea3
1 /*
2 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-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: interfaceiter.c,v 1.42 2007/06/19 23:47:18 tbox Exp $ */
20 /*! \file */
22 #include <config.h>
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #ifdef HAVE_SYS_SOCKIO_H
27 #include <sys/sockio.h> /* Required for ifiter_ioctl.c. */
28 #endif
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <errno.h>
35 #include <isc/interfaceiter.h>
36 #include <isc/log.h>
37 #include <isc/magic.h>
38 #include <isc/mem.h>
39 #include <isc/msgs.h>
40 #include <isc/net.h>
41 #include <isc/print.h>
42 #include <isc/result.h>
43 #include <isc/strerror.h>
44 #include <isc/string.h>
45 #include <isc/types.h>
46 #include <isc/util.h>
48 /* Must follow <isc/net.h>. */
49 #ifdef HAVE_NET_IF6_H
50 #include <net/if6.h>
51 #endif
52 #include <net/if.h>
54 /* Common utility functions */
56 /*%
57 * Extract the network address part from a "struct sockaddr".
58 * \brief
59 * The address family is given explicitly
60 * instead of using src->sa_family, because the latter does not work
61 * for copying a network mask obtained by SIOCGIFNETMASK (it does
62 * not have a valid address family).
65 static void
66 get_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src,
67 char *ifname)
69 struct sockaddr_in6 *sa6;
71 #if !defined(ISC_PLATFORM_HAVEIFNAMETOINDEX) || \
72 !defined(ISC_PLATFORM_HAVESCOPEID)
73 UNUSED(ifname);
74 #endif
76 /* clear any remaining value for safety */
77 memset(dst, 0, sizeof(*dst));
79 dst->family = family;
80 switch (family) {
81 case AF_INET:
82 memcpy(&dst->type.in,
83 &((struct sockaddr_in *) src)->sin_addr,
84 sizeof(struct in_addr));
85 break;
86 case AF_INET6:
87 sa6 = (struct sockaddr_in6 *)src;
88 memcpy(&dst->type.in6, &sa6->sin6_addr,
89 sizeof(struct in6_addr));
90 #ifdef ISC_PLATFORM_HAVESCOPEID
91 if (sa6->sin6_scope_id != 0)
92 isc_netaddr_setzone(dst, sa6->sin6_scope_id);
93 else {
95 * BSD variants embed scope zone IDs in the 128bit
96 * address as a kernel internal form. Unfortunately,
97 * the embedded IDs are not hidden from applications
98 * when getting access to them by sysctl or ioctl.
99 * We convert the internal format to the pure address
100 * part and the zone ID part.
101 * Since multicast addresses should not appear here
102 * and they cannot be distinguished from netmasks,
103 * we only consider unicast link-local addresses.
105 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
106 isc_uint16_t zone16;
108 memcpy(&zone16, &sa6->sin6_addr.s6_addr[2],
109 sizeof(zone16));
110 zone16 = ntohs(zone16);
111 if (zone16 != 0) {
112 /* the zone ID is embedded */
113 isc_netaddr_setzone(dst,
114 (isc_uint32_t)zone16);
115 dst->type.in6.s6_addr[2] = 0;
116 dst->type.in6.s6_addr[3] = 0;
117 #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
118 } else if (ifname != NULL) {
119 unsigned int zone;
122 * sin6_scope_id is still not provided,
123 * but the corresponding interface name
124 * is know. Use the interface ID as
125 * the link ID.
127 zone = if_nametoindex(ifname);
128 if (zone != 0) {
129 isc_netaddr_setzone(dst,
130 (isc_uint32_t)zone);
132 #endif
136 #endif
137 break;
138 default:
139 INSIST(0);
140 break;
145 * Include system-dependent code.
148 #if HAVE_GETIFADDRS
149 #include "ifiter_getifaddrs.c"
150 #elif HAVE_IFLIST_SYSCTL
151 #include "ifiter_sysctl.c"
152 #else
153 #include "ifiter_ioctl.c"
154 #endif
157 * The remaining code is common to the sysctl and ioctl case.
160 isc_result_t
161 isc_interfaceiter_current(isc_interfaceiter_t *iter,
162 isc_interface_t *ifdata)
164 REQUIRE(iter->result == ISC_R_SUCCESS);
165 memcpy(ifdata, &iter->current, sizeof(*ifdata));
166 return (ISC_R_SUCCESS);
169 isc_result_t
170 isc_interfaceiter_first(isc_interfaceiter_t *iter) {
171 isc_result_t result;
173 REQUIRE(VALID_IFITER(iter));
175 internal_first(iter);
176 for (;;) {
177 result = internal_current(iter);
178 if (result != ISC_R_IGNORE)
179 break;
180 result = internal_next(iter);
181 if (result != ISC_R_SUCCESS)
182 break;
184 iter->result = result;
185 return (result);
188 isc_result_t
189 isc_interfaceiter_next(isc_interfaceiter_t *iter) {
190 isc_result_t result;
192 REQUIRE(VALID_IFITER(iter));
193 REQUIRE(iter->result == ISC_R_SUCCESS);
195 for (;;) {
196 result = internal_next(iter);
197 if (result != ISC_R_SUCCESS)
198 break;
199 result = internal_current(iter);
200 if (result != ISC_R_IGNORE)
201 break;
203 iter->result = result;
204 return (result);
207 void
208 isc_interfaceiter_destroy(isc_interfaceiter_t **iterp)
210 isc_interfaceiter_t *iter;
211 REQUIRE(iterp != NULL);
212 iter = *iterp;
213 REQUIRE(VALID_IFITER(iter));
215 internal_destroy(iter);
216 if (iter->buf != NULL)
217 isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
219 iter->magic = 0;
220 isc_mem_put(iter->mctx, iter, sizeof(*iter));
221 *iterp = NULL;