From c64c571591170478eead416216523c21e29a19ec Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Tue, 11 Dec 2007 22:23:31 +0100 Subject: r26405: Import support for getifaddrs from Samba3. (This used to be commit 5827b7460a058e8565a406f1c3533e5f589674b5) --- source4/lib/socket/config.m4 | 45 +++++++++++++++++++++++++++++++ source4/lib/socket/netif.c | 63 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 1 deletion(-) (limited to 'source4') diff --git a/source4/lib/socket/config.m4 b/source4/lib/socket/config.m4 index 3296730c82..f8a4915adb 100644 --- a/source4/lib/socket/config.m4 +++ b/source4/lib/socket/config.m4 @@ -96,6 +96,29 @@ fi dnl don't build ipv6 by default, unless the above test enables it, or dnl the configure uses --with-static-modules=socket_ipv6 +AC_CHECK_HEADERS([ifaddrs.h]) + +dnl test for getifaddrs and freeifaddrs +AC_CACHE_CHECK([for getifaddrs and freeifaddrs],samba_cv_HAVE_GETIFADDRS,[ +AC_TRY_COMPILE([ +#include +#include +#include +#include +#include +#include ], +[ +struct ifaddrs *ifp = NULL; +int ret = getifaddrs (&ifp); +freeifaddrs(ifp); +], +samba_cv_HAVE_GETIFADDRS=yes,samba_cv_HAVE_GETIFADDRS=no)]) +if test x"$samba_cv_HAVE_GETIFADDRS" = x"yes"; then + AC_DEFINE(HAVE_GETIFADDRS,1,[Whether the system has getifaddrs]) + AC_DEFINE(HAVE_FREEIFADDRS,1,[Whether the system has freeifaddrs]) +fi + + ################## # look for a method of finding the list of network interfaces @@ -107,6 +130,26 @@ old_LIBS=$LIBS LIBS="$NSL_LIBS $SOCKET_LIBS" CFLAGS="$CFLAGS -Ilib/replace" iface=no; +################## +# look for a method of finding the list of network interfaces +iface=no; +AC_CACHE_CHECK([for iface getifaddrs],samba_cv_HAVE_IFACE_GETIFADDRS,[ +SAVE_CPPFLAGS="$CPPFLAGS" +CPPFLAGS="$CPPFLAGS ${SAMBA_CONFIGURE_CPPFLAGS}" +AC_TRY_RUN([ +#define NO_CONFIG_H 1 +#define HAVE_IFACE_GETIFADDRS 1 +#define AUTOCONF_TEST 1 +#include "${srcdir-.}/lib/replace/replace.c" +#include "${srcdir-.}/lib/socket/netif.c"], + samba_cv_HAVE_IFACE_GETIFADDRS=yes,samba_cv_HAVE_IFACE_GETIFADDRS=no,samba_cv_HAVE_IFACE_GETIFADDRS=cross)]) +CPPFLAGS="$SAVE_CPPFLAGS" +if test x"$samba_cv_HAVE_IFACE_GETIFADDRS" = x"yes"; then + iface=yes;AC_DEFINE(HAVE_IFACE_GETIFADDRS,1,[Whether iface getifaddrs is available]) +fi + + +if test $iface = no; then AC_CACHE_CHECK([for iface AIX],samba_cv_HAVE_IFACE_AIX,[ AC_TRY_RUN([ #define HAVE_IFACE_AIX 1 @@ -117,6 +160,8 @@ AC_TRY_RUN([ if test x"$samba_cv_HAVE_IFACE_AIX" = x"yes"; then iface=yes;AC_DEFINE(HAVE_IFACE_AIX,1,[Whether iface AIX is available]) fi +fi + if test $iface = no; then AC_CACHE_CHECK([for iface ifconf],samba_cv_HAVE_IFACE_IFCONF,[ diff --git a/source4/lib/socket/netif.c b/source4/lib/socket/netif.c index d85bf4c716..cd9ea6c977 100644 --- a/source4/lib/socket/netif.c +++ b/source4/lib/socket/netif.c @@ -2,6 +2,8 @@ Unix SMB/CIFS implementation. return a list of network interfaces Copyright (C) Andrew Tridgell 1998 + Copyright (C) Jeremy Allison 2007 + Copyright (C) Jelmer Vernooij 2007 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -40,7 +42,6 @@ #include #include #include -#include #include #include #include @@ -76,12 +77,72 @@ #define QSORT_CAST (int (*)(const void *, const void *)) #endif +#ifdef HAVE_IFADDRS_H +#include +#endif + #ifdef HAVE_NET_IF_H #include #endif #include "netif.h" +/**************************************************************************** + Try the "standard" getifaddrs/freeifaddrs interfaces. + Also gets IPv6 interfaces. +****************************************************************************/ + +#if HAVE_IFACE_GETIFADDRS +/**************************************************************************** + Get the netmask address for a local interface. +****************************************************************************/ + +static int _get_interfaces(struct iface_struct *ifaces, int max_interfaces) +{ + struct ifaddrs *iflist = NULL; + struct ifaddrs *ifptr = NULL; + int total = 0; + + if (getifaddrs(&iflist) < 0) { + return -1; + } + + /* Loop through interfaces, looking for given IP address */ + for (ifptr = iflist, total = 0; + ifptr != NULL && total < max_interfaces; + ifptr = ifptr->ifa_next) { + + memset(&ifaces[total], '\0', sizeof(ifaces[total])); + + if (!ifptr->ifa_addr || !ifptr->ifa_netmask) { + continue; + } + + /* Check the interface is up. */ + if (!(ifptr->ifa_flags & IFF_UP)) { + continue; + } + + /* We don't support IPv6 *yet* */ + if (ifptr->ifa_addr->sa_family != AF_INET) { + continue; + } + + ifaces[total].ip = ((struct sockaddr_in *)ifptr->ifa_addr)->sin_addr; + ifaces[total].netmask = ((struct sockaddr_in *)ifptr->ifa_netmask)->sin_addr; + + strlcpy(ifaces[total].name, ifptr->ifa_name, + sizeof(ifaces[total].name)); + total++; + } + + freeifaddrs(iflist); + + return total; +} + +#define _FOUND_IFACE_ANY +#endif /* HAVE_IFACE_GETIFADDRS */ #if HAVE_IFACE_IFCONF /* this works for Linux 2.2, Solaris 2.5, SunOS4, HPUX 10.20, OSF1 -- cgit