2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 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.2 2004/03/09 06:12:10 marka Exp $ */
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #ifdef HAVE_SYS_SOCKIO_H
25 #include <sys/sockio.h> /* Required for ifiter_ioctl.c. */
33 #include <isc/magic.h>
37 #include <isc/result.h>
38 #include <isc/strerror.h>
39 #include <isc/string.h>
40 #include <isc/types.h>
41 #include <isc/interfaceiter.h>
44 #include <net/if.h> /* Must follow <isc/net.h>. */
46 /* Common utility functions */
49 * Extract the network address part from a "struct sockaddr".
51 * The address family is given explicity
52 * instead of using src->sa_family, because the latter does not work
53 * for copying a network mask obtained by SIOCGIFNETMASK (it does
54 * not have a valid address family).
58 get_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src) {
63 &((struct sockaddr_in *) src)->sin_addr,
64 sizeof(struct in_addr));
67 memcpy(&dst->type.in6,
68 &((struct sockaddr_in6 *) src)->sin6_addr,
69 sizeof(struct in6_addr));
78 * Include system-dependent code.
81 #if HAVE_IFLIST_SYSCTL
82 #include "ifiter_sysctl.c"
84 #include "ifiter_ioctl.c"
88 * The remaining code is common to the sysctl and ioctl case.
92 isc_interfaceiter_current(isc_interfaceiter_t *iter,
93 isc_interface_t *ifdata)
95 REQUIRE(iter->result == ISC_R_SUCCESS);
96 memcpy(ifdata, &iter->current, sizeof(*ifdata));
97 return (ISC_R_SUCCESS);
101 isc_interfaceiter_first(isc_interfaceiter_t *iter) {
104 REQUIRE(VALID_IFITER(iter));
108 result = internal_current(iter);
109 if (result != ISC_R_IGNORE)
111 result = internal_next(iter);
112 if (result != ISC_R_SUCCESS)
115 iter->result = result;
120 isc_interfaceiter_next(isc_interfaceiter_t *iter) {
123 REQUIRE(VALID_IFITER(iter));
124 REQUIRE(iter->result == ISC_R_SUCCESS);
127 result = internal_next(iter);
128 if (result != ISC_R_SUCCESS)
130 result = internal_current(iter);
131 if (result != ISC_R_IGNORE)
134 iter->result = result;
139 isc_interfaceiter_destroy(isc_interfaceiter_t **iterp)
141 isc_interfaceiter_t *iter;
142 REQUIRE(iterp != NULL);
144 REQUIRE(VALID_IFITER(iter));
146 internal_destroy(iter);
147 isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
150 isc_mem_put(iter->mctx, iter, sizeof *iter);