No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / unix / ifiter_sysctl.c
blob8679cf38abff1c19d2d42fd6c3a5916ae5dfde6d
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or 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: ifiter_sysctl.c,v 1.25 2007/06/19 23:47:18 tbox Exp */
22 /*! \file
23 * \brief
24 * Obtain the list of network interfaces using sysctl.
25 * See TCP/IP Illustrated Volume 2, sections 19.8, 19.14,
26 * and 19.16.
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
32 #include <net/route.h>
33 #include <net/if_dl.h>
35 /* XXX what about Alpha? */
36 #ifdef sgi
37 #define ROUNDUP(a) ((a) > 0 ? \
38 (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) : \
39 sizeof(__uint64_t))
40 #else
41 #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
42 : sizeof(long))
43 #endif
45 #define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'S')
46 #define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
48 struct isc_interfaceiter {
49 unsigned int magic; /* Magic number. */
50 isc_mem_t *mctx;
51 void *buf; /* Buffer for sysctl data. */
52 unsigned int bufsize; /* Bytes allocated. */
53 unsigned int bufused; /* Bytes used. */
54 unsigned int pos; /* Current offset in
55 sysctl data. */
56 isc_interface_t current; /* Current interface data. */
57 isc_result_t result; /* Last result code. */
60 static int mib[6] = {
61 CTL_NET,
62 PF_ROUTE,
64 0, /* Any address family. */
65 NET_RT_IFLIST,
66 0 /* Flags. */
69 isc_result_t
70 isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
71 isc_interfaceiter_t *iter;
72 isc_result_t result;
73 size_t bufsize;
74 size_t bufused;
75 char strbuf[ISC_STRERRORSIZE];
77 REQUIRE(mctx != NULL);
78 REQUIRE(iterp != NULL);
79 REQUIRE(*iterp == NULL);
81 iter = isc_mem_get(mctx, sizeof(*iter));
82 if (iter == NULL)
83 return (ISC_R_NOMEMORY);
85 iter->mctx = mctx;
86 iter->buf = 0;
89 * Determine the amount of memory needed.
91 bufsize = 0;
92 if (sysctl(mib, 6, NULL, &bufsize, NULL, (size_t) 0) < 0) {
93 isc__strerror(errno, strbuf, sizeof(strbuf));
94 UNEXPECTED_ERROR(__FILE__, __LINE__,
95 isc_msgcat_get(isc_msgcat,
96 ISC_MSGSET_IFITERSYSCTL,
97 ISC_MSG_GETIFLISTSIZE,
98 "getting interface "
99 "list size: sysctl: %s"),
100 strbuf);
101 result = ISC_R_UNEXPECTED;
102 goto failure;
104 iter->bufsize = bufsize;
106 iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
107 if (iter->buf == NULL) {
108 result = ISC_R_NOMEMORY;
109 goto failure;
112 bufused = bufsize;
113 if (sysctl(mib, 6, iter->buf, &bufused, NULL, (size_t) 0) < 0) {
114 isc__strerror(errno, strbuf, sizeof(strbuf));
115 UNEXPECTED_ERROR(__FILE__, __LINE__,
116 isc_msgcat_get(isc_msgcat,
117 ISC_MSGSET_IFITERSYSCTL,
118 ISC_MSG_GETIFLIST,
119 "getting interface list: "
120 "sysctl: %s"),
121 strbuf);
122 result = ISC_R_UNEXPECTED;
123 goto failure;
125 iter->bufused = bufused;
126 INSIST(iter->bufused <= iter->bufsize);
129 * A newly created iterator has an undefined position
130 * until isc_interfaceiter_first() is called.
132 iter->pos = (unsigned int) -1;
133 iter->result = ISC_R_FAILURE;
135 iter->magic = IFITER_MAGIC;
136 *iterp = iter;
137 return (ISC_R_SUCCESS);
139 failure:
140 if (iter->buf != NULL)
141 isc_mem_put(mctx, iter->buf, iter->bufsize);
142 isc_mem_put(mctx, iter, sizeof(*iter));
143 return (result);
147 * Get information about the current interface to iter->current.
148 * If successful, return ISC_R_SUCCESS.
149 * If the interface has an unsupported address family,
150 * return ISC_R_IGNORE. In case of other failure,
151 * return ISC_R_UNEXPECTED.
154 static isc_result_t
155 internal_current(isc_interfaceiter_t *iter) {
156 struct ifa_msghdr *ifam, *ifam_end;
158 REQUIRE(VALID_IFITER(iter));
159 REQUIRE (iter->pos < (unsigned int) iter->bufused);
161 ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
162 ifam_end = (struct ifa_msghdr *) ((char *) iter->buf + iter->bufused);
164 if (ifam->ifam_type == RTM_IFINFO) {
165 struct if_msghdr *ifm = (struct if_msghdr *) ifam;
166 struct sockaddr_dl *sdl = (struct sockaddr_dl *) (ifm + 1);
167 unsigned int namelen;
169 memset(&iter->current, 0, sizeof(iter->current));
171 namelen = sdl->sdl_nlen;
172 if (namelen > sizeof(iter->current.name) - 1)
173 namelen = sizeof(iter->current.name) - 1;
175 memset(iter->current.name, 0, sizeof(iter->current.name));
176 memcpy(iter->current.name, sdl->sdl_data, namelen);
178 iter->current.flags = 0;
180 if ((ifam->ifam_flags & IFF_UP) != 0)
181 iter->current.flags |= INTERFACE_F_UP;
183 if ((ifam->ifam_flags & IFF_POINTOPOINT) != 0)
184 iter->current.flags |= INTERFACE_F_POINTTOPOINT;
186 if ((ifam->ifam_flags & IFF_LOOPBACK) != 0)
187 iter->current.flags |= INTERFACE_F_LOOPBACK;
190 * This is not an interface address.
191 * Force another iteration.
193 return (ISC_R_IGNORE);
194 } else if (ifam->ifam_type == RTM_NEWADDR) {
195 int i;
196 int family;
197 struct sockaddr *mask_sa = NULL;
198 struct sockaddr *addr_sa = NULL;
199 struct sockaddr *dst_sa = NULL;
201 struct sockaddr *sa = (struct sockaddr *)(ifam + 1);
202 family = sa->sa_family;
204 for (i = 0; i < RTAX_MAX; i++)
206 if ((ifam->ifam_addrs & (1 << i)) == 0)
207 continue;
209 INSIST(sa < (struct sockaddr *) ifam_end);
211 switch (i) {
212 case RTAX_NETMASK: /* Netmask */
213 mask_sa = sa;
214 break;
215 case RTAX_IFA: /* Interface address */
216 addr_sa = sa;
217 break;
218 case RTAX_BRD: /* Broadcast or destination address */
219 dst_sa = sa;
220 break;
222 #ifdef ISC_PLATFORM_HAVESALEN
223 sa = (struct sockaddr *)((char*)(sa)
224 + ROUNDUP(sa->sa_len));
225 #else
226 #ifdef sgi
228 * Do as the contributed SGI code does.
230 sa = (struct sockaddr *)((char*)(sa)
231 + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
232 #else
233 /* XXX untested. */
234 sa = (struct sockaddr *)((char*)(sa)
235 + ROUNDUP(sizeof(struct sockaddr)));
236 #endif
237 #endif
240 if (addr_sa == NULL)
241 return (ISC_R_IGNORE);
243 family = addr_sa->sa_family;
244 if (family != AF_INET && family != AF_INET6)
245 return (ISC_R_IGNORE);
247 iter->current.af = family;
249 get_addr(family, &iter->current.address, addr_sa,
250 iter->current.name);
252 if (mask_sa != NULL)
253 get_addr(family, &iter->current.netmask, mask_sa,
254 iter->current.name);
256 if (dst_sa != NULL &&
257 (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
258 get_addr(family, &iter->current.dstaddress, dst_sa,
259 iter->current.name);
261 return (ISC_R_SUCCESS);
262 } else {
263 printf(isc_msgcat_get(isc_msgcat, ISC_MSGSET_IFITERSYSCTL,
264 ISC_MSG_UNEXPECTEDTYPE,
265 "warning: unexpected interface list "
266 "message type\n"));
267 return (ISC_R_IGNORE);
272 * Step the iterator to the next interface. Unlike
273 * isc_interfaceiter_next(), this may leave the iterator
274 * positioned on an interface that will ultimately
275 * be ignored. Return ISC_R_NOMORE if there are no more
276 * interfaces, otherwise ISC_R_SUCCESS.
278 static isc_result_t
279 internal_next(isc_interfaceiter_t *iter) {
280 struct ifa_msghdr *ifam;
281 REQUIRE (iter->pos < (unsigned int) iter->bufused);
283 ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
285 iter->pos += ifam->ifam_msglen;
287 if (iter->pos >= iter->bufused)
288 return (ISC_R_NOMORE);
290 return (ISC_R_SUCCESS);
293 static void
294 internal_destroy(isc_interfaceiter_t *iter) {
295 UNUSED(iter); /* Unused. */
297 * Do nothing.
301 static
302 void internal_first(isc_interfaceiter_t *iter) {
303 iter->pos = 0;