Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / libisc / interfaceiter.c
blobe9e65ff1796c308dadb5d2aa0d28ec37865cc1e4
1 /* $NetBSD: interfaceiter.c,v 1.3 2003/12/04 16:52:28 drochner Exp $ */
3 /*
4 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: interfaceiter.c,v 1.22.2.1.10.14 2004/08/28 06:25:22 marka Exp */
22 #include <config.h>
24 #define ISC_ONLY_IPV6
26 #include <sys/types.h>
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_SOCKIO_H
29 #include <sys/sockio.h> /* Required for ifiter_ioctl.c. */
30 #endif
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <errno.h>
37 #include <isc/interfaceiter.h>
38 #include <isc/magic.h>
39 #include <isc/mem.h>
40 #include <isc/msgs.h>
41 #include <isc/net.h>
42 #include <isc/print.h>
43 #include <isc/result.h>
44 #include <isc/strerror.h>
45 #include <isc/string.h>
46 #include <isc/types.h>
47 #include <isc/util.h>
49 /* Must follow <isc/net.h>. */
50 #ifdef HAVE_NET_IF6_H
51 #include <net/if6.h>
52 #endif
54 /* Common utility functions */
57 * Extract the network address part from a "struct sockaddr".
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;