This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch: FYI: PR 28491


I'm checking this in.

This fixes PR 28491.  With this patch, if getifaddrs is available, we
use it in preference to the old SIOCGIFCONF ioctl to find the
machine's interfaces.  On other platforms we continue to do what we
did before.

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>
	PR libgcj/28491:
	* configure, include/config.h.in: Rebuilt.
	* configure.ac: Look for ifaddrs.h, getifaddrs.
	* java/net/natVMNetworkInterfacePosix.cc (getInterfaces): Use
	getifaddrs if available.

Index: configure.ac
===================================================================
--- configure.ac	(revision 115941)
+++ configure.ac	(working copy)
@@ -913,7 +913,8 @@
 		   access stat lstat mkdir rename rmdir unlink utime chmod readlink \
 		   nl_langinfo setlocale \
 		   inet_pton uname inet_ntoa \
-		   fork execvp pipe sigaction ftruncate mmap])
+		   fork execvp pipe sigaction ftruncate mmap \
+		   getifaddrs])
    AC_CHECK_FUNCS(inet_aton inet_addr, break)
    AC_CHECK_HEADERS(execinfo.h unistd.h dlfcn.h)
    # Do an additional check on dld, HP-UX for example has dladdr in libdld.sl
@@ -1301,7 +1302,7 @@
 		  sys/ioctl.h sys/filio.h sys/stat.h sys/select.h \
 		  sys/socket.h netinet/in.h arpa/inet.h netdb.h net/if.h \
 		  pwd.h sys/config.h stdint.h langinfo.h locale.h \
-		  dirent.h sys/rw_lock.h])
+		  dirent.h sys/rw_lock.h ifaddrs.h])
 AC_CHECK_HEADERS(inttypes.h, [
     AC_DEFINE(HAVE_INTTYPES_H, 1, [Define if <inttypes.h> is available])
     AC_DEFINE(JV_HAVE_INTTYPES_H, 1, [Define if <inttypes.h> is available])
Index: java/net/natVMNetworkInterfacePosix.cc
===================================================================
--- java/net/natVMNetworkInterfacePosix.cc	(revision 115941)
+++ java/net/natVMNetworkInterfacePosix.cc	(working copy)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2003, 2005  Free Software Foundation
+/* Copyright (C) 2003, 2005, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -34,6 +34,9 @@
 #ifdef HAVE_NET_IF_H
 #include <net/if.h>
 #endif
+#ifdef HAVE_IFADDRS_H
+#include <ifaddrs.h>
+#endif
 
 #include <gcj/cni.h>
 #include <jvm.h>
@@ -46,11 +49,59 @@
 ::java::util::Vector*
 java::net::VMNetworkInterface::getInterfaces ()
 {
+  ::java::util::Vector* ht = new ::java::util::Vector ();
+
+#ifdef HAVE_GETIFADDRS
+
+  struct ifaddrs *addrs;
+  if (::getifaddrs (&addrs) == -1)
+    throw new ::java::net::SocketException(JvNewStringUTF (strerror (errno)));
+
+  for (struct ifaddrs *work = addrs; work != NULL; work = work->ifa_next)
+    {
+      // Sometimes the address can be NULL; I don't know why but
+      // there's nothing we can do with this.
+      if (! work->ifa_addr)
+	continue;
+      // We only return Inet4 or Inet6 addresses.
+      jbyteArray laddr;
+      if (work->ifa_addr->sa_family == AF_INET)
+	{
+	  sockaddr_in *real = reinterpret_cast<sockaddr_in *> (work->ifa_addr);
+	  laddr = JvNewByteArray(4);
+	  memcpy (elements (laddr), &real->sin_addr, 4);
+	}
+#ifdef HAVE_INET6
+      else if (work->ifa_addr->sa_family == AF_INET6)
+	{
+	  sockaddr_in6 *real
+	    = reinterpret_cast<sockaddr_in6 *> (work->ifa_addr);
+	  laddr = JvNewByteArray(16);
+	  memcpy (elements (laddr), &real->sin6_addr, 16);
+	}
+#endif
+      else
+	continue;
+
+      ::java::net::InetAddress *inaddr
+	  =  ::java::net::InetAddress::getByAddress(laddr);
+
+      // It is ok to make a new NetworkInterface for each struct; the
+      // java code will unify these as necessary; see
+      // NetworkInterface.condense().
+      jstring name = JvNewStringUTF (work->ifa_name);
+
+      ht->add (new NetworkInterface (name, inaddr));
+    }
+
+  freeifaddrs (addrs);
+
+#else /* ! HAVE_GETIFADDRS */
+
   int fd;
   int num_interfaces = 0;
   struct ifconf if_data;
   struct ifreq* if_record;
-  ::java::util::Vector* ht = new ::java::util::Vector ();
 
   if_data.ifc_len = 0;
   if_data.ifc_buf = NULL;
@@ -103,14 +154,11 @@
       if_record++;
     }
 
-#ifdef HAVE_INET6
-      // FIXME: read /proc/net/if_inet6 (on Linux 2.4)
-#endif
-
   _Jv_Free (if_data.ifc_buf);
   
   if (fd >= 0)
     _Jv_close (fd);
-  
+#endif /* HAVE_GETIFADDRS */ 
+
   return ht;
 }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]