2
* Copyright (C) 1999-2001 Internet Software Consortium.
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.
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.
18
/* $Id: interfaceiter.c,v 1.7 2001/11/27 01:56:21 gson Exp $ */
21
* Note that this code will need to be revisited to support IPv6 Interfaces.
22
* For now we just iterate through IPv4 interfaces.
28
#include <sys/types.h>
34
#include <isc/interfaceiter.h>
36
#include <isc/result.h>
37
#include <isc/string.h>
38
#include <isc/strerror.h>
39
#include <isc/types.h>
42
/* Common utility functions */
45
* Extract the network address part from a "struct sockaddr".
47
* The address family is given explicity
48
* instead of using src->sa_family, because the latter does not work
49
* for copying a network mask obtained by SIOCGIFNETMASK (it does
50
* not have a valid address family).
54
#define IFITER_MAGIC 0x49464954U /* IFIT. */
55
#define VALID_IFITER(t) ((t) != NULL && (t)->magic == IFITER_MAGIC)
57
struct isc_interfaceiter {
58
unsigned int magic; /* Magic number. */
61
INTERFACE_INFO IFData; /* Current Interface Info */
62
int numIF; /* Current Interface count */
63
int totalIF; /* Total Number
65
INTERFACE_INFO *buf; /* Buffer for WSAIoctl data. */
66
unsigned int bufsize; /* Bytes allocated. */
67
INTERFACE_INFO *pos; /* Current offset in IF List */
68
isc_interface_t current; /* Current interface data. */
69
isc_result_t result; /* Last result code. */
74
* Size of buffer for SIO_GET_INTERFACE_LIST, in number of interfaces.
75
* We assume no sane system will have more than than 1K of IP addresses on
76
* all of its adapters.
78
#define IFCONF_SIZE_INITIAL 16
79
#define IFCONF_SIZE_INCREMENT 64
80
#define IFCONF_SIZE_MAX 1040
83
get_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src) {
88
&((struct sockaddr_in *) src)->sin_addr,
89
sizeof(struct in_addr));
92
memcpy(&dst->type.in6,
93
&((struct sockaddr_in6 *) src)->sin6_addr,
94
sizeof(struct in6_addr));
103
isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
104
char strbuf[ISC_STRERRORSIZE];
105
isc_interfaceiter_t *iter;
108
unsigned long bytesReturned = 0;
110
REQUIRE(mctx != NULL);
111
REQUIRE(iterp != NULL);
112
REQUIRE(*iterp == NULL);
114
iter = isc_mem_get(mctx, sizeof(*iter));
116
return (ISC_R_NOMEMORY);
122
* Create an unbound datagram socket to do the
123
* SIO_GET_INTERFACE_LIST WSAIoctl on.
125
if ((iter->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
126
error = WSAGetLastError();
127
isc__strerror(error, strbuf, sizeof(strbuf));
128
UNEXPECTED_ERROR(__FILE__, __LINE__,
129
"making interface scan socket: %s",
131
result = ISC_R_UNEXPECTED;
136
* Get the interface configuration, allocating more memory if
139
iter->bufsize = IFCONF_SIZE_INITIAL*sizeof(INTERFACE_INFO);
142
iter->buf = isc_mem_get(mctx, iter->bufsize);
143
if (iter->buf == NULL) {
144
result = ISC_R_NOMEMORY;
148
if (WSAIoctl(iter->socket, SIO_GET_INTERFACE_LIST,
149
0, 0, iter->buf, iter->bufsize,
150
&bytesReturned, 0, 0) == SOCKET_ERROR)
152
error = WSAGetLastError();
153
if (error != WSAEFAULT && error != WSAENOBUFS) {
155
isc__strerror(error, strbuf, sizeof(strbuf));
156
UNEXPECTED_ERROR(__FILE__, __LINE__,
157
"get interface configuration: %s",
159
result = ISC_R_UNEXPECTED;
163
* EINVAL. Retry with a bigger buffer.
167
* The WSAIoctl succeeded.
168
* If the number of the returned bytes is the same
169
* as the buffer size, we will grow it just in
172
if (bytesReturned > 0 &&
173
(bytesReturned < iter->bufsize))
176
if (iter->bufsize >= IFCONF_SIZE_MAX*sizeof(INTERFACE_INFO)) {
177
UNEXPECTED_ERROR(__FILE__, __LINE__,
178
"get interface configuration: "
179
"maximum buffer size exceeded");
180
result = ISC_R_UNEXPECTED;
183
isc_mem_put(mctx, iter->buf, iter->bufsize);
185
iter->bufsize += IFCONF_SIZE_INCREMENT *
186
sizeof(INTERFACE_INFO);
190
* A newly created iterator has an undefined position
191
* until isc_interfaceiter_first() is called.
194
iter->result = ISC_R_FAILURE;
196
iter->totalIF = bytesReturned/sizeof(INTERFACE_INFO);
199
iter->magic = IFITER_MAGIC;
201
/* We don't need the socket any more, so close it */
202
closesocket(iter->socket);
203
return (ISC_R_SUCCESS);
206
isc_mem_put(mctx, iter->buf, iter->bufsize);
209
(void) closesocket(iter->socket);
212
isc_mem_put(mctx, iter, sizeof(*iter));
217
* Get information about the current interface to iter->current.
218
* If successful, return ISC_R_SUCCESS.
219
* If the interface has an unsupported address family, or if
220
* some operation on it fails, return ISC_R_IGNORE to make
221
* the higher-level iterator code ignore it.
225
internal_current(isc_interfaceiter_t *iter, int family) {
226
BOOL ifNamed = FALSE;
229
REQUIRE(VALID_IFITER(iter));
230
REQUIRE(iter->numIF >= 0);
232
memset(&iter->current, 0, sizeof(iter->current));
233
iter->current.af = family;
235
get_addr(family, &iter->current.address,
236
(struct sockaddr *)&(iter->IFData.iiAddress));
239
* Get interface flags.
242
iter->current.flags = 0;
243
flags = iter->IFData.iiFlags;
245
if ((flags & IFF_UP) != 0)
246
iter->current.flags |= INTERFACE_F_UP;
248
if ((flags & IFF_POINTTOPOINT) != 0) {
249
iter->current.flags |= INTERFACE_F_POINTTOPOINT;
250
sprintf(iter->current.name, "PPP Interface %d", iter->numIF);
254
if ((flags & IFF_LOOPBACK) != 0) {
255
iter->current.flags |= INTERFACE_F_LOOPBACK;
256
sprintf(iter->current.name, "Loopback Interface %d",
262
* If the interface is point-to-point, get the destination address.
264
if ((iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) {
265
get_addr(family, &iter->current.dstaddress,
266
(struct sockaddr *)&(iter->IFData.iiBroadcastAddress));
269
if (ifNamed == FALSE)
270
sprintf(iter->current.name,
271
"TCP/IP Interface %d", iter->numIF);
274
* Get the network mask.
278
get_addr(family, &iter->current.netmask,
279
(struct sockaddr *)&(iter->IFData.iiNetmask));
285
return (ISC_R_SUCCESS);
289
* Step the iterator to the next interface. Unlike
290
* isc_interfaceiter_next(), this may leave the iterator
291
* positioned on an interface that will ultimately
292
* be ignored. Return ISC_R_NOMORE if there are no more
293
* interfaces, otherwise ISC_R_SUCCESS.
296
internal_next(isc_interfaceiter_t *iter) {
297
if (iter->numIF >= iter->totalIF)
298
return (ISC_R_NOMORE);
301
* The first one needs to be set up to point to the last
302
* Element of the array. Go to the end and back up
303
* Microsoft's implementation is peculiar for returning
304
* the list in reverse order
307
if (iter->numIF == 0)
308
iter->pos = (INTERFACE_INFO *)(iter->buf + (iter->totalIF));
311
if (&(iter->pos) < &(iter->buf))
312
return (ISC_R_NOMORE);
314
memset(&(iter->IFData), 0, sizeof(INTERFACE_INFO));
315
memcpy(&(iter->IFData), iter->pos, sizeof(INTERFACE_INFO));
318
return (ISC_R_SUCCESS);
322
isc_interfaceiter_current(isc_interfaceiter_t *iter,
323
isc_interface_t *ifdata) {
324
REQUIRE(iter->result == ISC_R_SUCCESS);
325
memcpy(ifdata, &iter->current, sizeof(*ifdata));
326
return (ISC_R_SUCCESS);
330
isc_interfaceiter_first(isc_interfaceiter_t *iter) {
333
REQUIRE(VALID_IFITER(iter));
337
result = internal_next(iter);
338
if (result != ISC_R_SUCCESS)
340
result = internal_current(iter, AF_INET);
341
if (result != ISC_R_IGNORE)
344
iter->result = result;
349
isc_interfaceiter_next(isc_interfaceiter_t *iter) {
352
REQUIRE(VALID_IFITER(iter));
353
REQUIRE(iter->result == ISC_R_SUCCESS);
356
result = internal_next(iter);
357
if (result != ISC_R_SUCCESS)
359
result = internal_current(iter,AF_INET);
360
if (result != ISC_R_IGNORE)
363
iter->result = result;
368
isc_interfaceiter_destroy(isc_interfaceiter_t **iterp) {
369
isc_interfaceiter_t *iter;
370
REQUIRE(iterp != NULL);
372
REQUIRE(VALID_IFITER(iter));
374
isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
377
isc_mem_put(iter->mctx, iter, sizeof(*iter));