interfaceiter.c revision 15a44745412679c30a6d022733925af70a38b715
4a73cf8ee07ae396be20231ac252a24830f3c801Bob Halley/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1999, 2000 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * copyright notice and this permission notice appear in all copies.
4a73cf8ee07ae396be20231ac252a24830f3c801Bob Halley *
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
d362465c77b375be2707bc83cebc731d0645d12dAutomatic Updater * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence */
47b7dfffe5d806c6a5e99ef17f07bcde812c2132Francis Dupont
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence/* $Id: interfaceiter.c,v 1.18 2000/07/27 09:52:46 tale Exp $ */
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#include <config.h>
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#include <sys/types.h>
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#include <sys/ioctl.h>
4be63b1fd8c18dbeca1648d6cf22fa14f057a469David Lawrence#ifdef HAVE_SYS_SOCKIO_H
4be63b1fd8c18dbeca1648d6cf22fa14f057a469David Lawrence#include <sys/sockio.h> /* Required for ifiter_ioctl.c. */
4be63b1fd8c18dbeca1648d6cf22fa14f057a469David Lawrence#endif
33f236e3a196ce9d9e4fa60e3386b30ff8de4905David Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <stdio.h>
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#include <stdlib.h>
33f236e3a196ce9d9e4fa60e3386b30ff8de4905David Lawrence#include <unistd.h>
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#include <errno.h>
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence
b09983678f5d116d3c8387aaeab4f2dc4deb0454David Lawrence#include <isc/mem.h>
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#include <isc/net.h>
e7fb847ed570dd8c1bcdacabb3d69bd81feb79aeMark Andrews#include <isc/result.h>
e7fb847ed570dd8c1bcdacabb3d69bd81feb79aeMark Andrews#include <isc/string.h>
e7fb847ed570dd8c1bcdacabb3d69bd81feb79aeMark Andrews#include <isc/types.h>
e7fb847ed570dd8c1bcdacabb3d69bd81feb79aeMark Andrews#include <isc/interfaceiter.h>
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#include <isc/util.h>
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#include <net/if.h> /* Must follow <isc/net.h>. */
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence/* Common utility functions */
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence/*
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence * Extract the network address part from a "struct sockaddr".
b09983678f5d116d3c8387aaeab4f2dc4deb0454David Lawrence *
b09983678f5d116d3c8387aaeab4f2dc4deb0454David Lawrence * The address family is given explicity
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence * instead of using src->sa_family, because the latter does not work
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence * for copying a network mask obtained by SIOCGIFNETMASK (it does
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence * not have a valid address family).
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence */
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrencestatic void
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrenceget_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src) {
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence dst->family = family;
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence switch (family) {
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence case AF_INET:
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence memcpy(&dst->type.in,
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence &((struct sockaddr_in *) src)->sin_addr,
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence sizeof(struct in_addr));
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence break;
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence case AF_INET6:
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence memcpy(&dst->type.in6,
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence &((struct sockaddr_in6 *) src)->sin6_addr,
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence sizeof(struct in6_addr));
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence break;
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence default:
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence INSIST(0);
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence break;
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence }
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence}
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence/*
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence * Include system-dependent code.
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence */
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#if HAVE_IFLIST_SYSCTL
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#include "ifiter_sysctl.c"
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#else
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson#include "ifiter_ioctl.c"
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence#endif
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence/*
53bd3f325f898b6892742ce928da3daa02ba0eddDavid Lawrence * The remaining code is common to the sysctl and ioctl case.
b09983678f5d116d3c8387aaeab4f2dc4deb0454David Lawrence */
b09983678f5d116d3c8387aaeab4f2dc4deb0454David Lawrence
4be63b1fd8c18dbeca1648d6cf22fa14f057a469David Lawrenceisc_result_t
isc_interfaceiter_current(isc_interfaceiter_t *iter,
isc_interface_t *ifdata)
{
REQUIRE(iter->result == ISC_R_SUCCESS);
memcpy(ifdata, &iter->current, sizeof(*ifdata));
return (ISC_R_SUCCESS);
}
isc_result_t
isc_interfaceiter_first(isc_interfaceiter_t *iter) {
isc_result_t result;
REQUIRE(VALID_IFITER(iter));
iter->pos = 0;
for (;;) {
result = internal_current(iter);
if (result != ISC_R_IGNORE)
break;
result = internal_next(iter);
if (result != ISC_R_SUCCESS)
break;
}
iter->result = result;
return (result);
}
isc_result_t
isc_interfaceiter_next(isc_interfaceiter_t *iter) {
isc_result_t result;
REQUIRE(VALID_IFITER(iter));
REQUIRE(iter->result == ISC_R_SUCCESS);
for (;;) {
result = internal_next(iter);
if (result != ISC_R_SUCCESS)
break;
result = internal_current(iter);
if (result != ISC_R_IGNORE)
break;
}
iter->result = result;
return (result);
}
void
isc_interfaceiter_destroy(isc_interfaceiter_t **iterp)
{
isc_interfaceiter_t *iter;
REQUIRE(iterp != NULL);
iter = *iterp;
REQUIRE(VALID_IFITER(iter));
internal_destroy(iter);
isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
iter->magic = 0;
isc_mem_put(iter->mctx, iter, sizeof *iter);
*iterp = NULL;
}