ifiter_ioctl.c revision 91cd0f93ad34d23e8b09dca337120f64fbe8f0a1
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Copyright (C) 1999-2001 Internet Software Consortium.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Permission to use, copy, modify, and distribute this software for any
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * purpose with or without fee is hereby granted, provided that the above
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * copyright notice and this permission notice appear in all copies.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/* $Id: ifiter_ioctl.c,v 1.25 2001/11/27 01:56:14 gson Exp $ */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Obtain the list of network interfaces using the SIOCGLIFCONF ioctl.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * See netintro(4).
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#ifdef ISC_PLATFORM_HAVEIF_LADDRCONF
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define lifc_len iflc_len
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define lifc_buf iflc_buf
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define lifc_req iflc_req
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define LIFCONF if_laddrconf
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#else
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define ISC_HAVE_LIFC_FAMILY 1
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define ISC_HAVE_LIFC_FLAGS 1
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define LIFCONF lifconf
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#endif
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#ifdef ISC_PLATFORM_HAVEIF_LADDRREQ
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define lifr_addr iflr_addr
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define lifr_name iflr_name
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define lifr_dstaddr iflr_dstaddr
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define lifr_flags iflr_flags
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define ss_family sa_family
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define LIFREQ if_laddrreq
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#else
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define LIFREQ lifreq
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#endif
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#endif
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'T')
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstruct isc_interfaceiter {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned int magic; /* Magic number. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mem_t *mctx;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews int socket;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews int mode;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews struct ifconf ifc;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews struct LIFCONF lifc;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#endif
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews void *buf; /* Buffer for sysctl data. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned int bufsize; /* Bytes allocated. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned int pos; /* Current offset in
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews SIOCGLIFCONF data */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_interface_t current; /* Current interface data. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_result_t result; /* Last result code. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews};
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Size of buffer for SIOCGLIFCONF, in bytes. We assume no sane system
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * will have more than a megabyte of interface configuration data.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define IFCONF_BUFSIZE_INITIAL 4096
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define IFCONF_BUFSIZE_MAX 1048576
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic isc_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsgetbuf4(isc_interfaceiter_t *iter) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews char strbuf[ISC_STRERRORSIZE];
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->bufsize = IFCONF_BUFSIZE_INITIAL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews for (;;) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (iter->buf == NULL)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_NOMEMORY);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews memset(&iter->ifc.ifc_len, 0, sizeof(iter->ifc.ifc_len));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->ifc.ifc_len = iter->bufsize;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->ifc.ifc_buf = iter->buf;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Ignore the HP/UX warning about "interger overflow during
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * conversion". It comes from its own macro definition,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * and is really hard to shut up.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (ioctl(iter->socket, SIOCGIFCONF, (char *)&iter->ifc)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews == -1) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (errno != EINVAL) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc__strerror(errno, strbuf, sizeof(strbuf));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_msgcat_get(isc_msgcat,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_MSGSET_IFITERIOCTL,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_MSG_GETIFCONFIG,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "get interface "
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "configuration: %s"),
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews strbuf);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto unexpected;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * EINVAL. Retry with a bigger buffer.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews } else {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * The ioctl succeeded.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Some OS's just return what will fit rather
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * than set EINVAL if the buffer is too small
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * to fit all the interfaces in. If
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * ifc.lifc_len is too near to the end of the
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * buffer we will grow it just in case and
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * retry.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (iter->ifc.ifc_len + 2 * sizeof(struct ifreq)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews < iter->bufsize)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews break;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (iter->bufsize >= IFCONF_BUFSIZE_MAX) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_msgcat_get(isc_msgcat,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_MSGSET_IFITERIOCTL,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_MSG_BUFFERMAX,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "get interface "
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "configuration: "
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "maximum buffer "
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "size exceeded"));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto unexpected;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->bufsize *= 2;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->mode = 4;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_SUCCESS);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unexpected:
611a37e72daff14666f7dcc781674775e0c8e5eaMark Andrews isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->buf = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_UNEXPECTED);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
7c89c5c5747cacffddc6dfff44bbb5010d1beb3fMark Andrewsstatic isc_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsgetbuf6(isc_interfaceiter_t *iter) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#if !defined(SIOCGLIFCONF) || !defined(SIOCGLIFADDR)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNUSED(iter);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_NOTIMPLEMENTED);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#else
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews char strbuf[ISC_STRERRORSIZE];
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->bufsize = IFCONF_BUFSIZE_INITIAL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews for (;;) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (iter->buf == NULL)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_NOMEMORY);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews memset(&iter->lifc.lifc_len, 0, sizeof(iter->lifc.lifc_len));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#ifdef ISC_HAVE_LIFC_FAMILY
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->lifc.lifc_family = AF_UNSPEC;
611a37e72daff14666f7dcc781674775e0c8e5eaMark Andrews#endif
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#ifdef ISC_HAVE_LIFC_FLAGS
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->lifc.lifc_flags = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#endif
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->lifc.lifc_len = iter->bufsize;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->lifc.lifc_buf = iter->buf;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Ignore the HP/UX warning about "interger overflow during
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * conversion". It comes from its own macro definition,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * and is really hard to shut up.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (ioctl(iter->socket, SIOCGLIFCONF, (char *)&iter->lifc)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews == -1) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (errno != EINVAL) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc__strerror(errno, strbuf, sizeof(strbuf));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_msgcat_get(isc_msgcat,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_MSGSET_IFITERIOCTL,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_MSG_GETIFCONFIG,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "get interface "
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "configuration: %s"),
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews strbuf);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto unexpected;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * EINVAL. Retry with a bigger buffer.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews } else {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * The ioctl succeeded.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Some OS's just return what will fit rather
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * than set EINVAL if the buffer is too small
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * to fit all the interfaces in. If
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * ifc.ifc_len is too near to the end of the
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * buffer we will grow it just in case and
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * retry.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (iter->lifc.lifc_len + 2 * sizeof(struct LIFREQ)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews < iter->bufsize)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews break;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (iter->bufsize >= IFCONF_BUFSIZE_MAX) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_msgcat_get(isc_msgcat,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_MSGSET_IFITERIOCTL,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_MSG_BUFFERMAX,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "get interface "
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "configuration: "
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "maximum buffer "
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "size exceeded"));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto unexpected;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->bufsize *= 2;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->mode = 6;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_SUCCESS);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unexpected:
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->buf = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_UNEXPECTED);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#endif
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsisc_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_interfaceiter_t *iter;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_result_t result;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews char strbuf[ISC_STRERRORSIZE];
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews REQUIRE(mctx != NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews REQUIRE(iterp != NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews REQUIRE(*iterp == NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter = isc_mem_get(mctx, sizeof(*iter));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (iter == NULL)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_NOMEMORY);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->mctx = mctx;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->buf = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->mode = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Create an unbound datagram socket to do the SIOCGLIFADDR ioctl on.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if ((iter->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc__strerror(errno, strbuf, sizeof(strbuf));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_msgcat_get(isc_msgcat,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_MSGSET_IFITERIOCTL,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_MSG_MAKESCANSOCKET,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "making interface "
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "scan socket: %s"),
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews strbuf);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = ISC_R_UNEXPECTED;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto socket_failure;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Get the interface configuration, allocating more memory if
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * necessary.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = isc_net_probeipv6();
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (result == ISC_R_SUCCESS)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = getbuf6(iter);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (result != ISC_R_SUCCESS)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = getbuf4(iter);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (result != ISC_R_SUCCESS)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto ioctl_failure;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * A newly created iterator has an undefined position
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * until isc_interfaceiter_first() is called.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->pos = (unsigned int) -1;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->result = ISC_R_FAILURE;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->magic = IFITER_MAGIC;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *iterp = iter;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_SUCCESS);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ioctl_failure:
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (iter->buf != NULL)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mem_put(mctx, iter->buf, iter->bufsize);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews (void) close(iter->socket);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews socket_failure:
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mem_put(mctx, iter, sizeof(*iter));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (result);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Get information about the current interface to iter->current.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * If successful, return ISC_R_SUCCESS.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * If the interface has an unsupported address family, or if
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * some operation on it fails, return ISC_R_IGNORE to make
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * the higher-level iterator code ignore it.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic isc_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsinternal_current4(isc_interfaceiter_t *iter) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews struct ifreq *ifrp;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews struct ifreq ifreq;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews int family;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews char strbuf[ISC_STRERRORSIZE];
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews REQUIRE(VALID_IFITER(iter));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews REQUIRE (iter->pos < (unsigned int) iter->ifc.ifc_len);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ifrp = (struct ifreq *)((char *) iter->ifc.ifc_req + iter->pos);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews memset(&ifreq, 0, sizeof(ifreq));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews memcpy(&ifreq, ifrp, sizeof(ifreq));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews family = ifreq.ifr_addr.sa_family;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#ifdef ISC_PLATFORM_HAVEIPV6
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (family != AF_INET && family != AF_INET6)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#else
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (family != AF_INET)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#endif
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_IGNORE);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews memset(&iter->current, 0, sizeof(iter->current));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->current.af = family;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews INSIST(sizeof(ifreq.ifr_name) <= sizeof(iter->current.name));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews memset(iter->current.name, 0, sizeof(iter->current.name));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews memcpy(iter->current.name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews get_addr(family, &iter->current.address,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews (struct sockaddr *)&ifreq.ifr_addr);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Get interface flags.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->current.flags = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Ignore the HP/UX warning about "interger overflow during
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * conversion. It comes from its own macro definition,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * and is really hard to shut up.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (ioctl(iter->socket, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc__strerror(errno, strbuf, sizeof(strbuf));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "%s: getting interface flags: %s",
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ifreq.ifr_name, strbuf);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_R_IGNORE);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if ((ifreq.ifr_flags & IFF_UP) != 0)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->current.flags |= INTERFACE_F_UP;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if ((ifreq.ifr_flags & IFF_POINTOPOINT) != 0)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->current.flags |= INTERFACE_F_POINTTOPOINT;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if ((ifreq.ifr_flags & IFF_LOOPBACK) != 0)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews iter->current.flags |= INTERFACE_F_LOOPBACK;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * If the interface is point-to-point, get the destination address.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
if ((iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) {
/*
* Ignore the HP/UX warning about "interger overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket, SIOCGIFDSTADDR, (char *)&ifreq)
< 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETDESTADDR,
"%s: getting "
"destination address: %s"),
ifreq.ifr_name, strbuf);
return (ISC_R_IGNORE);
}
get_addr(family, &iter->current.dstaddress,
(struct sockaddr *)&ifreq.ifr_dstaddr);
}
/*
* Get the network mask.
*/
memset(&ifreq, 0, sizeof(ifreq));
memcpy(&ifreq, ifrp, sizeof(ifreq));
switch (family) {
case AF_INET:
/*
* Ignore the HP/UX warning about "interger overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket, SIOCGIFNETMASK, (char *)&ifreq)
< 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETNETMASK,
"%s: getting netmask: %s"),
ifreq.ifr_name, strbuf);
return (ISC_R_IGNORE);
}
get_addr(family, &iter->current.netmask,
(struct sockaddr *)&ifreq.ifr_addr);
break;
case AF_INET6:
break;
}
return (ISC_R_SUCCESS);
}
static isc_result_t
internal_current6(isc_interfaceiter_t *iter) {
#if !defined(SIOCGLIFCONF) || !defined(SIOCGLIFADDR)
UNUSED(iter);
return (ISC_R_NOTIMPLEMENTED);
#else
struct LIFREQ *ifrp;
struct LIFREQ lifreq;
int family;
char strbuf[ISC_STRERRORSIZE];
REQUIRE(VALID_IFITER(iter));
REQUIRE (iter->pos < (unsigned int) iter->lifc.lifc_len);
ifrp = (struct LIFREQ *)((char *) iter->lifc.lifc_req + iter->pos);
memset(&lifreq, 0, sizeof(lifreq));
memcpy(&lifreq, ifrp, sizeof(lifreq));
family = lifreq.lifr_addr.ss_family;
#ifdef ISC_PLATFORM_HAVEIPV6
if (family != AF_INET && family != AF_INET6)
#else
if (family != AF_INET)
#endif
return (ISC_R_IGNORE);
memset(&iter->current, 0, sizeof(iter->current));
iter->current.af = family;
INSIST(sizeof(lifreq.lifr_name) <= sizeof(iter->current.name));
memset(iter->current.name, 0, sizeof(iter->current.name));
memcpy(iter->current.name, lifreq.lifr_name, sizeof(lifreq.lifr_name));
get_addr(family, &iter->current.address,
(struct sockaddr *)&lifreq.lifr_addr);
/*
* Get interface flags.
*/
iter->current.flags = 0;
/*
* Ignore the HP/UX warning about "interger overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket, SIOCGLIFFLAGS, (char *) &lifreq) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: getting interface flags: %s",
lifreq.lifr_name, strbuf);
return (ISC_R_IGNORE);
}
if ((lifreq.lifr_flags & IFF_UP) != 0)
iter->current.flags |= INTERFACE_F_UP;
if ((lifreq.lifr_flags & IFF_POINTOPOINT) != 0)
iter->current.flags |= INTERFACE_F_POINTTOPOINT;
if ((lifreq.lifr_flags & IFF_LOOPBACK) != 0)
iter->current.flags |= INTERFACE_F_LOOPBACK;
/*
* If the interface is point-to-point, get the destination address.
*/
if ((iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0) {
/*
* Ignore the HP/UX warning about "interger overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket, SIOCGLIFDSTADDR, (char *)&lifreq)
< 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETDESTADDR,
"%s: getting "
"destination address: %s"),
lifreq.lifr_name, strbuf);
return (ISC_R_IGNORE);
}
get_addr(family, &iter->current.dstaddress,
(struct sockaddr *)&lifreq.lifr_dstaddr);
}
/*
* Get the network mask.
*/
memset(&lifreq, 0, sizeof(lifreq));
memcpy(&lifreq, ifrp, sizeof(lifreq));
switch (family) {
case AF_INET:
/*
* Ignore the HP/UX warning about "interger overflow during
* conversion. It comes from its own macro definition,
* and is really hard to shut up.
*/
if (ioctl(iter->socket, SIOCGLIFNETMASK, (char *)&lifreq)
< 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETNETMASK,
"%s: getting netmask: %s"),
lifreq.lifr_name, strbuf);
return (ISC_R_IGNORE);
}
get_addr(family, &iter->current.netmask,
(struct sockaddr *)&lifreq.lifr_addr);
break;
case AF_INET6: {
#ifdef lifr_addrlen
int i, bits;
/*
* Netmask already zeroed.
*/
iter->current.netmask.family = family;
for (i = 0; i < lifreq.lifr_addrlen; i += 8) {
bits = lifreq.lifr_addrlen - i;
bits = (bits < 8) ? (8 - bits) : 0;
iter->current.netmask.type.in6.s6_addr[i / 8] =
(~0 << bits) & 0xff;
}
#endif
break;
}
}
return (ISC_R_SUCCESS);
#endif
}
static isc_result_t
internal_current(isc_interfaceiter_t *iter) {
if (iter->mode == 6)
return (internal_current6(iter));
return (internal_current4(iter));
}
/*
* 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
internal_next4(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_PLATFORM_HAVESALEN
if (ifrp->ifr_addr.sa_len > sizeof(struct sockaddr))
iter->pos += sizeof(ifrp->ifr_name) + ifrp->ifr_addr.sa_len;
else
#endif
iter->pos += sizeof(*ifrp);
if (iter->pos >= (unsigned int) iter->ifc.ifc_len)
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
}
static isc_result_t
internal_next6(isc_interfaceiter_t *iter) {
#if !defined(SIOCGLIFCONF) || !defined(SIOCGLIFADDR)
UNUSED(iter);
return (ISC_R_NOTIMPLEMENTED);
#else
struct LIFREQ *ifrp;
REQUIRE (iter->pos < (unsigned int) iter->lifc.lifc_len);
ifrp = (struct LIFREQ *)((char *) iter->lifc.lifc_req + iter->pos);
#ifdef ISC_PLATFORM_HAVESALEN
if (ifrp->lifr_addr.sa_len > sizeof(struct sockaddr))
iter->pos += sizeof(ifrp->lifr_name) + ifrp->lifr_addr.sa_len;
else
#endif
iter->pos += sizeof(*ifrp);
if (iter->pos >= (unsigned int) iter->lifc.lifc_len)
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
#endif
}
static isc_result_t
internal_next(isc_interfaceiter_t *iter) {
if (iter->mode == 6)
return (internal_next6(iter));
return (internal_next4(iter));
}
static void
internal_destroy(isc_interfaceiter_t *iter) {
(void) close(iter->socket);
}