Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/ch/Net.java
38918 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.io.*;28import java.net.*;29import jdk.net.*;30import java.nio.channels.*;31import java.util.*;32import java.security.AccessController;33import java.security.PrivilegedAction;34import java.security.PrivilegedExceptionAction;35import sun.net.ExtendedOptionsImpl;36import static jdk.net.ExtendedSocketOptions.TCP_KEEPCOUNT;37import static jdk.net.ExtendedSocketOptions.TCP_KEEPIDLE;38import static jdk.net.ExtendedSocketOptions.TCP_KEEPINTERVAL;394041public class Net {4243private Net() { }4445// unspecified protocol family46static final ProtocolFamily UNSPEC = new ProtocolFamily() {47public String name() {48return "UNSPEC";49}50};5152// set to true if exclusive binding is on for Windows53private static final boolean exclusiveBind;5455// set to true if the fast tcp loopback should be enabled on Windows56private static final boolean fastLoopback;5758// -- Miscellaneous utilities --5960private static volatile boolean checkedIPv6 = false;61private static volatile boolean isIPv6Available;6263/**64* Tells whether dual-IPv4/IPv6 sockets should be used.65*/66static boolean isIPv6Available() {67if (!checkedIPv6) {68isIPv6Available = isIPv6Available0();69checkedIPv6 = true;70}71return isIPv6Available;72}7374/**75* Returns true if exclusive binding is on76*/77static boolean useExclusiveBind() {78return exclusiveBind;79}8081/**82* Tells whether IPv6 sockets can join IPv4 multicast groups83*/84static boolean canIPv6SocketJoinIPv4Group() {85return canIPv6SocketJoinIPv4Group0();86}8788/**89* Tells whether {@link #join6} can be used to join an IPv490* multicast group (IPv4 group as IPv4-mapped IPv6 address)91*/92static boolean canJoin6WithIPv4Group() {93return canJoin6WithIPv4Group0();94}9596public static InetSocketAddress checkAddress(SocketAddress sa) {97if (sa == null)98throw new NullPointerException();99if (!(sa instanceof InetSocketAddress))100throw new UnsupportedAddressTypeException(); // ## needs arg101InetSocketAddress isa = (InetSocketAddress)sa;102if (isa.isUnresolved())103throw new UnresolvedAddressException(); // ## needs arg104InetAddress addr = isa.getAddress();105if (!(addr instanceof Inet4Address || addr instanceof Inet6Address))106throw new IllegalArgumentException("Invalid address type");107return isa;108}109110static InetSocketAddress asInetSocketAddress(SocketAddress sa) {111if (!(sa instanceof InetSocketAddress))112throw new UnsupportedAddressTypeException();113return (InetSocketAddress)sa;114}115116static void translateToSocketException(Exception x)117throws SocketException118{119if (x instanceof SocketException)120throw (SocketException)x;121Exception nx = x;122if (x instanceof ClosedChannelException)123nx = new SocketException("Socket is closed");124else if (x instanceof NotYetConnectedException)125nx = new SocketException("Socket is not connected");126else if (x instanceof AlreadyBoundException)127nx = new SocketException("Already bound");128else if (x instanceof NotYetBoundException)129nx = new SocketException("Socket is not bound yet");130else if (x instanceof UnsupportedAddressTypeException)131nx = new SocketException("Unsupported address type");132else if (x instanceof UnresolvedAddressException) {133nx = new SocketException("Unresolved address");134}135if (nx != x)136nx.initCause(x);137138if (nx instanceof SocketException)139throw (SocketException)nx;140else if (nx instanceof RuntimeException)141throw (RuntimeException)nx;142else143throw new Error("Untranslated exception", nx);144}145146static void translateException(Exception x,147boolean unknownHostForUnresolved)148throws IOException149{150if (x instanceof IOException)151throw (IOException)x;152// Throw UnknownHostException from here since it cannot153// be thrown as a SocketException154if (unknownHostForUnresolved &&155(x instanceof UnresolvedAddressException))156{157throw new UnknownHostException();158}159translateToSocketException(x);160}161162static void translateException(Exception x)163throws IOException164{165translateException(x, false);166}167168/**169* Returns the local address after performing a SecurityManager#checkConnect.170*/171static InetSocketAddress getRevealedLocalAddress(InetSocketAddress addr) {172SecurityManager sm = System.getSecurityManager();173if (addr == null || sm == null)174return addr;175176try{177sm.checkConnect(addr.getAddress().getHostAddress(), -1);178// Security check passed179} catch (SecurityException e) {180// Return loopback address only if security check fails181addr = getLoopbackAddress(addr.getPort());182}183return addr;184}185186static String getRevealedLocalAddressAsString(InetSocketAddress addr) {187return System.getSecurityManager() == null ? addr.toString() :188getLoopbackAddress(addr.getPort()).toString();189}190191private static InetSocketAddress getLoopbackAddress(int port) {192return new InetSocketAddress(InetAddress.getLoopbackAddress(),193port);194}195196/**197* Returns any IPv4 address of the given network interface, or198* null if the interface does not have any IPv4 addresses.199*/200static Inet4Address anyInet4Address(final NetworkInterface interf) {201return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() {202public Inet4Address run() {203Enumeration<InetAddress> addrs = interf.getInetAddresses();204while (addrs.hasMoreElements()) {205InetAddress addr = addrs.nextElement();206if (addr instanceof Inet4Address) {207return (Inet4Address)addr;208}209}210return null;211}212});213}214215/**216* Returns an IPv4 address as an int.217*/218static int inet4AsInt(InetAddress ia) {219if (ia instanceof Inet4Address) {220byte[] addr = ia.getAddress();221int address = addr[3] & 0xFF;222address |= ((addr[2] << 8) & 0xFF00);223address |= ((addr[1] << 16) & 0xFF0000);224address |= ((addr[0] << 24) & 0xFF000000);225return address;226}227throw new AssertionError("Should not reach here");228}229230/**231* Returns an InetAddress from the given IPv4 address232* represented as an int.233*/234static InetAddress inet4FromInt(int address) {235byte[] addr = new byte[4];236addr[0] = (byte) ((address >>> 24) & 0xFF);237addr[1] = (byte) ((address >>> 16) & 0xFF);238addr[2] = (byte) ((address >>> 8) & 0xFF);239addr[3] = (byte) (address & 0xFF);240try {241return InetAddress.getByAddress(addr);242} catch (UnknownHostException uhe) {243throw new AssertionError("Should not reach here");244}245}246247/**248* Returns an IPv6 address as a byte array249*/250static byte[] inet6AsByteArray(InetAddress ia) {251if (ia instanceof Inet6Address) {252return ia.getAddress();253}254255// need to construct IPv4-mapped address256if (ia instanceof Inet4Address) {257byte[] ip4address = ia.getAddress();258byte[] address = new byte[16];259address[10] = (byte)0xff;260address[11] = (byte)0xff;261address[12] = ip4address[0];262address[13] = ip4address[1];263address[14] = ip4address[2];264address[15] = ip4address[3];265return address;266}267268throw new AssertionError("Should not reach here");269}270271// -- Socket options272273static void setSocketOption(FileDescriptor fd, ProtocolFamily family,274SocketOption<?> name, Object value)275throws IOException276{277if (value == null)278throw new IllegalArgumentException("Invalid option value");279280// only simple values supported by this method281Class<?> type = name.type();282283if (type == SocketFlow.class) {284ExtendedOptionsImpl.checkSetOptionPermission(name);285ExtendedOptionsImpl.checkValueType(value, SocketFlow.class);286ExtendedOptionsImpl.setFlowOption(fd, (SocketFlow)value);287return;288}289if (name == TCP_KEEPINTERVAL) {290ExtendedOptionsImpl.checkSetOptionPermission(name);291ExtendedOptionsImpl.checkValueType(value, Integer.class);292ExtendedOptionsImpl.setTcpKeepAliveIntvl(fd, (Integer)value);293return;294}295if (name == TCP_KEEPIDLE) {296ExtendedOptionsImpl.checkSetOptionPermission(name);297ExtendedOptionsImpl.checkValueType(value, Integer.class);298ExtendedOptionsImpl.setTcpKeepAliveTime(fd, (Integer)value);299return;300}301if (name == TCP_KEEPCOUNT) {302ExtendedOptionsImpl.checkSetOptionPermission(name);303ExtendedOptionsImpl.checkValueType(value, Integer.class);304ExtendedOptionsImpl.setTcpKeepAliveProbes(fd, (Integer)value);305return;306}307308if (type != Integer.class && type != Boolean.class)309throw new AssertionError("Should not reach here");310311// special handling312if (name == StandardSocketOptions.SO_RCVBUF ||313name == StandardSocketOptions.SO_SNDBUF)314{315int i = ((Integer)value).intValue();316if (i < 0)317throw new IllegalArgumentException("Invalid send/receive buffer size");318}319if (name == StandardSocketOptions.SO_LINGER) {320int i = ((Integer)value).intValue();321if (i < 0)322value = Integer.valueOf(-1);323if (i > 65535)324value = Integer.valueOf(65535);325}326if (name == StandardSocketOptions.IP_TOS) {327int i = ((Integer)value).intValue();328if (i < 0 || i > 255)329throw new IllegalArgumentException("Invalid IP_TOS value");330}331if (name == StandardSocketOptions.IP_MULTICAST_TTL) {332int i = ((Integer)value).intValue();333if (i < 0 || i > 255)334throw new IllegalArgumentException("Invalid TTL/hop value");335}336337// map option name to platform level/name338OptionKey key = SocketOptionRegistry.findOption(name, family);339if (key == null)340throw new AssertionError("Option not found");341342int arg;343if (type == Integer.class) {344arg = ((Integer)value).intValue();345} else {346boolean b = ((Boolean)value).booleanValue();347arg = (b) ? 1 : 0;348}349350boolean mayNeedConversion = (family == UNSPEC);351boolean isIPv6 = (family == StandardProtocolFamily.INET6);352setIntOption0(fd, mayNeedConversion, key.level(), key.name(), arg, isIPv6);353}354355static Object getSocketOption(FileDescriptor fd, ProtocolFamily family,356SocketOption<?> name)357throws IOException358{359Class<?> type = name.type();360361if (type == SocketFlow.class) {362ExtendedOptionsImpl.checkGetOptionPermission(name);363SocketFlow flow = SocketFlow.create();364ExtendedOptionsImpl.getFlowOption(fd, flow);365return flow;366}367if (name == TCP_KEEPINTERVAL) {368ExtendedOptionsImpl.checkGetOptionPermission(name);369return ExtendedOptionsImpl.getTcpKeepAliveIntvl(fd);370}371if (name == TCP_KEEPIDLE) {372ExtendedOptionsImpl.checkGetOptionPermission(name);373return ExtendedOptionsImpl.getTcpKeepAliveTime(fd);374}375if (name == TCP_KEEPCOUNT) {376ExtendedOptionsImpl.checkGetOptionPermission(name);377return ExtendedOptionsImpl.getTcpKeepAliveProbes(fd);378}379380// only simple values supported by this method381if (type != Integer.class && type != Boolean.class)382throw new AssertionError("Should not reach here");383384// map option name to platform level/name385OptionKey key = SocketOptionRegistry.findOption(name, family);386if (key == null)387throw new AssertionError("Option not found");388389boolean mayNeedConversion = (family == UNSPEC);390int value = getIntOption0(fd, mayNeedConversion, key.level(), key.name());391392if (type == Integer.class) {393return Integer.valueOf(value);394} else {395return (value == 0) ? Boolean.FALSE : Boolean.TRUE;396}397}398399public static boolean isFastTcpLoopbackRequested() {400String loopbackProp = java.security.AccessController.doPrivileged(401new PrivilegedAction<String>() {402@Override403public String run() {404return System.getProperty("jdk.net.useFastTcpLoopback");405}406});407boolean enable;408if ("".equals(loopbackProp)) {409enable = true;410} else {411enable = Boolean.parseBoolean(loopbackProp);412}413return enable;414}415416// -- Socket operations --417418private static native boolean isIPv6Available0();419420/*421* Returns 1 for Windows versions that support exclusive binding by default, 0422* for those that do not, and -1 for Solaris/Linux/Mac OS423*/424private static native int isExclusiveBindAvailable();425426private static native boolean canIPv6SocketJoinIPv4Group0();427428private static native boolean canJoin6WithIPv4Group0();429430static FileDescriptor socket(boolean stream) throws IOException {431return socket(UNSPEC, stream);432}433434static FileDescriptor socket(ProtocolFamily family, boolean stream)435throws IOException {436boolean preferIPv6 = isIPv6Available() &&437(family != StandardProtocolFamily.INET);438return IOUtil.newFD(socket0(preferIPv6, stream, false, fastLoopback));439}440441static FileDescriptor serverSocket(boolean stream) {442return IOUtil.newFD(socket0(isIPv6Available(), stream, true, fastLoopback));443}444445// Due to oddities SO_REUSEADDR on windows reuse is ignored446private static native int socket0(boolean preferIPv6, boolean stream, boolean reuse,447boolean fastLoopback);448449public static void bind(FileDescriptor fd, InetAddress addr, int port)450throws IOException451{452bind(UNSPEC, fd, addr, port);453}454455static void bind(ProtocolFamily family, FileDescriptor fd,456InetAddress addr, int port) throws IOException457{458boolean preferIPv6 = isIPv6Available() &&459(family != StandardProtocolFamily.INET);460bind0(fd, preferIPv6, exclusiveBind, addr, port);461}462463private static native void bind0(FileDescriptor fd, boolean preferIPv6,464boolean useExclBind, InetAddress addr,465int port)466throws IOException;467468static native void listen(FileDescriptor fd, int backlog) throws IOException;469470static int connect(FileDescriptor fd, InetAddress remote, int remotePort)471throws IOException472{473return connect(UNSPEC, fd, remote, remotePort);474}475476static int connect(ProtocolFamily family, FileDescriptor fd, InetAddress remote, int remotePort)477throws IOException478{479boolean preferIPv6 = isIPv6Available() &&480(family != StandardProtocolFamily.INET);481return connect0(preferIPv6, fd, remote, remotePort);482}483484private static native int connect0(boolean preferIPv6,485FileDescriptor fd,486InetAddress remote,487int remotePort)488throws IOException;489490491public final static int SHUT_RD = 0;492public final static int SHUT_WR = 1;493public final static int SHUT_RDWR = 2;494495static native void shutdown(FileDescriptor fd, int how) throws IOException;496497private static native int localPort(FileDescriptor fd)498throws IOException;499500private static native InetAddress localInetAddress(FileDescriptor fd)501throws IOException;502503public static InetSocketAddress localAddress(FileDescriptor fd)504throws IOException505{506return new InetSocketAddress(localInetAddress(fd), localPort(fd));507}508509private static native int remotePort(FileDescriptor fd)510throws IOException;511512private static native InetAddress remoteInetAddress(FileDescriptor fd)513throws IOException;514515static InetSocketAddress remoteAddress(FileDescriptor fd)516throws IOException517{518return new InetSocketAddress(remoteInetAddress(fd), remotePort(fd));519}520521private static native int getIntOption0(FileDescriptor fd, boolean mayNeedConversion,522int level, int opt)523throws IOException;524525private static native void setIntOption0(FileDescriptor fd, boolean mayNeedConversion,526int level, int opt, int arg, boolean isIPv6)527throws IOException;528529static native int poll(FileDescriptor fd, int events, long timeout)530throws IOException;531532// -- Multicast support --533534535/**536* Join IPv4 multicast group537*/538static int join4(FileDescriptor fd, int group, int interf, int source)539throws IOException540{541return joinOrDrop4(true, fd, group, interf, source);542}543544/**545* Drop membership of IPv4 multicast group546*/547static void drop4(FileDescriptor fd, int group, int interf, int source)548throws IOException549{550joinOrDrop4(false, fd, group, interf, source);551}552553private static native int joinOrDrop4(boolean join, FileDescriptor fd, int group, int interf, int source)554throws IOException;555556/**557* Block IPv4 source558*/559static int block4(FileDescriptor fd, int group, int interf, int source)560throws IOException561{562return blockOrUnblock4(true, fd, group, interf, source);563}564565/**566* Unblock IPv6 source567*/568static void unblock4(FileDescriptor fd, int group, int interf, int source)569throws IOException570{571blockOrUnblock4(false, fd, group, interf, source);572}573574private static native int blockOrUnblock4(boolean block, FileDescriptor fd, int group,575int interf, int source)576throws IOException;577578/**579* Join IPv6 multicast group580*/581static int join6(FileDescriptor fd, byte[] group, int index, byte[] source)582throws IOException583{584return joinOrDrop6(true, fd, group, index, source);585}586587/**588* Drop membership of IPv6 multicast group589*/590static void drop6(FileDescriptor fd, byte[] group, int index, byte[] source)591throws IOException592{593joinOrDrop6(false, fd, group, index, source);594}595596private static native int joinOrDrop6(boolean join, FileDescriptor fd, byte[] group, int index, byte[] source)597throws IOException;598599/**600* Block IPv6 source601*/602static int block6(FileDescriptor fd, byte[] group, int index, byte[] source)603throws IOException604{605return blockOrUnblock6(true, fd, group, index, source);606}607608/**609* Unblock IPv6 source610*/611static void unblock6(FileDescriptor fd, byte[] group, int index, byte[] source)612throws IOException613{614blockOrUnblock6(false, fd, group, index, source);615}616617static native int blockOrUnblock6(boolean block, FileDescriptor fd, byte[] group, int index, byte[] source)618throws IOException;619620static native void setInterface4(FileDescriptor fd, int interf) throws IOException;621622static native int getInterface4(FileDescriptor fd) throws IOException;623624static native void setInterface6(FileDescriptor fd, int index) throws IOException;625626static native int getInterface6(FileDescriptor fd) throws IOException;627628private static native void initIDs();629630/**631* Event masks for the various poll system calls.632* They will be set platform dependant in the static initializer below.633*/634public static final short POLLIN;635public static final short POLLOUT;636public static final short POLLERR;637public static final short POLLHUP;638public static final short POLLNVAL;639public static final short POLLCONN;640641static native short pollinValue();642static native short polloutValue();643static native short pollerrValue();644static native short pollhupValue();645static native short pollnvalValue();646static native short pollconnValue();647648static {649IOUtil.load();650initIDs();651652POLLIN = pollinValue();653POLLOUT = polloutValue();654POLLERR = pollerrValue();655POLLHUP = pollhupValue();656POLLNVAL = pollnvalValue();657POLLCONN = pollconnValue();658}659660static {661int availLevel = isExclusiveBindAvailable();662if (availLevel >= 0) {663String exclBindProp =664java.security.AccessController.doPrivileged(665new PrivilegedAction<String>() {666@Override667public String run() {668return System.getProperty(669"sun.net.useExclusiveBind");670}671});672if (exclBindProp != null) {673exclusiveBind = exclBindProp.length() == 0 ?674true : Boolean.parseBoolean(exclBindProp);675} else if (availLevel == 1) {676exclusiveBind = true;677} else {678exclusiveBind = false;679}680} else {681exclusiveBind = false;682}683684fastLoopback = isFastTcpLoopbackRequested();685}686}687688689