Commit 1e1804bb authored by Ondřej Surý's avatar Ondřej Surý
Browse files

getifaddrs() is available on all supported Unix platforms (Linux, BSDs, macOS and Solaris)

parent 62fb0759
......@@ -949,7 +949,6 @@ with_gperftools_profiler
enable_backtrace
enable_symtable
enable_tcp_fastopen
enable_getifaddrs
with_readline
enable_isc_spnego
enable_chroot
......@@ -1639,7 +1638,6 @@ Optional Features:
--enable-symtable use internal symbol table for backtrace
[all|minimal(default)|none]
--disable-tcp-fastopen disable TCP Fast Open support [default=autodetect]
--enable-getifaddrs enable the use of getifaddrs() [yes|no].
--disable-isc-spnego use SPNEGO from GSSAPI library
--disable-chroot disable chroot
--disable-linux-caps disable Linux capabilities
......@@ -17962,75 +17960,6 @@ $as_echo "disabled" >&6; }
esac
 
 
# Check whether --enable-getifaddrs was given.
if test "${enable_getifaddrs+set}" = set; then :
enableval=$enable_getifaddrs; want_getifaddrs="$enableval"
else
want_getifaddrs="yes"
fi
#
# This interface iteration code for getifaddrs() will fall back to using
# /proc/net/if_inet6 if getifaddrs() in glibc doesn't return any IPv6
# addresses.
#
case $want_getifaddrs in
glibc)
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: \"--enable-getifaddrs=glibc is no longer required\"" >&5
$as_echo "$as_me: WARNING: \"--enable-getifaddrs=glibc is no longer required\"" >&2;}
ac_fn_c_check_func "$LINENO" "getifaddrs" "ac_cv_func_getifaddrs"
if test "x$ac_cv_func_getifaddrs" = xyes; then :
$as_echo "#define HAVE_GETIFADDRS 1" >>confdefs.h
fi
;;
yes)
ac_fn_c_check_func "$LINENO" "getifaddrs" "ac_cv_func_getifaddrs"
if test "x$ac_cv_func_getifaddrs" = xyes; then :
$as_echo "#define HAVE_GETIFADDRS 1" >>confdefs.h
fi
;;
no)
;;
esac
#
# Look for a sysctl call to get the list of network interfaces.
#
case $ac_cv_header_sys_sysctl_h in
yes)
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for interface list sysctl" >&5
$as_echo_n "checking for interface list sysctl... " >&6; }
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#ifdef NET_RT_IFLIST
found_rt_iflist
#endif
_ACEOF
if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
$EGREP "found_rt_iflist" >/dev/null 2>&1; then :
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; }
$as_echo "#define HAVE_IFLIST_SYSCTL 1" >>confdefs.h
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
fi
rm -f conftest*
;;
esac
#
# Check for some other useful functions that are not ever-present.
#
......
......@@ -2011,48 +2011,6 @@ int has_tfo() { return (0); }
esac
AC_SUBST(ISC_PLATFORM_HAVETFO)
AC_ARG_ENABLE(getifaddrs,
AS_HELP_STRING([--enable-getifaddrs],
[enable the use of getifaddrs() [yes|no].]),
want_getifaddrs="$enableval", want_getifaddrs="yes")
#
# This interface iteration code for getifaddrs() will fall back to using
# /proc/net/if_inet6 if getifaddrs() in glibc doesn't return any IPv6
# addresses.
#
case $want_getifaddrs in
glibc)
AC_MSG_WARN("--enable-getifaddrs=glibc is no longer required")
AC_CHECK_FUNC(getifaddrs, AC_DEFINE(HAVE_GETIFADDRS))
;;
yes)
AC_CHECK_FUNC(getifaddrs, AC_DEFINE(HAVE_GETIFADDRS))
;;
no)
;;
esac
#
# Look for a sysctl call to get the list of network interfaces.
#
case $ac_cv_header_sys_sysctl_h in
yes)
AC_MSG_CHECKING(for interface list sysctl)
AC_EGREP_CPP(found_rt_iflist, [
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#ifdef NET_RT_IFLIST
found_rt_iflist
#endif
],
[AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_IFLIST_SYSCTL)],
[AC_MSG_RESULT(no)])
;;
esac
#
# Check for some other useful functions that are not ever-present.
#
......
This diff is collapsed.
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#include <config.h>
/*! \file
* \brief
* Obtain the list of network interfaces using sysctl.
* See TCP/IP Illustrated Volume 2, sections 19.8, 19.14,
* and 19.16.
*/
#include <inttypes.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <net/route.h>
#include <net/if_dl.h>
#include <isc/print.h>
/* XXX what about Alpha? */
#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
: sizeof(long))
#define IFITER_MAGIC ISC_MAGIC('I', 'F', 'I', 'S')
#define VALID_IFITER(t) ISC_MAGIC_VALID(t, IFITER_MAGIC)
struct isc_interfaceiter {
unsigned int magic; /* Magic number. */
isc_mem_t *mctx;
void *buf; /* Buffer for sysctl data. */
unsigned int bufsize; /* Bytes allocated. */
unsigned int bufused; /* Bytes used. */
unsigned int pos; /* Current offset in
sysctl data. */
isc_interface_t current; /* Current interface data. */
isc_result_t result; /* Last result code. */
};
static int mib[6] = {
CTL_NET,
PF_ROUTE,
0,
0, /* Any address family. */
NET_RT_IFLIST,
0 /* Flags. */
};
isc_result_t
isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
isc_interfaceiter_t *iter;
isc_result_t result;
size_t bufsize;
size_t bufused;
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 = 0;
/*
* Determine the amount of memory needed.
*/
bufsize = 0;
if (sysctl(mib, 6, NULL, &bufsize, NULL, (size_t) 0) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERSYSCTL,
ISC_MSG_GETIFLISTSIZE,
"getting interface "
"list size: sysctl: %s"),
strbuf);
result = ISC_R_UNEXPECTED;
goto failure;
}
iter->bufsize = bufsize;
iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
if (iter->buf == NULL) {
result = ISC_R_NOMEMORY;
goto failure;
}
bufused = bufsize;
if (sysctl(mib, 6, iter->buf, &bufused, NULL, (size_t) 0) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_IFITERSYSCTL,
ISC_MSG_GETIFLIST,
"getting interface list: "
"sysctl: %s"),
strbuf);
result = ISC_R_UNEXPECTED;
goto failure;
}
iter->bufused = bufused;
INSIST(iter->bufused <= iter->bufsize);
/*
* 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);
failure:
if (iter->buf != NULL)
isc_mem_put(mctx, iter->buf, iter->bufsize);
isc_mem_put(mctx, iter, sizeof(*iter));
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,
* return ISC_R_IGNORE. In case of other failure,
* return ISC_R_UNEXPECTED.
*/
static isc_result_t
internal_current(isc_interfaceiter_t *iter) {
struct ifa_msghdr *ifam, *ifam_end;
REQUIRE(VALID_IFITER(iter));
REQUIRE (iter->pos < (unsigned int) iter->bufused);
ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
ifam_end = (struct ifa_msghdr *) ((char *) iter->buf + iter->bufused);
if (ifam->ifam_type == RTM_IFINFO) {
struct if_msghdr *ifm = (struct if_msghdr *) ifam;
struct sockaddr_dl *sdl = (struct sockaddr_dl *) (ifm + 1);
unsigned int namelen;
memset(&iter->current, 0, sizeof(iter->current));
namelen = sdl->sdl_nlen;
if (namelen > sizeof(iter->current.name) - 1)
namelen = sizeof(iter->current.name) - 1;
memset(iter->current.name, 0, sizeof(iter->current.name));
memmove(iter->current.name, sdl->sdl_data, namelen);
iter->current.flags = 0;
if ((ifam->ifam_flags & IFF_UP) != 0)
iter->current.flags |= INTERFACE_F_UP;
if ((ifam->ifam_flags & IFF_POINTOPOINT) != 0)
iter->current.flags |= INTERFACE_F_POINTTOPOINT;
if ((ifam->ifam_flags & IFF_LOOPBACK) != 0)
iter->current.flags |= INTERFACE_F_LOOPBACK;
/*
* This is not an interface address.
* Force another iteration.
*/
return (ISC_R_IGNORE);
} else if (ifam->ifam_type == RTM_NEWADDR) {
int i;
int family;
struct sockaddr *mask_sa = NULL;
struct sockaddr *addr_sa = NULL;
struct sockaddr *dst_sa = NULL;
struct sockaddr *sa = (struct sockaddr *)(ifam + 1);
family = sa->sa_family;
for (i = 0; i < RTAX_MAX; i++)
{
if ((ifam->ifam_addrs & (1 << i)) == 0)
continue;
INSIST(sa < (struct sockaddr *) ifam_end);
switch (i) {
case RTAX_NETMASK: /* Netmask */
mask_sa = sa;
break;
case RTAX_IFA: /* Interface address */
addr_sa = sa;
break;
case RTAX_BRD: /* Broadcast or destination address */
dst_sa = sa;
break;
}
sa = (struct sockaddr *)((char*)(sa)
+ ROUNDUP(sizeof(struct sockaddr)));
}
if (addr_sa == NULL)
return (ISC_R_IGNORE);
family = addr_sa->sa_family;
if (family != AF_INET && family != AF_INET6)
return (ISC_R_IGNORE);
iter->current.af = family;
get_addr(family, &iter->current.address, addr_sa,
iter->current.name);
if (mask_sa != NULL)
get_addr(family, &iter->current.netmask, mask_sa,
iter->current.name);
if (dst_sa != NULL &&
(iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
get_addr(family, &iter->current.dstaddress, dst_sa,
iter->current.name);
return (ISC_R_SUCCESS);
} else {
printf("%s",
isc_msgcat_get(isc_msgcat, ISC_MSGSET_IFITERSYSCTL,
ISC_MSG_UNEXPECTEDTYPE,
"warning: unexpected interface list "
"message type\n"));
return (ISC_R_IGNORE);
}
}
/*
* 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_next(isc_interfaceiter_t *iter) {
struct ifa_msghdr *ifam;
REQUIRE (iter->pos < (unsigned int) iter->bufused);
ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
iter->pos += ifam->ifam_msglen;
if (iter->pos >= iter->bufused)
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
}
static void
internal_destroy(isc_interfaceiter_t *iter) {
UNUSED(iter); /* Unused. */
/*
* Do nothing.
*/
}
static
void internal_first(isc_interfaceiter_t *iter) {
iter->pos = 0;
}
......@@ -144,13 +144,7 @@ static isc_result_t linux_if_inet6_current(isc_interfaceiter_t *);
static void linux_if_inet6_first(isc_interfaceiter_t *iter);
#endif
#if HAVE_GETIFADDRS
#include "ifiter_getifaddrs.c"
#elif HAVE_IFLIST_SYSCTL
#include "ifiter_sysctl.c"
#else
#include "ifiter_ioctl.c"
#endif
#ifdef __linux
static void
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment