package sun.nio.ch;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.nio.channels.*;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
class Net {
private Net() { }
private static boolean revealLocalAddress;
private static volatile boolean propRevealLocalAddress;
private static final boolean exclusiveBind;
static {
int availLevel = isExclusiveBindAvailable();
if (availLevel >= 0) {
String exclBindProp =
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<String>() {
public String run() {
return System.getProperty(
"sun.net.useExclusiveBind");
}
});
if (exclBindProp != null) {
exclusiveBind = exclBindProp.length() == 0 ?
true : Boolean.parseBoolean(exclBindProp);
} else if (availLevel == 1) {
exclusiveBind = true;
} else {
exclusiveBind = false;
}
} else {
exclusiveBind = false;
}
}
static boolean useExclusiveBind() {
return exclusiveBind;
}
static InetSocketAddress checkAddress(SocketAddress sa) {
if (sa == null)
throw new IllegalArgumentException();
if (!(sa instanceof InetSocketAddress))
throw new UnsupportedAddressTypeException();
InetSocketAddress isa = (InetSocketAddress)sa;
if (isa.isUnresolved())
throw new UnresolvedAddressException();
InetAddress addr = isa.getAddress();
if (!(addr instanceof Inet4Address || addr instanceof Inet6Address))
throw new IllegalArgumentException("Invalid address type");
return isa;
}
static InetSocketAddress asInetSocketAddress(SocketAddress sa) {
if (!(sa instanceof InetSocketAddress))
throw new UnsupportedAddressTypeException();
return (InetSocketAddress)sa;
}
static void translateToSocketException(Exception x)
throws SocketException
{
if (x instanceof SocketException)
throw (SocketException)x;
Exception nx = x;
if (x instanceof ClosedChannelException)
nx = new SocketException("Socket is closed");
else if (x instanceof AlreadyBoundException)
nx = new SocketException("Already bound");
else if (x instanceof NotYetBoundException)
nx = new SocketException("Socket is not bound yet");
else if (x instanceof UnsupportedAddressTypeException)
nx = new SocketException("Unsupported address type");
else if (x instanceof UnresolvedAddressException) {
nx = new SocketException("Unresolved address");
}
if (nx != x)
nx.initCause(x);
if (nx instanceof SocketException)
throw (SocketException)nx;
else if (nx instanceof RuntimeException)
throw (RuntimeException)nx;
else
throw new Error("Untranslated exception", nx);
}
static void translateException(Exception x,
boolean unknownHostForUnresolved)
throws IOException
{
if (x instanceof IOException)
throw (IOException)x;
if (unknownHostForUnresolved &&
(x instanceof UnresolvedAddressException))
{
throw new UnknownHostException();
}
translateToSocketException(x);
}
static void translateException(Exception x)
throws IOException
{
translateException(x, false);
}
static FileDescriptor socket(boolean stream) throws IOException {
return IOUtil.newFD(socket0(stream, false));
}
static FileDescriptor serverSocket(boolean stream) {
return IOUtil.newFD(socket0(stream, true));
}
private static native int isExclusiveBindAvailable();
private static native int socket0(boolean stream, boolean reuse);
static void bind(FileDescriptor fd, InetAddress addr, int port)
throws IOException {
bind0(fd, exclusiveBind, addr, port);
}
private static native void bind0(FileDescriptor fd, boolean useExclBind, InetAddress addr, int port)
throws IOException;
static native int connect(FileDescriptor fd,
InetAddress remote,
int remotePort,
int trafficClass)
throws IOException;
private static native int localPort(FileDescriptor fd)
throws IOException;
private static native InetAddress localInetAddress(FileDescriptor fd)
throws IOException;
static InetSocketAddress localAddress(FileDescriptor fd) {
try {
return new InetSocketAddress(localInetAddress(fd),
localPort(fd));
} catch (IOException x) {
throw new Error(x);
}
}
static int localPortNumber(FileDescriptor fd) {
try {
return localPort(fd);
} catch (IOException x) {
throw new Error(x);
}
}
private static native int getIntOption0(FileDescriptor fd, int opt)
throws IOException;
static int getIntOption(FileDescriptor fd, int opt)
throws IOException
{
return getIntOption0(fd, opt);
}
private static native void setIntOption0(FileDescriptor fd,
int opt, int arg)
throws IOException;
static void setIntOption(FileDescriptor fd, int opt, int arg)
throws IOException
{
setIntOption0(fd, opt, arg);
}
private static native void initIDs();
static {
IOUtil.load();
initIDs();
}
static InetSocketAddress getRevealedLocalAddress(InetSocketAddress addr) {
SecurityManager sm = System.getSecurityManager();
if (addr == null || sm == null)
return addr;
if (!getRevealLocalAddress()) {
try{
sm.checkConnect(addr.getAddress().getHostAddress(), -1);
} catch (SecurityException e) {
addr = getLoopbackAddress(addr.getPort());
}
}
return addr;
}
static String getRevealedLocalAddressAsString(InetSocketAddress addr) {
if (!getRevealLocalAddress() && System.getSecurityManager() != null)
addr = getLoopbackAddress(addr.getPort());
return addr.toString();
}
private static boolean getRevealLocalAddress() {
if (!propRevealLocalAddress) {
try {
revealLocalAddress = Boolean.parseBoolean(
AccessController.doPrivileged(
new PrivilegedExceptionAction<String>() {
public String run() {
return System.getProperty(
"jdk.net.revealLocalAddress");
}
}));
} catch (Exception e) {
}
propRevealLocalAddress = true;
}
return revealLocalAddress;
}
private static InetSocketAddress getLoopbackAddress(int port) {
try {
return new InetSocketAddress(InetAddress.getByName(null),
port);
} catch (UnknownHostException e) {
throw new InternalError("Shouldn't reach here.");
}
}
}