diff options
author | Michael Koch <konqueror@gmx.de> | 2002-09-17 06:58:40 +0000 |
---|---|---|
committer | Michael Koch <mkoch@gcc.gnu.org> | 2002-09-17 06:58:40 +0000 |
commit | 26ad77f1732451be616ebe941ddda6a2ac882039 (patch) | |
tree | 13d5d6ac2b8083173a97a7a24982d5eef4998a53 /libjava/java/net | |
parent | cb38fd88af859b9c125ba6d4768969289ea0ebd9 (diff) | |
download | gcc-26ad77f1732451be616ebe941ddda6a2ac882039.zip gcc-26ad77f1732451be616ebe941ddda6a2ac882039.tar.gz gcc-26ad77f1732451be616ebe941ddda6a2ac882039.tar.bz2 |
NetworkInterface.java: New file.
2002-09-17 Michael Koch <konqueror@gmx.de>
* java/net/NetworkInterface.java: New file.
* java/net/natNetworkInterface.java: New file.
* configure.in: Added check for net/if.h.
* configure: Regenerated.
* Makefile.am
(ordinary_java_source_files): Added NetworkInterface.java.
(nat_source_files): Added natNetworkInterface.cc.
* Makefile.in: Regenerated.
From-SVN: r57234
Diffstat (limited to 'libjava/java/net')
-rw-r--r-- | libjava/java/net/NetworkInterface.java | 228 | ||||
-rw-r--r-- | libjava/java/net/natNetworkInterface.cc | 144 |
2 files changed, 372 insertions, 0 deletions
diff --git a/libjava/java/net/NetworkInterface.java b/libjava/java/net/NetworkInterface.java new file mode 100644 index 0000000..d42e69d --- /dev/null +++ b/libjava/java/net/NetworkInterface.java @@ -0,0 +1,228 @@ +/* NetworkInterface.java + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package java.net; + +import java.util.Enumeration; +import java.util.Vector; + +/** + * @author Michael Koch <konqueror@gmx.de> + * @since 1.4 + */ +public final class NetworkInterface +{ + private static Vector networkInterfaces; + + private String name; + + private Vector inetAddresses; + + private NetworkInterface (String name, InetAddress address) + { + this.name = name; + this.inetAddresses = new Vector (1, 1); + this.inetAddresses.add (address); + } + + private native static Vector getRealNetworkInterfaces () + throws SocketException; + + /** + * Returns the name of the network interface + */ + public String getName () + { + return name; + } + + /** + * Returns all available addresses of the network interface + * + * If a @see SecurityManager is available all addresses are checked + * with @see SecurityManager::checkConnect() if they are available. + * Only InetAddresses are returned where the security manager doesn't + * thrown an exception. + * + * @return An enumeration of all addresses. + */ + public Enumeration getInetAddresses () + { + SecurityManager s = System.getSecurityManager (); + + if (s == null) + return inetAddresses.elements (); + + Vector tmpInetAddresses = new Vector (1, 1); + + for (Enumeration addresses = inetAddresses.elements (); + addresses.hasMoreElements (); ) + { + InetAddress addr = (InetAddress) addresses.nextElement (); + try + { + s.checkConnect (addr.getHostAddress (), 58000); + tmpInetAddresses.add (addr); + } + catch (SecurityException e) + { + } + } + + return tmpInetAddresses.elements (); + } + + /** + * Returns the display name of the interface + */ + public String getDisplayName () + { + return name; + } + + /** + * Returns an network interface by name + * + * @param name The name of the interface to return + */ + public static NetworkInterface getByName (String name) + throws SocketException + { + if (networkInterfaces == null) + networkInterfaces = getRealNetworkInterfaces (); + + for (Enumeration e = networkInterfaces.elements (); + e.hasMoreElements (); ) + { + NetworkInterface tmp = (NetworkInterface) e.nextElement (); + + if (name.equals (tmp.getName ())) + return tmp; + } + + throw new SocketException ("no network interface with this name exists"); + } + + /** + * Return a network interface by its address + * + * @param addr The address of the interface to return + */ + public static NetworkInterface getByInetAddress (InetAddress addr) + throws SocketException + { + if (networkInterfaces == null) + networkInterfaces = getRealNetworkInterfaces (); + + for (Enumeration interfaces = networkInterfaces.elements (); + interfaces.hasMoreElements (); ) + { + NetworkInterface tmp = (NetworkInterface) interfaces.nextElement (); + + for (Enumeration addresses = tmp.inetAddresses.elements (); + addresses.hasMoreElements (); ) + { + if (addr.equals ((InetAddress) addresses.nextElement ())) + return tmp; + } + } + + throw new SocketException ( + "no network interface is bound to such an IP address"); + } + + /** + * Return an Enumeration of all available network interfaces + */ + public static Enumeration getNetworkInterfaces () + throws SocketException + { + if (networkInterfaces == null) + networkInterfaces = getRealNetworkInterfaces (); + + Enumeration tmp = networkInterfaces.elements (); + if (tmp.hasMoreElements ()) + return tmp; + + return null; + } + + /** + * Checks if the current instance is equal to obj + * + * @param obj The object to compare with + */ + public boolean equals (Object obj) + { + if (!(obj instanceof NetworkInterface)) + return false; + + NetworkInterface tmp = (NetworkInterface) obj; + return name.equals (tmp.name) && + inetAddresses.equals (tmp.inetAddresses); + } + + /** + * Returns the hashcode of the current instance + */ + public int hashCode () + { + // FIXME: hash correctly + return name.hashCode () + inetAddresses.hashCode (); + } + + /** + * Returns a string representation of the interface + */ + public String toString () + { + // FIXME: check if this is correct + String result; + + result = "name: " + getDisplayName () + " (" + getName () + + ") addresses:\n"; + + for (Enumeration e = inetAddresses.elements (); + e.hasMoreElements (); ) + { + InetAddress address = (InetAddress) e.nextElement (); + result += address.toString () + "\n"; + } + + return result; + } +} // class NetworkInterface diff --git a/libjava/java/net/natNetworkInterface.cc b/libjava/java/net/natNetworkInterface.cc new file mode 100644 index 0000000..9273592 --- /dev/null +++ b/libjava/java/net/natNetworkInterface.cc @@ -0,0 +1,144 @@ +// natNetworkInterface.cc + +/* Copyright (C) 2002 Free Software Foundation + + This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +#include <config.h> + +#ifdef WIN32 + +#include <windows.h> +#include <winsock.h> +#undef STRICT + +#else /* WIN32 */ + +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif +#include <string.h> +#include <errno.h> +#include <stdlib.h> + +#include <sys/param.h> +#include <sys/types.h> +#ifdef HAVE_SYS_SOCKET_H +#include <sys/socket.h> +#endif +#ifdef HAVE_NETINET_IN_H +#include <netinet/in.h> +#endif +#ifdef HAVE_ARPA_INET_H +#include <arpa/inet.h> +#endif +#ifdef HAVE_NETDB_H +#include <netdb.h> +#endif +#ifdef HAVE_SYS_IOCTL_H +#define BSD_COMP /* Get FIONREAD on Solaris2. */ +#include <sys/ioctl.h> +#endif +#ifdef HAVE_NET_IF_H +#include <net/if.h> +#endif + +#endif /* WIN32 */ + +#include <gcj/cni.h> +#include <jvm.h> +#include <java/net/NetworkInterface.h> +#include <java/net/Inet4Address.h> +#include <java/net/Inet6Address.h> +#include <java/net/SocketException.h> +#include <java/util/Vector.h> + +#ifdef DISABLE_JAVA_NET + +::java::util::Vector* +java::net::NetworkInterface::getRealNetworkInterfaces () +{ + ::java::util::Vector* ht = new ::java::util::Vector(); + return ht; +} + +#else /* DISABLE_JAVA_NET */ + +::java::util::Vector* +java::net::NetworkInterface::getRealNetworkInterfaces () +{ +#if 0 + 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; + + // Open a (random) socket to have a file descriptor for the ioctl calls. + fd = ::socket (PF_INET, SOCK_DGRAM, htons (IPPROTO_IP)); + + if (fd < 0) + throw new ::java::net::SocketException; + + // Get all interfaces. If not enough buffers are available try it + // with a bigger buffer size. + do + { + num_interfaces += 16; + + if_data.ifc_len = sizeof (struct ifreq) * num_interfaces; + if_data.ifc_buf = + (char*) _Jv_Realloc (if_data.ifc_buf, if_data.ifc_len); + + // Try to get all local interfaces. + if (::ioctl (fd, SIOCGIFCONF, &if_data) < 0) + throw new java::net::SocketException; + } + while (if_data.ifc_len >= (sizeof (struct ifreq) * num_interfaces)); + + // Get addresses of all interfaces. + if_record = if_data.ifc_req; + for (int n = 0; n < if_data.ifc_len; n += sizeof (struct ifreq)) + { + struct ifreq ifr; + + memset (&ifr, 0, sizeof (ifr)); + strcpy (ifr.ifr_name, if_record->ifr_name); + + // Try to get the IPv4-address of the local interface + if (::ioctl (fd, SIOCGIFADDR, &ifr) < 0) + throw new java::net::SocketException; + + int len = 4; + struct sockaddr_in sa = *((sockaddr_in*) &(ifr.ifr_addr)); + + jbyteArray baddr = JvNewByteArray (len); + memcpy (elements (baddr), &(sa.sin_addr), len); + jstring if_name = JvNewStringLatin1 (if_record->ifr_name); + Inet4Address* address = + new java::net::Inet4Address (baddr, JvNewStringLatin1 ("")); + ht->add (new NetworkInterface (if_name, address)); + if_record++; + } + +#ifdef HAVE_INET6 + // FIXME: read /proc/net/if_inet6 +#endif + + _Jv_Free (if_data.ifc_buf); + + if (fd >= 0) + ::close (fd); + + return ht; +#endif +} + +#endif // DISABLE_JAVA_NET // |