interfaceiter.c revision 68c2ccc953059f389cefc0f8a5ce0f83be7458c9
/*
* Copyright (C) 1999, 2000 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 INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM 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.2 2001/07/08 05:09:02 mayer 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 "errno2result.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;
}
}
int error;
unsigned long bytesReturned = 0;
return (ISC_R_NOMEMORY);
/*
* Create an unbound datagram socket to do the SIO_GET_INTERFACE_LIST WSAIoctl on.
*/
"making interface scan socket: %s",
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",
NTstrerror(error));
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.
*/
break;
}
"get interface configuration: "
"maximum buffer size exceeded");
goto ioctl_failure;
}
}
/*
* 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:
/*
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
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);
}
/*
* The remaining code is common to the sysctl and ioctl case.
*/
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
{
}