aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/net
diff options
context:
space:
mode:
author(no author) <(no author)@138bc75d-0d04-0410-961f-82ee72b054a4>2003-09-22 21:39:00 +0000
committer(no author) <(no author)@138bc75d-0d04-0410-961f-82ee72b054a4>2003-09-22 21:39:00 +0000
commit2e9c0dc38e2692b04281844366dbb367ae1294e1 (patch)
treee56f44693864144d87710daeab9743d4e76b6037 /libjava/java/net
parent780b7d9b87ddf306aa31c750d5eb9d96a9cb69ea (diff)
This commit was manufactured by cvs2svn to create tagobjc-improvements-candidate-20030922
'objc-improvements-candidate-20030922'. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/tags/objc-improvements-candidate-20030922@71666 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/java/net')
-rw-r--r--libjava/java/net/DatagramSocket.java56
-rw-r--r--libjava/java/net/InetAddress.java140
-rw-r--r--libjava/java/net/JarURLConnection.java29
-rw-r--r--libjava/java/net/PlainDatagramSocketImpl.java272
-rw-r--r--libjava/java/net/PlainSocketImpl.java395
-rw-r--r--libjava/java/net/ServerSocket.java82
-rw-r--r--libjava/java/net/Socket.java123
-rw-r--r--libjava/java/net/SocketImpl.java3
-rw-r--r--libjava/java/net/SocketInputStream.java204
-rw-r--r--libjava/java/net/SocketOutputStream.java165
-rw-r--r--libjava/java/net/URL.java59
-rw-r--r--libjava/java/net/URLClassLoader.java131
-rw-r--r--libjava/java/net/URLEncoder.java27
-rw-r--r--libjava/java/net/URLStreamHandler.java4
-rw-r--r--libjava/java/net/natInetAddressWin32.cc265
-rw-r--r--libjava/java/net/natNetworkInterfaceWin32.cc210
-rw-r--r--libjava/java/net/natPlainDatagramSocketImplNoNet.cc119
-rw-r--r--libjava/java/net/natPlainDatagramSocketImplPosix.cc750
-rw-r--r--libjava/java/net/natPlainDatagramSocketImplWin32.cc872
-rw-r--r--libjava/java/net/natPlainSocketImplNoNet.cc128
-rw-r--r--libjava/java/net/natPlainSocketImplPosix.cc856
-rw-r--r--libjava/java/net/natPlainSocketImplWin32.cc1019
22 files changed, 487 insertions, 5422 deletions
diff --git a/libjava/java/net/DatagramSocket.java b/libjava/java/net/DatagramSocket.java
index 57f3da70862..1d89d688401 100644
--- a/libjava/java/net/DatagramSocket.java
+++ b/libjava/java/net/DatagramSocket.java
@@ -35,8 +35,10 @@ 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 gnu.java.net.PlainDatagramSocketImpl;
import java.io.IOException;
import java.nio.channels.DatagramChannel;
import java.nio.channels.IllegalBlockingModeException;
@@ -73,12 +75,6 @@ public class DatagramSocket
DatagramSocketImpl impl;
/**
- * The unique DatagramChannel object associated with this datagram socket,
- * or null.
- */
- DatagramChannel ch;
-
- /**
* This is the address we are "connected" to
*/
private InetAddress remoteAddress;
@@ -266,38 +262,30 @@ public class DatagramSocket
*/
public InetAddress getLocalAddress()
{
- // FIXME: JCL p. 510 says this should call checkConnect. But what
- // string should be used as the hostname? Maybe this is just a side
- // effect of calling InetAddress.getLocalHost.
- //
- // And is getOption with SO_BINDADDR the right way to get the address?
- // Doesn't seem to be since this method doesn't throw a SocketException
- // and SO_BINADDR can throw one.
- //
- // Also see RETURNS section in JCL p. 510 about returning any local
- // addr "if the current execution context is not allowed to connect to
- // the network interface that is actually bound to this datagram socket."
- // How is that done? via InetAddress.getLocalHost? But that throws
- // an UnknownHostException and this method doesn't.
- //
- // if (s != null)
- // s.checkConnect("localhost", -1);
+ if (impl == null
+ || closed)
+ return null;
+
+ InetAddress localAddr;
+
try
{
- return (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
- }
- catch (SocketException ex)
- {
- }
+ localAddr = (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
- try
+ SecurityManager s = System.getSecurityManager();
+ if (s != null)
+ s.checkConnect (localAddr.getHostName(), -1);
+ }
+ catch (SecurityException e)
{
- return InetAddress.getLocalHost();
+ localAddr = InetAddress.ANY_IF;
}
- catch (UnknownHostException ex)
+ catch (SocketException e)
{
return null;
}
+
+ return localAddr;
}
/**
@@ -525,7 +513,8 @@ public class DatagramSocket
throw new IOException (
"Socket connected to a multicast address my not receive");
- if (ch != null && !ch.isBlocking ())
+ if (getChannel() != null
+ && !getChannel().isBlocking ())
throw new IllegalBlockingModeException ();
impl.receive(p);
@@ -574,7 +563,8 @@ public class DatagramSocket
// FIXME: if this is a subclass of MulticastSocket,
// use getTimeToLive for TTL val.
- if (ch != null && !ch.isBlocking ())
+ if (getChannel() != null
+ && !getChannel().isBlocking ())
throw new IllegalBlockingModeException ();
impl.send(p);
@@ -624,7 +614,7 @@ public class DatagramSocket
*/
public DatagramChannel getChannel()
{
- return ch;
+ return null;
}
/**
diff --git a/libjava/java/net/InetAddress.java b/libjava/java/net/InetAddress.java
index 8ef63668a55..6a841d08a2a 100644
--- a/libjava/java/net/InetAddress.java
+++ b/libjava/java/net/InetAddress.java
@@ -70,61 +70,63 @@ public class InetAddress implements Serializable
{
private static final long serialVersionUID = 3286316764910316507L;
- // The Serialized Form specifies that an int 'address' is saved/restored.
- // This class uses a byte array internally so we'll just do the conversion
- // at serialization time and leave the rest of the algorithm as is.
+ static final byte[] zeros = { 0, 0, 0, 0 };
+
+ /**
+ * Dummy InetAddress, used to bind socket to any (all) network interfaces.
+ */
+ static final InetAddress ANY_IF = new InetAddress (zeros, null);
+
+ private static final byte[] localhostAddress = { 127, 0, 0, 1 };
+
+ private static InetAddress localhost = null;
+
+ /**
+ * The Serialized Form specifies that an int 'address' is saved/restored.
+ * This class uses a byte array internally so we'll just do the conversion
+ * at serialization time and leave the rest of the algorithm as is.
+ */
private int address;
+
+ /**
+ * An array of octets representing an IP address.
+ */
transient byte[] addr;
+
+ /**
+ * The name of the host for this address.
+ */
String hostName;
- // The field 'family' seems to be the AF_ value.
- // FIXME: Much of the code in the other java.net classes does not make
- // use of this family field. A better implementation would be to make
- // use of getaddrinfo() and have other methods just check the family
- // field rather than examining the length of the address each time.
+ /**
+ * The field 'family' seems to be the AF_ value.
+ * FIXME: Much of the code in the other java.net classes does not make
+ * use of this family field. A better implementation would be to make
+ * use of getaddrinfo() and have other methods just check the family
+ * field rather than examining the length of the address each time.
+ */
int family;
/**
- * Needed for serialization
+ * Initializes this object's addr instance variable from the passed in
+ * int array. Note that this constructor is protected and is called
+ * only by static methods in this class.
+ *
+ * @param ipaddr The IP number of this address as an array of bytes
*/
- private void readResolve () throws ObjectStreamException
+ InetAddress (byte[] address)
{
- // FIXME: implement this
- }
-
- private void readObject (ObjectInputStream ois)
- throws IOException, ClassNotFoundException
- {
- ois.defaultReadObject ();
- addr = new byte [4];
- addr [3] = (byte) address;
-
- for (int i = 2; i >= 0; --i)
- addr [i] = (byte) (address >>= 8);
-
- // Ignore family from serialized data. Since the saved address is 32 bits
- // the deserialized object will have an IPv4 address i.e. AF_INET family.
- // FIXME: An alternative is to call the aton method on the deserialized
- // hostname to get a new address. The Serialized Form doc is silent
- // on how these fields are used.
- family = getFamily (addr);
- }
-
- private void writeObject (ObjectOutputStream oos) throws IOException
- {
- // Build a 32 bit address from the last 4 bytes of a 4 byte IPv4 address
- // or a 16 byte IPv6 address.
- int len = addr.length;
- int i = len - 4;
-
- for (; i < len; i++)
- address = address << 8 | (((int) addr [i]) & 0xFF);
-
- oos.defaultWriteObject ();
+ this (address, null);
}
- private static native int getFamily (byte[] address);
-
+ /**
+ * Initializes this object's addr instance variable from the passed in
+ * int array. Note that this constructor is protected and is called
+ * only by static methods in this class.
+ *
+ * @param ipaddr The IP number of this address as an array of bytes
+ * @param hostname The hostname of this IP address.
+ */
InetAddress (byte[] address, String hostname)
{
addr = address;
@@ -530,6 +532,8 @@ public class InetAddress implements Serializable
private static native InetAddress[] lookup (String hostname,
InetAddress addr, boolean all);
+ private static native int getFamily (byte[] address);
+
/**
* Determines the IP address of a host, given the host's name.
*
@@ -606,17 +610,8 @@ public class InetAddress implements Serializable
return lookup (hostname, null, true);
}
- static final byte[] zeros = { 0, 0, 0, 0 };
-
- /* dummy InetAddress, used to bind socket to any (all) network interfaces */
- static final InetAddress ANY_IF = new InetAddress (zeros, null);
-
- private static final byte[] localhostAddress = { 127, 0, 0, 1 };
-
private static native String getLocalHostname ();
- private static InetAddress localhost = null;
-
/**
* Returns the local host
*
@@ -681,4 +676,43 @@ public class InetAddress implements Serializable
if (localhost == null)
localhost = new InetAddress (localhostAddress, "localhost");
}
+
+ /**
+ * Needed for serialization
+ */
+ private void readResolve () throws ObjectStreamException
+ {
+ // FIXME: implement this
+ }
+
+ private void readObject (ObjectInputStream ois)
+ throws IOException, ClassNotFoundException
+ {
+ ois.defaultReadObject ();
+ addr = new byte [4];
+ addr [3] = (byte) address;
+
+ for (int i = 2; i >= 0; --i)
+ addr [i] = (byte) (address >>= 8);
+
+ // Ignore family from serialized data. Since the saved address is 32 bits
+ // the deserialized object will have an IPv4 address i.e. AF_INET family.
+ // FIXME: An alternative is to call the aton method on the deserialized
+ // hostname to get a new address. The Serialized Form doc is silent
+ // on how these fields are used.
+ family = getFamily (addr);
+ }
+
+ private void writeObject (ObjectOutputStream oos) throws IOException
+ {
+ // Build a 32 bit address from the last 4 bytes of a 4 byte IPv4 address
+ // or a 16 byte IPv6 address.
+ int len = addr.length;
+ int i = len - 4;
+
+ for (; i < len; i++)
+ address = address << 8 | (((int) addr [i]) & 0xFF);
+
+ oos.defaultWriteObject ();
+ }
}
diff --git a/libjava/java/net/JarURLConnection.java b/libjava/java/net/JarURLConnection.java
index b8e9de07c52..d90ea0cc376 100644
--- a/libjava/java/net/JarURLConnection.java
+++ b/libjava/java/net/JarURLConnection.java
@@ -103,21 +103,21 @@ public abstract class JarURLConnection extends URLConnection
*
* @specnote This constructor is protected since JDK 1.4
*/
- protected JarURLConnection(URL url)
+ protected JarURLConnection (URL url)
throws MalformedURLException
{
- super(url);
+ super (url);
String spec = url.getFile();
- int bang = spec.indexOf ("!/", 0);
+ int bang = spec.indexOf ("!/");
if (bang == -1)
throw new MalformedURLException (url + ": No `!/' in spec.");
- // Extact the url for the jar itself.
- jarFileURL = new URL(spec.substring (0, bang));
+ // Extract the url for the jar itself.
+ jarFileURL = new URL (spec.substring (0, bang));
// Get the name of the element, if any.
- element = (bang+2==spec.length() ? null : spec.substring (bang+2));
+ element = (spec.length() == (bang + 2) ? null : spec.substring (bang + 2));
}
/**
@@ -428,7 +428,9 @@ public abstract class JarURLConnection extends URLConnection
*/
public Certificate[] getCertificates () throws IOException
{
- return getJarEntry ().getCertificates ();
+ JarEntry entry = getJarEntry();
+
+ return entry != null ? entry.getCertificates() : null;
}
/**
@@ -441,7 +443,9 @@ public abstract class JarURLConnection extends URLConnection
*/
public Attributes getMainAttributes () throws IOException
{
- return getManifest ().getMainAttributes ();
+ Manifest manifest = getManifest();
+
+ return manifest != null ? manifest.getMainAttributes() : null;
}
/**
@@ -455,8 +459,9 @@ public abstract class JarURLConnection extends URLConnection
*/
public Attributes getAttributes () throws IOException
{
- // FIXME: implement this
- return null;
+ JarEntry entry = getJarEntry();
+
+ return entry != null ? entry.getAttributes() : null;
}
/**
@@ -469,8 +474,8 @@ public abstract class JarURLConnection extends URLConnection
*/
public Manifest getManifest () throws IOException
{
- JarFile file = getJarFile ();
+ JarFile file = getJarFile();
- return (file != null) ? file.getManifest() : null;
+ return file != null ? file.getManifest() : null;
}
}
diff --git a/libjava/java/net/PlainDatagramSocketImpl.java b/libjava/java/net/PlainDatagramSocketImpl.java
deleted file mode 100644
index 83fdb633d99..00000000000
--- a/libjava/java/net/PlainDatagramSocketImpl.java
+++ /dev/null
@@ -1,272 +0,0 @@
-/* PlainDatagramSocketImpl.java -- Default DatagramSocket implementation
- Copyright (C) 1998, 1999, 2001 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.io.IOException;
-import gnu.classpath.Configuration;
-
-/**
- * Written using on-line Java Platform 1.2 API Specification, as well
- * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
- * Status: Believed complete and correct.
- */
-
-/**
- * This is the default socket implementation for datagram sockets.
- * It makes native calls to C routines that implement BSD style
- * SOCK_DGRAM sockets in the AF_INET family.
- *
- * @author Aaron M. Renn <arenn@urbanophile.com>
- * @author Warren Levy <warrenl@cygnus.com>
- */
-class PlainDatagramSocketImpl extends DatagramSocketImpl
-{
- // Static initializer to load native library
- static
- {
- if (Configuration.INIT_LOAD_LIBRARY)
- {
- System.loadLibrary("javanet");
- }
- }
-
- // These fields are mirrored for use in native code to avoid cpp conflicts
- // when the #defines in system header files are the same as the public fields.
- static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
- _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
- _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
- _Jv_SO_BROADCAST_ = SocketOptions.SO_BROADCAST,
- _Jv_SO_OOBINLINE_ = SocketOptions.SO_OOBINLINE,
- _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
- _Jv_IP_MULTICAST_IF2_ = SocketOptions.IP_MULTICAST_IF2,
- _Jv_IP_MULTICAST_LOOP_ = SocketOptions.IP_MULTICAST_LOOP,
- _Jv_IP_TOS_ = SocketOptions.IP_TOS,
- _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
- _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
- _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
- _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF,
- _Jv_SO_KEEPALIVE_ = SocketOptions.SO_KEEPALIVE;
-
- /**
- * This is the actual underlying file descriptor
- */
- int fnum = -1;
-
- // FIXME: Is this necessary? Could it help w/ DatagramSocket.getLocalAddress?
- // InetAddress address;
-
- // localAddress cache
- InetAddress localAddress;
-
- // 'timeout' is set/read by setOption/getOption.
- int timeout = 0;
-
- /**
- * Default do nothing constructor
- */
- public PlainDatagramSocketImpl()
- {
- }
-
- /**
- * Binds this socket to a particular port and interface
- *
- * @param port The port to bind to
- * @param addr The address to bind to
- *
- * @exception SocketException If an error occurs
- */
- protected native void bind(int lport, InetAddress laddr)
- throws SocketException;
-
- protected native void connect (InetAddress i, int port)
- throws SocketException;
-
- protected native void disconnect ();
-
- /**
- * Creates a new datagram socket
- *
- * @exception SocketException If an error occurs
- */
- protected native void create() throws SocketException;
-
- protected native int peek(InetAddress i) throws IOException;
-
- protected native int peekData (DatagramPacket dp) throws IOException;
-
- /**
- * Sets the Time to Live value for the socket
- *
- * @param ttl The new TTL value
- *
- * @exception IOException If an error occurs
- */
- protected native void setTimeToLive(int ttl) throws IOException;
-
- /**
- * Gets the Time to Live value for the socket
- *
- * @return The TTL value
- *
- * @exception IOException If an error occurs
- */
- protected native int getTimeToLive() throws IOException;
-
- /**
- * Sends a packet of data to a remote host
- *
- * @param packet The packet to send
- *
- * @exception IOException If an error occurs
- */
- protected native void send(DatagramPacket p) throws IOException;
-
- /**
- * Receives a UDP packet from the network
- *
- * @param packet The packet to fill in with the data received
- *
- * @exception IOException IOException If an error occurs
- */
- protected native void receive(DatagramPacket p) throws IOException;
-
- /**
- * Sets the value of an option on the socket
- *
- * @param option_id The identifier of the option to set
- * @param val The value of the option to set
- *
- * @exception SocketException If an error occurs
- */
- public native void setOption(int optID, Object value) throws SocketException;
-
- /**
- * Retrieves the value of an option on the socket
- *
- * @param option_id The identifier of the option to retrieve
- *
- * @return The value of the option
- *
- * @exception SocketException If an error occurs
- */
- public native Object getOption(int optID) throws SocketException;
-
- private native void mcastGrp(InetAddress inetaddr, NetworkInterface netIf,
- boolean join) throws IOException;
-
- /**
- * Closes the socket
- */
- protected native void close();
-
- /**
- * Gets the Time to Live value for the socket
- *
- * @return The TTL value
- *
- * @exception IOException If an error occurs
- *
- * @deprecated 1.2
- */
- protected byte getTTL() throws IOException
- {
- return (byte) getTimeToLive();
- }
-
- /**
- * Sets the Time to Live value for the socket
- *
- * @param ttl The new TTL value
- *
- * @exception IOException If an error occurs
- *
- * @deprecated 1.2
- */
- protected void setTTL(byte ttl) throws IOException
- {
- setTimeToLive(((int) ttl) & 0xFF);
- }
-
- /**
- * Joins a multicast group
- *
- * @param addr The group to join
- *
- * @exception IOException If an error occurs
- */
- protected void join(InetAddress inetaddr) throws IOException
- {
- mcastGrp(inetaddr, null, true);
- }
-
- /**
- * Leaves a multicast group
- *
- * @param addr The group to leave
- *
- * @exception IOException If an error occurs
- */
- protected void leave(InetAddress inetaddr) throws IOException
- {
- mcastGrp(inetaddr, null, false);
- }
-
- protected void joinGroup (SocketAddress mcastaddr, NetworkInterface netIf)
- throws IOException
- {
- mcastGrp(((InetSocketAddress)mcastaddr).getAddress(), netIf, true);
- }
-
- protected void leaveGroup (SocketAddress mcastaddr, NetworkInterface netIf)
- throws IOException
- {
- mcastGrp(((InetSocketAddress)mcastaddr).getAddress(), netIf, false);
- }
-
- protected void finalize() throws Throwable
- {
- synchronized (this)
- {
- if (fnum != -1)
- close();
- }
- super.finalize();
- }
-}
diff --git a/libjava/java/net/PlainSocketImpl.java b/libjava/java/net/PlainSocketImpl.java
deleted file mode 100644
index ad0ce46c7ea..00000000000
--- a/libjava/java/net/PlainSocketImpl.java
+++ /dev/null
@@ -1,395 +0,0 @@
-/* PlainSocketImpl.java -- Default socket implementation
- Copyright (C) 1998, 1999 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.io.InputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import gnu.classpath.Configuration;
-
-/**
- * Written using on-line Java Platform 1.2 API Specification, as well
- * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
- * Status: Believed complete and correct.
- */
-
-/**
- * Unless the application installs its own SocketImplFactory, this is the
- * default socket implemetation that will be used. It simply uses a
- * combination of Java and native routines to implement standard BSD
- * style sockets of family AF_INET and types SOCK_STREAM and SOCK_DGRAM
- *
- * @author Per Bothner <bothner@cygnus.com>
- * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
- * @author Aaron M. Renn <arenn@urbanophile.com>
- */
-class PlainSocketImpl extends SocketImpl
-{
- // Static initializer to load native library.
- static
- {
- if (Configuration.INIT_LOAD_LIBRARY)
- {
- System.loadLibrary("javanet");
- }
- }
-
- // These fields are mirrored for use in native code to avoid cpp conflicts
- // when the #defines in system header files are the same as the public fields.
- static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
- _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
- _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
- _Jv_SO_BROADCAST_ = SocketOptions.SO_BROADCAST,
- _Jv_SO_OOBINLINE_ = SocketOptions.SO_OOBINLINE,
- _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
- _Jv_IP_MULTICAST_IF2_ = SocketOptions.IP_MULTICAST_IF2,
- _Jv_IP_MULTICAST_LOOP_ = SocketOptions.IP_MULTICAST_LOOP,
- _Jv_IP_TOS_ = SocketOptions.IP_TOS,
- _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
- _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
- _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
- _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF,
- _Jv_SO_KEEPALIVE_ = SocketOptions.SO_KEEPALIVE;
-
- /**
- * The OS file handle representing the socket.
- * This is used for reads and writes to/from the socket and
- * to close it.
- *
- * When the socket is closed this is reset to -1.
- */
- int fnum = -1;
-
- // This value is set/read by setOption/getOption.
- int timeout = 0;
-
- // localAddress cache
- InetAddress localAddress;
-
- /**
- * A cached copy of the in stream for reading from the socket.
- */
- private InputStream in;
-
- /**
- * A cached copy of the out stream for writing to the socket.
- */
- private OutputStream out;
-
- /**
- * Default do nothing constructor
- */
- public PlainSocketImpl()
- {
- }
-
- protected void finalize() throws Throwable
- {
- synchronized (this)
- {
- if (fnum != -1)
- try
- {
- close();
- }
- catch (IOException ex)
- {
- // ignore
- }
- }
- super.finalize();
- }
-
- /**
- * Sets the specified option on a socket to the passed in object. For
- * options that take an integer argument, the passed in object is an
- * Integer. The option_id parameter is one of the defined constants in
- * this interface.
- *
- * @param option_id The identifier of the option
- * @param val The value to set the option to
- *
- * @exception SocketException If an error occurs
- */
- public native void setOption(int optID, Object value) throws SocketException;
-
- /**
- * Returns the current setting of the specified option. The Object returned
- * will be an Integer for options that have integer values. The option_id
- * is one of the defined constants in this interface.
- *
- * @param option_id The option identifier
- *
- * @return The current value of the option
- *
- * @exception SocketException If an error occurs
- */
- public native Object getOption(int optID) throws SocketException;
-
- public native void shutdownInput () throws IOException;
-
- public native void shutdownOutput () throws IOException;
-
- /**
- * Creates a new socket that is not bound to any local address/port and
- * is not connected to any remote address/port. This will be created as
- * a stream socket if the stream parameter is true, or a datagram socket
- * if the stream parameter is false.
- *
- * @param stream true for a stream socket, false for a datagram socket
- */
- protected native void create (boolean stream) throws IOException;
-
- /**
- * Connects to the remote hostname and port specified as arguments.
- *
- * @param hostname The remote hostname to connect to
- * @param port The remote port to connect to
- *
- * @exception IOException If an error occurs
- */
- protected void connect (String host, int port) throws IOException
- {
- connect (new InetSocketAddress (InetAddress.getByName(host), port), 0);
- }
-
- /**
- * Connects to the remote address and port specified as arguments.
- *
- * @param addr The remote address to connect to
- * @param port The remote port to connect to
- *
- * @exception IOException If an error occurs
- */
- protected void connect (InetAddress host, int port) throws IOException
- {
- connect (new InetSocketAddress (host, port), 0);
- }
-
- protected native void connect (SocketAddress addr, int timeout)
- throws IOException;
-
- /**
- * Binds to the specified port on the specified addr. Note that this addr
- * must represent a local IP address. **** How bind to INADDR_ANY? ****
- *
- * @param addr The address to bind to
- * @param port The port number to bind to
- *
- * @exception IOException If an error occurs
- */
- protected native void bind (InetAddress host, int port) throws IOException;
-
- /**
- * Starts listening for connections on a socket. The queuelen parameter
- * is how many pending connections will queue up waiting to be serviced
- * before being accept'ed. If the queue of pending requests exceeds this
- * number, additional connections will be refused.
- *
- * @param queuelen The length of the pending connection queue
- *
- * @exception IOException If an error occurs
- */
- protected native void listen (int backlog) throws IOException;
-
- private native void accept (PlainSocketImpl s) throws IOException;
-
- /**
- * Accepts a new connection on this socket and returns in in the
- * passed in SocketImpl.
- *
- * @param impl The SocketImpl object to accept this connection.
- */
- protected void accept (SocketImpl s) throws IOException
- {
- accept((PlainSocketImpl) s);
- }
-
- /**
- * Returns the number of bytes that the caller can read from this socket
- * without blocking.
- *
- * @return The number of readable bytes before blocking
- *
- * @exception IOException If an error occurs
- */
- protected native int available() throws IOException;
-
- /**
- * Closes the socket. This will cause any InputStream or OutputStream
- * objects for this Socket to be closed as well.
- * <p>
- * Note that if the SO_LINGER option is set on this socket, then the
- * operation could block.
- *
- * @exception IOException If an error occurs
- */
- protected native void close () throws IOException;
-
- protected native void sendUrgentData(int data)
- throws IOException;
-
- native int read() throws IOException;
-
- /**
- * Internal method used by SocketInputStream for reading data from
- * the connection. Reads up to len bytes of data into the buffer
- * buf starting at offset bytes into the buffer.
- *
- * @return The actual number of bytes read or -1 if end of stream.
- *
- * @exception IOException If an error occurs
- */
- native int read(byte[] buffer, int offset, int count)
- throws IOException;
-
- native void write(int c) throws IOException;
-
- /**
- * Internal method used by SocketOuputStream for writing data to
- * the connection. Writes up to len bytes of data from the buffer
- * buf starting at offset bytes into the buffer.
- *
- * @exception IOException If an error occurs
- */
- native void write(byte[] buffer, int offset, int count)
- throws IOException;
-
- /**
- * Returns an InputStream object for reading from this socket. This will
- * be an instance of SocketInputStream.
- *
- * @return An input stream attached to the socket.
- *
- * @exception IOException If an error occurs
- */
- protected synchronized InputStream getInputStream() throws IOException
- {
- if (in == null)
- in = new SocketInputStream();
-
- return in;
- }
-
- /**
- * Returns an OutputStream object for writing to this socket. This will
- * be an instance of SocketOutputStream.
- *
- * @return An output stream attached to the socket.
- *
- * @exception IOException If an error occurs
- */
- protected synchronized OutputStream getOutputStream() throws IOException
- {
- if (out == null)
- out = new SocketOutputStream();
-
- return out;
- }
-
- /**
- * A stream which reads from the socket implementation.
- *
- * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
- */
- class SocketInputStream
- extends InputStream
- {
- SocketInputStream()
- {
- }
-
- public final void close() throws IOException
- {
- PlainSocketImpl.this.close();
- }
-
- public final int available() throws IOException
- {
- return PlainSocketImpl.this.available();
- }
-
- public final int read() throws IOException
- {
- return PlainSocketImpl.this.read();
- }
-
- public final int read(byte[] buffer, int offset, int length)
- throws IOException
- {
- return PlainSocketImpl.this.read(buffer, offset, length);
- }
-
- public final int read(byte[] buffer)
- throws IOException
- {
- return PlainSocketImpl.this.read(buffer, 0, buffer.length);
- }
- }
-
- /** A stream which writes to the socket implementation.
- *
- * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
- */
- class SocketOutputStream
- extends OutputStream
- {
- public final void close() throws IOException
- {
- PlainSocketImpl.this.close();
- }
-
- public final void write(int c) throws IOException
- {
- PlainSocketImpl.this.write(c);
- }
-
- public final void write(byte[] buffer, int offset, int length)
- throws IOException
- {
- PlainSocketImpl.this.write(buffer, offset, length);
- }
-
- public final void write(byte[] buffer)
- throws IOException
- {
- PlainSocketImpl.this.write(buffer, 0, buffer.length);
- }
- }
-}
diff --git a/libjava/java/net/ServerSocket.java b/libjava/java/net/ServerSocket.java
index 699319e70b2..44281783dc8 100644
--- a/libjava/java/net/ServerSocket.java
+++ b/libjava/java/net/ServerSocket.java
@@ -1,5 +1,5 @@
/* ServerSocket.java -- Class for implementing server side sockets
- Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -35,8 +35,10 @@ 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 gnu.java.net.PlainSocketImpl;
import java.io.IOException;
import java.nio.channels.IllegalBlockingModeException;
import java.nio.channels.ServerSocketChannel;
@@ -71,12 +73,6 @@ public class ServerSocket
*/
private SocketImpl impl;
- /**
- * ServerSocketChannel of this ServerSocket. This channel only exists
- * when the socket is created by ServerSocketChannel.open().
- */
- private ServerSocketChannel ch;
-
private boolean closed = false;
/**
@@ -154,24 +150,10 @@ public class ServerSocket
{
this();
- if (impl == null)
- throw new IOException("Cannot initialize Socket implementation");
-
- // create socket
- impl.create(true);
-
// bind/listen socket
bind (new InetSocketAddress (bindAddr, port), backlog);
}
- /*
- * This method may only be used by java.nio.channels.ServerSocketChannel.open.
- */
- void setChannel (ServerSocketChannel ch)
- {
- this.ch = ch;
- }
-
/**
* Binds the server socket to a specified socket address
*
@@ -208,9 +190,6 @@ public class ServerSocket
if (closed)
throw new SocketException ("ServerSocket is closed");
- if (impl == null)
- throw new IOException ("Cannot initialize Socket implementation");
-
if (! (endpoint instanceof InetSocketAddress))
throw new IllegalArgumentException ("Address type not supported");
@@ -220,45 +199,24 @@ public class ServerSocket
if (s != null)
s.checkListen (tmp.getPort ());
- // bind to address/port
try
{
- impl.bind (tmp.getAddress (), tmp.getPort ());
- }
- catch (IOException exception)
- {
- impl.close();
- throw exception;
- }
- catch (RuntimeException exception)
- {
- impl.close();
- throw exception;
+ impl.bind (tmp.getAddress (), tmp.getPort ());
+ impl.listen(backlog);
}
- catch (Error error)
- {
- impl.close();
- throw error;
- }
-
- // listen on socket
- try
- {
- impl.listen(backlog);
- }
catch (IOException exception)
{
- impl.close();
+ close();
throw exception;
}
catch (RuntimeException exception)
{
- impl.close();
+ close();
throw exception;
}
catch (Error error)
{
- impl.close();
+ close();
throw error;
}
}
@@ -320,9 +278,6 @@ public class ServerSocket
*/
public Socket accept () throws IOException
{
- if (impl == null)
- throw new IOException ("Cannot initialize Socket implementation");
-
SecurityManager sm = System.getSecurityManager ();
if (sm != null)
sm.checkListen (impl.getLocalPort ());
@@ -349,7 +304,8 @@ public class ServerSocket
protected final void implAccept (Socket s)
throws IOException
{
- if (ch != null && !ch.isBlocking())
+ if (getChannel() != null
+ && !getChannel().isBlocking())
throw new IllegalBlockingModeException();
impl.accept(s.impl);
@@ -365,8 +321,8 @@ public class ServerSocket
if (impl != null)
impl.close ();
- if (ch != null)
- ch.close ();
+ if (getChannel() != null)
+ getChannel().close ();
closed = true;
}
@@ -382,7 +338,7 @@ public class ServerSocket
*/
public ServerSocketChannel getChannel()
{
- return ch;
+ return null;
}
/**
@@ -466,9 +422,6 @@ public class ServerSocket
public void setReuseAddress (boolean on)
throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
}
@@ -482,9 +435,6 @@ public class ServerSocket
public boolean getReuseAddress()
throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR);
if (!(reuseaddr instanceof Boolean))
@@ -508,9 +458,6 @@ public class ServerSocket
public void setReceiveBufferSize (int size)
throws SocketException
{
- if (impl == null)
- throw new SocketException ("Not connected");
-
if (size <= 0)
throw new IllegalArgumentException ("SO_RCVBUF value must be > 0");
@@ -531,9 +478,6 @@ public class ServerSocket
public int getReceiveBufferSize ()
throws SocketException
{
- if (impl == null)
- throw new SocketException ("Not connected");
-
Object buf = impl.getOption (SocketOptions.SO_RCVBUF);
if (!(buf instanceof Integer))
diff --git a/libjava/java/net/Socket.java b/libjava/java/net/Socket.java
index ef88de5c151..a5397bf7a49 100644
--- a/libjava/java/net/Socket.java
+++ b/libjava/java/net/Socket.java
@@ -1,5 +1,5 @@
/* Socket.java -- Client socket implementation
- Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -35,8 +35,10 @@ 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 gnu.java.net.PlainSocketImpl;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
@@ -82,10 +84,8 @@ public class Socket
*/
SocketImpl impl;
- private boolean inputShutdown;
- private boolean outputShutdown;
-
- SocketChannel ch; // this field must have been set if created by SocketChannel
+ private boolean inputShutdown = false;
+ private boolean outputShutdown = false;
private boolean closed = false;
@@ -103,9 +103,6 @@ public class Socket
impl = factory.createSocketImpl();
else
impl = new PlainSocketImpl();
-
- inputShutdown = false;
- outputShutdown = false;
}
/**
@@ -115,9 +112,8 @@ public class Socket
* <p>
* Additionally, this socket will be created using the supplied
* implementation class instead the default class or one returned by a
- * factory. This value can be <code>null</code>, but if it is, all instance
- * methods in <code>Socket</code> should be overridden because most of them
- * rely on this value being populated.
+ * factory. If this value is <code>null</code>, the default Socket
+ * implementation is used.
*
* @param impl The <code>SocketImpl</code> to use for this
* <code>Socket</code>
@@ -128,9 +124,10 @@ public class Socket
*/
protected Socket (SocketImpl impl) throws SocketException
{
- this.impl = impl;
- this.inputShutdown = false;
- this.outputShutdown = false;
+ if (impl == null)
+ this.impl = new PlainSocketImpl();
+ else
+ this.impl = impl;
}
/**
@@ -282,12 +279,6 @@ public class Socket
{
this();
- if (raddr == null)
- throw new NullPointerException ();
-
- if (impl == null)
- throw new IOException("Cannot initialize Socket implementation");
-
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkConnect(raddr.getHostName(), rport);
@@ -305,15 +296,6 @@ public class Socket
// that default. JDK 1.2 doc infers not to do a bind.
}
- /*
- * This method may only be used by java.nio.channels.ServerSocketChannel.accept and
- * java.nio.channels.SocketChannel.open.
- */
- void setChannel (SocketChannel ch)
- {
- this.ch = ch;
- }
-
/**
* Binds the socket to the givent local address/port
*
@@ -351,17 +333,17 @@ public class Socket
}
catch (IOException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (RuntimeException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (Error error)
{
- impl.close ();
+ close ();
throw error;
}
}
@@ -408,7 +390,8 @@ public class Socket
if (! (endpoint instanceof InetSocketAddress))
throw new IllegalArgumentException ("Address type not supported");
- if (ch != null && !ch.isBlocking ())
+ if (getChannel() != null
+ && !getChannel().isBlocking ())
throw new IllegalBlockingModeException ();
if (!isBound ())
@@ -420,17 +403,17 @@ public class Socket
}
catch (IOException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (RuntimeException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (Error error)
{
- impl.close ();
+ close ();
throw error;
}
}
@@ -443,10 +426,7 @@ public class Socket
*/
public InetAddress getInetAddress ()
{
- if (impl != null)
- return impl.getInetAddress();
-
- return null;
+ return impl.getInetAddress();
}
/**
@@ -459,9 +439,6 @@ public class Socket
*/
public InetAddress getLocalAddress ()
{
- if (impl == null)
- return null;
-
InetAddress addr = null;
try
{
@@ -586,9 +563,6 @@ public class Socket
*/
public void setTcpNoDelay (boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
impl.setOption(SocketOptions.TCP_NODELAY, new Boolean(on));
}
@@ -606,9 +580,6 @@ public class Socket
*/
public boolean getTcpNoDelay() throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object on = impl.getOption(SocketOptions.TCP_NODELAY);
if (on instanceof Boolean)
@@ -636,9 +607,6 @@ public class Socket
*/
public void setSoLinger(boolean on, int linger) throws SocketException
{
- if (impl == null)
- throw new SocketException("No socket created");
-
if (on == true)
{
if (linger < 0)
@@ -673,9 +641,6 @@ public class Socket
*/
public int getSoLinger() throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object linger = impl.getOption(SocketOptions.SO_LINGER);
if (linger instanceof Integer)
return(((Integer)linger).intValue());
@@ -709,9 +674,6 @@ public class Socket
*/
public void setOOBInline (boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
impl.setOption(SocketOptions.SO_OOBINLINE, new Boolean(on));
}
@@ -724,9 +686,6 @@ public class Socket
*/
public boolean getOOBInline () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_OOBINLINE);
if (buf instanceof Boolean)
@@ -754,9 +713,6 @@ public class Socket
*/
public synchronized void setSoTimeout (int timeout) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
if (timeout < 0)
throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
@@ -782,9 +738,6 @@ public class Socket
*/
public synchronized int getSoTimeout () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
if (timeout instanceof Integer)
return(((Integer)timeout).intValue());
@@ -806,9 +759,6 @@ public class Socket
*/
public void setSendBufferSize (int size) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
if (size <= 0)
throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
@@ -828,9 +778,6 @@ public class Socket
*/
public int getSendBufferSize () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_SNDBUF);
if (buf instanceof Integer)
@@ -853,9 +800,6 @@ public class Socket
*/
public void setReceiveBufferSize (int size) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
if (size <= 0)
throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
@@ -875,9 +819,6 @@ public class Socket
*/
public int getReceiveBufferSize () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_RCVBUF);
if (buf instanceof Integer)
@@ -898,9 +839,6 @@ public class Socket
*/
public void setKeepAlive (boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on));
}
@@ -916,9 +854,6 @@ public class Socket
*/
public boolean getKeepAlive () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_KEEPALIVE);
if (buf instanceof Boolean)
@@ -937,8 +872,8 @@ public class Socket
if (impl != null)
impl.close();
- if (ch != null)
- ch.close();
+ if (getChannel() != null)
+ getChannel().close();
closed = true;
}
@@ -1025,7 +960,7 @@ public class Socket
*/
public SocketChannel getChannel()
{
- return ch;
+ return null;
}
/**
@@ -1037,9 +972,6 @@ public class Socket
*/
public boolean getReuseAddress () throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR);
if (!(reuseaddr instanceof Boolean))
@@ -1057,9 +989,6 @@ public class Socket
*/
public void setReuseAddress (boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
}
@@ -1074,9 +1003,6 @@ public class Socket
*/
public int getTrafficClass () throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
Object obj = impl.getOption(SocketOptions.IP_TOS);
if (obj instanceof Integer)
@@ -1099,9 +1025,6 @@ public class Socket
*/
public void setTrafficClass (int tc) throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
if (tc < 0 || tc > 255)
throw new IllegalArgumentException();
diff --git a/libjava/java/net/SocketImpl.java b/libjava/java/net/SocketImpl.java
index 14101513e09..e43b49ed599 100644
--- a/libjava/java/net/SocketImpl.java
+++ b/libjava/java/net/SocketImpl.java
@@ -276,7 +276,8 @@ public abstract class SocketImpl implements SocketOptions
*/
public String toString()
{
- return "[addr=" + address
+ return "[addr=" + ((address == null) ? "0.0.0.0/0.0.0.0" :
+ address.toString())
+ ",port=" + port
+ ",localport=" + localport + "]";
}
diff --git a/libjava/java/net/SocketInputStream.java b/libjava/java/net/SocketInputStream.java
deleted file mode 100644
index f2d4d399218..00000000000
--- a/libjava/java/net/SocketInputStream.java
+++ /dev/null
@@ -1,204 +0,0 @@
-/* SocketInputStream.java -- An InputStream for Sockets
- Copyright (C) 1998, 2000, 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.io.InputStream;
-import java.io.IOException;
-
-/**
- * This class contains an implementation of <code>InputStream</code> for
- * sockets. It in an internal only class used by <code>PlainSocketImpl</code>.
- *
- * @author Aaron M. Renn (arenn@urbanophile.com)
- */
-class SocketInputStream extends InputStream
-{
-
-/*************************************************************************/
-
-/*
- * Instance Variables
- */
-
-/**
- * The PlainSocketImpl object this stream is associated with
- */
-private PlainSocketImpl impl;
-
-/*************************************************************************/
-
-/*
- * Constructors
- */
-
-/**
- * Builds an instance of this class from a PlainSocketImpl object
- */
-protected
-SocketInputStream(PlainSocketImpl impl)
-{
- this.impl = impl;
-}
-
-/*************************************************************************/
-
-/*
- * Instance Methods
- */
-
-/**
- * Returns the number of bytes available to be read before blocking
- */
-public int
-available() throws IOException
-{
- return(impl.available());
-}
-
-/*************************************************************************/
-
-/**
- * Determines if "mark" functionality is supported on this stream. For
- * sockets, this is always false. Note that the superclass default is
- * false, but it is overridden out of safety concerns and/or paranoia.
- */
-public boolean
-markSupported()
-{
- return(false);
-}
-
-/*************************************************************************/
-
-/**
- * Do nothing mark method since we don't support this functionality. Again,
- * overriding out of paranoia.
- *
- * @param readlimit In theory, the number of bytes we can read before the mark becomes invalid
- */
-public void
-mark(int readlimit)
-{
-}
-
-/*************************************************************************/
-
-/**
- * Since we don't support mark, this method always throws an exception
- *
- * @exception IOException Everytime since we don't support this functionality
- */
-public void
-reset() throws IOException
-{
- throw new IOException("Socket InputStreams do not support mark/reset");
-}
-
-/*************************************************************************/
-
-/**
- * This method not only closes the stream, it closes the underlying socket
- * (and thus any connection) and invalidates any other Input/Output streams
- * for the underlying impl object
- */
-public void
-close() throws IOException
-{
- impl.close();
-}
-
-/*************************************************************************/
-
-/**
- * Reads the next byte of data and returns it as an int.
- *
- * @return The byte read (as an int) or -1 if end of stream);
- *
- * @exception IOException If an error occurs.
- */
-public int
-read() throws IOException
-{
- byte buf[] = new byte[1];
-
- int bytes_read = read(buf, 0, buf.length);
-
- if (bytes_read != -1)
- return(buf[0] & 0xFF);
- else
- return(-1);
-}
-
-/*************************************************************************/
-
-/**
- * Reads up to buf.length bytes of data into the caller supplied buffer.
- *
- * @return The actual number of bytes read or -1 if end of stream
- *
- * @exception IOException If an error occurs.
- */
-public int
-read(byte[] buf) throws IOException
-{
- return(read(buf, 0, buf.length));
-}
-
-/*************************************************************************/
-
-/**
- * Reads up to len bytes of data into the caller supplied buffer starting
- * at offset bytes from the start of the buffer
- *
- * @return The number of bytes actually read or -1 if end of stream
- *
- * @exception IOException If an error occurs.
- */
-public int
-read(byte[] buf, int offset, int len) throws IOException
-{
- int bytes_read = impl.read(buf, offset, len);
- if (bytes_read == 0)
- return(-1);
-
- return(bytes_read);
-}
-
-} // class SocketInputStream
-
diff --git a/libjava/java/net/SocketOutputStream.java b/libjava/java/net/SocketOutputStream.java
deleted file mode 100644
index 7ce19ae0ef6..00000000000
--- a/libjava/java/net/SocketOutputStream.java
+++ /dev/null
@@ -1,165 +0,0 @@
-/* SocketOutputStream.java -- OutputStream for PlainSocketImpl
- Copyright (C) 1998,2000 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.io.OutputStream;
-import java.io.IOException;
-
-/**
- * This class is used internally by <code>PlainSocketImpl</code> to be the
- * <code>OutputStream</code> subclass returned by its
- * <code>getOutputStream method</code>. It expects only to be used in that
- * context.
- *
- * @author Aaron M. Renn (arenn@urbanophile.com)
- */
-class SocketOutputStream extends OutputStream
-{
-
-/*************************************************************************/
-
-/*
- * Instance Variables
- */
-
-/**
- * The PlainSocketImpl object this stream is associated with
- */
-private PlainSocketImpl impl;
-
-/*************************************************************************/
-
-/*
- * Constructors
- */
-
-/**
- * Build an instance of this class from a PlainSocketImpl object
- */
-protected
-SocketOutputStream(PlainSocketImpl impl)
-{
- this.impl = impl;
-}
-
-/*************************************************************************/
-
-/*
- * Instance Methods
- */
-
-/**
- * This method closes the stream and the underlying socket connection. This
- * action also effectively closes any other InputStream or OutputStream
- * object associated with the connection.
- *
- * @exception IOException If an error occurs
- */
-public void
-close() throws IOException
-{
- impl.close();
-}
-
-/*************************************************************************/
-
-/**
- * Hmmm, we don't seem to have a flush() method in Socket impl, so just
- * return for now, but this might need to be looked at later.
- *
- * @exception IOException Can't happen
- */
-public void
-flush() throws IOException
-{
- return;
-}
-
-/*************************************************************************/
-
-/**
- * Writes a byte (passed in as an int) to the given output stream
- *
- * @param b The byte to write
- *
- * @exception IOException If an error occurs
- */
-public void
-write(int b) throws IOException
-{
- byte buf[] = new byte[1];
-
- Integer i = new Integer(b);
- buf[0] = i.byteValue();
-
- write(buf, 0, buf.length);
-}
-
-/*************************************************************************/
-
-/**
- * Write an array of bytes to the output stream
- *
- * @param buf The array of bytes to write
- *
- * @exception IOException If an error occurs
- */
-public void
-write(byte[] buf) throws IOException
-{
- write(buf, 0, buf.length);
-}
-
-/*************************************************************************/
-
-/**
- * Writes len number of bytes from the array buf to the stream starting
- * at offset bytes into the buffer.
- *
- * @param buf The buffer
- * @param offset Offset into the buffer to start writing from
- * @param len The number of bytes to write
- */
-public void
-write(byte[] buf, int offset, int len) throws IOException
-{
- impl.write(buf, offset, len);
-}
-
-} // class SocketOutputStream
-
diff --git a/libjava/java/net/URL.java b/libjava/java/net/URL.java
index 12c86914e30..2db8c4dd1cd 100644
--- a/libjava/java/net/URL.java
+++ b/libjava/java/net/URL.java
@@ -98,6 +98,14 @@ import java.util.StringTokenizer;
* <p>
* Please note that a protocol handler must be a subclass of
* URLStreamHandler.
+ * <p>
+ * Normally, this class caches protocol handlers. Once it finds a handler
+ * for a particular protocol, it never tries to look up a new handler
+ * again. However, if the system property
+ * gnu.java.net.nocache_protocol_handlers is set, then this
+ * caching behavior is disabled. This property is specific to this
+ * implementation. Sun's JDK may or may not do protocol caching, but it
+ * almost certainly does not examine this property.
*
* @author Aaron M. Renn <arenn@urbanophile.com>
* @author Warren Levy <warrenl@cygnus.com>
@@ -150,18 +158,32 @@ public final class URL implements Serializable
transient URLStreamHandler ph;
/**
+ * If an application installs its own protocol handler factory, this is
+ * where we keep track of it.
+ */
+ private static URLStreamHandlerFactory factory;
+
+ private static final long serialVersionUID = -7627629688361524110L;
+
+ /**
* This a table where we cache protocol handlers to avoid the overhead
* of looking them up each time.
*/
- private static Hashtable handlers = new Hashtable();
+ private static Hashtable ph_cache = new Hashtable();
/**
- * If an application installs its own protocol handler factory, this is
- * where we keep track of it.
+ * Whether or not to cache protocol handlers.
*/
- private static URLStreamHandlerFactory factory;
+ private static boolean cache_handlers;
- private static final long serialVersionUID = -7627629688361524110L;
+ static
+ {
+ String s = System.getProperty("gnu.java.net.nocache_protocol_handlers");
+ if (s == null)
+ cache_handlers = true;
+ else
+ cache_handlers = false;
+ }
/**
* Constructs a URL and loads a protocol handler for the values passed as
@@ -213,7 +235,7 @@ public final class URL implements Serializable
* @param port The port number to use, or -1 to use the protocol's default
* port
* @param file The "file" portion of the URL.
- * @param handler The protocol handler to use with this URL.
+ * @param ph The protocol handler to use with this URL.
*
* @exception MalformedURLException If no protocol handler can be loaded
* for the specified protocol.
@@ -222,8 +244,8 @@ public final class URL implements Serializable
*
* @since 1.2
*/
- public URL(String protocol, String host, int port, String file,
- URLStreamHandler ph)
+ public URL (String protocol, String host, int port, String file,
+ URLStreamHandler ph)
throws MalformedURLException
{
if (protocol == null)
@@ -320,7 +342,7 @@ public final class URL implements Serializable
*
* @param context The context in which to parse the specification
* @param spec The string to parse as an URL
- * @param handler The stream handler for the URL
+ * @param ph The stream handler for the URL
*
* @exception MalformedURLException If a protocol handler cannot be found
* or the URL cannot be parsed
@@ -718,18 +740,28 @@ public final class URL implements Serializable
return ph.toExternalForm(this);
}
+ /**
+ * This internal method is used in two different constructors to load
+ * a protocol handler for this URL.
+ *
+ * @param protocol The protocol to load a handler for
+ *
+ * @return A URLStreamHandler for this protocol, or null when not found.
+ */
private static synchronized URLStreamHandler
getURLStreamHandler (String protocol)
{
URLStreamHandler ph;
// See if a handler has been cached for this protocol.
- if ((ph = (URLStreamHandler) handlers.get(protocol)) != null)
+ if ((ph = (URLStreamHandler) ph_cache.get(protocol)) != null)
return ph;
// If a non-default factory has been set, use it to find the protocol.
if (factory != null)
- ph = factory.createURLStreamHandler(protocol);
+ {
+ ph = factory.createURLStreamHandler(protocol);
+ }
else if (protocol.equals ("core"))
{
ph = new gnu.gcj.protocol.core.Handler ();
@@ -780,9 +812,10 @@ public final class URL implements Serializable
}
// Update the hashtable with the new protocol handler.
- if (ph != null)
+ if (ph != null
+ && cache_handlers)
if (ph instanceof URLStreamHandler)
- handlers.put(protocol, ph);
+ ph_cache.put(protocol, ph);
else
ph = null;
diff --git a/libjava/java/net/URLClassLoader.java b/libjava/java/net/URLClassLoader.java
index 10b67352f04..e1c789ddd64 100644
--- a/libjava/java/net/URLClassLoader.java
+++ b/libjava/java/net/URLClassLoader.java
@@ -1,5 +1,5 @@
/* URLClassLoader.java -- ClassLoader that loads classes from one or more URLs
- Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -59,6 +59,7 @@ import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
+import gnu.gcj.runtime.SharedLibHelper;
/**
* A secure class loader that can load classes and resources from
@@ -194,6 +195,17 @@ public class URLClassLoader extends SecureClassLoader
}
/**
+ * Returns a <code>Class</code> loaded by this
+ * <code>URLLoader</code>, or <code>null</code> when this loader
+ * either can't load the class or doesn't know how to load classes
+ * at all.
+ */
+ Class getClass(String className)
+ {
+ return null;
+ }
+
+ /**
* Returns a <code>Resource</code> loaded by this
* <code>URLLoader</code>, or <code>null</code> when no
* <code>Resource</code> with the given name exists.
@@ -282,7 +294,7 @@ public class URLClassLoader extends SecureClassLoader
{
super(classloader, baseURL);
- // cache url prefix for all resources in this jar url
+ // Cache url prefix for all resources in this jar url.
String external = baseURL.toExternalForm();
StringBuffer sb = new StringBuffer(external.length() + 6);
sb.append("jar:");
@@ -311,6 +323,9 @@ public class URLClassLoader extends SecureClassLoader
if (jarfile == null)
return null;
+ if (name.startsWith("/"))
+ name = name.substring(1);
+
JarEntry je = jarfile.getJarEntry(name);
if(je != null)
return new JarURLResource(this, name, je);
@@ -445,12 +460,12 @@ public class URLClassLoader extends SecureClassLoader
{
return stream;
}
-
+
public int getLength()
{
return length;
}
-
+
public URL getURL()
{
return url;
@@ -458,6 +473,63 @@ public class URLClassLoader extends SecureClassLoader
}
/**
+ * A <code>SoURLLoader</code> is a type of <code>URLLoader</code>
+ * that loads classes and resources from a shared library.
+ */
+ final static class SoURLLoader extends URLLoader
+ {
+ SharedLibHelper helper;
+
+ SoURLLoader(URLClassLoader classloader, URL url)
+ {
+ super(classloader, url);
+ helper = SharedLibHelper.findHelper(classloader, url.getFile(),
+ noCertCodeSource);
+ }
+
+ Class getClass(String className)
+ {
+ return helper.findClass(className);
+ }
+
+ Resource getResource(String name)
+ {
+ URL url = helper.findResource(name);
+ if (url == null)
+ return null;
+ return new SoResource(this, name, url);
+ }
+ }
+
+ final static class SoResource extends Resource
+ {
+ SoResource(SoURLLoader loader, String name, URL url)
+ {
+ super(loader, name);
+ this.url = url;
+ }
+
+ InputStream getInputStream() throws IOException
+ {
+ URLConnection conn = url.openConnection();
+ return conn.getInputStream();
+ }
+
+ public int getLength()
+ {
+ // FIXME we could find this by asking the core object.
+ return -1;
+ }
+
+ public URL getURL ()
+ {
+ return url;
+ }
+
+ final URL url;
+ }
+
+ /**
* A <code>FileURLLoader</code> is a type of <code>URLLoader</code>
* only loading from file url.
*/
@@ -641,7 +713,7 @@ public class URLClassLoader extends SecureClassLoader
// for cache initial size
synchronized(factoryCache)
{
- if(factory != null && factoryCache.get(factory) == null)
+ if (factory != null && factoryCache.get(factory) == null)
factoryCache.put(factory, new HashMap(5));
}
}
@@ -654,26 +726,34 @@ public class URLClassLoader extends SecureClassLoader
*/
protected void addURL(URL newUrl)
{
+ addURLImpl(newUrl);
+ }
+
+ private void addURLImpl(URL newUrl)
+ {
synchronized(urlloaders)
{
if (newUrl == null)
return; // Silently ignore...
- // check global cache to see if there're already url loader
- // for this url
+ // Check global cache to see if there're already url loader
+ // for this url.
URLLoader loader = (URLLoader)urlloaders.get(newUrl);
if (loader == null)
{
String file = newUrl.getFile();
+ String protocol = newUrl.getProtocol();
// Check that it is not a directory
- if (! (file.endsWith("/") || file.endsWith(File.separator)))
+ if ("gcjlib".equals(protocol))
+ loader = new SoURLLoader(this, newUrl);
+ else if (! (file.endsWith("/") || file.endsWith(File.separator)))
loader = new JarURLLoader(this, newUrl);
- else if ("file".equals(newUrl.getProtocol()))
+ else if ("file".equals(protocol))
loader = new FileURLLoader(this, newUrl);
else
loader = new RemoteURLLoader(this, newUrl);
- // cache it
+ // Cache it.
urlloaders.put(newUrl, loader);
}
@@ -690,7 +770,7 @@ public class URLClassLoader extends SecureClassLoader
{
for (int i = 0; i < newUrls.length; i++)
{
- addURL(newUrls[i]);
+ addURLImpl(newUrls[i]);
}
}
@@ -756,7 +836,20 @@ public class URLClassLoader extends SecureClassLoader
{
// Just try to find the resource by the (almost) same name
String resourceName = className.replace('.', '/') + ".class";
- Resource resource = findURLResource(resourceName);
+ int max = urls.size();
+ Resource resource = null;
+ for (int i = 0; i < max && resource == null; i++)
+ {
+ URLLoader loader = (URLLoader)urlinfos.elementAt(i);
+ if (loader == null)
+ continue;
+
+ Class k = loader.getClass(className);
+ if (k != null)
+ return k;
+
+ resource = loader.getResource(resourceName);
+ }
if (resource == null)
throw new ClassNotFoundException(className + " not found in " + urls);
@@ -899,12 +992,12 @@ public class URLClassLoader extends SecureClassLoader
URLStreamHandler handler;
synchronized (factoryCache)
{
- // check if there're handler for the same protocol in cache
+ // Check if there're handler for the same protocol in cache.
HashMap cache = (HashMap)factoryCache.get(factory);
handler = (URLStreamHandler)cache.get(protocol);
if(handler == null)
{
- // add it to cache
+ // Add it to cache.
handler = factory.createURLStreamHandler(protocol);
cache.put(protocol, handler);
}
@@ -963,23 +1056,23 @@ public class URLClassLoader extends SecureClassLoader
// First get the permissions that would normally be granted
PermissionCollection permissions = super.getPermissions(source);
- // Now add the any extra permissions depending on the URL location
+ // Now add any extra permissions depending on the URL location.
URL url = source.getLocation();
String protocol = url.getProtocol();
if (protocol.equals("file"))
{
String file = url.getFile();
- // If the file end in / it must be an directory
+ // If the file end in / it must be an directory.
if (file.endsWith("/") || file.endsWith(File.separator))
{
// Grant permission to read everything in that directory and
- // all subdirectories
+ // all subdirectories.
permissions.add(new FilePermission(file + "-", "read"));
}
else
{
- // It is a 'normal' file
- // Grant permission to access that file
+ // It is a 'normal' file.
+ // Grant permission to access that file.
permissions.add(new FilePermission(file, "read"));
}
}
diff --git a/libjava/java/net/URLEncoder.java b/libjava/java/net/URLEncoder.java
index f03b8a1d8f8..24d0d25b6d8 100644
--- a/libjava/java/net/URLEncoder.java
+++ b/libjava/java/net/URLEncoder.java
@@ -1,5 +1,5 @@
/* URLEncoder.java -- Class to convert strings to a properly encoded URL
- Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -53,7 +53,8 @@ import java.io.UnsupportedEncodingException;
* US alphabet remain as is, the space character (' ') is replaced with
* '+' sign, and all other characters are converted to a "%XX" format
* where XX is the hexadecimal representation of that character in a
- * certain encoding (by default "UTF-8").
+ * certain encoding (by default, the platform encoding, though the
+ * standard is "UTF-8").
* <p>
* This method is very useful for encoding strings to be sent to CGI scripts
*
@@ -65,8 +66,9 @@ public class URLEncoder
{
/**
* This method translates the passed in string into x-www-form-urlencoded
- * format using the standard "UTF-8" character encoding to hex-encode the
- * unsafe characters.
+ * format using the default encoding. The standard encoding is
+ * "UTF-8", and the two-argument form of this method should be used
+ * instead.
*
* @param s The String to convert
*
@@ -78,11 +80,13 @@ public class URLEncoder
{
try
{
- return encode(s, "UTF-8");
+ // We default to 8859_1 for compatibility with the same
+ // default elsewhere in the library.
+ return encode(s, System.getProperty("file.encoding", "8859_1"));
}
catch (UnsupportedEncodingException uee)
{
- // Should never happen since UTF-8 should always be supported
+ // Should never happen since default should always be supported
return s;
}
}
@@ -139,7 +143,9 @@ public class URLEncoder
for (int j = 0; j < bytes.length; j++)
{
result.append('%');
- result.append(Integer.toHexString(((int) bytes[j]) & 0xFF));
+ int val = bytes[j];
+ result.append(hex.charAt((val & 0xf0) >> 4));
+ result.append(hex.charAt(val & 0x0f));
}
}
start = i;
@@ -166,4 +172,11 @@ public class URLEncoder
*/
private URLEncoder() { }
+ /**
+ * Used to convert to hex. We don't use Integer.toHexString, since
+ * it converts to lower case (and the Sun docs pretty clearly
+ * specify upper case here), and because it doesn't provide a
+ * leading 0.
+ */
+ private static final String hex = "0123456789ABCDEF";
} // class URLEncoder
diff --git a/libjava/java/net/URLStreamHandler.java b/libjava/java/net/URLStreamHandler.java
index 93a8ab27814..61b466cce6d 100644
--- a/libjava/java/net/URLStreamHandler.java
+++ b/libjava/java/net/URLStreamHandler.java
@@ -196,7 +196,11 @@ public abstract class URLStreamHandler
// need to canonicalise the file path.
try
{
+ boolean endsWithSlash = file.charAt(file.length() - 1) == '/';
file = new File (file).getCanonicalPath ();
+ if (endsWithSlash
+ && file.charAt(file.length() - 1) != '/')
+ file += '/';
}
catch (IOException e)
{
diff --git a/libjava/java/net/natInetAddressWin32.cc b/libjava/java/net/natInetAddressWin32.cc
index f6748fd5691..42c7d7db9e8 100644
--- a/libjava/java/net/natInetAddressWin32.cc
+++ b/libjava/java/net/natInetAddressWin32.cc
@@ -7,124 +7,26 @@ Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */
#include <config.h>
+#include <platform.h>
-#ifdef WIN32
-
-#include <windows.h>
-#include <winsock.h>
#undef STRICT
-#ifndef MAXHOSTNAMELEN
-#define MAXHOSTNAMELEN 64
-#endif /* MAXHOSTNAMELEN */
-
-#else /* WIN32 */
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#include <string.h>
-#include <errno.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
-
-#endif /* WIN32 */
-
-#include <gcj/cni.h>
-#include <jvm.h>
#include <java/net/InetAddress.h>
#include <java/net/UnknownHostException.h>
#include <java/lang/SecurityException.h>
-#if defined(HAVE_UNAME) && ! defined(HAVE_GETHOSTNAME)
-#include <sys/utsname.h>
-#endif
-
-#ifndef HAVE_GETHOSTNAME_DECL
-extern "C" int gethostname (char *name, int namelen);
-#endif
-
-#ifdef DISABLE_JAVA_NET
-
-jbyteArray
-java::net::InetAddress::aton (jstring)
-{
- return NULL;
-}
-
-jint
-java::net::InetAddress::getFamily (jbyteArray bytes)
-{
- return 0;
-}
-
-JArray<java::net::InetAddress*> *
-java::net::InetAddress::lookup (jstring, java::net::InetAddress *, jboolean)
-{
- return NULL;
-}
-
-jstring
-java::net::InetAddress::getLocalHostname ()
-{
- return NULL;
-}
-
-#else /* DISABLE_JAVA_NET */
-
jbyteArray
java::net::InetAddress::aton (jstring host)
{
- char *hostname;
- char buf[100];
- int len = JvGetStringUTFLength(host);
- if (len < 100)
- hostname = buf;
- else
- hostname = (char*) _Jv_AllocBytes (len+1);
- JvGetStringUTFRegion (host, 0, host->length(), hostname);
- buf[len] = '\0';
+ JV_TEMP_UTF_STRING (hostname, host);
char* bytes = NULL;
int blen = 0;
-#ifdef HAVE_INET_ATON
- struct in_addr laddr;
- if (inet_aton (hostname, &laddr))
+ unsigned long laddr = inet_addr (hostname);
+ if (laddr != INADDR_NONE)
{
bytes = (char*) &laddr;
blen = 4;
}
-#elif defined(HAVE_INET_ADDR)
-#if ! HAVE_IN_ADDR_T
- typedef jint in_addr_t;
-#endif
- in_addr_t laddr = inet_addr (hostname);
- if (laddr != (in_addr_t)(-1))
- {
- bytes = (char*) &laddr;
- blen = 4;
- }
-#endif
-#if defined (HAVE_INET_PTON) && defined (HAVE_INET6)
- char inet6_addr[16];
- if (len != 0 && inet_pton (AF_INET6, hostname, inet6_addr) > 0)
- {
- bytes = inet6_addr;
- blen = 16;
- }
-#endif
if (blen == 0)
return NULL;
jbyteArray result = JvNewByteArray (blen);
@@ -149,69 +51,17 @@ java::net::InetAddress::getFamily (jbyteArray bytes)
JArray<java::net::InetAddress*> *
java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
- jboolean all)
+ jboolean all)
{
struct hostent *hptr = NULL;
-#if defined (HAVE_GETHOSTBYNAME_R) || defined (HAVE_GETHOSTBYADDR_R)
- struct hostent hent_r;
-#if HAVE_STRUCT_HOSTENT_DATA
- struct hostent_data fixed_buffer, *buffer_r = &fixed_buffer;
-#else
-#if defined (__GLIBC__)
- // FIXME: in glibc, gethostbyname_r returns NETDB_INTERNAL to herr and
- // ERANGE to errno if the buffer size is too small, rather than what is
- // expected here. We work around this by setting a bigger buffer size and
- // hoping that it is big enough.
- char fixed_buffer[1024];
-#else
- char fixed_buffer[200];
-#endif
- char *buffer_r = fixed_buffer;
- int size_r = sizeof (fixed_buffer);
-#endif
-#endif
-
if (host != NULL)
{
- char *hostname;
- char buf[100];
- int len = JvGetStringUTFLength(host);
- if (len < 100)
- hostname = buf;
- else
- hostname = (char*) _Jv_AllocBytes (len+1);
- JvGetStringUTFRegion (host, 0, host->length(), hostname);
- buf[len] = '\0';
-#ifdef HAVE_GETHOSTBYNAME_R
- while (true)
- {
- int ok;
-#if HAVE_STRUCT_HOSTENT_DATA
- ok = ! gethostbyname_r (hostname, &hent_r, buffer_r);
-#else
- int herr = 0;
-#ifdef GETHOSTBYNAME_R_RETURNS_INT
- ok = ! gethostbyname_r (hostname, &hent_r, buffer_r, size_r,
- &hptr, &herr);
-#else
- hptr = gethostbyname_r (hostname, &hent_r, buffer_r, size_r, &herr);
- ok = hptr != NULL;
-#endif /* GETHOSTNAME_R_RETURNS_INT */
- if (! ok && herr == ERANGE)
- {
- size_r *= 2;
- buffer_r = (char *) _Jv_AllocBytes (size_r);
- }
- else
-#endif /* HAVE_STRUCT_HOSTENT_DATA */
- break;
- }
-#else
+ JV_TEMP_UTF_STRING (hostname, host);
+
// FIXME: this is insufficient if some other piece of code calls
// this gethostbyname.
JvSynchronize sync (java::net::InetAddress::localhostAddress);
hptr = gethostbyname (hostname);
-#endif /* HAVE_GETHOSTBYNAME_R */
}
else
{
@@ -221,51 +71,24 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
int type;
char *val;
if (len == 4)
- {
- val = chars;
- type = iaddr->family = AF_INET;
- }
+ {
+ val = chars;
+ type = iaddr->family = AF_INET;
+ }
#ifdef HAVE_INET6
else if (len == 16)
- {
- val = (char *) &chars;
- type = iaddr->family = AF_INET6;
- }
+ {
+ val = (char *) &chars;
+ type = iaddr->family = AF_INET6;
+ }
#endif /* HAVE_INET6 */
else
- JvFail ("unrecognized size");
+ JvFail ("unrecognized size");
-#ifdef HAVE_GETHOSTBYADDR_R
- while (true)
- {
- int ok;
-#if HAVE_STRUCT_HOSTENT_DATA
- ok = ! gethostbyaddr_r (val, len, type, &hent_r, buffer_r);
-#else
- int herr = 0;
-#ifdef GETHOSTBYADDR_R_RETURNS_INT
- ok = ! gethostbyaddr_r (val, len, type, &hent_r,
- buffer_r, size_r, &hptr, &herr);
-#else
- hptr = gethostbyaddr_r (val, len, type, &hent_r,
- buffer_r, size_r, &herr);
- ok = hptr != NULL;
-#endif /* GETHOSTBYADDR_R_RETURNS_INT */
- if (! ok && herr == ERANGE)
- {
- size_r *= 2;
- buffer_r = (char *) _Jv_AllocBytes (size_r);
- }
- else
-#endif /* HAVE_STRUCT_HOSTENT_DATA */
- break;
- }
-#else /* HAVE_GETHOSTBYADDR_R */
// FIXME: this is insufficient if some other piece of code calls
// this gethostbyaddr.
JvSynchronize sync (java::net::InetAddress::localhostAddress);
hptr = gethostbyaddr (val, len, type);
-#endif /* HAVE_GETHOSTBYADDR_R */
}
if (hptr != NULL)
{
@@ -273,22 +96,23 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
host = JvNewStringUTF (hptr->h_name);
java::lang::SecurityException *ex = checkConnect (host);
if (ex != NULL)
- {
- if (iaddr == NULL || iaddr->addr == NULL)
- throw ex;
- hptr = NULL;
- }
+ {
+ if (iaddr == NULL || iaddr->addr == NULL)
+ throw ex;
+ hptr = NULL;
+ }
}
if (hptr == NULL)
{
if (iaddr != NULL && iaddr->addr != NULL)
- {
- iaddr->hostName = iaddr->getHostAddress();
- return NULL;
- }
+ {
+ iaddr->hostName = iaddr->getHostAddress();
+ return NULL;
+ }
else
- throw new java::net::UnknownHostException(host);
+ throw new java::net::UnknownHostException(host);
}
+
int count;
if (all)
{
@@ -298,6 +122,7 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
}
else
count = 1;
+
JArray<java::net::InetAddress*> *result;
java::net::InetAddress** iaddrs;
if (all)
@@ -314,42 +139,30 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
for (int i = 0; i < count; i++)
{
if (iaddrs[i] == NULL)
- iaddrs[i] = new java::net::InetAddress (NULL, NULL);
+ iaddrs[i] = new java::net::InetAddress (NULL, NULL);
if (iaddrs[i]->hostName == NULL)
iaddrs[i]->hostName = host;
if (iaddrs[i]->addr == NULL)
- {
- char *bytes = hptr->h_addr_list[i];
- iaddrs[i]->addr = JvNewByteArray (hptr->h_length);
- iaddrs[i]->family = getFamily (iaddrs[i]->addr);
- memcpy (elements (iaddrs[i]->addr), bytes, hptr->h_length);
- }
+ {
+ char *bytes = hptr->h_addr_list[i];
+ iaddrs[i]->addr = JvNewByteArray (hptr->h_length);
+ iaddrs[i]->family = getFamily (iaddrs[i]->addr);
+ memcpy (elements (iaddrs[i]->addr), bytes, hptr->h_length);
+ }
}
+
return result;
}
jstring
java::net::InetAddress::getLocalHostname ()
{
- char *chars;
-#ifdef HAVE_GETHOSTNAME
- char buffer[MAXHOSTNAMELEN];
- if (gethostname (buffer, MAXHOSTNAMELEN))
+ char buffer[400];
+ if (gethostname (buffer, sizeof(buffer)))
return NULL;
- chars = buffer;
-#elif HAVE_UNAME
- struct utsname stuff;
- if (uname (&stuff) != 0)
- return NULL;
- chars = stuff.nodename;
-#else
- return NULL;
-#endif
// It is admittedly non-optimal to convert the hostname to Unicode
// only to convert it back in getByName, but simplicity wins. Note
// that unless there is a SecurityManager, we only get called once
// anyway, thanks to the InetAddress.localhost cache.
- return JvNewStringUTF (chars);
+ return JvNewStringUTF (buffer);
}
-
-#endif /* DISABLE_JAVA_NET */
diff --git a/libjava/java/net/natNetworkInterfaceWin32.cc b/libjava/java/net/natNetworkInterfaceWin32.cc
index 47d68b5fd65..20c9a9b5967 100644
--- a/libjava/java/net/natNetworkInterfaceWin32.cc
+++ b/libjava/java/net/natNetworkInterfaceWin32.cc
@@ -9,134 +9,126 @@ details. */
#include <config.h>
#include <platform.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_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/SocketException.h>
#include <java/util/Vector.h>
-#ifdef DISABLE_JAVA_NET
+/* As of this writing, NetworkInterface.java has
+ getName() == getDisplayName() and only one IP address
+ per interface. If this changes, we'll need to use
+ iphlpapi (not supported on Win95) to retrieve richer
+ adapter information via GetAdaptersInfo(). In this
+ module, we provide the necessary hooks to detect the
+ presence of iphlpapi and use it if necessary, but
+ comment things out for now to avoid compiler warnings. */
-::java::util::Vector*
-java::net::NetworkInterface::getRealNetworkInterfaces ()
-{
- ::java::util::Vector* ht = new ::java::util::Vector();
- return ht;
-}
+enum {MAX_INTERFACES = 50};
-#else /* DISABLE_JAVA_NET */
+typedef int
+(*PfnGetRealNetworkInterfaces) (jstring* pjstrName,
+ java::net::InetAddress** ppAddress);
-::java::util::Vector*
-java::net::NetworkInterface::getRealNetworkInterfaces ()
+static int
+winsock2GetRealNetworkInterfaces (jstring* pjstrName,
+ java::net::InetAddress** ppAddress)
{
-#ifdef WIN32
- throw new ::java::net::SocketException;
-#else
- 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 = _Jv_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));
-
+ // FIXME: Add IPv6 support.
+
+ INTERFACE_INFO arInterfaceInfo[MAX_INTERFACES];
+
+ // Open a (random) socket to have a file descriptor for the WSAIoctl call.
+ SOCKET skt = ::socket (AF_INET, SOCK_DGRAM, 0);
+ if (skt == INVALID_SOCKET)
+ _Jv_ThrowSocketException ();
+
+ DWORD dwOutBufSize;
+ int nRetCode = ::WSAIoctl (skt, SIO_GET_INTERFACE_LIST,
+ NULL, 0, &arInterfaceInfo, sizeof(arInterfaceInfo),
+ &dwOutBufSize, NULL, NULL);
+
+ if (nRetCode == SOCKET_ERROR)
+ {
+ DWORD dwLastErrorCode = WSAGetLastError ();
+ ::closesocket (skt);
+ _Jv_ThrowSocketException (dwLastErrorCode);
+ }
+
// Get addresses of all interfaces.
- if_record = if_data.ifc_req;
-
- for (int n = 0; n < if_data.ifc_len; n += sizeof (struct ifreq))
+ int nNbInterfaces = dwOutBufSize / sizeof(INTERFACE_INFO);
+ int nCurETHInterface = 0;
+ for (int i=0; i < nNbInterfaces; ++i)
{
- 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 =
+ SOCKADDR_IN* pAddr = (SOCKADDR_IN*) &arInterfaceInfo[i].iiAddress;
+ memcpy (elements (baddr), &(pAddr->sin_addr), len);
+
+ // Concoct a name for this interface. Since we don't
+ // have access to the real name under Winsock 2, we use
+ // "lo" for the loopback interface and ethX for the
+ // real ones.
+ char szName[30];
+ u_long lFlags = arInterfaceInfo[i].iiFlags;
+
+ if (lFlags & IFF_LOOPBACK)
+ strcpy (szName, "lo");
+ else
+ {
+ strcpy (szName, "eth");
+ wsprintf(szName+3, "%d", nCurETHInterface++);
+ }
+
+ jstring if_name = JvNewStringLatin1 (szName);
+ java::net::Inet4Address* address =
new java::net::Inet4Address (baddr, JvNewStringLatin1 (""));
- ht->add (new NetworkInterface (if_name, address));
- if_record++;
+ pjstrName[i] = if_name;
+ ppAddress[i] = address;
}
-#ifdef HAVE_INET6
- // FIXME: read /proc/net/if_inet6 (on Linux 2.4)
-#endif
-
- _Jv_Free (if_data.ifc_buf);
+ ::closesocket (skt);
- if (fd >= 0)
- _Jv_close (fd);
+ return nNbInterfaces;
+}
+
+/*
+static int
+iphlpapiGetRealNetworkInterfaces (jstring* pjstrName,
+ java::net::InetAddress** ppAddress)
+{
+ return 0;
+}
+*/
+
+static PfnGetRealNetworkInterfaces
+determineGetRealNetworkInterfacesFN ()
+{
+ /* FIXME: Try to dynamically load iphlpapi.dll and
+ detect the presence of GetAdaptersInfo() using
+ GetProcAddress(). If successful, return
+ iphlpapiGetRealNetworkInterfaces; if not,
+ return winsock2GetRealNetworkInterfaces */
+ return &winsock2GetRealNetworkInterfaces;
+}
+
+::java::util::Vector*
+java::net::NetworkInterface::getRealNetworkInterfaces ()
+{
+ static PfnGetRealNetworkInterfaces pfn =
+ determineGetRealNetworkInterfacesFN ();
+
+ jstring arIFName[MAX_INTERFACES];
+ InetAddress* arpInetAddress[MAX_INTERFACES];
+ ::java::util::Vector* ht = new ::java::util::Vector ();
+ int nNbInterfaces = (*pfn) (arIFName, arpInetAddress);
+ for (int i=0; i < nNbInterfaces; ++i)
+ {
+ ht->add (new java::net::NetworkInterface (arIFName[i],
+ arpInetAddress[i]));
+ }
+
return ht;
-#endif /* WIN32 */
}
-
-#endif // DISABLE_JAVA_NET //
diff --git a/libjava/java/net/natPlainDatagramSocketImplNoNet.cc b/libjava/java/net/natPlainDatagramSocketImplNoNet.cc
deleted file mode 100644
index a2a08d719c4..00000000000
--- a/libjava/java/net/natPlainDatagramSocketImplNoNet.cc
+++ /dev/null
@@ -1,119 +0,0 @@
-/* Copyright (C) 2003 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>
-#include <platform.h>
-
-#include <java/io/IOException.h>
-#include <java/lang/Object.h>
-#include <java/net/BindException.h>
-#include <java/net/DatagramPacket.h>
-#include <java/net/InetAddress.h>
-#include <java/net/NetworkInterface.h>
-#include <java/net/PlainDatagramSocketImpl.h>
-#include <java/net/SocketException.h>
-
-void
-java::net::PlainDatagramSocketImpl::create ()
-{
- throw new SocketException (
- JvNewStringLatin1 ("DatagramSocketImpl.create: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::bind (jint, java::net::InetAddress *)
-{
- throw new BindException (
- JvNewStringLatin1 ("DatagramSocketImpl.bind: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::connect (java::net::InetAddress *, jint)
-{
- throw new SocketException (
- JvNewStringLatin1 ("DatagramSocketImpl.connect: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::disconnect ()
-{
- throw new SocketException (
- JvNewStringLatin1 ("DatagramSocketImpl.disconnect: unimplemented"));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.peek: unimplemented"));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::peekData(java::net::DatagramPacket *)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.peekData: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::close ()
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.close: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.send: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::receive (java::net::DatagramPacket *)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.receive: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::setTimeToLive (jint)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.setTimeToLive: unimplemented"));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::getTimeToLive ()
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.getTimeToLive: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::mcastGrp (java::net::InetAddress *,
- java::net::NetworkInterface *,
- jboolean)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.mcastGrp: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::setOption (jint, java::lang::Object *)
-{
- throw new SocketException (
- JvNewStringLatin1 ("DatagramSocketImpl.setOption: unimplemented"));
-}
-
-java::lang::Object *
-java::net::PlainDatagramSocketImpl::getOption (jint)
-{
- throw new SocketException (
- JvNewStringLatin1 ("DatagramSocketImpl.getOption: unimplemented"));
-}
diff --git a/libjava/java/net/natPlainDatagramSocketImplPosix.cc b/libjava/java/net/natPlainDatagramSocketImplPosix.cc
deleted file mode 100644
index 14f6fee2df0..00000000000
--- a/libjava/java/net/natPlainDatagramSocketImplPosix.cc
+++ /dev/null
@@ -1,750 +0,0 @@
-/* Copyright (C) 2003 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>
-#include <platform.h>
-
-#ifdef HAVE_NETINET_IN_H
-#include <netinet/in.h>
-#endif
-#ifdef HAVE_ARPA_INET_H
-#include <arpa/inet.h>
-#endif
-#include <errno.h>
-#include <string.h>
-
-#if HAVE_BSTRING_H
-// Needed for bzero, implicitly used by FD_ZERO on IRIX 5.2
-#include <bstring.h>
-#endif
-
-#include <gcj/cni.h>
-#include <java/io/IOException.h>
-#include <java/io/InterruptedIOException.h>
-#include <java/net/BindException.h>
-#include <java/net/SocketException.h>
-#include <java/net/PlainDatagramSocketImpl.h>
-#include <java/net/InetAddress.h>
-#include <java/net/NetworkInterface.h>
-#include <java/net/DatagramPacket.h>
-#include <java/net/PortUnreachableException.h>
-#include <java/lang/InternalError.h>
-#include <java/lang/Object.h>
-#include <java/lang/Boolean.h>
-#include <java/lang/Integer.h>
-
-union SockAddr
-{
- struct sockaddr_in address;
-#ifdef HAVE_INET6
- struct sockaddr_in6 address6;
-#endif
-};
-
-union McastReq
-{
-#if HAVE_STRUCT_IP_MREQ
- struct ip_mreq mreq;
-#endif
-#if HAVE_STRUCT_IPV6_MREQ
- struct ipv6_mreq mreq6;
-#endif
-};
-
-union InAddr
-{
- struct in_addr addr;
-#ifdef HAVE_INET6
- struct in6_addr addr6;
-#endif
-};
-
-
-// FIXME: routines here and/or in natPlainSocketImpl.cc could throw
-// NoRouteToHostException; also consider UnknownHostException, ConnectException.
-
-void
-java::net::PlainDatagramSocketImpl::create ()
-{
- int sock = _Jv_socket (AF_INET, SOCK_DGRAM, 0);
-
- if (sock < 0)
- {
- char* strerr = strerror (errno);
- throw new java::net::SocketException (JvNewStringUTF (strerr));
- }
-
- _Jv_platform_close_on_exec (sock);
-
- // We use fnum in place of fd here. From leaving fd null we avoid
- // the double close problem in FileDescriptor.finalize.
- fnum = sock;
-}
-
-void
-java::net::PlainDatagramSocketImpl::bind (jint lport,
- java::net::InetAddress *host)
-{
- union SockAddr u;
- struct sockaddr *ptr = (struct sockaddr *) &u.address;
- // FIXME: Use getaddrinfo() to get actual protocol instead of assuming ipv4.
- jbyteArray haddress = host->addr;
- jbyte *bytes = elements (haddress);
- int len = haddress->length;
-
- if (len == 4)
- {
- u.address.sin_family = AF_INET;
-
- if (host != NULL)
- memcpy (&u.address.sin_addr, bytes, len);
- else
- u.address.sin_addr.s_addr = htonl (INADDR_ANY);
-
- len = sizeof (struct sockaddr_in);
- u.address.sin_port = htons (lport);
- }
-#ifdef HAVE_INET6
- else if (len == 16)
- {
- u.address6.sin6_family = AF_INET6;
- memcpy (&u.address6.sin6_addr, bytes, len);
- len = sizeof (struct sockaddr_in6);
- u.address6.sin6_port = htons (lport);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
- if (_Jv_bind (fnum, ptr, len) == 0)
- {
- socklen_t addrlen = sizeof(u);
-
- if (lport != 0)
- localPort = lport;
- else if (::getsockname (fnum, (sockaddr*) &u, &addrlen) == 0)
- localPort = ntohs (u.address.sin_port);
- else
- goto error;
-
- /* Allow broadcast by default. */
- int broadcast = 1;
- if (::setsockopt (fnum, SOL_SOCKET, SO_BROADCAST, (char *) &broadcast,
- sizeof (broadcast)) != 0)
- goto error;
-
- return;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::BindException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainDatagramSocketImpl::connect (java::net::InetAddress *, jint)
-{
- throw new ::java::lang::InternalError (JvNewStringLatin1 (
- "PlainDatagramSocketImpl::connect: not implemented yet"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::disconnect ()
-{
- throw new ::java::lang::InternalError (JvNewStringLatin1 (
- "PlainDatagramSocketImpl::disconnect: not implemented yet"));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *i)
-{
- // FIXME: Deal with Multicast and if the socket is connected.
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- ssize_t retlen =
- ::recvfrom (fnum, (char *) NULL, 0, MSG_PEEK, (sockaddr*) &u,
- &addrlen);
- if (retlen < 0)
- goto error;
- // FIXME: Deal with Multicast addressing and if the socket is connected.
- jbyteArray raddr;
- jint rport;
- if (u.address.sin_family == AF_INET)
- {
- raddr = JvNewByteArray (4);
- memcpy (elements (raddr), &u.address.sin_addr, 4);
- rport = ntohs (u.address.sin_port);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- raddr = JvNewByteArray (16);
- memcpy (elements (raddr), &u.address6.sin6_addr, 16);
- rport = ntohs (u.address6.sin6_port);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid family"));
-
- i->addr = raddr;
- return rport;
- error:
- char* strerr = strerror (errno);
-
- if (errno == ECONNREFUSED)
- throw new PortUnreachableException (JvNewStringUTF (strerr));
-
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::peekData(java::net::DatagramPacket *p)
-{
- // FIXME: Deal with Multicast and if the socket is connected.
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- jbyte *dbytes = elements (p->getData());
- ssize_t retlen = 0;
-
- // Do timeouts via select since SO_RCVTIMEO is not always available.
- if (timeout > 0 && fnum >= 0 && fnum < FD_SETSIZE)
- {
- fd_set rset;
- struct timeval tv;
- FD_ZERO(&rset);
- FD_SET(fnum, &rset);
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout % 1000) * 1000;
- int retval;
- if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
- goto error;
- else if (retval == 0)
- throw new java::io::InterruptedIOException ();
- }
-
- retlen =
- ::recvfrom (fnum, (char *) dbytes, p->getLength(), MSG_PEEK, (sockaddr*) &u,
- &addrlen);
- if (retlen < 0)
- goto error;
- // FIXME: Deal with Multicast addressing and if the socket is connected.
- jbyteArray raddr;
- jint rport;
- if (u.address.sin_family == AF_INET)
- {
- raddr = JvNewByteArray (4);
- memcpy (elements (raddr), &u.address.sin_addr, 4);
- rport = ntohs (u.address.sin_port);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- raddr = JvNewByteArray (16);
- memcpy (elements (raddr), &u.address6.sin6_addr, 16);
- rport = ntohs (u.address6.sin6_port);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid family"));
-
- p->setAddress (new InetAddress (raddr, NULL));
- p->setPort (rport);
- p->setLength ((jint) retlen);
- return rport;
-
- error:
- char* strerr = strerror (errno);
-
- if (errno == ECONNREFUSED)
- throw new PortUnreachableException (JvNewStringUTF (strerr));
-
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-// Close(shutdown) the socket.
-void
-java::net::PlainDatagramSocketImpl::close ()
-{
- // Avoid races from asynchronous finalization.
- JvSynchronize sync (this);
-
- // The method isn't declared to throw anything, so we disregard
- // the return value.
- _Jv_close (fnum);
- fnum = -1;
- timeout = 0;
-}
-
-void
-java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *p)
-{
- // FIXME: Deal with Multicast and if the socket is connected.
- jint rport = p->getPort();
- union SockAddr u;
- jbyteArray haddress = p->getAddress()->addr;
- jbyte *bytes = elements (haddress);
- int len = haddress->length;
- struct sockaddr *ptr = (struct sockaddr *) &u.address;
- jbyte *dbytes = elements (p->getData());
- if (len == 4)
- {
- u.address.sin_family = AF_INET;
- memcpy (&u.address.sin_addr, bytes, len);
- len = sizeof (struct sockaddr_in);
- u.address.sin_port = htons (rport);
- }
-#ifdef HAVE_INET6
- else if (len == 16)
- {
- u.address6.sin6_family = AF_INET6;
- memcpy (&u.address6.sin6_addr, bytes, len);
- len = sizeof (struct sockaddr_in6);
- u.address6.sin6_port = htons (rport);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
- if (::sendto (fnum, (char *) dbytes, p->getLength(), 0, ptr, len) >= 0)
- return;
-
- char* strerr = strerror (errno);
-
- if (errno == ECONNREFUSED)
- throw new PortUnreachableException (JvNewStringUTF (strerr));
-
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainDatagramSocketImpl::receive (java::net::DatagramPacket *p)
-{
- // FIXME: Deal with Multicast and if the socket is connected.
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- jbyte *dbytes = elements (p->getData());
- ssize_t retlen = 0;
-
- // Do timeouts via select since SO_RCVTIMEO is not always available.
- if (timeout > 0 && fnum >= 0 && fnum < FD_SETSIZE)
- {
- fd_set rset;
- struct timeval tv;
- FD_ZERO(&rset);
- FD_SET(fnum, &rset);
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout % 1000) * 1000;
- int retval;
- if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
- goto error;
- else if (retval == 0)
- throw new java::io::InterruptedIOException ();
- }
-
- retlen =
- ::recvfrom (fnum, (char *) dbytes, p->getLength(), 0, (sockaddr*) &u,
- &addrlen);
- if (retlen < 0)
- goto error;
- // FIXME: Deal with Multicast addressing and if the socket is connected.
- jbyteArray raddr;
- jint rport;
- if (u.address.sin_family == AF_INET)
- {
- raddr = JvNewByteArray (4);
- memcpy (elements (raddr), &u.address.sin_addr, 4);
- rport = ntohs (u.address.sin_port);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- raddr = JvNewByteArray (16);
- memcpy (elements (raddr), &u.address6.sin6_addr, 16);
- rport = ntohs (u.address6.sin6_port);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid family"));
-
- p->setAddress (new InetAddress (raddr, NULL));
- p->setPort (rport);
- p->setLength ((jint) retlen);
- return;
-
- error:
- char* strerr = strerror (errno);
-
- if (errno == ECONNREFUSED)
- throw new PortUnreachableException (JvNewStringUTF (strerr));
-
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainDatagramSocketImpl::setTimeToLive (jint ttl)
-{
- // Assumes IPPROTO_IP rather than IPPROTO_IPV6 since socket created is IPv4.
- char val = (char) ttl;
- socklen_t val_len = sizeof(val);
-
- if (::setsockopt (fnum, IPPROTO_IP, IP_MULTICAST_TTL, &val, val_len) == 0)
- return;
-
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::getTimeToLive ()
-{
- // Assumes IPPROTO_IP rather than IPPROTO_IPV6 since socket created is IPv4.
- char val;
- socklen_t val_len = sizeof(val);
-
- if (::getsockopt (fnum, IPPROTO_IP, IP_MULTICAST_TTL, &val, &val_len) == 0)
- return ((int) val) & 0xFF;
-
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainDatagramSocketImpl::mcastGrp (java::net::InetAddress *inetaddr,
- java::net::NetworkInterface *,
- jboolean join)
-{
- // FIXME: implement use of NetworkInterface
-
- union McastReq u;
- jbyteArray haddress = inetaddr->addr;
- jbyte *bytes = elements (haddress);
- int len = haddress->length;
- int level, opname;
- const char *ptr;
- if (0)
- ;
-#if HAVE_STRUCT_IP_MREQ
- else if (len == 4)
- {
- level = IPPROTO_IP;
- opname = join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
- memcpy (&u.mreq.imr_multiaddr, bytes, len);
- // FIXME: If a non-default interface is set, use it; see Stevens p. 501.
- // Maybe not, see note in last paragraph at bottom of Stevens p. 497.
- u.mreq.imr_interface.s_addr = htonl (INADDR_ANY);
- len = sizeof (struct ip_mreq);
- ptr = (const char *) &u.mreq;
- }
-#endif
-#if HAVE_STRUCT_IPV6_MREQ
- else if (len == 16)
- {
- level = IPPROTO_IPV6;
-
- /* Prefer new RFC 2553 names. */
-#ifndef IPV6_JOIN_GROUP
-#define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
-#endif
-#ifndef IPV6_LEAVE_GROUP
-#define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
-#endif
-
- opname = join ? IPV6_JOIN_GROUP : IPV6_LEAVE_GROUP;
- memcpy (&u.mreq6.ipv6mr_multiaddr, bytes, len);
- // FIXME: If a non-default interface is set, use it; see Stevens p. 501.
- // Maybe not, see note in last paragraph at bottom of Stevens p. 497.
- u.mreq6.ipv6mr_interface = 0;
- len = sizeof (struct ipv6_mreq);
- ptr = (const char *) &u.mreq6;
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
- if (::setsockopt (fnum, level, opname, ptr, len) == 0)
- return;
-
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainDatagramSocketImpl::setOption (jint optID,
- java::lang::Object *value)
-{
- int val;
- socklen_t val_len = sizeof (val);
-
- if (fnum < 0)
- throw new java::net::SocketException (JvNewStringUTF ("Socket closed"));
-
- if (_Jv_IsInstanceOf (value, &java::lang::Boolean::class$))
- {
- java::lang::Boolean *boolobj =
- static_cast<java::lang::Boolean *> (value);
- val = boolobj->booleanValue() ? 1 : 0;
- }
- else if (_Jv_IsInstanceOf (value, &java::lang::Integer::class$))
- {
- java::lang::Integer *intobj =
- static_cast<java::lang::Integer *> (value);
- val = (int) intobj->intValue();
- }
- // Else assume value to be an InetAddress for use with IP_MULTICAST_IF.
-
- switch (optID)
- {
- case _Jv_TCP_NODELAY_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("TCP_NODELAY not valid for UDP"));
- return;
- case _Jv_SO_LINGER_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_LINGER not valid for UDP"));
- return;
- case _Jv_SO_KEEPALIVE_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_KEEPALIVE not valid for UDP"));
- return;
-
- case _Jv_SO_BROADCAST_ :
- if (::setsockopt (fnum, SOL_SOCKET, SO_BROADCAST, (char *) &val,
- val_len) != 0)
- goto error;
- break;
-
- case _Jv_SO_OOBINLINE_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_OOBINLINE: not valid for UDP"));
- break;
-
- case _Jv_SO_SNDBUF_ :
- case _Jv_SO_RCVBUF_ :
-#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
- int opt;
- optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
- if (::setsockopt (fnum, SOL_SOCKET, opt, (char *) &val, val_len) != 0)
- goto error;
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
-#endif
- return;
- case _Jv_SO_REUSEADDR_ :
-#if defined(SO_REUSEADDR)
- if (::setsockopt (fnum, SOL_SOCKET, SO_REUSEADDR, (char *) &val,
- val_len) != 0)
- goto error;
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_REUSEADDR not supported"));
-#endif
- return;
- case _Jv_SO_BINDADDR_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_BINDADDR: read only option"));
- return;
- case _Jv_IP_MULTICAST_IF_ :
- union InAddr u;
- jbyteArray haddress;
- jbyte *bytes;
- int len;
- int level, opname;
- const char *ptr;
-
- haddress = ((java::net::InetAddress *) value)->addr;
- bytes = elements (haddress);
- len = haddress->length;
- if (len == 4)
- {
- level = IPPROTO_IP;
- opname = IP_MULTICAST_IF;
- memcpy (&u.addr, bytes, len);
- len = sizeof (struct in_addr);
- ptr = (const char *) &u.addr;
- }
-// Tru64 UNIX V5.0 has struct sockaddr_in6, but no IPV6_MULTICAST_IF
-#if defined (HAVE_INET6) && defined (IPV6_MULTICAST_IF)
- else if (len == 16)
- {
- level = IPPROTO_IPV6;
- opname = IPV6_MULTICAST_IF;
- memcpy (&u.addr6, bytes, len);
- len = sizeof (struct in6_addr);
- ptr = (const char *) &u.addr6;
- }
-#endif
- else
- throw
- new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
- if (::setsockopt (fnum, level, opname, ptr, len) != 0)
- goto error;
- return;
-
- case _Jv_IP_MULTICAST_IF2_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_IF2: not yet implemented"));
- break;
-
- case _Jv_IP_MULTICAST_LOOP_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_LOOP: not yet implemented"));
- break;
-
- case _Jv_IP_TOS_ :
- if (::setsockopt (fnum, SOL_SOCKET, IP_TOS, (char *) &val,
- val_len) != 0)
- goto error;
- return;
-
- case _Jv_SO_TIMEOUT_ :
- timeout = val;
- return;
- default :
- errno = ENOPROTOOPT;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::SocketException (JvNewStringUTF (strerr));
-}
-
-java::lang::Object *
-java::net::PlainDatagramSocketImpl::getOption (jint optID)
-{
- int val;
- socklen_t val_len = sizeof(val);
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
-
- switch (optID)
- {
- case _Jv_TCP_NODELAY_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("TCP_NODELAY not valid for UDP"));
- break;
- case _Jv_SO_LINGER_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_LINGER not valid for UDP"));
- break;
- case _Jv_SO_KEEPALIVE_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_KEEPALIVE not valid for UDP"));
- break;
-
- case _Jv_SO_BROADCAST_ :
- if (::getsockopt (fnum, SOL_SOCKET, SO_BROADCAST, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Boolean (val != 0);
-
- case _Jv_SO_OOBINLINE_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_OOBINLINE not valid for UDP"));
- break;
-
- case _Jv_SO_RCVBUF_ :
- case _Jv_SO_SNDBUF_ :
-#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
- int opt;
- optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
- if (::getsockopt (fnum, SOL_SOCKET, opt, (char *) &val, &val_len) != 0)
- goto error;
- else
- return new java::lang::Integer (val);
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
-#endif
- break;
- case _Jv_SO_BINDADDR_:
- // cache the local address
- if (localAddress == NULL)
- {
- jbyteArray laddr;
- if (::getsockname (fnum, (sockaddr*) &u, &addrlen) != 0)
- goto error;
- if (u.address.sin_family == AF_INET)
- {
- laddr = JvNewByteArray (4);
- memcpy (elements (laddr), &u.address.sin_addr, 4);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- laddr = JvNewByteArray (16);
- memcpy (elements (laddr), &u.address6.sin6_addr, 16);
- }
-#endif
- else
- throw new java::net::SocketException (
- JvNewStringUTF ("invalid family"));
- localAddress = new java::net::InetAddress (laddr, NULL);
- }
- return localAddress;
- break;
- case _Jv_SO_REUSEADDR_ :
-#if defined(SO_REUSEADDR)
- if (::getsockopt (fnum, SOL_SOCKET, SO_REUSEADDR, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Boolean (val != 0);
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_REUSEADDR not supported"));
-#endif
- break;
- case _Jv_IP_MULTICAST_IF_ :
-#ifdef HAVE_INET_NTOA
- struct in_addr inaddr;
- socklen_t inaddr_len;
- char *bytes;
-
- inaddr_len = sizeof(inaddr);
- if (::getsockopt (fnum, IPPROTO_IP, IP_MULTICAST_IF, (char *) &inaddr,
- &inaddr_len) != 0)
- goto error;
-
- bytes = inet_ntoa (inaddr);
-
- return java::net::InetAddress::getByName (JvNewStringLatin1 (bytes));
-#else
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_IF: not available - no inet_ntoa()"));
-#endif
- break;
- case _Jv_SO_TIMEOUT_ :
- return new java::lang::Integer (timeout);
- break;
-
- case _Jv_IP_MULTICAST_IF2_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_IF2: not yet implemented"));
- break;
-
- case _Jv_IP_MULTICAST_LOOP_ :
- if (::getsockopt (fnum, SOL_SOCKET, IP_MULTICAST_LOOP, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Boolean (val != 0);
-
- case _Jv_IP_TOS_ :
- if (::getsockopt (fnum, SOL_SOCKET, IP_TOS, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Integer (val);
-
- default :
- errno = ENOPROTOOPT;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::SocketException (JvNewStringUTF (strerr));
-}
diff --git a/libjava/java/net/natPlainDatagramSocketImplWin32.cc b/libjava/java/net/natPlainDatagramSocketImplWin32.cc
deleted file mode 100644
index d0d006d4b43..00000000000
--- a/libjava/java/net/natPlainDatagramSocketImplWin32.cc
+++ /dev/null
@@ -1,872 +0,0 @@
-/* Copyright (C) 2003 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>
-#include <platform.h>
-
-#ifdef WIN32
-
-#include <errno.h>
-#include <string.h>
-
-#else /* WIN32 */
-
-#ifdef HAVE_NETINET_IN_H
-#include <netinet/in.h>
-#endif
-#ifdef HAVE_ARPA_INET_H
-#include <arpa/inet.h>
-#endif
-#include <errno.h>
-#include <string.h>
-
-#endif /* WIN32 */
-
-#if HAVE_BSTRING_H
-// Needed for bzero, implicitly used by FD_ZERO on IRIX 5.2
-#include <bstring.h>
-#endif
-
-#include <gcj/cni.h>
-#include <java/io/IOException.h>
-#include <java/io/InterruptedIOException.h>
-#include <java/net/BindException.h>
-#include <java/net/SocketException.h>
-#include <java/net/PlainDatagramSocketImpl.h>
-#include <java/net/InetAddress.h>
-#include <java/net/NetworkInterface.h>
-#include <java/net/DatagramPacket.h>
-#include <java/net/PortUnreachableException.h>
-#include <java/lang/InternalError.h>
-#include <java/lang/Object.h>
-#include <java/lang/Boolean.h>
-#include <java/lang/Integer.h>
-
-#ifdef DISABLE_JAVA_NET
-
-void
-java::net::PlainDatagramSocketImpl::create ()
-{
- throw new SocketException (
- JvNewStringLatin1 ("DatagramSocketImpl.create: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::bind (jint, java::net::InetAddress *)
-{
- throw new BindException (
- JvNewStringLatin1 ("DatagramSocketImpl.bind: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::connect (java::net::InetAddress *, jint)
-{
- throw new SocketException (
- JvNewStringLatin1 ("DatagramSocketImpl.connect: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::disconnect ()
-{
- throw new SocketException (
- JvNewStringLatin1 ("DatagramSocketImpl.disconnect: unimplemented"));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.peek: unimplemented"));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::peekData(java::net::DatagramPacket *)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.peekData: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::close ()
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.close: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.send: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::receive (java::net::DatagramPacket *)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.receive: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::setTimeToLive (jint)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.setTimeToLive: unimplemented"));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::getTimeToLive ()
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.getTimeToLive: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::mcastGrp (java::net::InetAddress *,
- java::net::NetworkInterface *,
- jboolean)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("DatagramSocketImpl.mcastGrp: unimplemented"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::setOption (jint, java::lang::Object *)
-{
- throw new SocketException (
- JvNewStringLatin1 ("DatagramSocketImpl.setOption: unimplemented"));
-}
-
-java::lang::Object *
-java::net::PlainDatagramSocketImpl::getOption (jint)
-{
- throw new SocketException (
- JvNewStringLatin1 ("DatagramSocketImpl.getOption: unimplemented"));
-}
-
-#else /* DISABLE_JAVA_NET */
-
-
-union SockAddr
-{
- struct sockaddr_in address;
-#ifdef HAVE_INET6
- struct sockaddr_in6 address6;
-#endif
-};
-
-union McastReq
-{
-#if HAVE_STRUCT_IP_MREQ
- struct ip_mreq mreq;
-#endif
-#if HAVE_STRUCT_IPV6_MREQ
- struct ipv6_mreq mreq6;
-#endif
-};
-
-union InAddr
-{
- struct in_addr addr;
-#ifdef HAVE_INET6
- struct in6_addr addr6;
-#endif
-};
-
-
-// FIXME: routines here and/or in natPlainSocketImpl.cc could throw
-// NoRouteToHostException; also consider UnknownHostException, ConnectException.
-
-void
-java::net::PlainDatagramSocketImpl::create ()
-{
- int sock = _Jv_socket (AF_INET, SOCK_DGRAM, 0);
-
- if (sock < 0)
- {
- char* strerr = strerror (errno);
- throw new java::net::SocketException (JvNewStringUTF (strerr));
- }
-
- _Jv_platform_close_on_exec (sock);
-
- // We use fnum in place of fd here. From leaving fd null we avoid
- // the double close problem in FileDescriptor.finalize.
- fnum = sock;
-}
-
-void
-java::net::PlainDatagramSocketImpl::bind (jint lport,
- java::net::InetAddress *host)
-{
- union SockAddr u;
- struct sockaddr *ptr = (struct sockaddr *) &u.address;
- // FIXME: Use getaddrinfo() to get actual protocol instead of assuming ipv4.
- jbyteArray haddress = host->addr;
- jbyte *bytes = elements (haddress);
- int len = haddress->length;
-
- if (len == 4)
- {
- u.address.sin_family = AF_INET;
-
- if (host != NULL)
- memcpy (&u.address.sin_addr, bytes, len);
- else
- u.address.sin_addr.s_addr = htonl (INADDR_ANY);
-
- len = sizeof (struct sockaddr_in);
- u.address.sin_port = htons (lport);
- }
-#ifdef HAVE_INET6
- else if (len == 16)
- {
- u.address6.sin6_family = AF_INET6;
- memcpy (&u.address6.sin6_addr, bytes, len);
- len = sizeof (struct sockaddr_in6);
- u.address6.sin6_port = htons (lport);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
- if (_Jv_bind (fnum, ptr, len) == 0)
- {
- socklen_t addrlen = sizeof(u);
-
- if (lport != 0)
- localPort = lport;
- else if (::getsockname (fnum, (sockaddr*) &u, &addrlen) == 0)
- localPort = ntohs (u.address.sin_port);
- else
- goto error;
-
- /* Allow broadcast by default. */
- int broadcast = 1;
- if (::setsockopt (fnum, SOL_SOCKET, SO_BROADCAST, (char *) &broadcast,
- sizeof (broadcast)) != 0)
- goto error;
-
- return;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::BindException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainDatagramSocketImpl::connect (java::net::InetAddress *, jint)
-{
- throw new ::java::lang::InternalError (JvNewStringLatin1 (
- "PlainDatagramSocketImpl::connect: not implemented yet"));
-}
-
-void
-java::net::PlainDatagramSocketImpl::disconnect ()
-{
- throw new ::java::lang::InternalError (JvNewStringLatin1 (
- "PlainDatagramSocketImpl::disconnect: not implemented yet"));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *i)
-{
- // FIXME: Deal with Multicast and if the socket is connected.
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- ssize_t retlen =
- ::recvfrom (fnum, (char *) NULL, 0, MSG_PEEK, (sockaddr*) &u,
- &addrlen);
- if (retlen < 0)
- goto error;
- // FIXME: Deal with Multicast addressing and if the socket is connected.
- jbyteArray raddr;
- jint rport;
- if (u.address.sin_family == AF_INET)
- {
- raddr = JvNewByteArray (4);
- memcpy (elements (raddr), &u.address.sin_addr, 4);
- rport = ntohs (u.address.sin_port);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- raddr = JvNewByteArray (16);
- memcpy (elements (raddr), &u.address6.sin6_addr, 16);
- rport = ntohs (u.address6.sin6_port);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid family"));
-
- i->addr = raddr;
- return rport;
- error:
- char* strerr = strerror (errno);
-
- if (errno == ECONNREFUSED)
- throw new PortUnreachableException (JvNewStringUTF (strerr));
-
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::peekData(java::net::DatagramPacket *p)
-{
- // FIXME: Deal with Multicast and if the socket is connected.
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- jbyte *dbytes = elements (p->getData());
- ssize_t retlen = 0;
-
-// FIXME: implement timeout support for Win32
-#ifndef WIN32
- // Do timeouts via select since SO_RCVTIMEO is not always available.
- if (timeout > 0 && fnum >= 0 && fnum < FD_SETSIZE)
- {
- fd_set rset;
- struct timeval tv;
- FD_ZERO(&rset);
- FD_SET(fnum, &rset);
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout % 1000) * 1000;
- int retval;
- if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
- goto error;
- else if (retval == 0)
- throw new java::io::InterruptedIOException ();
- }
-#endif /* WIN32 */
-
- retlen =
- ::recvfrom (fnum, (char *) dbytes, p->getLength(), MSG_PEEK, (sockaddr*) &u,
- &addrlen);
- if (retlen < 0)
- goto error;
- // FIXME: Deal with Multicast addressing and if the socket is connected.
- jbyteArray raddr;
- jint rport;
- if (u.address.sin_family == AF_INET)
- {
- raddr = JvNewByteArray (4);
- memcpy (elements (raddr), &u.address.sin_addr, 4);
- rport = ntohs (u.address.sin_port);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- raddr = JvNewByteArray (16);
- memcpy (elements (raddr), &u.address6.sin6_addr, 16);
- rport = ntohs (u.address6.sin6_port);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid family"));
-
- p->setAddress (new InetAddress (raddr, NULL));
- p->setPort (rport);
- p->setLength ((jint) retlen);
- return rport;
-
- error:
- char* strerr = strerror (errno);
-
- if (errno == ECONNREFUSED)
- throw new PortUnreachableException (JvNewStringUTF (strerr));
-
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-// Close(shutdown) the socket.
-void
-java::net::PlainDatagramSocketImpl::close ()
-{
- // Avoid races from asynchronous finalization.
- JvSynchronize sync (this);
-
- // The method isn't declared to throw anything, so we disregard
- // the return value.
- _Jv_close (fnum);
- fnum = -1;
- timeout = 0;
-}
-
-void
-java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *p)
-{
- // FIXME: Deal with Multicast and if the socket is connected.
- jint rport = p->getPort();
- union SockAddr u;
- jbyteArray haddress = p->getAddress()->addr;
- jbyte *bytes = elements (haddress);
- int len = haddress->length;
- struct sockaddr *ptr = (struct sockaddr *) &u.address;
- jbyte *dbytes = elements (p->getData());
- if (len == 4)
- {
- u.address.sin_family = AF_INET;
- memcpy (&u.address.sin_addr, bytes, len);
- len = sizeof (struct sockaddr_in);
- u.address.sin_port = htons (rport);
- }
-#ifdef HAVE_INET6
- else if (len == 16)
- {
- u.address6.sin6_family = AF_INET6;
- memcpy (&u.address6.sin6_addr, bytes, len);
- len = sizeof (struct sockaddr_in6);
- u.address6.sin6_port = htons (rport);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
- if (::sendto (fnum, (char *) dbytes, p->getLength(), 0, ptr, len) >= 0)
- return;
-
- char* strerr = strerror (errno);
-
- if (errno == ECONNREFUSED)
- throw new PortUnreachableException (JvNewStringUTF (strerr));
-
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainDatagramSocketImpl::receive (java::net::DatagramPacket *p)
-{
- // FIXME: Deal with Multicast and if the socket is connected.
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- jbyte *dbytes = elements (p->getData());
- ssize_t retlen = 0;
-
-// FIXME: implement timeout support for Win32
-#ifndef WIN32
- // Do timeouts via select since SO_RCVTIMEO is not always available.
- if (timeout > 0 && fnum >= 0 && fnum < FD_SETSIZE)
- {
- fd_set rset;
- struct timeval tv;
- FD_ZERO(&rset);
- FD_SET(fnum, &rset);
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout % 1000) * 1000;
- int retval;
- if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
- goto error;
- else if (retval == 0)
- throw new java::io::InterruptedIOException ();
- }
-#endif /* WIN32 */
-
- retlen =
- ::recvfrom (fnum, (char *) dbytes, p->getLength(), 0, (sockaddr*) &u,
- &addrlen);
- if (retlen < 0)
- goto error;
- // FIXME: Deal with Multicast addressing and if the socket is connected.
- jbyteArray raddr;
- jint rport;
- if (u.address.sin_family == AF_INET)
- {
- raddr = JvNewByteArray (4);
- memcpy (elements (raddr), &u.address.sin_addr, 4);
- rport = ntohs (u.address.sin_port);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- raddr = JvNewByteArray (16);
- memcpy (elements (raddr), &u.address6.sin6_addr, 16);
- rport = ntohs (u.address6.sin6_port);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid family"));
-
- p->setAddress (new InetAddress (raddr, NULL));
- p->setPort (rport);
- p->setLength ((jint) retlen);
- return;
-
- error:
- char* strerr = strerror (errno);
-
- if (errno == ECONNREFUSED)
- throw new PortUnreachableException (JvNewStringUTF (strerr));
-
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainDatagramSocketImpl::setTimeToLive (jint ttl)
-{
- // Assumes IPPROTO_IP rather than IPPROTO_IPV6 since socket created is IPv4.
- char val = (char) ttl;
- socklen_t val_len = sizeof(val);
-
- if (::setsockopt (fnum, IPPROTO_IP, IP_MULTICAST_TTL, &val, val_len) == 0)
- return;
-
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-jint
-java::net::PlainDatagramSocketImpl::getTimeToLive ()
-{
- // Assumes IPPROTO_IP rather than IPPROTO_IPV6 since socket created is IPv4.
- char val;
- socklen_t val_len = sizeof(val);
-
- if (::getsockopt (fnum, IPPROTO_IP, IP_MULTICAST_TTL, &val, &val_len) == 0)
- return ((int) val) & 0xFF;
-
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainDatagramSocketImpl::mcastGrp (java::net::InetAddress *inetaddr,
- java::net::NetworkInterface *,
- jboolean join)
-{
- // FIXME: implement use of NetworkInterface
-
- union McastReq u;
- jbyteArray haddress = inetaddr->addr;
- jbyte *bytes = elements (haddress);
- int len = haddress->length;
- int level, opname;
- const char *ptr;
- if (0)
- ;
-#if HAVE_STRUCT_IP_MREQ
- else if (len == 4)
- {
- level = IPPROTO_IP;
- opname = join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
- memcpy (&u.mreq.imr_multiaddr, bytes, len);
- // FIXME: If a non-default interface is set, use it; see Stevens p. 501.
- // Maybe not, see note in last paragraph at bottom of Stevens p. 497.
- u.mreq.imr_interface.s_addr = htonl (INADDR_ANY);
- len = sizeof (struct ip_mreq);
- ptr = (const char *) &u.mreq;
- }
-#endif
-#if HAVE_STRUCT_IPV6_MREQ
- else if (len == 16)
- {
- level = IPPROTO_IPV6;
-
- /* Prefer new RFC 2553 names. */
-#ifndef IPV6_JOIN_GROUP
-#define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
-#endif
-#ifndef IPV6_LEAVE_GROUP
-#define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
-#endif
-
- opname = join ? IPV6_JOIN_GROUP : IPV6_LEAVE_GROUP;
- memcpy (&u.mreq6.ipv6mr_multiaddr, bytes, len);
- // FIXME: If a non-default interface is set, use it; see Stevens p. 501.
- // Maybe not, see note in last paragraph at bottom of Stevens p. 497.
- u.mreq6.ipv6mr_interface = 0;
- len = sizeof (struct ipv6_mreq);
- ptr = (const char *) &u.mreq6;
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
- if (::setsockopt (fnum, level, opname, ptr, len) == 0)
- return;
-
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainDatagramSocketImpl::setOption (jint optID,
- java::lang::Object *value)
-{
- int val;
- socklen_t val_len = sizeof (val);
-
- if (fnum < 0)
- throw new java::net::SocketException (JvNewStringUTF ("Socket closed"));
-
- if (_Jv_IsInstanceOf (value, &java::lang::Boolean::class$))
- {
- java::lang::Boolean *boolobj =
- static_cast<java::lang::Boolean *> (value);
- val = boolobj->booleanValue() ? 1 : 0;
- }
- else if (_Jv_IsInstanceOf (value, &java::lang::Integer::class$))
- {
- java::lang::Integer *intobj =
- static_cast<java::lang::Integer *> (value);
- val = (int) intobj->intValue();
- }
- // Else assume value to be an InetAddress for use with IP_MULTICAST_IF.
-
- switch (optID)
- {
- case _Jv_TCP_NODELAY_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("TCP_NODELAY not valid for UDP"));
- return;
- case _Jv_SO_LINGER_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_LINGER not valid for UDP"));
- return;
- case _Jv_SO_KEEPALIVE_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_KEEPALIVE not valid for UDP"));
- return;
-
- case _Jv_SO_BROADCAST_ :
- if (::setsockopt (fnum, SOL_SOCKET, SO_BROADCAST, (char *) &val,
- val_len) != 0)
- goto error;
- break;
-
- case _Jv_SO_OOBINLINE_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_OOBINLINE: not valid for UDP"));
- break;
-
- case _Jv_SO_SNDBUF_ :
- case _Jv_SO_RCVBUF_ :
-#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
- int opt;
- optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
- if (::setsockopt (fnum, SOL_SOCKET, opt, (char *) &val, val_len) != 0)
- goto error;
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
-#endif
- return;
- case _Jv_SO_REUSEADDR_ :
-#if defined(SO_REUSEADDR)
- if (::setsockopt (fnum, SOL_SOCKET, SO_REUSEADDR, (char *) &val,
- val_len) != 0)
- goto error;
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_REUSEADDR not supported"));
-#endif
- return;
- case _Jv_SO_BINDADDR_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_BINDADDR: read only option"));
- return;
- case _Jv_IP_MULTICAST_IF_ :
- union InAddr u;
- jbyteArray haddress;
- jbyte *bytes;
- int len;
- int level, opname;
- const char *ptr;
-
- haddress = ((java::net::InetAddress *) value)->addr;
- bytes = elements (haddress);
- len = haddress->length;
- if (len == 4)
- {
- level = IPPROTO_IP;
- opname = IP_MULTICAST_IF;
- memcpy (&u.addr, bytes, len);
- len = sizeof (struct in_addr);
- ptr = (const char *) &u.addr;
- }
-// Tru64 UNIX V5.0 has struct sockaddr_in6, but no IPV6_MULTICAST_IF
-#if defined (HAVE_INET6) && defined (IPV6_MULTICAST_IF)
- else if (len == 16)
- {
- level = IPPROTO_IPV6;
- opname = IPV6_MULTICAST_IF;
- memcpy (&u.addr6, bytes, len);
- len = sizeof (struct in6_addr);
- ptr = (const char *) &u.addr6;
- }
-#endif
- else
- throw
- new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
- if (::setsockopt (fnum, level, opname, ptr, len) != 0)
- goto error;
- return;
-
- case _Jv_IP_MULTICAST_IF2_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_IF2: not yet implemented"));
- break;
-
- case _Jv_IP_MULTICAST_LOOP_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_LOOP: not yet implemented"));
- break;
-
- case _Jv_IP_TOS_ :
- if (::setsockopt (fnum, SOL_SOCKET, IP_TOS, (char *) &val,
- val_len) != 0)
- goto error;
- return;
-
- case _Jv_SO_TIMEOUT_ :
- timeout = val;
- return;
- default :
- errno = ENOPROTOOPT;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::SocketException (JvNewStringUTF (strerr));
-}
-
-java::lang::Object *
-java::net::PlainDatagramSocketImpl::getOption (jint optID)
-{
- int val;
- socklen_t val_len = sizeof(val);
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
-
- switch (optID)
- {
- case _Jv_TCP_NODELAY_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("TCP_NODELAY not valid for UDP"));
- break;
- case _Jv_SO_LINGER_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_LINGER not valid for UDP"));
- break;
- case _Jv_SO_KEEPALIVE_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_KEEPALIVE not valid for UDP"));
- break;
-
- case _Jv_SO_BROADCAST_ :
- if (::getsockopt (fnum, SOL_SOCKET, SO_BROADCAST, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Boolean (val != 0);
-
- case _Jv_SO_OOBINLINE_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_OOBINLINE not valid for UDP"));
- break;
-
- case _Jv_SO_RCVBUF_ :
- case _Jv_SO_SNDBUF_ :
-#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
- int opt;
- optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
- if (::getsockopt (fnum, SOL_SOCKET, opt, (char *) &val, &val_len) != 0)
- goto error;
- else
- return new java::lang::Integer (val);
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
-#endif
- break;
- case _Jv_SO_BINDADDR_:
- // cache the local address
- if (localAddress == NULL)
- {
- jbyteArray laddr;
- if (::getsockname (fnum, (sockaddr*) &u, &addrlen) != 0)
- goto error;
- if (u.address.sin_family == AF_INET)
- {
- laddr = JvNewByteArray (4);
- memcpy (elements (laddr), &u.address.sin_addr, 4);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- laddr = JvNewByteArray (16);
- memcpy (elements (laddr), &u.address6.sin6_addr, 16);
- }
-#endif
- else
- throw new java::net::SocketException (
- JvNewStringUTF ("invalid family"));
- localAddress = new java::net::InetAddress (laddr, NULL);
- }
- return localAddress;
- break;
- case _Jv_SO_REUSEADDR_ :
-#if defined(SO_REUSEADDR)
- if (::getsockopt (fnum, SOL_SOCKET, SO_REUSEADDR, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Boolean (val != 0);
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_REUSEADDR not supported"));
-#endif
- break;
- case _Jv_IP_MULTICAST_IF_ :
-#ifdef HAVE_INET_NTOA
- struct in_addr inaddr;
- socklen_t inaddr_len;
- char *bytes;
-
- inaddr_len = sizeof(inaddr);
- if (::getsockopt (fnum, IPPROTO_IP, IP_MULTICAST_IF, (char *) &inaddr,
- &inaddr_len) != 0)
- goto error;
-
- bytes = inet_ntoa (inaddr);
-
- return java::net::InetAddress::getByName (JvNewStringLatin1 (bytes));
-#else
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_IF: not available - no inet_ntoa()"));
-#endif
- break;
- case _Jv_SO_TIMEOUT_ :
- return new java::lang::Integer (timeout);
- break;
-
- case _Jv_IP_MULTICAST_IF2_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_IF2: not yet implemented"));
- break;
-
- case _Jv_IP_MULTICAST_LOOP_ :
- if (::getsockopt (fnum, SOL_SOCKET, IP_MULTICAST_LOOP, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Boolean (val != 0);
-
- case _Jv_IP_TOS_ :
- if (::getsockopt (fnum, SOL_SOCKET, IP_TOS, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Integer (val);
-
- default :
- errno = ENOPROTOOPT;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::SocketException (JvNewStringUTF (strerr));
-}
-
-#endif /* DISABLE_JAVA_NET */
diff --git a/libjava/java/net/natPlainSocketImplNoNet.cc b/libjava/java/net/natPlainSocketImplNoNet.cc
deleted file mode 100644
index e65438ef1d5..00000000000
--- a/libjava/java/net/natPlainSocketImplNoNet.cc
+++ /dev/null
@@ -1,128 +0,0 @@
-/* Copyright (C) 2003 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>
-#include <platform.h>
-
-#include <java/io/IOException.h>
-#include <java/net/BindException.h>
-#include <java/net/ConnectException.h>
-#include <java/net/PlainSocketImpl.h>
-#include <java/net/SocketException.h>
-
-void
-java::net::PlainSocketImpl::create (jboolean)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("SocketImpl.create: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::bind (java::net::InetAddress *, jint)
-{
- throw new BindException (
- JvNewStringLatin1 ("SocketImpl.bind: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::connect (java::net::SocketAddress *, jint)
-{
- throw new ConnectException (
- JvNewStringLatin1 ("SocketImpl.connect: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::listen (jint)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("SocketImpl.listen: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("SocketImpl.accept: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::setOption (jint, java::lang::Object *)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.setOption: unimplemented"));
-}
-
-java::lang::Object *
-java::net::PlainSocketImpl::getOption (jint)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.getOption: unimplemented"));
-}
-
-jint
-java::net::PlainSocketImpl::read(void)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.read: unimplemented"));
-}
-
-jint
-java::net::PlainSocketImpl::read(jbyteArray buffer, jint offset, jint count)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.read: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::write(jint b)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.write: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::write(jbyteArray b, jint offset, jint len)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.write: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::sendUrgentData(jint data)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.sendUrgentData: unimplemented"));
-}
-
-jint
-java::net::PlainSocketImpl::available(void)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.available: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::close(void)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.close: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::shutdownInput (void)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.shutdownInput: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::shutdownOutput (void)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.shutdownOutput: unimplemented"));
-}
diff --git a/libjava/java/net/natPlainSocketImplPosix.cc b/libjava/java/net/natPlainSocketImplPosix.cc
deleted file mode 100644
index 65feac87324..00000000000
--- a/libjava/java/net/natPlainSocketImplPosix.cc
+++ /dev/null
@@ -1,856 +0,0 @@
-/* Copyright (C) 2003 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>
-#include <platform.h>
-
-#ifdef HAVE_SYS_IOCTL_H
-#define BSD_COMP /* Get FIONREAD on Solaris2. */
-#include <sys/ioctl.h>
-#endif
-
-// Pick up FIONREAD on Solaris 2.5.
-#ifdef HAVE_SYS_FILIO_H
-#include <sys/filio.h>
-#endif
-
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <errno.h>
-#include <string.h>
-
-#if HAVE_BSTRING_H
-// Needed for bzero, implicitly used by FD_ZERO on IRIX 5.2
-#include <bstring.h>
-#endif
-
-#include <gcj/cni.h>
-#include <gcj/javaprims.h>
-#include <java/io/IOException.h>
-#include <java/io/InterruptedIOException.h>
-#include <java/net/BindException.h>
-#include <java/net/ConnectException.h>
-#include <java/net/PlainSocketImpl.h>
-#include <java/net/InetAddress.h>
-#include <java/net/InetSocketAddress.h>
-#include <java/net/SocketException.h>
-#include <java/net/SocketTimeoutException.h>
-#include <java/lang/InternalError.h>
-#include <java/lang/Object.h>
-#include <java/lang/Boolean.h>
-#include <java/lang/Class.h>
-#include <java/lang/Integer.h>
-#include <java/lang/Thread.h>
-#include <java/lang/NullPointerException.h>
-#include <java/lang/ArrayIndexOutOfBoundsException.h>
-#include <java/lang/IllegalArgumentException.h>
-
-union SockAddr
-{
- struct sockaddr_in address;
-#ifdef HAVE_INET6
- struct sockaddr_in6 address6;
-#endif
-};
-
-void
-java::net::PlainSocketImpl::create (jboolean stream)
-{
- int sock = _Jv_socket (AF_INET, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
-
- if (sock < 0)
- {
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
- }
-
- _Jv_platform_close_on_exec (sock);
-
- // We use fnum in place of fd here. From leaving fd null we avoid
- // the double close problem in FileDescriptor.finalize.
- fnum = sock;
-}
-
-void
-java::net::PlainSocketImpl::bind (java::net::InetAddress *host, jint lport)
-{
- union SockAddr u;
- struct sockaddr *ptr = (struct sockaddr *) &u.address;
- jbyteArray haddress = host->addr;
- jbyte *bytes = elements (haddress);
- int len = haddress->length;
- int i = 1;
-
- if (len == 4)
- {
- u.address.sin_family = AF_INET;
-
- if (host != NULL)
- memcpy (&u.address.sin_addr, bytes, len);
- else
- u.address.sin_addr.s_addr = htonl (INADDR_ANY);
-
- len = sizeof (struct sockaddr_in);
- u.address.sin_port = htons (lport);
- }
-#ifdef HAVE_INET6
- else if (len == 16)
- {
- u.address6.sin6_family = AF_INET6;
- memcpy (&u.address6.sin6_addr, bytes, len);
- len = sizeof (struct sockaddr_in6);
- u.address6.sin6_port = htons (lport);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
- // Enable SO_REUSEADDR, so that servers can reuse ports left in TIME_WAIT.
- ::setsockopt(fnum, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof(i));
-
- if (_Jv_bind (fnum, ptr, len) == 0)
- {
- address = host;
- socklen_t addrlen = sizeof(u);
-
- if (lport != 0)
- localport = lport;
- else if (::getsockname (fnum, (sockaddr*) &u, &addrlen) == 0)
- localport = ntohs (u.address.sin_port);
- else
- goto error;
-
- return;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::BindException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainSocketImpl::connect (java::net::SocketAddress *addr,
- jint timeout)
-{
- java::net::InetSocketAddress *tmp = (java::net::InetSocketAddress*) addr;
- java::net::InetAddress *host = tmp->getAddress();
- jint rport = tmp->getPort();
-
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- jbyteArray haddress = host->addr;
- jbyte *bytes = elements (haddress);
- int len = haddress->length;
- struct sockaddr *ptr = (struct sockaddr *) &u.address;
- if (len == 4)
- {
- u.address.sin_family = AF_INET;
- memcpy (&u.address.sin_addr, bytes, len);
- len = sizeof (struct sockaddr_in);
- u.address.sin_port = htons (rport);
- }
-#ifdef HAVE_INET6
- else if (len == 16)
- {
- u.address6.sin6_family = AF_INET6;
- memcpy (&u.address6.sin6_addr, bytes, len);
- len = sizeof (struct sockaddr_in6);
- u.address6.sin6_port = htons (rport);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
- if (timeout > 0)
- {
- int flags = ::fcntl (fnum, F_GETFL);
- ::fcntl (fnum, F_SETFL, flags | O_NONBLOCK);
-
- if ((_Jv_connect (fnum, ptr, len) != 0) && (errno != EINPROGRESS))
- goto error;
-
- fd_set rset;
- struct timeval tv;
- FD_ZERO(&rset);
- FD_SET(fnum, &rset);
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout % 1000) * 1000;
- int retval;
-
- if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
- goto error;
- else if (retval == 0)
- throw new java::net::SocketTimeoutException
- (JvNewStringUTF ("Connect timed out"));
- }
- else
- {
- if (_Jv_connect (fnum, ptr, len) != 0)
- goto error;
- }
-
- address = host;
- port = rport;
-
- // A bind may not have been done on this socket; if so, set localport now.
- if (localport == 0)
- {
- if (::getsockname (fnum, (sockaddr*) &u, &addrlen) == 0)
- localport = ntohs (u.address.sin_port);
- else
- goto error;
- }
-
- return;
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::ConnectException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainSocketImpl::listen (jint backlog)
-{
- if (::listen (fnum, backlog) != 0)
- {
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
- }
-}
-
-void
-java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *s)
-{
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- int new_socket = 0;
-
- // Do timeouts via select since SO_RCVTIMEO is not always available.
- if (timeout > 0 && fnum >= 0 && fnum < FD_SETSIZE)
- {
- fd_set rset;
- struct timeval tv;
- FD_ZERO(&rset);
- FD_SET(fnum, &rset);
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout % 1000) * 1000;
- int retval;
- if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
- goto error;
- else if (retval == 0)
- throw new java::io::InterruptedIOException (
- JvNewStringUTF("Accept timed out"));
- }
-
- new_socket = _Jv_accept (fnum, (sockaddr*) &u, &addrlen);
-
- if (new_socket < 0)
- goto error;
-
- _Jv_platform_close_on_exec (new_socket);
-
- jbyteArray raddr;
- jint rport;
- if (u.address.sin_family == AF_INET)
- {
- raddr = JvNewByteArray (4);
- memcpy (elements (raddr), &u.address.sin_addr, 4);
- rport = ntohs (u.address.sin_port);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- raddr = JvNewByteArray (16);
- memcpy (elements (raddr), &u.address6.sin6_addr, 16);
- rport = ntohs (u.address6.sin6_port);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid family"));
-
- s->fnum = new_socket;
- s->localport = localport;
- s->address = new InetAddress (raddr, NULL);
- s->port = rport;
- return;
-
- error:
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-// Close(shutdown) the socket.
-void
-java::net::PlainSocketImpl::close()
-{
- // Avoid races from asynchronous finalization.
- JvSynchronize sync (this);
-
- // should we use shutdown here? how would that effect so_linger?
- int res = _Jv_close (fnum);
-
- if (res == -1)
- {
- // These three errors are not errors according to tests performed
- // on the reference implementation.
- if (errno != ENOTCONN && errno != ECONNRESET && errno != EBADF)
- throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
- }
- // Safe place to reset the file pointer.
- fnum = -1;
- timeout = 0;
-}
-
-// Write a byte to the socket.
-void
-java::net::PlainSocketImpl::write(jint b)
-{
- jbyte d =(jbyte) b;
- int r = 0;
-
- while (r != 1)
- {
- r = _Jv_write (fnum, &d, 1);
- if (r == -1)
- {
- if (java::lang::Thread::interrupted())
- {
- java::io::InterruptedIOException *iioe
- = new java::io::InterruptedIOException
- (JvNewStringLatin1 (strerror (errno)));
- iioe->bytesTransferred = 0;
- throw iioe;
- }
- // Some errors should not cause exceptions.
- if (errno != ENOTCONN && errno != ECONNRESET && errno != EBADF)
- throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
- break;
- }
- }
-}
-
-// Write some bytes to the socket.
-void
-java::net::PlainSocketImpl::write(jbyteArray b, jint offset, jint len)
-{
- if (! b)
- throw new java::lang::NullPointerException;
- if (offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
- throw new java::lang::ArrayIndexOutOfBoundsException;
-
- jbyte *bytes = elements (b) + offset;
- int written = 0;
-
- while (len > 0)
- {
- int r = _Jv_write (fnum, bytes, len);
-
- if (r == -1)
- {
- if (java::lang::Thread::interrupted())
- {
- java::io::InterruptedIOException *iioe
- = new java::io::InterruptedIOException
- (JvNewStringLatin1 (strerror (errno)));
- iioe->bytesTransferred = written;
- throw iioe;
- }
- // Some errors should not cause exceptions.
- if (errno != ENOTCONN && errno != ECONNRESET && errno != EBADF)
- throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
- break;
- }
-
- written += r;
- len -= r;
- bytes += r;
- }
-}
-
-void
-java::net::PlainSocketImpl::sendUrgentData (jint)
-{
- throw new SocketException (JvNewStringLatin1 (
- "PlainSocketImpl: sending of urgent data not supported by this socket"));
-}
-
-// Read a single byte from the socket.
-jint
-java::net::PlainSocketImpl::read(void)
-{
- jbyte b;
-
- // Do timeouts via select.
- if (timeout > 0 && fnum >= 0 && fnum < FD_SETSIZE)
- {
- // Create the file descriptor set.
- fd_set read_fds;
- FD_ZERO (&read_fds);
- FD_SET (fnum,&read_fds);
- // Create the timeout struct based on our internal timeout value.
- struct timeval timeout_value;
- timeout_value.tv_sec = timeout / 1000;
- timeout_value.tv_usec = (timeout % 1000) * 1000;
- // Select on the fds.
- int sel_retval =
- _Jv_select (fnum + 1, &read_fds, NULL, NULL, &timeout_value);
- // If select returns 0 we've waited without getting data...
- // that means we've timed out.
- if (sel_retval == 0)
- throw new java::io::InterruptedIOException
- (JvNewStringUTF ("read timed out") );
- // If select returns ok we know we either got signalled or read some data...
- // either way we need to try to read.
- }
-
- int r = _Jv_read (fnum, &b, 1);
-
- if (r == 0)
- return -1;
-
- if (java::lang::Thread::interrupted())
- {
- java::io::InterruptedIOException *iioe =
- new java::io::InterruptedIOException
- (JvNewStringUTF("read interrupted"));
- iioe->bytesTransferred = r == -1 ? 0 : r;
- throw iioe;
- }
- else if (r == -1)
- {
- // Some errors cause us to return end of stream...
- if (errno == ENOTCONN)
- return -1;
-
- // Other errors need to be signalled.
- throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
- }
-
- return b & 0xFF;
-}
-
-// Read count bytes into the buffer, starting at offset.
-jint
-java::net::PlainSocketImpl::read(jbyteArray buffer, jint offset, jint count)
-{
- if (! buffer)
- throw new java::lang::NullPointerException;
-
- jsize bsize = JvGetArrayLength (buffer);
-
- if (offset < 0 || count < 0 || offset + count > bsize)
- throw new java::lang::ArrayIndexOutOfBoundsException;
-
- jbyte *bytes = elements (buffer) + offset;
-
- // Do timeouts via select.
- if (timeout > 0 && fnum >= 0 && fnum < FD_SETSIZE)
- {
- // Create the file descriptor set.
- fd_set read_fds;
- FD_ZERO (&read_fds);
- FD_SET (fnum, &read_fds);
- // Create the timeout struct based on our internal timeout value.
- struct timeval timeout_value;
- timeout_value.tv_sec = timeout / 1000;
- timeout_value.tv_usec =(timeout % 1000) * 1000;
- // Select on the fds.
- int sel_retval =
- _Jv_select (fnum + 1, &read_fds, NULL, NULL, &timeout_value);
- // We're only interested in the 0 return.
- // error returns still require us to try to read
- // the socket to see what happened.
- if (sel_retval == 0)
- {
- java::io::InterruptedIOException *iioe =
- new java::io::InterruptedIOException
- (JvNewStringUTF ("read interrupted"));
- iioe->bytesTransferred = 0;
- throw iioe;
- }
- }
-
- // Read the socket.
- int r = ::recv (fnum, (char *) bytes, count, 0);
-
- if (r == 0)
- return -1;
-
- if (java::lang::Thread::interrupted())
- {
- java::io::InterruptedIOException *iioe =
- new java::io::InterruptedIOException
- (JvNewStringUTF ("read interrupted"));
- iioe->bytesTransferred = r == -1 ? 0 : r;
- throw iioe;
- }
- else if (r == -1)
- {
- // Some errors cause us to return end of stream...
- if (errno == ENOTCONN)
- return -1;
-
- // Other errors need to be signalled.
- throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
- }
-
- return r;
-}
-
-// How many bytes are available?
-jint
-java::net::PlainSocketImpl::available(void)
-{
-#if defined(FIONREAD) || defined(HAVE_SELECT)
- long num = 0;
- int r = 0;
- bool num_set = false;
-
-#if defined(FIONREAD)
- r = ::ioctl (fnum, FIONREAD, &num);
-
- if (r == -1 && errno == ENOTTY)
- {
- // If the ioctl doesn't work, we don't care.
- r = 0;
- num = 0;
- }
- else
- num_set = true;
-#elif defined(HAVE_SELECT)
- if (fnum < 0)
- {
- errno = EBADF;
- r = -1;
- }
-#endif
-
- if (r == -1)
- {
- posix_error:
- throw new java::io::IOException(JvNewStringUTF(strerror(errno)));
- }
-
- // If we didn't get anything we can use select.
-
-#if defined(HAVE_SELECT)
- if (! num_set)
- if (! num_set && fnum >= 0 && fnum < FD_SETSIZE)
- {
- fd_set rd;
- FD_ZERO (&rd);
- FD_SET (fnum, &rd);
- struct timeval tv;
- tv.tv_sec = 0;
- tv.tv_usec = 0;
- r = _Jv_select (fnum + 1, &rd, NULL, NULL, &tv);
- if(r == -1)
- goto posix_error;
- num = r == 0 ? 0 : 1;
- }
-#endif /* HAVE_SELECT */
-
- return (jint) num;
-#else
- throw new java::io::IOException (JvNewStringUTF ("unimplemented"));
-#endif
-}
-
-void
-java::net::PlainSocketImpl::setOption (jint optID, java::lang::Object *value)
-{
- int val;
- socklen_t val_len = sizeof (val);
-
- if (fnum < 0)
- throw new java::net::SocketException (JvNewStringUTF ("Socket closed"));
-
- if (_Jv_IsInstanceOf (value, &java::lang::Boolean::class$))
- {
- java::lang::Boolean *boolobj =
- static_cast<java::lang::Boolean *> (value);
- if (boolobj->booleanValue())
- val = 1;
- else
- {
- if (optID == _Jv_SO_LINGER_)
- val = -1;
- else
- val = 0;
- }
- }
- else if (_Jv_IsInstanceOf (value, &java::lang::Integer::class$))
- {
- java::lang::Integer *intobj =
- static_cast<java::lang::Integer *> (value);
- val = (int) intobj->intValue();
- }
- else
- {
- throw new java::lang::IllegalArgumentException (
- JvNewStringLatin1 ("`value' must be Boolean or Integer"));
- }
-
- switch (optID)
- {
- case _Jv_TCP_NODELAY_ :
-#ifdef TCP_NODELAY
- if (::setsockopt (fnum, IPPROTO_TCP, TCP_NODELAY, (char *) &val,
- val_len) != 0)
- goto error;
-#else
- throw new java::lang::InternalError
- (JvNewStringUTF ("TCP_NODELAY not supported"));
-#endif /* TCP_NODELAY */
- return;
-
- case _Jv_SO_KEEPALIVE_ :
- if (::setsockopt (fnum, SOL_SOCKET, SO_KEEPALIVE, (char *) &val,
- val_len) != 0)
- goto error;
- break;
-
- case _Jv_SO_BROADCAST_ :
- throw new java::net::SocketException
- (JvNewStringUTF ("SO_BROADCAST not valid for TCP"));
- break;
-
- case _Jv_SO_OOBINLINE_ :
- if (::setsockopt (fnum, SOL_SOCKET, SO_OOBINLINE, (char *) &val,
- val_len) != 0)
- goto error;
- break;
-
- case _Jv_SO_LINGER_ :
-#ifdef SO_LINGER
- struct linger l_val;
- l_val.l_onoff = (val != -1);
- l_val.l_linger = val;
-
- if (::setsockopt (fnum, SOL_SOCKET, SO_LINGER, (char *) &l_val,
- sizeof(l_val)) != 0)
- goto error;
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_LINGER not supported"));
-#endif /* SO_LINGER */
- return;
-
- case _Jv_SO_SNDBUF_ :
- case _Jv_SO_RCVBUF_ :
-#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
- int opt;
- optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
- if (::setsockopt (fnum, SOL_SOCKET, opt, (char *) &val, val_len) != 0)
- goto error;
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
-#endif
- return;
-
- case _Jv_SO_BINDADDR_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_BINDADDR: read only option"));
- return;
-
- case _Jv_IP_MULTICAST_IF_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP"));
- return;
-
- case _Jv_IP_MULTICAST_IF2_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_IF2: not valid for TCP"));
- break;
-
- case _Jv_IP_MULTICAST_LOOP_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_LOOP: not valid for TCP"));
- break;
-
- case _Jv_IP_TOS_ :
- if (::setsockopt (fnum, SOL_SOCKET, IP_TOS, (char *) &val,
- val_len) != 0)
- goto error;
- break;
-
- case _Jv_SO_REUSEADDR_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_REUSEADDR: not valid for TCP"));
- return;
-
- case _Jv_SO_TIMEOUT_ :
- timeout = val;
- return;
-
- default :
- errno = ENOPROTOOPT;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::SocketException (JvNewStringUTF (strerr));
-}
-
-java::lang::Object *
-java::net::PlainSocketImpl::getOption (jint optID)
-{
- int val;
- socklen_t val_len = sizeof(val);
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- struct linger l_val;
- socklen_t l_val_len = sizeof(l_val);
-
- switch (optID)
- {
-#ifdef TCP_NODELAY
- case _Jv_TCP_NODELAY_ :
- if (::getsockopt (fnum, IPPROTO_TCP, TCP_NODELAY, (char *) &val,
- &val_len) != 0)
- goto error;
- else
- return new java::lang::Boolean (val != 0);
-#else
- throw new java::lang::InternalError
- (JvNewStringUTF ("TCP_NODELAY not supported"));
-#endif
- break;
-
- case _Jv_SO_LINGER_ :
-#ifdef SO_LINGER
- if (::getsockopt (fnum, SOL_SOCKET, SO_LINGER, (char *) &l_val,
- &l_val_len) != 0)
- goto error;
-
- if (l_val.l_onoff)
- return new java::lang::Integer (l_val.l_linger);
- else
- return new java::lang::Boolean ((jboolean)false);
-#else
- throw new java::lang::InternalError
- (JvNewStringUTF ("SO_LINGER not supported"));
-#endif
- break;
-
- case _Jv_SO_KEEPALIVE_ :
- if (::getsockopt (fnum, SOL_SOCKET, SO_KEEPALIVE, (char *) &val,
- &val_len) != 0)
- goto error;
- else
- return new java::lang::Boolean (val != 0);
-
- case _Jv_SO_BROADCAST_ :
- if (::getsockopt (fnum, SOL_SOCKET, SO_BROADCAST, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Boolean ((jboolean)val);
-
- case _Jv_SO_OOBINLINE_ :
- if (::getsockopt (fnum, SOL_SOCKET, SO_OOBINLINE, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Boolean ((jboolean)val);
-
- case _Jv_SO_RCVBUF_ :
- case _Jv_SO_SNDBUF_ :
-#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
- int opt;
- optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
- if (::getsockopt (fnum, SOL_SOCKET, opt, (char *) &val, &val_len) != 0)
- goto error;
- else
- return new java::lang::Integer (val);
-#else
- throw new java::lang::InternalError
- (JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
-#endif
- break;
- case _Jv_SO_BINDADDR_:
- // cache the local address
- if (localAddress == NULL)
- {
- jbyteArray laddr;
-
- if (::getsockname (fnum, (sockaddr*) &u, &addrlen) != 0)
- goto error;
-
- if (u.address.sin_family == AF_INET)
- {
- laddr = JvNewByteArray (4);
- memcpy (elements (laddr), &u.address.sin_addr, 4);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- laddr = JvNewByteArray (16);
- memcpy (elements (laddr), &u.address6.sin6_addr, 16);
- }
-#endif
- else
- throw new java::net::SocketException
- (JvNewStringUTF ("invalid family"));
- localAddress = new java::net::InetAddress (laddr, NULL);
- }
-
- return localAddress;
- break;
- case _Jv_IP_MULTICAST_IF_ :
- throw new java::net::SocketException
- (JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP"));
- break;
-
- case _Jv_IP_MULTICAST_IF2_ :
- throw new java::net::SocketException
- (JvNewStringUTF ("IP_MULTICAST_IF2: not valid for TCP"));
- break;
-
- case _Jv_IP_MULTICAST_LOOP_ :
- throw new java::net::SocketException
- (JvNewStringUTF ("IP_MULTICAST_LOOP: not valid for TCP"));
- break;
-
- case _Jv_IP_TOS_ :
- if (::getsockopt (fnum, SOL_SOCKET, IP_TOS, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Integer (val);
- break;
-
- case _Jv_SO_REUSEADDR_ :
- throw new java::net::SocketException
- (JvNewStringUTF ("SO_REUSEADDR: not valid for TCP"));
- break;
-
- case _Jv_SO_TIMEOUT_ :
- return new java::lang::Integer (timeout);
- break;
-
- default :
- errno = ENOPROTOOPT;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::SocketException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainSocketImpl::shutdownInput (void)
-{
- if (::shutdown (fnum, 0))
- throw new SocketException (JvNewStringUTF (strerror (errno)));
-}
-
-void
-java::net::PlainSocketImpl::shutdownOutput (void)
-{
- if (::shutdown (fnum, 1))
- throw new SocketException (JvNewStringUTF (strerror (errno)));
-}
diff --git a/libjava/java/net/natPlainSocketImplWin32.cc b/libjava/java/net/natPlainSocketImplWin32.cc
deleted file mode 100644
index 1485ea842f9..00000000000
--- a/libjava/java/net/natPlainSocketImplWin32.cc
+++ /dev/null
@@ -1,1019 +0,0 @@
-/* Copyright (C) 2003 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>
-#include <platform.h>
-
-#ifndef DISABLE_JAVA_NET
-
-#ifdef WIN32
-
-#include <windows.h>
-#include <winsock.h>
-#include <errno.h>
-#include <string.h>
-#undef STRICT
-#undef MAX_PRIORITY
-#undef MIN_PRIORITY
-#undef FIONREAD
-
-// These functions make the Win32 socket API look more POSIXy
-static inline int
-write(int s, void *buf, int len)
-{
- return send(s, (char*)buf, len, 0);
-}
-
-static inline int
-read(int s, void *buf, int len)
-{
- return recv(s, (char*)buf, len, 0);
-}
-
-// these errors cannot occur on Win32
-#else /* WIN32 */
-
-#ifdef HAVE_SYS_IOCTL_H
-#define BSD_COMP /* Get FIONREAD on Solaris2. */
-#include <sys/ioctl.h>
-#endif
-
-// Pick up FIONREAD on Solaris 2.5.
-#ifdef HAVE_SYS_FILIO_H
-#include <sys/filio.h>
-#endif
-
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <errno.h>
-#include <string.h>
-
-#endif /* WIN32 */
-#endif /* DISABLE_JAVA_NET */
-
-#if HAVE_BSTRING_H
-// Needed for bzero, implicitly used by FD_ZERO on IRIX 5.2
-#include <bstring.h>
-#endif
-
-
-#include <gcj/cni.h>
-#include <gcj/javaprims.h>
-#include <java/io/IOException.h>
-#include <java/io/InterruptedIOException.h>
-#include <java/net/BindException.h>
-#include <java/net/ConnectException.h>
-#include <java/net/PlainSocketImpl.h>
-#include <java/net/InetAddress.h>
-#include <java/net/InetSocketAddress.h>
-#include <java/net/SocketException.h>
-#include <java/net/SocketTimeoutException.h>
-#include <java/lang/InternalError.h>
-#include <java/lang/Object.h>
-#include <java/lang/Boolean.h>
-#include <java/lang/Class.h>
-#include <java/lang/Integer.h>
-#include <java/lang/Thread.h>
-#include <java/lang/NullPointerException.h>
-#include <java/lang/ArrayIndexOutOfBoundsException.h>
-#include <java/lang/IllegalArgumentException.h>
-
-#ifdef DISABLE_JAVA_NET
-
-void
-java::net::PlainSocketImpl::create (jboolean)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("SocketImpl.create: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::bind (java::net::InetAddress *, jint)
-{
- throw new BindException (
- JvNewStringLatin1 ("SocketImpl.bind: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::connect (java::net::SocketAddress *, jint)
-{
- throw new ConnectException (
- JvNewStringLatin1 ("SocketImpl.connect: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::listen (jint)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("SocketImpl.listen: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *)
-{
- throw new java::io::IOException (
- JvNewStringLatin1 ("SocketImpl.accept: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::setOption (jint, java::lang::Object *)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.setOption: unimplemented"));
-}
-
-java::lang::Object *
-java::net::PlainSocketImpl::getOption (jint)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.getOption: unimplemented"));
-}
-
-jint
-java::net::PlainSocketImpl::read(void)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.read: unimplemented"));
-}
-
-jint
-java::net::PlainSocketImpl::read(jbyteArray buffer, jint offset, jint count)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.read: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::write(jint b)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.write: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::write(jbyteArray b, jint offset, jint len)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.write: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::sendUrgentData(jint data)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.sendUrgentData: unimplemented"));
-}
-
-jint
-java::net::PlainSocketImpl::available(void)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.available: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::close(void)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.close: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::shutdownInput (void)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.shutdownInput: unimplemented"));
-}
-
-void
-java::net::PlainSocketImpl::shutdownOutput (void)
-{
- throw new SocketException (
- JvNewStringLatin1 ("SocketImpl.shutdownOutput: unimplemented"));
-}
-
-#else /* DISABLE_JAVA_NET */
-
-union SockAddr
-{
- struct sockaddr_in address;
-#ifdef HAVE_INET6
- struct sockaddr_in6 address6;
-#endif
-};
-
-void
-java::net::PlainSocketImpl::create (jboolean stream)
-{
- int sock = _Jv_socket (AF_INET, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
-
- if (sock < 0)
- {
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
- }
-
- _Jv_platform_close_on_exec (sock);
-
- // We use fnum in place of fd here. From leaving fd null we avoid
- // the double close problem in FileDescriptor.finalize.
- fnum = sock;
-}
-
-void
-java::net::PlainSocketImpl::bind (java::net::InetAddress *host, jint lport)
-{
- union SockAddr u;
- struct sockaddr *ptr = (struct sockaddr *) &u.address;
- jbyteArray haddress = host->addr;
- jbyte *bytes = elements (haddress);
- int len = haddress->length;
- int i = 1;
-
- if (len == 4)
- {
- u.address.sin_family = AF_INET;
-
- if (host != NULL)
- memcpy (&u.address.sin_addr, bytes, len);
- else
- u.address.sin_addr.s_addr = htonl (INADDR_ANY);
-
- len = sizeof (struct sockaddr_in);
- u.address.sin_port = htons (lport);
- }
-#ifdef HAVE_INET6
- else if (len == 16)
- {
- u.address6.sin6_family = AF_INET6;
- memcpy (&u.address6.sin6_addr, bytes, len);
- len = sizeof (struct sockaddr_in6);
- u.address6.sin6_port = htons (lport);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
- // Enable SO_REUSEADDR, so that servers can reuse ports left in TIME_WAIT.
- ::setsockopt(fnum, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof(i));
-
- if (_Jv_bind (fnum, ptr, len) == 0)
- {
- address = host;
- socklen_t addrlen = sizeof(u);
-
- if (lport != 0)
- localport = lport;
- else if (::getsockname (fnum, (sockaddr*) &u, &addrlen) == 0)
- localport = ntohs (u.address.sin_port);
- else
- goto error;
-
- return;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::BindException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainSocketImpl::connect (java::net::SocketAddress *addr,
- jint timeout)
-{
- java::net::InetSocketAddress *tmp = (java::net::InetSocketAddress*) addr;
- java::net::InetAddress *host = tmp->getAddress();
- jint rport = tmp->getPort();
-
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- jbyteArray haddress = host->addr;
- jbyte *bytes = elements (haddress);
- int len = haddress->length;
- struct sockaddr *ptr = (struct sockaddr *) &u.address;
- if (len == 4)
- {
- u.address.sin_family = AF_INET;
- memcpy (&u.address.sin_addr, bytes, len);
- len = sizeof (struct sockaddr_in);
- u.address.sin_port = htons (rport);
- }
-#ifdef HAVE_INET6
- else if (len == 16)
- {
- u.address6.sin6_family = AF_INET6;
- memcpy (&u.address6.sin6_addr, bytes, len);
- len = sizeof (struct sockaddr_in6);
- u.address6.sin6_port = htons (rport);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid length"));
-
-// FIXME: implement timeout support for Win32
-#ifndef WIN32
- if (timeout > 0)
- {
- int flags = ::fcntl (fnum, F_GETFL);
- ::fcntl (fnum, F_SETFL, flags | O_NONBLOCK);
-
- if ((_Jv_connect (fnum, ptr, len) != 0) && (errno != EINPROGRESS))
- goto error;
-
- fd_set rset;
- struct timeval tv;
- FD_ZERO(&rset);
- FD_SET(fnum, &rset);
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout % 1000) * 1000;
- int retval;
-
- if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
- goto error;
- else if (retval == 0)
- throw new java::net::SocketTimeoutException
- (JvNewStringUTF ("Connect timed out"));
- }
- else
-#endif
- {
- if (_Jv_connect (fnum, ptr, len) != 0)
- goto error;
- }
-
- address = host;
- port = rport;
-
- // A bind may not have been done on this socket; if so, set localport now.
- if (localport == 0)
- {
- if (::getsockname (fnum, (sockaddr*) &u, &addrlen) == 0)
- localport = ntohs (u.address.sin_port);
- else
- goto error;
- }
-
- return;
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::ConnectException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainSocketImpl::listen (jint backlog)
-{
- if (::listen (fnum, backlog) != 0)
- {
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
- }
-}
-
-void
-java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *s)
-{
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- int new_socket = 0;
-
-// FIXME: implement timeout support for Win32
-#ifndef WIN32
- // Do timeouts via select since SO_RCVTIMEO is not always available.
- if (timeout > 0 && fnum >= 0 && fnum < FD_SETSIZE)
- {
- fd_set rset;
- struct timeval tv;
- FD_ZERO(&rset);
- FD_SET(fnum, &rset);
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout % 1000) * 1000;
- int retval;
- if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
- goto error;
- else if (retval == 0)
- throw new java::io::InterruptedIOException (
- JvNewStringUTF("Accept timed out"));
- }
-#endif /* WIN32 */
-
- new_socket = _Jv_accept (fnum, (sockaddr*) &u, &addrlen);
-
- if (new_socket < 0)
- goto error;
-
- _Jv_platform_close_on_exec (new_socket);
-
- jbyteArray raddr;
- jint rport;
- if (u.address.sin_family == AF_INET)
- {
- raddr = JvNewByteArray (4);
- memcpy (elements (raddr), &u.address.sin_addr, 4);
- rport = ntohs (u.address.sin_port);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- raddr = JvNewByteArray (16);
- memcpy (elements (raddr), &u.address6.sin6_addr, 16);
- rport = ntohs (u.address6.sin6_port);
- }
-#endif
- else
- throw new java::net::SocketException (JvNewStringUTF ("invalid family"));
-
- s->fnum = new_socket;
- s->localport = localport;
- s->address = new InetAddress (raddr, NULL);
- s->port = rport;
- return;
-
- error:
- char* strerr = strerror (errno);
- throw new java::io::IOException (JvNewStringUTF (strerr));
-}
-
-// Close(shutdown) the socket.
-void
-java::net::PlainSocketImpl::close()
-{
- // Avoid races from asynchronous finalization.
- JvSynchronize sync (this);
-
- // should we use shutdown here? how would that effect so_linger?
- int res = _Jv_close (fnum);
-
- if (res == -1)
- {
- // These three errors are not errors according to tests performed
- // on the reference implementation.
- if (errno != ENOTCONN && errno != ECONNRESET && errno != EBADF)
- throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
- }
- // Safe place to reset the file pointer.
- fnum = -1;
- timeout = 0;
-}
-
-// Write a byte to the socket.
-void
-java::net::PlainSocketImpl::write(jint b)
-{
- jbyte d =(jbyte) b;
- int r = 0;
-
- while (r != 1)
- {
- r = _Jv_write (fnum, &d, 1);
- if (r == -1)
- {
- if (java::lang::Thread::interrupted())
- {
- java::io::InterruptedIOException *iioe
- = new java::io::InterruptedIOException
- (JvNewStringLatin1 (strerror (errno)));
- iioe->bytesTransferred = 0;
- throw iioe;
- }
- // Some errors should not cause exceptions.
- if (errno != ENOTCONN && errno != ECONNRESET && errno != EBADF)
- throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
- break;
- }
- }
-}
-
-// Write some bytes to the socket.
-void
-java::net::PlainSocketImpl::write(jbyteArray b, jint offset, jint len)
-{
- if (! b)
- throw new java::lang::NullPointerException;
- if (offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
- throw new java::lang::ArrayIndexOutOfBoundsException;
-
- jbyte *bytes = elements (b) + offset;
- int written = 0;
-
- while (len > 0)
- {
- int r = _Jv_write (fnum, bytes, len);
-
- if (r == -1)
- {
- if (java::lang::Thread::interrupted())
- {
- java::io::InterruptedIOException *iioe
- = new java::io::InterruptedIOException
- (JvNewStringLatin1 (strerror (errno)));
- iioe->bytesTransferred = written;
- throw iioe;
- }
- // Some errors should not cause exceptions.
- if (errno != ENOTCONN && errno != ECONNRESET && errno != EBADF)
- throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
- break;
- }
-
- written += r;
- len -= r;
- bytes += r;
- }
-}
-
-void
-java::net::PlainSocketImpl::sendUrgentData (jint)
-{
- throw new SocketException (JvNewStringLatin1 (
- "PlainSocketImpl: sending of urgent data not supported by this socket"));
-}
-
-// Read a single byte from the socket.
-jint
-java::net::PlainSocketImpl::read(void)
-{
- jbyte b;
-
-// FIXME: implement timeout support for Win32
-#ifndef WIN32
- // Do timeouts via select.
- if (timeout > 0 && fnum >= 0 && fnum < FD_SETSIZE)
- {
- // Create the file descriptor set.
- fd_set read_fds;
- FD_ZERO (&read_fds);
- FD_SET (fnum,&read_fds);
- // Create the timeout struct based on our internal timeout value.
- struct timeval timeout_value;
- timeout_value.tv_sec = timeout / 1000;
- timeout_value.tv_usec = (timeout % 1000) * 1000;
- // Select on the fds.
- int sel_retval =
- _Jv_select (fnum + 1, &read_fds, NULL, NULL, &timeout_value);
- // If select returns 0 we've waited without getting data...
- // that means we've timed out.
- if (sel_retval == 0)
- throw new java::io::InterruptedIOException
- (JvNewStringUTF ("read timed out") );
- // If select returns ok we know we either got signalled or read some data...
- // either way we need to try to read.
- }
-#endif /* WIN32 */
-
- int r = _Jv_read (fnum, &b, 1);
-
- if (r == 0)
- return -1;
-
- if (java::lang::Thread::interrupted())
- {
- java::io::InterruptedIOException *iioe =
- new java::io::InterruptedIOException
- (JvNewStringUTF("read interrupted"));
- iioe->bytesTransferred = r == -1 ? 0 : r;
- throw iioe;
- }
- else if (r == -1)
- {
- // Some errors cause us to return end of stream...
- if (errno == ENOTCONN)
- return -1;
-
- // Other errors need to be signalled.
- throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
- }
-
- return b & 0xFF;
-}
-
-// Read count bytes into the buffer, starting at offset.
-jint
-java::net::PlainSocketImpl::read(jbyteArray buffer, jint offset, jint count)
-{
- if (! buffer)
- throw new java::lang::NullPointerException;
-
- jsize bsize = JvGetArrayLength (buffer);
-
- if (offset < 0 || count < 0 || offset + count > bsize)
- throw new java::lang::ArrayIndexOutOfBoundsException;
-
- jbyte *bytes = elements (buffer) + offset;
-
-// FIXME: implement timeout support for Win32
-#ifndef WIN32
- // Do timeouts via select.
- if (timeout > 0 && fnum >= 0 && fnum < FD_SETSIZE)
- {
- // Create the file descriptor set.
- fd_set read_fds;
- FD_ZERO (&read_fds);
- FD_SET (fnum, &read_fds);
- // Create the timeout struct based on our internal timeout value.
- struct timeval timeout_value;
- timeout_value.tv_sec = timeout / 1000;
- timeout_value.tv_usec =(timeout % 1000) * 1000;
- // Select on the fds.
- int sel_retval =
- _Jv_select (fnum + 1, &read_fds, NULL, NULL, &timeout_value);
- // We're only interested in the 0 return.
- // error returns still require us to try to read
- // the socket to see what happened.
- if (sel_retval == 0)
- {
- java::io::InterruptedIOException *iioe =
- new java::io::InterruptedIOException
- (JvNewStringUTF ("read interrupted"));
- iioe->bytesTransferred = 0;
- throw iioe;
- }
- }
-#endif
-
- // Read the socket.
- int r = ::recv (fnum, (char *) bytes, count, 0);
-
- if (r == 0)
- return -1;
-
- if (java::lang::Thread::interrupted())
- {
- java::io::InterruptedIOException *iioe =
- new java::io::InterruptedIOException
- (JvNewStringUTF ("read interrupted"));
- iioe->bytesTransferred = r == -1 ? 0 : r;
- throw iioe;
- }
- else if (r == -1)
- {
- // Some errors cause us to return end of stream...
- if (errno == ENOTCONN)
- return -1;
-
- // Other errors need to be signalled.
- throw new java::io::IOException (JvNewStringUTF (strerror (errno)));
- }
-
- return r;
-}
-
-// How many bytes are available?
-jint
-java::net::PlainSocketImpl::available(void)
-{
-#if defined(FIONREAD) || defined(HAVE_SELECT)
- long num = 0;
- int r = 0;
- bool num_set = false;
-
-#if defined(FIONREAD)
- r = ::ioctl (fnum, FIONREAD, &num);
-
- if (r == -1 && errno == ENOTTY)
- {
- // If the ioctl doesn't work, we don't care.
- r = 0;
- num = 0;
- }
- else
- num_set = true;
-#elif defined(HAVE_SELECT)
- if (fnum < 0)
- {
- errno = EBADF;
- r = -1;
- }
-#endif
-
- if (r == -1)
- {
- posix_error:
- throw new java::io::IOException(JvNewStringUTF(strerror(errno)));
- }
-
- // If we didn't get anything we can use select.
-
-#if defined(HAVE_SELECT)
- if (! num_set)
- if (! num_set && fnum >= 0 && fnum < FD_SETSIZE)
- {
- fd_set rd;
- FD_ZERO (&rd);
- FD_SET (fnum, &rd);
- struct timeval tv;
- tv.tv_sec = 0;
- tv.tv_usec = 0;
- r = _Jv_select (fnum + 1, &rd, NULL, NULL, &tv);
- if(r == -1)
- goto posix_error;
- num = r == 0 ? 0 : 1;
- }
-#endif /* HAVE_SELECT */
-
- return (jint) num;
-#else
- throw new java::io::IOException (JvNewStringUTF ("unimplemented"));
-#endif
-}
-
-void
-java::net::PlainSocketImpl::setOption (jint optID, java::lang::Object *value)
-{
- int val;
- socklen_t val_len = sizeof (val);
-
- if (fnum < 0)
- throw new java::net::SocketException (JvNewStringUTF ("Socket closed"));
-
- if (_Jv_IsInstanceOf (value, &java::lang::Boolean::class$))
- {
- java::lang::Boolean *boolobj =
- static_cast<java::lang::Boolean *> (value);
- if (boolobj->booleanValue())
- val = 1;
- else
- {
- if (optID == _Jv_SO_LINGER_)
- val = -1;
- else
- val = 0;
- }
- }
- else if (_Jv_IsInstanceOf (value, &java::lang::Integer::class$))
- {
- java::lang::Integer *intobj =
- static_cast<java::lang::Integer *> (value);
- val = (int) intobj->intValue();
- }
- else
- {
- throw new java::lang::IllegalArgumentException (
- JvNewStringLatin1 ("`value' must be Boolean or Integer"));
- }
-
- switch (optID)
- {
- case _Jv_TCP_NODELAY_ :
-#ifdef TCP_NODELAY
- if (::setsockopt (fnum, IPPROTO_TCP, TCP_NODELAY, (char *) &val,
- val_len) != 0)
- goto error;
-#else
- throw new java::lang::InternalError
- (JvNewStringUTF ("TCP_NODELAY not supported"));
-#endif /* TCP_NODELAY */
- return;
-
- case _Jv_SO_KEEPALIVE_ :
- if (::setsockopt (fnum, SOL_SOCKET, SO_KEEPALIVE, (char *) &val,
- val_len) != 0)
- goto error;
- break;
-
- case _Jv_SO_BROADCAST_ :
- throw new java::net::SocketException
- (JvNewStringUTF ("SO_BROADCAST not valid for TCP"));
- break;
-
- case _Jv_SO_OOBINLINE_ :
- if (::setsockopt (fnum, SOL_SOCKET, SO_OOBINLINE, (char *) &val,
- val_len) != 0)
- goto error;
- break;
-
- case _Jv_SO_LINGER_ :
-#ifdef SO_LINGER
- struct linger l_val;
- l_val.l_onoff = (val != -1);
- l_val.l_linger = val;
-
- if (::setsockopt (fnum, SOL_SOCKET, SO_LINGER, (char *) &l_val,
- sizeof(l_val)) != 0)
- goto error;
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_LINGER not supported"));
-#endif /* SO_LINGER */
- return;
-
- case _Jv_SO_SNDBUF_ :
- case _Jv_SO_RCVBUF_ :
-#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
- int opt;
- optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
- if (::setsockopt (fnum, SOL_SOCKET, opt, (char *) &val, val_len) != 0)
- goto error;
-#else
- throw new java::lang::InternalError (
- JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
-#endif
- return;
-
- case _Jv_SO_BINDADDR_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_BINDADDR: read only option"));
- return;
-
- case _Jv_IP_MULTICAST_IF_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP"));
- return;
-
- case _Jv_IP_MULTICAST_IF2_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_IF2: not valid for TCP"));
- break;
-
- case _Jv_IP_MULTICAST_LOOP_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("IP_MULTICAST_LOOP: not valid for TCP"));
- break;
-
- case _Jv_IP_TOS_ :
- if (::setsockopt (fnum, SOL_SOCKET, IP_TOS, (char *) &val,
- val_len) != 0)
- goto error;
- break;
-
- case _Jv_SO_REUSEADDR_ :
- throw new java::net::SocketException (
- JvNewStringUTF ("SO_REUSEADDR: not valid for TCP"));
- return;
-
- case _Jv_SO_TIMEOUT_ :
- timeout = val;
- return;
-
- default :
- errno = ENOPROTOOPT;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::SocketException (JvNewStringUTF (strerr));
-}
-
-java::lang::Object *
-java::net::PlainSocketImpl::getOption (jint optID)
-{
- int val;
- socklen_t val_len = sizeof(val);
- union SockAddr u;
- socklen_t addrlen = sizeof(u);
- struct linger l_val;
- socklen_t l_val_len = sizeof(l_val);
-
- switch (optID)
- {
-#ifdef TCP_NODELAY
- case _Jv_TCP_NODELAY_ :
- if (::getsockopt (fnum, IPPROTO_TCP, TCP_NODELAY, (char *) &val,
- &val_len) != 0)
- goto error;
- else
- return new java::lang::Boolean (val != 0);
-#else
- throw new java::lang::InternalError
- (JvNewStringUTF ("TCP_NODELAY not supported"));
-#endif
- break;
-
- case _Jv_SO_LINGER_ :
-#ifdef SO_LINGER
- if (::getsockopt (fnum, SOL_SOCKET, SO_LINGER, (char *) &l_val,
- &l_val_len) != 0)
- goto error;
-
- if (l_val.l_onoff)
- return new java::lang::Integer (l_val.l_linger);
- else
- return new java::lang::Boolean ((jboolean)false);
-#else
- throw new java::lang::InternalError
- (JvNewStringUTF ("SO_LINGER not supported"));
-#endif
- break;
-
- case _Jv_SO_KEEPALIVE_ :
- if (::getsockopt (fnum, SOL_SOCKET, SO_KEEPALIVE, (char *) &val,
- &val_len) != 0)
- goto error;
- else
- return new java::lang::Boolean (val != 0);
-
- case _Jv_SO_BROADCAST_ :
- if (::getsockopt (fnum, SOL_SOCKET, SO_BROADCAST, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Boolean ((jboolean)val);
-
- case _Jv_SO_OOBINLINE_ :
- if (::getsockopt (fnum, SOL_SOCKET, SO_OOBINLINE, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Boolean ((jboolean)val);
-
- case _Jv_SO_RCVBUF_ :
- case _Jv_SO_SNDBUF_ :
-#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
- int opt;
- optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
- if (::getsockopt (fnum, SOL_SOCKET, opt, (char *) &val, &val_len) != 0)
- goto error;
- else
- return new java::lang::Integer (val);
-#else
- throw new java::lang::InternalError
- (JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
-#endif
- break;
- case _Jv_SO_BINDADDR_:
- // cache the local address
- if (localAddress == NULL)
- {
- jbyteArray laddr;
-
- if (::getsockname (fnum, (sockaddr*) &u, &addrlen) != 0)
- goto error;
-
- if (u.address.sin_family == AF_INET)
- {
- laddr = JvNewByteArray (4);
- memcpy (elements (laddr), &u.address.sin_addr, 4);
- }
-#ifdef HAVE_INET6
- else if (u.address.sin_family == AF_INET6)
- {
- laddr = JvNewByteArray (16);
- memcpy (elements (laddr), &u.address6.sin6_addr, 16);
- }
-#endif
- else
- throw new java::net::SocketException
- (JvNewStringUTF ("invalid family"));
- localAddress = new java::net::InetAddress (laddr, NULL);
- }
-
- return localAddress;
- break;
- case _Jv_IP_MULTICAST_IF_ :
- throw new java::net::SocketException
- (JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP"));
- break;
-
- case _Jv_IP_MULTICAST_IF2_ :
- throw new java::net::SocketException
- (JvNewStringUTF ("IP_MULTICAST_IF2: not valid for TCP"));
- break;
-
- case _Jv_IP_MULTICAST_LOOP_ :
- throw new java::net::SocketException
- (JvNewStringUTF ("IP_MULTICAST_LOOP: not valid for TCP"));
- break;
-
- case _Jv_IP_TOS_ :
- if (::getsockopt (fnum, SOL_SOCKET, IP_TOS, (char *) &val,
- &val_len) != 0)
- goto error;
- return new java::lang::Integer (val);
- break;
-
- case _Jv_SO_REUSEADDR_ :
- throw new java::net::SocketException
- (JvNewStringUTF ("SO_REUSEADDR: not valid for TCP"));
- break;
-
- case _Jv_SO_TIMEOUT_ :
- return new java::lang::Integer (timeout);
- break;
-
- default :
- errno = ENOPROTOOPT;
- }
-
- error:
- char* strerr = strerror (errno);
- throw new java::net::SocketException (JvNewStringUTF (strerr));
-}
-
-void
-java::net::PlainSocketImpl::shutdownInput (void)
-{
- if (::shutdown (fnum, 0))
- throw new SocketException (JvNewStringUTF (strerror (errno)));
-}
-
-void
-java::net::PlainSocketImpl::shutdownOutput (void)
-{
- if (::shutdown (fnum, 1))
- throw new SocketException (JvNewStringUTF (strerror (errno)));
-}
-
-#endif /* DISABLE_JAVA_NET */