diff options
author | tromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-08-06 22:43:05 +0000 |
---|---|---|
committer | tromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-08-06 22:43:05 +0000 |
commit | 07c32f7bfac9c3367e04947f7a6303be1f3c490d (patch) | |
tree | da232522e2e183ad14c50a33158388c5e9df7f06 /libjava/java/net/natVMNetworkInterfacePosix.cc | |
parent | 0a4ac52267882142fcbf8be33620eb088a26691d (diff) | |
download | gcc-07c32f7bfac9c3367e04947f7a6303be1f3c490d.tar.gz |
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.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@115977 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/java/net/natVMNetworkInterfacePosix.cc')
-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 b840907ccab..8238d9c1fa6 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; } |