diff options
author | jfrijters <jfrijters> | 2010-08-27 10:21:56 +0400 |
---|---|---|
committer | jfrijters <jfrijters> | 2010-08-27 10:21:56 +0400 |
commit | 663c7284c44161667671a330cd8233e45e329b59 (patch) | |
tree | a196a44c97275a0c53ace687445a46e1caff2686 /openjdk/java/net | |
parent | 13955a090352de779a2bc53f147f6d626718b032 (diff) |
Moved shared socket functionality from PlainSocketImpl.java to SocketUtil.java.
Diffstat (limited to 'openjdk/java/net')
-rw-r--r-- | openjdk/java/net/PlainDatagramSocketImpl.java | 34 | ||||
-rw-r--r-- | openjdk/java/net/PlainSocketImpl.java | 160 | ||||
-rw-r--r-- | openjdk/java/net/SocketUtil.java | 169 |
3 files changed, 202 insertions, 161 deletions
diff --git a/openjdk/java/net/PlainDatagramSocketImpl.java b/openjdk/java/net/PlainDatagramSocketImpl.java index e797d2af..0ec993d9 100644 --- a/openjdk/java/net/PlainDatagramSocketImpl.java +++ b/openjdk/java/net/PlainDatagramSocketImpl.java @@ -123,7 +123,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl { if (false) throw new cli.System.Net.Sockets.SocketException(); if (false) throw new cli.System.ObjectDisposedException(""); - netSocket.Bind(new IPEndPoint(PlainSocketImpl.getAddressFromInetAddress(laddr), lport)); + netSocket.Bind(new IPEndPoint(SocketUtil.getAddressFromInetAddress(laddr), lport)); localPort = ((IPEndPoint)netSocket.get_LocalEndPoint()).get_Port(); } catch (cli.System.Net.Sockets.SocketException x) @@ -153,14 +153,14 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl { throw new SocketException("Invalid port"); } - if (netSocket.SendTo(p.getData(), p.getOffset(), len, SocketFlags.wrap(SocketFlags.None), new IPEndPoint(PlainSocketImpl.getAddressFromInetAddress(p.getAddress()), port)) != len) + if (netSocket.SendTo(p.getData(), p.getOffset(), len, SocketFlags.wrap(SocketFlags.None), new IPEndPoint(SocketUtil.getAddressFromInetAddress(p.getAddress()), port)) != len) { throw new SocketException("Not all data was sent"); } } catch (cli.System.Net.Sockets.SocketException x) { - throw PlainSocketImpl.convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ObjectDisposedException x1) { @@ -288,7 +288,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl length = p.bufLength; break; } - throw PlainSocketImpl.convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ObjectDisposedException x1) { @@ -296,7 +296,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl } } IPEndPoint endpoint = (IPEndPoint)remoteEP[0]; - p.address = PlainSocketImpl.getInetAddressFromIPEndPoint(endpoint); + p.address = SocketUtil.getInetAddressFromIPEndPoint(endpoint); p.port = endpoint.get_Port(); p.length = length; } @@ -400,7 +400,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl if (false) throw new cli.System.Net.Sockets.SocketException(); if (false) throw new cli.System.ArgumentException(); if (false) throw new cli.System.ObjectDisposedException(""); - IPAddress mcastAddr = PlainSocketImpl.getAddressFromInetAddress(inetaddr); + IPAddress mcastAddr = SocketUtil.getAddressFromInetAddress(inetaddr); if (netIf == null) { netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.AddMembership), new MulticastOption(mcastAddr)); @@ -410,7 +410,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl Enumeration e = netIf.getInetAddresses(); if (e.hasMoreElements()) { - IPAddress bindAddr = PlainSocketImpl.getAddressFromInetAddress((InetAddress)e.nextElement()); + IPAddress bindAddr = SocketUtil.getAddressFromInetAddress((InetAddress)e.nextElement()); MulticastOption mcastOption = new MulticastOption(mcastAddr, bindAddr); netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.AddMembership), mcastOption); } @@ -418,7 +418,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl } catch (cli.System.Net.Sockets.SocketException x) { - throw PlainSocketImpl.convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ArgumentException x1) { @@ -452,7 +452,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl if (false) throw new cli.System.Net.Sockets.SocketException(); if (false) throw new cli.System.ArgumentException(); if (false) throw new cli.System.ObjectDisposedException(""); - IPAddress mcastAddr = PlainSocketImpl.getAddressFromInetAddress(inetaddr); + IPAddress mcastAddr = SocketUtil.getAddressFromInetAddress(inetaddr); if (netIf == null) { netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.DropMembership), new MulticastOption(mcastAddr)); @@ -462,7 +462,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl Enumeration e = netIf.getInetAddresses(); if (e.hasMoreElements()) { - IPAddress bindAddr = PlainSocketImpl.getAddressFromInetAddress((InetAddress)e.nextElement()); + IPAddress bindAddr = SocketUtil.getAddressFromInetAddress((InetAddress)e.nextElement()); MulticastOption mcastOption = new MulticastOption(mcastAddr, bindAddr); netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.DropMembership), mcastOption); } @@ -470,7 +470,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl } catch (cli.System.Net.Sockets.SocketException x) { - throw PlainSocketImpl.convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ArgumentException x1) { @@ -643,7 +643,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl case SocketOptions.IP_MULTICAST_IF: { InetAddress addr = (InetAddress)val; - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface), (int)PlainSocketImpl.getAddressFromInetAddress(addr).get_Address()); + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface), (int)SocketUtil.getAddressFromInetAddress(addr).get_Address()); break; } case SocketOptions.IP_MULTICAST_IF2: @@ -655,7 +655,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl InetAddress addr = (InetAddress)e.nextElement(); if (addr.getAddress().length == 4) { - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface), (int)PlainSocketImpl.getAddressFromInetAddress(addr).get_Address()); + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface), (int)SocketUtil.getAddressFromInetAddress(addr).get_Address()); return; } } @@ -665,10 +665,10 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastLoopback), ((Boolean)val).booleanValue() ? 1 : 0); break; case SocketOptions.SO_REUSEADDR: - PlainSocketImpl.setCommonSocketOption(netSocket, opt, ((Boolean)val).booleanValue(), null); + SocketUtil.setCommonSocketOption(netSocket, opt, ((Boolean)val).booleanValue(), null); break; default: - PlainSocketImpl.setCommonSocketOption(netSocket, opt, false, val); + SocketUtil.setCommonSocketOption(netSocket, opt, false, val); break; } } @@ -723,7 +723,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl // TODO handle IPv6 here return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.TypeOfService))); case SocketOptions.SO_BINDADDR: - return PlainSocketImpl.getInetAddressFromIPEndPoint((IPEndPoint)netSocket.get_LocalEndPoint()); + return SocketUtil.getInetAddressFromIPEndPoint((IPEndPoint)netSocket.get_LocalEndPoint()); default: throw new SocketException("Invalid socket option: " + opt); } @@ -744,7 +744,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl { if (false) throw new cli.System.Net.Sockets.SocketException(); if (false) throw new cli.System.ObjectDisposedException(""); - IPEndPoint ep = new IPEndPoint(PlainSocketImpl.getAddressFromInetAddress(address), port); + IPEndPoint ep = new IPEndPoint(SocketUtil.getAddressFromInetAddress(address), port); // NOTE we use async connect to work around the issue that the .NET Socket class disallows sync Connect after the socket has received WSAECONNRESET netSocket.EndConnect(netSocket.BeginConnect(ep, null, null)); netSocket.IOControl(SIO_UDP_CONNRESET, new byte[] { 1 }, null); diff --git a/openjdk/java/net/PlainSocketImpl.java b/openjdk/java/net/PlainSocketImpl.java index 01f41439..84bc91c0 100644 --- a/openjdk/java/net/PlainSocketImpl.java +++ b/openjdk/java/net/PlainSocketImpl.java @@ -57,134 +57,6 @@ import sun.net.ConnectionResetException; @ikvm.lang.Internal public class PlainSocketImpl extends SocketImpl { - // Winsock Error Codes - public static final int WSAEWOULDBLOCK = 10035; - private static final int WSAEADDRINUSE = 10048; - private static final int WSAENETUNREACH = 10051; - public static final int WSAESHUTDOWN = 10058; - private static final int WSAETIMEDOUT = 10060; - private static final int WSAECONNREFUSED = 10061; - private static final int WSAEHOSTUNREACH = 10065; - private static final int WSAHOST_NOT_FOUND = 11001; - - public static IOException convertSocketExceptionToIOException(cli.System.Net.Sockets.SocketException x) throws IOException - { - switch (x.get_ErrorCode()) - { - case WSAEADDRINUSE: - return new BindException(x.getMessage()); - case WSAENETUNREACH: - case WSAEHOSTUNREACH: - return new NoRouteToHostException(x.getMessage()); - case WSAETIMEDOUT: - return new SocketTimeoutException(x.getMessage()); - case WSAECONNREFUSED: - return new PortUnreachableException(x.getMessage()); - case WSAHOST_NOT_FOUND: - return new UnknownHostException(x.getMessage()); - default: - return new SocketException(x.getMessage() + "\nError Code: " + x.get_ErrorCode()); - } - } - - public static IPAddress getAddressFromInetAddress(InetAddress addr) - { - byte[] b = addr.getAddress(); - if (b.length == 16) - { - // FXBUG in .NET 1.1 you can only construct IPv6 addresses (not IPv4) with this constructor - // (according to the documentation this was fixed in .NET 2.0) - return new IPAddress(b); - } - else - { - return new IPAddress((((b[3] & 0xff) << 24) + ((b[2] & 0xff) << 16) + ((b[1] & 0xff) << 8) + (b[0] & 0xff)) & 0xffffffffL); - } - } - - public static InetAddress getInetAddressFromIPEndPoint(IPEndPoint endpoint) - { - try - { - return InetAddress.getByAddress(endpoint.get_Address().GetAddressBytes()); - } - catch (UnknownHostException x) - { - // this exception only happens if the address byte array is of invalid length, which cannot happen unless - // the .NET socket returns a bogus address - throw (InternalError)new InternalError().initCause(x); - } - } - - static void setCommonSocketOption(cli.System.Net.Sockets.Socket netSocket, int cmd, boolean on, Object value) throws SocketException - { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - switch (cmd) - { - case SocketOptions.SO_REUSEADDR: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReuseAddress), on ? 1 : 0); - break; - case SocketOptions.SO_SNDBUF: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.SendBuffer), ((Integer)value).intValue()); - break; - case SocketOptions.SO_RCVBUF: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveBuffer), ((Integer)value).intValue()); - break; - case SocketOptions.IP_TOS: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.TypeOfService), ((Integer)value).intValue()); - break; - case SocketOptions.SO_BINDADDR: // read-only - default: - throw new SocketException("Invalid socket option: " + cmd); - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new SocketException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } - } - - static int getCommonSocketOption(cli.System.Net.Sockets.Socket netSocket, int opt, Object iaContainerObj) throws SocketException - { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - switch (opt) - { - case SocketOptions.SO_REUSEADDR: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReuseAddress))) == 0 ? -1 : 1; - case SocketOptions.SO_SNDBUF: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.SendBuffer))); - case SocketOptions.SO_RCVBUF: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveBuffer))); - case SocketOptions.IP_TOS: - // TODO handle IPv6 here - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.TypeOfService))); - case SocketOptions.SO_BINDADDR: - ((InetAddressContainer)iaContainerObj).addr = getInetAddressFromIPEndPoint((IPEndPoint)netSocket.get_LocalEndPoint()); - return 0; - default: - throw new SocketException("Invalid socket option: " + opt); - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new SocketException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } - } - private cli.System.Net.Sockets.Socket netSocket; /* instance variable for SO_TIMEOUT */ int timeout; // timeout in millisec @@ -827,7 +699,7 @@ public class PlainSocketImpl extends SocketImpl } catch (cli.System.Net.Sockets.SocketException x) { - throw convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ObjectDisposedException x1) { @@ -841,7 +713,7 @@ public class PlainSocketImpl extends SocketImpl { if (false) throw new cli.System.Net.Sockets.SocketException(); if (false) throw new cli.System.ObjectDisposedException(""); - IPEndPoint ep = new IPEndPoint(getAddressFromInetAddress(address), port); + IPEndPoint ep = new IPEndPoint(SocketUtil.getAddressFromInetAddress(address), port); if (timeout <= 0) { netSocket.Connect(ep); @@ -879,7 +751,7 @@ public class PlainSocketImpl extends SocketImpl { if (false) throw new cli.System.Net.Sockets.SocketException(); if (false) throw new cli.System.ObjectDisposedException(""); - netSocket.Bind(new IPEndPoint(getAddressFromInetAddress(address), localport)); + netSocket.Bind(new IPEndPoint(SocketUtil.getAddressFromInetAddress(address), localport)); this.address = address; if (localport == 0) { @@ -910,7 +782,7 @@ public class PlainSocketImpl extends SocketImpl } catch (cli.System.Net.Sockets.SocketException x) { - throw convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ObjectDisposedException x1) { @@ -933,13 +805,13 @@ public class PlainSocketImpl extends SocketImpl cli.System.Net.Sockets.Socket accept = netSocket.Accept(); impl.netSocket = accept; IPEndPoint remoteEndPoint = ((IPEndPoint)accept.get_RemoteEndPoint()); - impl.address = getInetAddressFromIPEndPoint(remoteEndPoint); + impl.address = SocketUtil.getInetAddressFromIPEndPoint(remoteEndPoint); impl.port = remoteEndPoint.get_Port(); impl.localport = ((IPEndPoint)accept.get_LocalEndPoint()).get_Port(); } catch (cli.System.Net.Sockets.SocketException x) { - throw convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ObjectDisposedException x1) { @@ -958,7 +830,7 @@ public class PlainSocketImpl extends SocketImpl } catch (cli.System.Net.Sockets.SocketException x) { - throw convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ObjectDisposedException x1) { @@ -979,7 +851,7 @@ public class PlainSocketImpl extends SocketImpl } catch (cli.System.Net.Sockets.SocketException x) { - throw convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ObjectDisposedException x1) { @@ -997,7 +869,7 @@ public class PlainSocketImpl extends SocketImpl } catch (cli.System.Net.Sockets.SocketException x) { - throw convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ObjectDisposedException x1) { @@ -1039,7 +911,7 @@ public class PlainSocketImpl extends SocketImpl netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.OutOfBandInline), on ? 1 : 0); break; default: - setCommonSocketOption(netSocket, cmd, on, value); + SocketUtil.setCommonSocketOption(netSocket, cmd, on, value); break; } } @@ -1077,7 +949,7 @@ public class PlainSocketImpl extends SocketImpl case SocketOptions.SO_OOBINLINE: return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.OutOfBandInline))) == 0 ? -1 : 1; default: - return getCommonSocketOption(netSocket, opt, iaContainerObj); + return SocketUtil.getCommonSocketOption(netSocket, opt, iaContainerObj); } } catch (cli.System.Net.Sockets.SocketException x) @@ -1101,7 +973,7 @@ public class PlainSocketImpl extends SocketImpl } catch (cli.System.Net.Sockets.SocketException x) { - throw convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ObjectDisposedException x1) { @@ -1126,17 +998,17 @@ public class PlainSocketImpl extends SocketImpl } catch (cli.System.Net.Sockets.SocketException x) { - if (x.get_ErrorCode() == WSAESHUTDOWN) + if (x.get_ErrorCode() == SocketUtil.WSAESHUTDOWN) { // the socket was shutdown, so we have to return EOF return -1; } - else if (x.get_ErrorCode() == WSAEWOULDBLOCK) + else if (x.get_ErrorCode() == SocketUtil.WSAEWOULDBLOCK) { // nothing to read and would block return 0; } - throw convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ObjectDisposedException x1) { @@ -1155,7 +1027,7 @@ public class PlainSocketImpl extends SocketImpl } catch (cli.System.Net.Sockets.SocketException x) { - throw convertSocketExceptionToIOException(x); + throw SocketUtil.convertSocketExceptionToIOException(x); } catch (cli.System.ObjectDisposedException x1) { diff --git a/openjdk/java/net/SocketUtil.java b/openjdk/java/net/SocketUtil.java new file mode 100644 index 00000000..5bf69ead --- /dev/null +++ b/openjdk/java/net/SocketUtil.java @@ -0,0 +1,169 @@ +/* + Copyright (C) 2002-2010 Jeroen Frijters + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jeroen Frijters + jeroen@frijters.net + +*/ + +package java.net; + +import cli.System.Net.IPAddress; +import cli.System.Net.IPEndPoint; +import cli.System.Net.Sockets.SocketOptionName; +import cli.System.Net.Sockets.SocketOptionLevel; +import ikvm.lang.CIL; +import java.io.IOException; + +@ikvm.lang.Internal +public final class SocketUtil +{ + private SocketUtil() { } + + // Winsock Error Codes + public static final int WSAEINVAL = 10022; + public static final int WSAEWOULDBLOCK = 10035; + public static final int WSAEMSGSIZE = 10040; + public static final int WSAEADDRINUSE = 10048; + public static final int WSAENETUNREACH = 10051; + public static final int WSAECONNRESET = 10054; + public static final int WSAESHUTDOWN = 10058; + public static final int WSAETIMEDOUT = 10060; + public static final int WSAECONNREFUSED = 10061; + public static final int WSAEHOSTUNREACH = 10065; + public static final int WSAHOST_NOT_FOUND = 11001; + + public static IOException convertSocketExceptionToIOException(cli.System.Net.Sockets.SocketException x) throws IOException + { + switch (x.get_ErrorCode()) + { + case WSAEADDRINUSE: + return new BindException(x.getMessage()); + case WSAENETUNREACH: + case WSAEHOSTUNREACH: + return new NoRouteToHostException(x.getMessage()); + case WSAETIMEDOUT: + return new SocketTimeoutException(x.getMessage()); + case WSAECONNREFUSED: + return new PortUnreachableException(x.getMessage()); + case WSAHOST_NOT_FOUND: + return new UnknownHostException(x.getMessage()); + default: + return new SocketException(x.getMessage() + "\nError Code: " + x.get_ErrorCode()); + } + } + + public static IPAddress getAddressFromInetAddress(InetAddress addr) + { + byte[] b = addr.getAddress(); + if (b.length == 16) + { + // FXBUG in .NET 1.1 you can only construct IPv6 addresses (not IPv4) with this constructor + // (according to the documentation this was fixed in .NET 2.0) + return new IPAddress(b); + } + else + { + return new IPAddress((((b[3] & 0xff) << 24) + ((b[2] & 0xff) << 16) + ((b[1] & 0xff) << 8) + (b[0] & 0xff)) & 0xffffffffL); + } + } + + public static InetAddress getInetAddressFromIPEndPoint(IPEndPoint endpoint) + { + try + { + return InetAddress.getByAddress(endpoint.get_Address().GetAddressBytes()); + } + catch (UnknownHostException x) + { + // this exception only happens if the address byte array is of invalid length, which cannot happen unless + // the .NET socket returns a bogus address + throw (InternalError)new InternalError().initCause(x); + } + } + + static void setCommonSocketOption(cli.System.Net.Sockets.Socket netSocket, int cmd, boolean on, Object value) throws SocketException + { + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + switch (cmd) + { + case SocketOptions.SO_REUSEADDR: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReuseAddress), on ? 1 : 0); + break; + case SocketOptions.SO_SNDBUF: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.SendBuffer), ((Integer)value).intValue()); + break; + case SocketOptions.SO_RCVBUF: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveBuffer), ((Integer)value).intValue()); + break; + case SocketOptions.IP_TOS: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.TypeOfService), ((Integer)value).intValue()); + break; + case SocketOptions.SO_BINDADDR: // read-only + default: + throw new SocketException("Invalid socket option: " + cmd); + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new SocketException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } + } + + static int getCommonSocketOption(cli.System.Net.Sockets.Socket netSocket, int opt, Object iaContainerObj) throws SocketException + { + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + switch (opt) + { + case SocketOptions.SO_REUSEADDR: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReuseAddress))) == 0 ? -1 : 1; + case SocketOptions.SO_SNDBUF: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.SendBuffer))); + case SocketOptions.SO_RCVBUF: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveBuffer))); + case SocketOptions.IP_TOS: + // TODO handle IPv6 here + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.TypeOfService))); + case SocketOptions.SO_BINDADDR: + ((InetAddressContainer)iaContainerObj).addr = getInetAddressFromIPEndPoint((IPEndPoint)netSocket.get_LocalEndPoint()); + return 0; + default: + throw new SocketException("Invalid socket option: " + opt); + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new SocketException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } + } +} |