bind - Upgraded vendor branch to 9.5.2-P1
[dragonfly.git] / contrib / bind-9.5.2 / lib / isc / unix / interfaceiter.c
1 /*
2  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: interfaceiter.c,v 1.42 2007/06/19 23:47:18 tbox Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
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
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <errno.h>
34
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>
47
48 /* Must follow <isc/net.h>. */
49 #ifdef HAVE_NET_IF6_H
50 #include <net/if6.h>
51 #endif
52 #include <net/if.h>
53
54 /* Common utility functions */
55
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).
63  */
64
65 static void
66 get_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src,
67          char *ifname)
68 {
69         struct sockaddr_in6 *sa6;
70
71 #if !defined(ISC_PLATFORM_HAVEIFNAMETOINDEX) || \
72     !defined(ISC_PLATFORM_HAVESCOPEID)
73         UNUSED(ifname);
74 #endif
75
76         /* clear any remaining value for safety */
77         memset(dst, 0, sizeof(*dst));
78
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 {
94                         /*
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.
104                          */
105                         if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
106                                 isc_uint16_t zone16;
107
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;
120
121                                         /*
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.
126                                          */
127                                         zone = if_nametoindex(ifname);
128                                         if (zone != 0) {
129                                                 isc_netaddr_setzone(dst,
130                                                                     (isc_uint32_t)zone);
131                                         }
132 #endif
133                                 }
134                         }
135                 }
136 #endif
137                 break;
138         default:
139                 INSIST(0);
140                 break;
141         }
142 }
143
144 /*
145  * Include system-dependent code.
146  */
147
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
155
156 /*
157  * The remaining code is common to the sysctl and ioctl case.
158  */
159
160 isc_result_t
161 isc_interfaceiter_current(isc_interfaceiter_t *iter,
162                           isc_interface_t *ifdata)
163 {
164         REQUIRE(iter->result == ISC_R_SUCCESS);
165         memcpy(ifdata, &iter->current, sizeof(*ifdata));
166         return (ISC_R_SUCCESS);
167 }
168
169 isc_result_t
170 isc_interfaceiter_first(isc_interfaceiter_t *iter) {
171         isc_result_t result;
172
173         REQUIRE(VALID_IFITER(iter));
174
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;
183         }
184         iter->result = result;
185         return (result);
186 }
187
188 isc_result_t
189 isc_interfaceiter_next(isc_interfaceiter_t *iter) {
190         isc_result_t result;
191
192         REQUIRE(VALID_IFITER(iter));
193         REQUIRE(iter->result == ISC_R_SUCCESS);
194
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;
202         }
203         iter->result = result;
204         return (result);
205 }
206
207 void
208 isc_interfaceiter_destroy(isc_interfaceiter_t **iterp)
209 {
210         isc_interfaceiter_t *iter;
211         REQUIRE(iterp != NULL);
212         iter = *iterp;
213         REQUIRE(VALID_IFITER(iter));
214
215         internal_destroy(iter);
216         if (iter->buf != NULL)
217                 isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
218
219         iter->magic = 0;
220         isc_mem_put(iter->mctx, iter, sizeof(*iter));
221         *iterp = NULL;
222 }