ifiter_ioctl.c revision 40f53fa8d9c6a4fc38c0014495e7a42b08f52481
/*
* 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: ifiter_ioctl.c,v 1.13 2000/08/01 01:31:19 tale Exp $ */
/*
* Obtain the list of network interfaces using the SIOCGLIFCONF ioctl.
* See netintro(4).
*/
#ifndef SIOCGLIFCONF
#define SIOCGLIFCONF SIOCGIFCONF
#else
#define ISC_HAVE_LIFC_FAMILY 1
#define ISC_HAVE_LIFC_FLAGS 1
#endif
#ifndef SIOCGLIFADDR
#define SIOCGLIFADDR SIOCGIFADDR
#define SIOCGLIFFLAGS SIOCGIFFLAGS
#define SIOCGLIFDSTADDR SIOCGIFDSTADDR
#define SIOCGLIFNETMASK SIOCGIFNETMASK
#define lifr_dstaddr ifr_dstaddr
#define lifr_flags ifr_flags
#endif
struct isc_interfaceiter {
unsigned int magic; /* Magic number. */
int socket;
void *buf; /* Buffer for sysctl data. */
unsigned int bufsize; /* Bytes allocated. */
unsigned int pos; /* Current offset in
SIOCGLIFCONF data */
};
/*
* Size of buffer for SIOCGLIFCONF, in bytes. We assume no sane system
* will have more than a megabyte of interface configuration data.
*/
#define IFCONF_BUFSIZE_INITIAL 4096
#define IFCONF_BUFSIZE_MAX 1048576
return (ISC_R_NOMEMORY);
/*
* Create an unbound datagram socket to do the SIOCGLIFADDR ioctl on.
*/
"making interface scan socket: %s",
goto socket_failure;
}
/*
* Get the interface configuration, allocating more memory if
* necessary.
*/
for (;;) {
goto alloc_failure;
}
#ifdef ISC_HAVE_LIFC_FAMILY
#endif
#ifdef ISC_HAVE_LIFC_FLAGS
#endif
/*
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
== -1) {
"get interface configuration: %s",
goto ioctl_failure;
}
/*
* EINVAL. Retry with a bigger buffer.
*/
} else {
/*
* The ioctl succeeded.
* Some OS's just return what will fit rather
* than set EINVAL if the buffer is too small
* to fit all the interfaces in. If
* ifc.lifc_len is too near to the end of the
* buffer 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.
*/
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
int family;
return (ISC_R_IGNORE);
/*
* Get interface flags.
*/
/*
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
"%s: getting interface flags: %s",
return (ISC_R_IGNORE);
}
/*
* If the interface is point-to-point, get the destination address.
*/
/*
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
< 0) {
"%s: getting destination address: %s",
return (ISC_R_IGNORE);
}
}
/*
* Get the network mask.
*/
switch (family) {
case AF_INET:
/*
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
< 0) {
"%s: getting netmask: %s",
return (ISC_R_IGNORE);
}
break;
case AF_INET6: {
#ifdef lifr_addrlen
int i, bits;
/*
* Netmask already zeroed.
*/
(~0 << bits) &0xff;
}
#endif
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
#ifdef ISC_PLATFORM_HAVESALEN
else
#endif
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
}
static void
}