Commit 31d3464c authored by Mark Andrews's avatar Mark Andrews

1065. [func] Runtime support to select new / old style interface

                        scanning using ioctls.
parent 2f6d1483
1065. [func] Runtime support to select new / old style interface
scanning using ioctls.
1064. [bug] Do not shutdown active interfaces if we are unable 1064. [bug] Do not shutdown active interfaces if we are unable
to scan the list of interfaces. [RT #1921] to scan the list of interfaces. [RT #1921]
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
/* $Id: config.h.in,v 1.48 2001/08/30 04:33:17 marka Exp $ */ /* $Id: config.h.in,v 1.49 2001/10/22 04:01:26 marka Exp $ */
/*** /***
*** This file is not to be included by any public header files, because *** This file is not to be included by any public header files, because
...@@ -165,6 +165,9 @@ int sigwait(const unsigned int *set, int *sig); ...@@ -165,6 +165,9 @@ int sigwait(const unsigned int *set, int *sig);
/* Define if you have the <unistd.h> header file. */ /* Define if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H #undef HAVE_UNISTD_H
/* Define if you have the <net/if6.h> header file. */
#undef NET_IF6_H
/* Define if you have the nsl library (-lnsl). */ /* Define if you have the nsl library (-lnsl). */
#undef HAVE_LIBNSL #undef HAVE_LIBNSL
......
...@@ -18,7 +18,7 @@ AC_DIVERT_PUSH(AC_DIVERSION_NOTICE)dnl ...@@ -18,7 +18,7 @@ AC_DIVERT_PUSH(AC_DIVERSION_NOTICE)dnl
esyscmd([sed "s/^/# /" COPYRIGHT])dnl esyscmd([sed "s/^/# /" COPYRIGHT])dnl
AC_DIVERT_POP()dnl AC_DIVERT_POP()dnl
AC_REVISION($Revision: 1.304 $) AC_REVISION($Revision: 1.305 $)
AC_INIT(lib/dns/name.c) AC_INIT(lib/dns/name.c)
AC_PREREQ(2.13) AC_PREREQ(2.13)
...@@ -194,7 +194,7 @@ AC_PROG_CC ...@@ -194,7 +194,7 @@ AC_PROG_CC
AC_HEADER_STDC AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h sys/time.h unistd.h sys/sockio.h sys/select.h sys/sysctl.h) AC_CHECK_HEADERS(fcntl.h sys/time.h unistd.h sys/sockio.h sys/select.h sys/sysctl.h net/if6.h)
AC_C_CONST AC_C_CONST
AC_C_INLINE AC_C_INLINE
...@@ -1109,6 +1109,28 @@ AC_SUBST(ISC_ISCIPV6_O) ...@@ -1109,6 +1109,28 @@ AC_SUBST(ISC_ISCIPV6_O)
AC_SUBST(ISC_IPV6_C) AC_SUBST(ISC_IPV6_C)
AC_SUBST(LWRES_HAVE_SIN6_SCOPE_ID) AC_SUBST(LWRES_HAVE_SIN6_SCOPE_ID)
AC_MSG_CHECKING([for struct if_laddrreq])
AC_TRY_LINK([
#include <sys/types.h>
#include <net/if6.h>
],[ struct if_laddrreq a; ],
[AC_MSG_RESULT(yes)
ISC_PLATFORM_HAVEIF_LADDRREQ="#define ISC_PLATFORM_HAVEIF_LADDRREQ 1"],
[AC_MSG_RESULT(no)
ISC_PLATFORM_HAVEIF_LADDRREQ="#undef ISC_PLATFORM_HAVEIF_LADDRREQ"])
AC_SUBST(ISC_PLATFORM_HAVEIF_LADDRREQ)
AC_MSG_CHECKING([for struct if_laddrconf])
AC_TRY_LINK([
#include <sys/types.h>
#include <net/if6.h>
],[ struct if_laddrconf a; ],
[AC_MSG_RESULT(yes)
ISC_PLATFORM_HAVEIF_LADDRCONF="#define ISC_PLATFORM_HAVEIF_LADDRCONF 1"],
[AC_MSG_RESULT(no)
ISC_PLATFORM_HAVEIF_LADDRCONF="#undef ISC_PLATFORM_HAVEIF_LADDRCONF"])
AC_SUBST(ISC_PLATFORM_HAVEIF_LADDRCONF)
# #
# Check for network functions that are often missing. We do this # Check for network functions that are often missing. We do this
# after the libtool checking, so we can put the right suffix on # after the libtool checking, so we can put the right suffix on
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
/* $Id: platform.h.in,v 1.25 2001/10/03 05:08:30 marka Exp $ */ /* $Id: platform.h.in,v 1.26 2001/10/22 04:01:29 marka Exp $ */
#ifndef ISC_PLATFORM_H #ifndef ISC_PLATFORM_H
#define ISC_PLATFORM_H 1 #define ISC_PLATFORM_H 1
...@@ -153,6 +153,17 @@ ...@@ -153,6 +153,17 @@
*/ */
@ISC_PLATFORM_HAVELIFCONF@ @ISC_PLATFORM_HAVELIFCONF@
/*
* Define if the system has struct if_laddrconf which is a extended struct
* ifconf for IPv6.
*/
@ISC_PLATFORM_HAVEIF_LADDRCONF@
/*
* Define if the system has struct if_laddrreq.
*/
@ISC_PLATFORM_HAVEIF_LADDRREQ@
/* /*
* Used to control how extern data is linked; needed for Win32 platforms. * Used to control how extern data is linked; needed for Win32 platforms.
*/ */
......
...@@ -15,44 +15,36 @@ ...@@ -15,44 +15,36 @@
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
/* $Id: ifiter_ioctl.c,v 1.22 2001/10/03 05:08:32 marka Exp $ */ /* $Id: ifiter_ioctl.c,v 1.23 2001/10/22 04:01:31 marka Exp $ */
/* /*
* Obtain the list of network interfaces using the SIOCGLIFCONF ioctl. * Obtain the list of network interfaces using the SIOCGLIFCONF ioctl.
* See netintro(4). * See netintro(4).
*/ */
#ifndef ISC_PLATFORM_HAVELIFCONF #if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
/* #ifdef ISC_PLATFORM_HAVEIF_LADDRCONF
* HP uses these differently to Sun. #define lifc_len iflc_len
*/ #define lifc_buf iflc_buf
#undef SIOCGLIFCONF #define lifc_req iflc_req
#undef SIOCGLIFADDR #define LIFCONF if_laddrconf
#endif
#ifndef SIOCGLIFCONF
#define SIOCGLIFCONF SIOCGIFCONF
#define lifc_len ifc_len
#define lifc_buf ifc_buf
#define lifc_req ifc_req
#define lifconf ifconf
#else #else
#define ISC_HAVE_LIFC_FAMILY 1 #define ISC_HAVE_LIFC_FAMILY 1
#define ISC_HAVE_LIFC_FLAGS 1 #define ISC_HAVE_LIFC_FLAGS 1
#define LIFCONF lifconf
#endif #endif
#ifndef SIOCGLIFADDR
#define SIOCGLIFADDR SIOCGIFADDR #ifdef ISC_PLATFORM_HAVEIF_LADDRREQ
#define SIOCGLIFFLAGS SIOCGIFFLAGS #define lifr_addr iflr_addr
#define SIOCGLIFDSTADDR SIOCGIFDSTADDR #define lifr_name iflr_name
#define SIOCGLIFNETMASK SIOCGIFNETMASK #define lifr_dstaddr iflr_dstaddr
#define lifr_addr ifr_addr #define lifr_flags iflr_flags
#define lifr_name ifr_name
#define lifr_dstaddr ifr_dstaddr
#define lifr_flags ifr_flags
#define ss_family sa_family #define ss_family sa_family
#define lifreq ifreq #define LIFREQ if_laddrreq
#else
#define LIFREQ lifreq
#endif
#endif #endif
#define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'T') #define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'T')
#define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC) #define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
...@@ -61,7 +53,11 @@ struct isc_interfaceiter { ...@@ -61,7 +53,11 @@ struct isc_interfaceiter {
unsigned int magic; /* Magic number. */ unsigned int magic; /* Magic number. */
isc_mem_t *mctx; isc_mem_t *mctx;
int socket; int socket;
struct lifconf ifc; int mode;
struct ifconf ifc;
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR)
struct LIFCONF lifc;
#endif
void *buf; /* Buffer for sysctl data. */ void *buf; /* Buffer for sysctl data. */
unsigned int bufsize; /* Bytes allocated. */ unsigned int bufsize; /* Bytes allocated. */
unsigned int pos; /* Current offset in unsigned int pos; /* Current offset in
...@@ -78,67 +74,109 @@ struct isc_interfaceiter { ...@@ -78,67 +74,109 @@ struct isc_interfaceiter {
#define IFCONF_BUFSIZE_INITIAL 4096 #define IFCONF_BUFSIZE_INITIAL 4096
#define IFCONF_BUFSIZE_MAX 1048576 #define IFCONF_BUFSIZE_MAX 1048576
isc_result_t static isc_result_t
isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { getbuf4(isc_interfaceiter_t *iter) {
isc_interfaceiter_t *iter;
isc_result_t result;
char strbuf[ISC_STRERRORSIZE]; char strbuf[ISC_STRERRORSIZE];
REQUIRE(mctx != NULL); iter->bufsize = IFCONF_BUFSIZE_INITIAL;
REQUIRE(iterp != NULL);
REQUIRE(*iterp == NULL);
iter = isc_mem_get(mctx, sizeof(*iter)); for (;;) {
if (iter == NULL) iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
return (ISC_R_NOMEMORY); if (iter->buf == NULL)
return (ISC_R_NOMEMORY);
iter->mctx = mctx; memset(&iter->ifc.ifc_len, 0, sizeof(iter->ifc.ifc_len));
iter->buf = NULL; iter->ifc.ifc_len = iter->bufsize;
iter->ifc.ifc_buf = iter->buf;
/*
* 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, SIOCGIFCONF, (char *)&iter->ifc)
== -1) {
if (errno != EINVAL) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_GETIFCONFIG,
"get interface "
"configuration: %s"),
strbuf);
goto unexpected;
}
/*
* 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.
*/
if (iter->ifc.ifc_len + 2 * sizeof(struct ifreq)
< iter->bufsize)
break;
}
if (iter->bufsize >= IFCONF_BUFSIZE_MAX) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_BUFFERMAX,
"get interface "
"configuration: "
"maximum buffer "
"size exceeded"));
goto unexpected;
}
isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
/* iter->bufsize *= 2;
* Create an unbound datagram socket to do the SIOCGLIFADDR ioctl on.
*/
if ((iter->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_MAKESCANSOCKET,
"making interface "
"scan socket: %s"),
strbuf);
result = ISC_R_UNEXPECTED;
goto socket_failure;
} }
iter->mode = 4;
return (ISC_R_SUCCESS);
unexpected:
isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
iter->buf = NULL;
return (ISC_R_UNEXPECTED);
}
static isc_result_t
getbuf6(isc_interfaceiter_t *iter) {
#if !defined(SIOCGLIFCONF) || !defined(SIOCGLIFADDR)
UNUSED(iter);
return (ISC_R_NOTIMPLEMENTED);
#else
char strbuf[ISC_STRERRORSIZE];
/*
* Get the interface configuration, allocating more memory if
* necessary.
*/
iter->bufsize = IFCONF_BUFSIZE_INITIAL; iter->bufsize = IFCONF_BUFSIZE_INITIAL;
for (;;) { for (;;) {
iter->buf = isc_mem_get(mctx, iter->bufsize); iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
if (iter->buf == NULL) { if (iter->buf == NULL)
result = ISC_R_NOMEMORY; return (ISC_R_NOMEMORY);
goto alloc_failure;
}
memset(&iter->ifc.lifc_len, 0, sizeof(iter->ifc.lifc_len)); memset(&iter->lifc.lifc_len, 0, sizeof(iter->lifc.lifc_len));
#ifdef ISC_HAVE_LIFC_FAMILY #ifdef ISC_HAVE_LIFC_FAMILY
iter->ifc.lifc_family = AF_UNSPEC; iter->lifc.lifc_family = AF_UNSPEC;
#endif #endif
#ifdef ISC_HAVE_LIFC_FLAGS #ifdef ISC_HAVE_LIFC_FLAGS
iter->ifc.lifc_flags = 0; iter->lifc.lifc_flags = 0;
#endif #endif
iter->ifc.lifc_len = iter->bufsize; iter->lifc.lifc_len = iter->bufsize;
iter->ifc.lifc_buf = iter->buf; iter->lifc.lifc_buf = iter->buf;
/* /*
* Ignore the HP/UX warning about "interger overflow during * Ignore the HP/UX warning about "interger overflow during
* conversion". It comes from its own macro definition, * conversion". It comes from its own macro definition,
* and is really hard to shut up. * and is really hard to shut up.
*/ */
if (ioctl(iter->socket, SIOCGLIFCONF, (char *)&iter->ifc) if (ioctl(iter->socket, SIOCGLIFCONF, (char *)&iter->lifc)
== -1) { == -1) {
if (errno != EINVAL) { if (errno != EINVAL) {
isc__strerror(errno, strbuf, sizeof(strbuf)); isc__strerror(errno, strbuf, sizeof(strbuf));
...@@ -149,8 +187,7 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { ...@@ -149,8 +187,7 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
"get interface " "get interface "
"configuration: %s"), "configuration: %s"),
strbuf); strbuf);
result = ISC_R_UNEXPECTED; goto unexpected;
goto ioctl_failure;
} }
/* /*
* EINVAL. Retry with a bigger buffer. * EINVAL. Retry with a bigger buffer.
...@@ -161,11 +198,11 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { ...@@ -161,11 +198,11 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
* Some OS's just return what will fit rather * Some OS's just return what will fit rather
* than set EINVAL if the buffer is too small * than set EINVAL if the buffer is too small
* to fit all the interfaces in. If * to fit all the interfaces in. If
* ifc.lifc_len is too near to the end of the * ifc.ifc_len is too near to the end of the
* buffer we will grow it just in case and * buffer we will grow it just in case and
* retry. * retry.
*/ */
if (iter->ifc.lifc_len + 2 * sizeof(struct lifreq) if (iter->lifc.lifc_len + 2 * sizeof(struct LIFREQ)
< iter->bufsize) < iter->bufsize)
break; break;
} }
...@@ -178,14 +215,70 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { ...@@ -178,14 +215,70 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
"configuration: " "configuration: "
"maximum buffer " "maximum buffer "
"size exceeded")); "size exceeded"));
result = ISC_R_UNEXPECTED; goto unexpected;
goto ioctl_failure;
} }
isc_mem_put(mctx, iter->buf, iter->bufsize); isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
iter->bufsize *= 2; iter->bufsize *= 2;
} }
iter->mode = 6;
return (ISC_R_SUCCESS);
unexpected:
isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
iter->buf = NULL;
return (ISC_R_UNEXPECTED);
#endif
}
isc_result_t
isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
isc_interfaceiter_t *iter;
isc_result_t result;
char strbuf[ISC_STRERRORSIZE];
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;
iter->buf = NULL;
iter->mode = 0;
/*
* Create an unbound datagram socket to do the SIOCGLIFADDR ioctl on.
*/
if ((iter->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERIOCTL,
ISC_MSG_MAKESCANSOCKET,
"making interface "
"scan socket: %s"),
strbuf);
result = ISC_R_UNEXPECTED;
goto socket_failure;
}
/*
* Get the interface configuration, allocating more memory if
* necessary.
*/
result = isc_net_probeipv6();
if (result == ISC_R_SUCCESS)
result = getbuf6(iter);
if (result != ISC_R_SUCCESS)
result = getbuf4(iter);
if (result != ISC_R_SUCCESS)
goto ioctl_failure;
/* /*
* A newly created iterator has an undefined position * A newly created iterator has an undefined position
* until isc_interfaceiter_first() is called. * until isc_interfaceiter_first() is called.
...@@ -198,9 +291,8 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { ...@@ -198,9 +291,8 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
return (ISC_R_SUCCESS); return (ISC_R_SUCCESS);
ioctl_failure: ioctl_failure:
isc_mem_put(mctx, iter->buf, iter->bufsize); if (iter->buf != NULL)
isc_mem_put(mctx, iter->buf, iter->bufsize);
alloc_failure:
(void) close(iter->socket); (void) close(iter->socket);
socket_failure: socket_failure:
...@@ -217,22 +309,149 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { ...@@ -217,22 +309,149 @@ isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
*/ */
static isc_result_t static isc_result_t
internal_current(isc_interfaceiter_t *iter) { internal_current4(isc_interfaceiter_t *iter) {
struct lifreq *ifrp; struct ifreq *ifrp;
struct lifreq lifreq; struct ifreq ifreq;
int family;
char strbuf[ISC_STRERRORSIZE];
REQUIRE(VALID_IFITER(iter));
REQUIRE (iter->pos < (unsigned int) iter->ifc.ifc_len);
ifrp = (struct ifreq *)((char *) iter->ifc.ifc_req + iter->pos);
memset(&ifreq, 0, sizeof ifreq);
memcpy(&ifreq, ifrp, sizeof ifreq);
family = ifreq.ifr_addr.sa_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(ifreq.ifr_name) <= sizeof(iter->current.name));
memset(iter->current.name, 0, sizeof(iter->current.name));
memcpy(iter->current.name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
get_addr(family, &iter->current.address,
(struct sockaddr *)&ifreq.ifr_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, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: getting interface flags: %s",
ifreq.ifr_name, strbuf);
return (ISC_R_IGNORE);
}
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 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, 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__,