2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-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: interfaceiter.c,v 1.22.2.1.10.14 2004/08/28 06:25:22 marka Exp $ */
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. */
35 #include <isc/interfaceiter.h>
36 #include <isc/magic.h>
40 #include <isc/print.h>
41 #include <isc/result.h>
42 #include <isc/strerror.h>
43 #include <isc/string.h>
44 #include <isc/types.h>
47 /* Must follow <isc/net.h>. */
52 /* Common utility functions */
55 * Extract the network address part from a "struct sockaddr".
57 * The address family is given explicitly
58 * instead of using src->sa_family, because the latter does not work
59 * for copying a network mask obtained by SIOCGIFNETMASK (it does
60 * not have a valid address family).
64 get_addr(unsigned int family
, isc_netaddr_t
*dst
, struct sockaddr
*src
,
67 struct sockaddr_in6
*sa6
;
69 #if !defined(ISC_PLATFORM_HAVEIFNAMETOINDEX) || \
70 !defined(ISC_PLATFORM_HAVESCOPEID)
74 /* clear any remaining value for safety */
75 memset(dst
, 0, sizeof(*dst
));
81 &((struct sockaddr_in
*) src
)->sin_addr
,
82 sizeof(struct in_addr
));
85 sa6
= (struct sockaddr_in6
*)src
;
86 memcpy(&dst
->type
.in6
, &sa6
->sin6_addr
,
87 sizeof(struct in6_addr
));
88 #ifdef ISC_PLATFORM_HAVESCOPEID
89 if (sa6
->sin6_scope_id
!= 0)
90 isc_netaddr_setzone(dst
, sa6
->sin6_scope_id
);
93 * BSD variants embed scope zone IDs in the 128bit
94 * address as a kernel internal form. Unfortunately,
95 * the embedded IDs are not hidden from applications
96 * when getting access to them by sysctl or ioctl.
97 * We convert the internal format to the pure address
98 * part and the zone ID part.
99 * Since multicast addresses should not appear here
100 * and they cannot be distinguished from netmasks,
101 * we only consider unicast link-local addresses.
103 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
106 memcpy(&zone16
, &sa6
->sin6_addr
.s6_addr
[2],
108 zone16
= ntohs(zone16
);
110 /* the zone ID is embedded */
111 isc_netaddr_setzone(dst
,
112 (isc_uint32_t
)zone16
);
113 dst
->type
.in6
.s6_addr
[2] = 0;
114 dst
->type
.in6
.s6_addr
[3] = 0;
115 #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
116 } else if (ifname
!= NULL
) {
120 * sin6_scope_id is still not provided,
121 * but the corresponding interface name
122 * is know. Use the interface ID as
125 zone
= if_nametoindex(ifname
);
127 isc_netaddr_setzone(dst
,
143 * Include system-dependent code.
147 #include "ifiter_getifaddrs.c"
148 #elif HAVE_IFLIST_SYSCTL
149 #include "ifiter_sysctl.c"
151 #include "ifiter_ioctl.c"
155 * The remaining code is common to the sysctl and ioctl case.
159 isc_interfaceiter_current(isc_interfaceiter_t
*iter
,
160 isc_interface_t
*ifdata
)
162 REQUIRE(iter
->result
== ISC_R_SUCCESS
);
163 memcpy(ifdata
, &iter
->current
, sizeof(*ifdata
));
164 return (ISC_R_SUCCESS
);
168 isc_interfaceiter_first(isc_interfaceiter_t
*iter
) {
171 REQUIRE(VALID_IFITER(iter
));
173 internal_first(iter
);
175 result
= internal_current(iter
);
176 if (result
!= ISC_R_IGNORE
)
178 result
= internal_next(iter
);
179 if (result
!= ISC_R_SUCCESS
)
182 iter
->result
= result
;
187 isc_interfaceiter_next(isc_interfaceiter_t
*iter
) {
190 REQUIRE(VALID_IFITER(iter
));
191 REQUIRE(iter
->result
== ISC_R_SUCCESS
);
194 result
= internal_next(iter
);
195 if (result
!= ISC_R_SUCCESS
)
197 result
= internal_current(iter
);
198 if (result
!= ISC_R_IGNORE
)
201 iter
->result
= result
;
206 isc_interfaceiter_destroy(isc_interfaceiter_t
**iterp
)
208 isc_interfaceiter_t
*iter
;
209 REQUIRE(iterp
!= NULL
);
211 REQUIRE(VALID_IFITER(iter
));
213 internal_destroy(iter
);
214 if (iter
->buf
!= NULL
)
215 isc_mem_put(iter
->mctx
, iter
->buf
, iter
->bufsize
);
218 isc_mem_put(iter
->mctx
, iter
, sizeof(*iter
));