1 /* $NetBSD: interfaceiter.c,v 1.3 2003/12/04 16:52:28 drochner Exp $ */
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 */
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. */
37 #include <isc/interfaceiter.h>
38 #include <isc/magic.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>
49 /* Must follow <isc/net.h>. */
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).
66 get_addr(unsigned int family
, isc_netaddr_t
*dst
, struct sockaddr
*src
,
69 struct sockaddr_in6
*sa6
;
71 #if !defined(ISC_PLATFORM_HAVEIFNAMETOINDEX) || \
72 !defined(ISC_PLATFORM_HAVESCOPEID)
76 /* clear any remaining value for safety */
77 memset(dst
, 0, sizeof(*dst
));
83 &((struct sockaddr_in
*) src
)->sin_addr
,
84 sizeof(struct in_addr
));
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
);
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
)) {
108 memcpy(&zone16
, &sa6
->sin6_addr
.s6_addr
[2],
110 zone16
= ntohs(zone16
);
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
) {
122 * sin6_scope_id is still not provided,
123 * but the corresponding interface name
124 * is know. Use the interface ID as
127 zone
= if_nametoindex(ifname
);
129 isc_netaddr_setzone(dst
,
145 * Include system-dependent code.
149 #include "ifiter_getifaddrs.c"
150 #elif HAVE_IFLIST_SYSCTL
151 #include "ifiter_sysctl.c"
153 #include "ifiter_ioctl.c"
157 * The remaining code is common to the sysctl and ioctl case.
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
);
170 isc_interfaceiter_first(isc_interfaceiter_t
*iter
) {
173 REQUIRE(VALID_IFITER(iter
));
175 internal_first(iter
);
177 result
= internal_current(iter
);
178 if (result
!= ISC_R_IGNORE
)
180 result
= internal_next(iter
);
181 if (result
!= ISC_R_SUCCESS
)
184 iter
->result
= result
;
189 isc_interfaceiter_next(isc_interfaceiter_t
*iter
) {
192 REQUIRE(VALID_IFITER(iter
));
193 REQUIRE(iter
->result
== ISC_R_SUCCESS
);
196 result
= internal_next(iter
);
197 if (result
!= ISC_R_SUCCESS
)
199 result
= internal_current(iter
);
200 if (result
!= ISC_R_IGNORE
)
203 iter
->result
= result
;
208 isc_interfaceiter_destroy(isc_interfaceiter_t
**iterp
)
210 isc_interfaceiter_t
*iter
;
211 REQUIRE(iterp
!= NULL
);
213 REQUIRE(VALID_IFITER(iter
));
215 internal_destroy(iter
);
216 if (iter
->buf
!= NULL
)
217 isc_mem_put(iter
->mctx
, iter
->buf
, iter
->bufsize
);
220 isc_mem_put(iter
->mctx
, iter
, sizeof(*iter
));