diff options
author | Tom Tromey <tromey@redhat.com> | 2006-08-06 22:43:05 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 2006-08-06 22:43:05 +0000 |
commit | 9327c475dff65f2d2f906c098943910397aaf7e4 (patch) | |
tree | da232522e2e183ad14c50a33158388c5e9df7f06 /libjava/java | |
parent | bc19f9eedaf172fce1117c0e5baee2d8f72e04d6 (diff) | |
download | gcc-9327c475dff65f2d2f906c098943910397aaf7e4.zip gcc-9327c475dff65f2d2f906c098943910397aaf7e4.tar.gz gcc-9327c475dff65f2d2f906c098943910397aaf7e4.tar.bz2 |
re PR libgcj/28491 (NetworkInterface.getNetworkInterfaces() doesn't report ipv6 interfaces.)
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.
From-SVN: r115977
Diffstat (limited to 'libjava/java')
-rw-r--r-- | libjava/java/net/natVMNetworkInterfacePosix.cc | 62 |
1 files changed, 55 insertions, 7 deletions
diff --git a/libjava/java/net/natVMNetworkInterfacePosix.cc b/libjava/java/net/natVMNetworkInterfacePosix.cc index b840907..8238d9c 100644 --- a/libjava/java/net/natVMNetworkInterfacePosix.cc +++ b/libjava/java/net/natVMNetworkInterfacePosix.cc @@ -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 @@ details. */ #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 @@ details. */ ::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 @@ java::net::VMNetworkInterface::getInterfaces () 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; } |