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
This commit is contained in:
Michael Koch 2002-09-17 06:58:40 +00:00 committed by Michael Koch
parent cb38fd88af
commit 26ad77f173
7 changed files with 716 additions and 338 deletions

View File

@ -1,3 +1,14 @@
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.
2002-09-16 Tom Tromey <tromey@redhat.com>
* java/net/URLClassLoader.java (findClass): Code source for a

View File

@ -1894,6 +1894,7 @@ java/net/JarURLConnection.java \
java/net/MalformedURLException.java \
java/net/MulticastSocket.java \
java/net/NetPermission.java \
java/net/NetworkInterface.java \
java/net/NoRouteToHostException.java \
java/net/PasswordAuthentication.java \
java/net/PlainDatagramSocketImpl.java \
@ -2236,6 +2237,7 @@ java/lang/reflect/natArray.cc \
java/lang/reflect/natConstructor.cc \
java/lang/reflect/natField.cc \
java/lang/reflect/natMethod.cc \
java/net/natNetworkInterface.cc \
java/net/natInetAddress.cc \
java/net/natPlainDatagramSocketImpl.cc \
java/net/natPlainSocketImpl.cc \

View File

@ -1656,6 +1656,7 @@ java/net/JarURLConnection.java \
java/net/MalformedURLException.java \
java/net/MulticastSocket.java \
java/net/NetPermission.java \
java/net/NetworkInterface.java \
java/net/NoRouteToHostException.java \
java/net/PasswordAuthentication.java \
java/net/PlainDatagramSocketImpl.java \
@ -1997,6 +1998,7 @@ java/lang/reflect/natArray.cc \
java/lang/reflect/natConstructor.cc \
java/lang/reflect/natField.cc \
java/lang/reflect/natMethod.cc \
java/net/natNetworkInterface.cc \
java/net/natInetAddress.cc \
java/net/natPlainDatagramSocketImpl.cc \
java/net/natPlainSocketImpl.cc \
@ -2150,10 +2152,11 @@ java/lang/natStringBuffer.lo java/lang/natSystem.lo \
java/lang/natThread.lo java/lang/natVMThrowable.lo \
java/lang/ref/natReference.lo java/lang/reflect/natArray.lo \
java/lang/reflect/natConstructor.lo java/lang/reflect/natField.lo \
java/lang/reflect/natMethod.lo java/net/natInetAddress.lo \
java/net/natPlainDatagramSocketImpl.lo java/net/natPlainSocketImpl.lo \
java/text/natCollator.lo java/util/natTimeZone.lo \
java/util/zip/natDeflater.lo java/util/zip/natInflater.lo
java/lang/reflect/natMethod.lo java/net/natNetworkInterface.lo \
java/net/natInetAddress.lo java/net/natPlainDatagramSocketImpl.lo \
java/net/natPlainSocketImpl.lo java/text/natCollator.lo \
java/util/natTimeZone.lo java/util/zip/natDeflater.lo \
java/util/zip/natInflater.lo
libgcjx_la_OBJECTS = gnu/gcj/xlib/natClip.lo \
gnu/gcj/xlib/natColormap.lo gnu/gcj/xlib/natDisplay.lo \
gnu/gcj/xlib/natDrawable.lo gnu/gcj/xlib/natFont.lo \
@ -2873,7 +2876,8 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_DIR)/gen-from-JIS.P \
.deps/java/net/HttpURLConnection.P .deps/java/net/InetAddress.P \
.deps/java/net/InetSocketAddress.P .deps/java/net/JarURLConnection.P \
.deps/java/net/MalformedURLException.P .deps/java/net/MulticastSocket.P \
.deps/java/net/NetPermission.P .deps/java/net/NoRouteToHostException.P \
.deps/java/net/NetPermission.P .deps/java/net/NetworkInterface.P \
.deps/java/net/NoRouteToHostException.P \
.deps/java/net/PasswordAuthentication.P \
.deps/java/net/PlainDatagramSocketImpl.P \
.deps/java/net/PlainSocketImpl.P \
@ -2891,7 +2895,7 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_DIR)/gen-from-JIS.P \
.deps/java/net/URLStreamHandlerFactory.P \
.deps/java/net/UnknownHostException.P \
.deps/java/net/UnknownServiceException.P \
.deps/java/net/natInetAddress.P \
.deps/java/net/natInetAddress.P .deps/java/net/natNetworkInterface.P \
.deps/java/net/natPlainDatagramSocketImpl.P \
.deps/java/net/natPlainSocketImpl.P \
.deps/java/nio/channels/DatagramChannel.P \

651
libjava/configure vendored

File diff suppressed because it is too large Load Diff

View File

@ -813,7 +813,7 @@ CFLAGS="$save_CFLAGS"
dnl We check for sys/filio.h because Solaris 2.5 defines FIONREAD there.
dnl On that system, sys/ioctl.h will not include sys/filio.h unless
dnl BSD_COMP is defined; just including sys/filio.h is simpler.
AC_CHECK_HEADERS(unistd.h bstring.h sys/time.h sys/types.h fcntl.h sys/ioctl.h sys/filio.h sys/stat.h sys/select.h sys/socket.h netinet/in.h arpa/inet.h netdb.h pwd.h sys/config.h stdint.h langinfo.h locale.h)
AC_CHECK_HEADERS(unistd.h bstring.h sys/time.h sys/types.h fcntl.h 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)
dnl We avoid AC_HEADER_DIRENT since we really only care about dirent.h
dnl for now. If you change this, you also must update natFile.cc.
AC_CHECK_HEADERS(dirent.h)

View File

@ -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

View File

@ -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 //