interfaceiter.c revision e8c70b0c35c27a28ea2e0cafb252e1774ccc1727
/*
* Copyright (C) 1999-2001, 2004, 2007-2009, 2013-2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* $Id: interfaceiter.c,v 1.15 2009/01/18 23:48:14 tbox 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>
void InitSockets(void);
/* Common utility functions */
/*
* Extract the network address part from a "struct sockaddr".
*
* The address family is given explicitly
* 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 numIF; /* Current Interface count. */
int v4IF; /* Number of IPv4 Interfaces */
unsigned int buf4size; /* Bytes allocated. */
unsigned int buf6size; /* Bytes allocated. */
unsigned int pos6; /* Which entry to process. */
};
/*
* 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);
InitSockets();
/*
* Create an unbound datagram socket to do the
* SIO_GET_INTERFACE_LIST WSAIoctl on.
*/
error = WSAGetLastError();
if (error == WSAEAFNOSUPPORT)
goto inet6_only;
"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 */
/*
* Create an unbound datagram socket to do the
* SIO_ADDRESS_LIST_QUERY WSAIoctl on.
*/
error = WSAGetLastError();
if (error == WSAEAFNOSUPPORT)
goto inet_only;
"making interface scan socket: %s",
strbuf);
goto ioctl_failure;
}
/*
* Get the interface configuration, allocating more memory if
* necessary.
*/
IFCONF_SIZE_INITIAL*sizeof(SOCKET_ADDRESS);
for (;;) {
goto ioctl_failure;
}
&bytesReturned, 0, 0) == SOCKET_ERROR)
{
error = WSAGetLastError();
"sio address list query: %s",
strbuf);
goto ioctl6_failure;
}
/*
* EINVAL. Retry with a bigger buffer.
*/
} else
break;
"get interface configuration: "
"maximum buffer size exceeded");
goto ioctl6_failure;
}
sizeof(SOCKET_ADDRESS);
}
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.
*/
return (ISC_R_SUCCESS);
}
static isc_result_t
int i;
/*
* Set interface flags.
*/
for (i = 0; i < 16; i++)
} else {
/*
* See if we can bind to the ::1 and if so return ::1.
*/
struct sockaddr_in6 sin6;
if (fd == INVALID_SOCKET)
return (ISC_R_IGNORE);
return (ISC_R_IGNORE);
}
for (i = 0; i < 16; i++) {
if (i != 15)
else
}
}
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);
}
static isc_result_t
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
}
return (ISC_R_SUCCESS);
}
}
return (isc_interfaceiter_next(iter));
}
for (;;) {
if (result == ISC_R_NOMORE) {
if (result != ISC_R_SUCCESS)
break;
if (result == ISC_R_IGNORE)
continue;
break;
} else if (result != ISC_R_SUCCESS)
break;
if (result != ISC_R_IGNORE)
break;
}
return (result);
}
void
}