summaryrefslogtreecommitdiff
path: root/source4/lib
diff options
context:
space:
mode:
authorJelmer Vernooij <jelmer@samba.org>2007-12-11 22:23:31 +0100
committerStefan Metzmacher <metze@samba.org>2007-12-21 05:49:37 +0100
commitc64c571591170478eead416216523c21e29a19ec (patch)
treed8c39008d3500ff80c7a634bf75841a0a13cb95b /source4/lib
parent0b8ec6de02002e013775a1ba2d012a93e0950617 (diff)
downloadsamba-c64c571591170478eead416216523c21e29a19ec.tar.gz
samba-c64c571591170478eead416216523c21e29a19ec.tar.bz2
samba-c64c571591170478eead416216523c21e29a19ec.zip
r26405: Import support for getifaddrs from Samba3.
(This used to be commit 5827b7460a058e8565a406f1c3533e5f589674b5)
Diffstat (limited to 'source4/lib')
-rw-r--r--source4/lib/socket/config.m445
-rw-r--r--source4/lib/socket/netif.c63
2 files changed, 107 insertions, 1 deletions
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 <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <ifaddrs.h>
+#include <netdb.h>],
+[
+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 <netdb.h>
#include <sys/ioctl.h>
#include <netdb.h>
-#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -76,12 +77,72 @@
#define QSORT_CAST (int (*)(const void *, const void *))
#endif
+#ifdef HAVE_IFADDRS_H
+#include <ifaddrs.h>
+#endif
+
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#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