]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ntp/libisc/interfaceiter.c
This commit was generated by cvs2svn to compensate for changes in r178382,
[FreeBSD/FreeBSD.git] / contrib / ntp / libisc / interfaceiter.c
1 /*
2  * Copyright (C) 1999-2001  Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
9  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11  * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: interfaceiter.c,v 1.27 2002/07/02 05:51:43 marka Exp $ */
19
20 #include <config.h>
21
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. */
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <errno.h>
32
33 #include <isc/interfaceiter.h>
34 #include <isc/magic.h>
35 #include <isc/mem.h>
36 #include <isc/msgs.h>
37 #include <isc/net.h>
38 #include <isc/result.h>
39 #include <isc/strerror.h>
40 #include <isc/string.h>
41 #include <isc/types.h>
42 #include <isc/util.h>
43
44 /* Must follow <isc/net.h>. */
45 #ifdef HAVE_NET_IF6_H
46 #include <net/if6.h>
47 #endif
48 #include <net/if.h>
49
50 /* Common utility functions */
51
52 /*
53  * Extract the network address part from a "struct sockaddr".
54  *
55  * The address family is given explicity
56  * instead of using src->sa_family, because the latter does not work
57  * for copying a network mask obtained by SIOCGIFNETMASK (it does
58  * not have a valid address family).
59  */
60
61 static void
62 get_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src) {
63         dst->family = family;
64         switch (family) {
65         case AF_INET:
66                 memcpy(&dst->type.in,
67                        &((struct sockaddr_in *) src)->sin_addr,
68                        sizeof(struct in_addr));
69                 break;
70         case    AF_INET6:
71                 memcpy(&dst->type.in6,
72                        &((struct sockaddr_in6 *) src)->sin6_addr,
73                        sizeof(struct in6_addr));
74                 break;
75         default:
76                 INSIST(0);
77                 break;
78         }
79 }
80
81 /*
82  * Include system-dependent code.
83  */
84
85 #if HAVE_IFLIST_SYSCTL
86 #include "ifiter_sysctl.c"
87 #else
88 #include "ifiter_ioctl.c"
89 #endif
90
91 /*
92  * The remaining code is common to the sysctl and ioctl case.
93  */
94
95 isc_result_t
96 isc_interfaceiter_current(isc_interfaceiter_t *iter,
97                           isc_interface_t *ifdata)
98 {
99         REQUIRE(iter->result == ISC_R_SUCCESS);
100         memcpy(ifdata, &iter->current, sizeof(*ifdata));
101         return (ISC_R_SUCCESS);
102 }
103
104 isc_result_t
105 isc_interfaceiter_first(isc_interfaceiter_t *iter) {
106         isc_result_t result;
107
108         REQUIRE(VALID_IFITER(iter));
109
110         iter->pos = 0;
111 #ifdef HAVE_TRUCLUSTER
112         iter->clua_context = 0;
113 #endif
114         for (;;) {
115                 result = internal_current(iter);
116                 if (result != ISC_R_IGNORE)
117                         break;
118                 result = internal_next(iter);
119                 if (result != ISC_R_SUCCESS)
120                         break;
121         }
122         iter->result = result;
123         return (result);
124 }
125
126 isc_result_t
127 isc_interfaceiter_next(isc_interfaceiter_t *iter) {
128         isc_result_t result;
129
130         REQUIRE(VALID_IFITER(iter));
131         REQUIRE(iter->result == ISC_R_SUCCESS);
132
133         for (;;) {
134                 result = internal_next(iter);
135                 if (result != ISC_R_SUCCESS)
136                         break;
137                 result = internal_current(iter);
138                 if (result != ISC_R_IGNORE)
139                         break;
140         }
141         iter->result = result;
142         return (result);
143 }
144
145 void
146 isc_interfaceiter_destroy(isc_interfaceiter_t **iterp)
147 {
148         isc_interfaceiter_t *iter;
149         REQUIRE(iterp != NULL);
150         iter = *iterp;
151         REQUIRE(VALID_IFITER(iter));
152
153         internal_destroy(iter);
154         isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
155
156         iter->magic = 0;
157         isc_mem_put(iter->mctx, iter, sizeof(*iter));
158         *iterp = NULL;
159 }