2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 14:35:26 +00:00

sysctl-based network interface list scanning

This commit is contained in:
Andreas Gustafsson
1999-07-21 08:07:55 +00:00
parent 265cfd9711
commit ca485ab26f
6 changed files with 594 additions and 247 deletions

View File

@@ -40,112 +40,7 @@
#include <isc/types.h>
#include <isc/interfaceiter.h>
#define IFITER_MAGIC 0x49464954U /* IFIT. */
#define VALID_IFITER(t) ((t) != NULL && (t)->magic == IFITER_MAGIC)
struct isc_interfaceiter {
unsigned int magic; /* Magic number. */
isc_mem_t *mctx;
int socket;
struct ifconf ifc;
unsigned int bufsize;
unsigned int pos; /* Current offset in
SIOGCONF data */
isc_interface_t current; /* Current interface data. */
isc_result_t result; /* Last result code. */
};
/*
* Size of buffer for SIOCGIFCONF, 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
isc_result_t
isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp)
{
isc_interfaceiter_t *iter;
isc_result_t result;
char *buf;
REQUIRE(mctx != NULL);
REQUIRE(iterp != NULL);
REQUIRE(*iterp == NULL);
iter = isc_mem_get(mctx, sizeof(*iter));
if (iter == NULL)
return (ISC_R_NOMEMORY);
iter->mctx = mctx;
/* Create an unbound datagram socket to do the SIOCGIFADDR ioctl on. */
if ((iter->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"making interface scan socket: %s",
strerror(errno));
result = ISC_R_UNEXPECTED;
goto socket_failure;
}
/*
* Get the interface configuration, allocating more memory if
* necessary.
*/
iter->bufsize = IFCONF_BUFSIZE_INITIAL;
for (;;) {
buf = isc_mem_get(mctx, iter->bufsize);
if (buf == NULL) {
result = ISC_R_NOMEMORY;
goto alloc_failure;
}
iter->ifc.ifc_len = iter->bufsize;
iter->ifc.ifc_buf = buf;
if (ioctl(iter->socket, SIOCGIFCONF, (char *) &iter->ifc) >= 0)
break;
if (errno != EINVAL) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"get interface configuration: %s",
strerror(errno));
result = ISC_R_UNEXPECTED;
goto ioctl_failure;
}
if (iter->bufsize >= IFCONF_BUFSIZE_MAX) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"get interface configuration: "
"maximum buffer size exceeded");
result = ISC_R_UNEXPECTED;
goto ioctl_failure;
}
isc_mem_put(mctx, buf, iter->bufsize);
iter->bufsize *= 2;
}
/*
* A newly created iterator has an undefined position
* until isc_interfaceiter_first() is called.
*/
iter->pos = (unsigned int) -1;
iter->result = ISC_R_FAILURE;
iter->magic = IFITER_MAGIC;
*iterp = iter;
return (ISC_R_SUCCESS);
ioctl_failure:
isc_mem_put(mctx, buf, iter->bufsize);
alloc_failure:
(void) close(iter->socket);
socket_failure:
isc_mem_put(mctx, iter, sizeof *iter);
return (result);
}
/* Common utility functions */
/*
* Extract the network address part from a "struct sockaddr".
@@ -170,117 +65,20 @@ get_addr(int family, isc_netaddr_t *dst, struct sockaddr *src) {
break;
}
}
/*
* Get information about the current interface to iter->current.
* If successful, return ISC_R_SUCCESS.
* If the interface has an unsupported address family,
* return ISC_R_FAILURE. In case of other failure,
* return ISC_R_UNEXPECTED.
*/
static isc_result_t
internal_current(isc_interfaceiter_t *iter) {
struct ifreq *ifrp;
struct ifreq ifreq;
int family;
REQUIRE(VALID_IFITER(iter));
REQUIRE (iter->pos < (unsigned int) iter->ifc.ifc_len);
ifrp = (struct ifreq *)((char *) iter->ifc.ifc_req + iter->pos);
memcpy(&ifreq, ifrp, sizeof ifreq);
family = ifreq.ifr_addr.sa_family;
if (family != AF_INET) /* XXX IPv6 */
return (ISC_R_FAILURE);
memset(&iter->current, 0, sizeof(iter->current));
INSIST(sizeof(ifreq.ifr_name) <= sizeof(iter->current.name));
memcpy(iter->current.name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
get_addr(family, &iter->current.address, &ifreq.ifr_addr);
/* Get interface flags. */
iter->current.flags = 0;
if (ioctl(iter->socket, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: getting interface flags: %s",
ifreq.ifr_name,
strerror(errno));
return (ISC_R_UNEXPECTED);
}
if ((ifreq.ifr_flags & IFF_UP) != 0)
iter->current.flags |= INTERFACE_F_UP;
if ((ifreq.ifr_flags & IFF_POINTOPOINT) != 0)
iter->current.flags |= INTERFACE_F_POINTTOPOINT;
if ((ifreq.ifr_flags & IFF_LOOPBACK) != 0)
iter->current.flags |= INTERFACE_F_LOOPBACK;
/*
* If point-to-point, get the destination address;
* otherwise, get the network mask.
*/
if ((iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) {
if (ioctl(iter->socket, SIOCGIFDSTADDR, (char *) &ifreq) < 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: getting destination address: %s",
ifreq.ifr_name,
strerror(errno));
return (ISC_R_UNEXPECTED);
}
get_addr(family, &iter->current.dstaddress,
&ifreq.ifr_dstaddr);
} else {
if (ioctl(iter->socket, SIOCGIFNETMASK, (char *) &ifreq) < 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: getting netmask: %s",
ifreq.ifr_name,
strerror(errno));
return (ISC_R_UNEXPECTED);
}
get_addr(family, &iter->current.netmask,
&ifreq.ifr_addr);
}
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.
* Include system-dependent code.
*/
static isc_result_t
internal_next(isc_interfaceiter_t *iter) {
struct ifreq *ifrp;
REQUIRE (iter->pos < (unsigned int) iter->ifc.ifc_len);
ifrp = (struct ifreq *)((char *) iter->ifc.ifc_req + iter->pos);
#ifdef ISC_NET_HAVESALEN
if (ifrp->ifr_addr.sa_len > sizeof(struct sockaddr))
iter->pos += sizeof(ifrp->ifr_name) + ifrp->ifr_addr.sa_len;
else
#if HAVE_IFLIST_SYSCTL
#include "ifiter_sysctl.c"
#else
#include "ifiter_ioctl.c"
#endif
iter->pos += sizeof *ifrp;
if (iter->pos >= (unsigned int) iter->ifc.ifc_len)
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
}
/*
* The remaining code is common to the sysctl and ioctl case.
*/
isc_result_t
isc_interfaceiter_current(isc_interfaceiter_t *iter,
@@ -291,9 +89,6 @@ isc_interfaceiter_current(isc_interfaceiter_t *iter,
return (ISC_R_SUCCESS);
}
/*
* Position the iterator on the first user-visible interface.
*/
isc_result_t
isc_interfaceiter_first(isc_interfaceiter_t *iter) {
isc_result_t result;
@@ -305,6 +100,7 @@ isc_interfaceiter_first(isc_interfaceiter_t *iter) {
result = internal_current(iter);
if (result == ISC_R_SUCCESS)
break;
INSIST(result == ISC_R_FAILURE);
result = internal_next(iter);
if (result == ISC_R_NOMORE)
break;
@@ -314,9 +110,6 @@ isc_interfaceiter_first(isc_interfaceiter_t *iter) {
return (result);
}
/*
* Position the iterator on the next user-visible interface.
*/
isc_result_t
isc_interfaceiter_next(isc_interfaceiter_t *iter) {
isc_result_t result;
@@ -332,6 +125,7 @@ isc_interfaceiter_next(isc_interfaceiter_t *iter) {
result = internal_current(iter);
if (result == ISC_R_SUCCESS)
break;
INSIST(result == ISC_R_FAILURE);
}
iter->result = result;
return (result);
@@ -345,8 +139,8 @@ isc_interfaceiter_destroy(isc_interfaceiter_t **iterp)
iter = *iterp;
REQUIRE(VALID_IFITER(iter));
(void) close(iter->socket);
isc_mem_put(iter->mctx, iter->ifc.ifc_buf, iter->bufsize);
internal_destroy(iter);
isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
iter->magic = 0;
isc_mem_put(iter->mctx, iter, sizeof *iter);