interfaceiter.c revision dafcb997e390efa4423883dafd100c975c4095d6
/*
* Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: interfaceiter.c,v 1.8 2004/03/05 05:11:57 marka Exp $ */
/*
* Note that this code will need to be revisited to support IPv6 Interfaces.
* For now we just iterate through IPv4 interfaces.
*/
#include <config.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <isc/interfaceiter.h>
#include <isc/strerror.h>
/* Common utility functions */
/*
* Extract the network address part from a "struct sockaddr".
*
* The address family is given explicity
* instead of using src->sa_family, because the latter does not work
* for copying a network mask obtained by SIOCGIFNETMASK (it does
* not have a valid address family).
*/
struct isc_interfaceiter {
unsigned int magic; /* Magic number. */
int socket;
int numIF; /* Current Interface count */
int totalIF; /* Total Number
of Interfaces */
unsigned int bufsize; /* Bytes allocated. */
};
/*
* Size of buffer for SIO_GET_INTERFACE_LIST, in number of interfaces.
* We assume no sane system will have more than than 1K of IP addresses on
* all of its adapters.
*/
#define IFCONF_SIZE_INITIAL 16
#define IFCONF_SIZE_INCREMENT 64
#define IFCONF_SIZE_MAX 1040
static void
switch (family) {
case AF_INET:
sizeof(struct in_addr));
break;
case AF_INET6:
sizeof(struct in6_addr));
break;
default:
INSIST(0);
break;
}
}
char strbuf[ISC_STRERRORSIZE];
int error;
unsigned long bytesReturned = 0;
return (ISC_R_NOMEMORY);
/*
* Create an unbound datagram socket to do the
* SIO_GET_INTERFACE_LIST WSAIoctl on.
*/
error = WSAGetLastError();
"making interface scan socket: %s",
strbuf);
goto socket_failure;
}
/*
* Get the interface configuration, allocating more memory if
* necessary.
*/
for (;;) {
goto alloc_failure;
}
&bytesReturned, 0, 0) == SOCKET_ERROR)
{
error = WSAGetLastError();
"get interface configuration: %s",
strbuf);
goto ioctl_failure;
}
/*
* EINVAL. Retry with a bigger buffer.
*/
} else {
/*
* The WSAIoctl succeeded.
* If the number of the returned bytes is the same
* as the buffer size, we will grow it just in
* case and retry.
*/
if (bytesReturned > 0 &&
break;
}
"get interface configuration: "
"maximum buffer size exceeded");
goto ioctl_failure;
}
sizeof(INTERFACE_INFO);
}
/*
* A newly created iterator has an undefined position
* until isc_interfaceiter_first() is called.
*/
/* We don't need the socket any more, so close it */
return (ISC_R_SUCCESS);
return (result);
}
/*
* Get information about the current interface to iter->current.
* If successful, return ISC_R_SUCCESS.
* If the interface has an unsupported address family, or if
* some operation on it fails, return ISC_R_IGNORE to make
* the higher-level iterator code ignore it.
*/
static isc_result_t
unsigned long flags;
/*
* Get interface flags.
*/
if ((flags & IFF_POINTTOPOINT) != 0) {
}
if ((flags & IFF_LOOPBACK) != 0) {
}
/*
* If the interface is point-to-point, get the destination address.
*/
}
/*
* Get the network mask.
*/
switch (family) {
case AF_INET:
break;
case AF_INET6:
break;
}
return (ISC_R_SUCCESS);
}
/*
* Step the iterator to the next interface. Unlike
* isc_interfaceiter_next(), this may leave the iterator
* positioned on an interface that will ultimately
* be ignored. Return ISC_R_NOMORE if there are no more
* interfaces, otherwise ISC_R_SUCCESS.
*/
static isc_result_t
return (ISC_R_NOMORE);
/*
* The first one needs to be set up to point to the last
* Element of the array. Go to the end and back up
* Microsoft's implementation is peculiar for returning
* the list in reverse order
*/
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
}
return (ISC_R_SUCCESS);
}
for (;;) {
if (result != ISC_R_SUCCESS)
break;
if (result != ISC_R_IGNORE)
break;
}
return (result);
}
for (;;) {
if (result != ISC_R_SUCCESS)
break;
if (result != ISC_R_IGNORE)
break;
}
return (result);
}
void
}