src/java.base/share/classes/sun/nio/ch/InetServerSocketChannelImpl.java
branchunixdomainchannels
changeset 58801 119ac9128c1b
equal deleted inserted replaced
58799:eb491334113f 58801:119ac9128c1b
       
     1 /*
       
     2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.nio.ch;
       
    27 
       
    28 import java.io.FileDescriptor;
       
    29 import java.io.IOException;
       
    30 import java.net.InetSocketAddress;
       
    31 import java.net.ServerSocket;
       
    32 import java.net.SocketAddress;
       
    33 import java.net.SocketOption;
       
    34 import java.net.SocketTimeoutException;
       
    35 import java.net.StandardSocketOptions;
       
    36 import java.nio.channels.AlreadyBoundException;
       
    37 import java.nio.channels.AsynchronousCloseException;
       
    38 import java.nio.channels.ClosedChannelException;
       
    39 import java.nio.channels.IllegalBlockingModeException;
       
    40 import java.nio.channels.NotYetBoundException;
       
    41 import java.nio.channels.SelectionKey;
       
    42 import java.nio.channels.ServerSocketChannel;
       
    43 import java.nio.channels.SocketChannel;
       
    44 import java.nio.channels.spi.SelectorProvider;
       
    45 import java.util.Collections;
       
    46 import java.util.HashSet;
       
    47 import java.util.Objects;
       
    48 import java.util.Set;
       
    49 import java.util.concurrent.locks.ReentrantLock;
       
    50 
       
    51 import sun.net.NetHooks;
       
    52 import sun.net.ext.ExtendedSocketOptions;
       
    53 
       
    54 /**
       
    55  * An implementation of ServerSocketChannels
       
    56  */
       
    57 
       
    58 class InetServerSocketChannelImpl
       
    59     extends ServerSocketChannelImpl
       
    60 {
       
    61     // set true when exclusive binding is on and SO_REUSEADDR is emulated
       
    62     private boolean isReuseAddress;
       
    63 
       
    64     // Our socket adaptor, if any
       
    65     private ServerSocket socket;
       
    66 
       
    67     // -- End of fields protected by stateLock
       
    68 
       
    69 
       
    70     InetServerSocketChannelImpl(SelectorProvider sp) throws IOException {
       
    71         super(sp, Net.serverSocket(true));
       
    72     }
       
    73 
       
    74     InetServerSocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound)
       
    75         throws IOException
       
    76     {
       
    77         super(sp, fd);
       
    78         if (bound) {
       
    79             synchronized (stateLock) {
       
    80                 localAddress = Net.localAddress(fd);
       
    81             }
       
    82         }
       
    83     }
       
    84 
       
    85     @Override
       
    86     public ServerSocket socket() {
       
    87         synchronized (stateLock) {
       
    88             if (socket == null)
       
    89                 socket = ServerSocketAdaptor.create(this);
       
    90             return socket;
       
    91         }
       
    92     }
       
    93 
       
    94     @Override
       
    95     public <T> ServerSocketChannel setOption(SocketOption<T> name, T value)
       
    96         throws IOException
       
    97     {
       
    98         Objects.requireNonNull(name);
       
    99         if (!supportedOptions().contains(name))
       
   100             throw new UnsupportedOperationException("'" + name + "' not supported");
       
   101         if (!name.type().isInstance(value))
       
   102             throw new IllegalArgumentException("Invalid value '" + value + "'");
       
   103 
       
   104         synchronized (stateLock) {
       
   105             ensureOpen();
       
   106 
       
   107             if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) {
       
   108                 // SO_REUSEADDR emulated when using exclusive bind
       
   109                 isReuseAddress = (Boolean)value;
       
   110             } else {
       
   111                 // no options that require special handling
       
   112                 Net.setSocketOption(fd, Net.UNSPEC, name, value);
       
   113             }
       
   114             return this;
       
   115         }
       
   116     }
       
   117 
       
   118     @Override
       
   119     @SuppressWarnings("unchecked")
       
   120     public <T> T getOption(SocketOption<T> name)
       
   121         throws IOException
       
   122     {
       
   123         Objects.requireNonNull(name);
       
   124         if (!supportedOptions().contains(name))
       
   125             throw new UnsupportedOperationException("'" + name + "' not supported");
       
   126 
       
   127         synchronized (stateLock) {
       
   128             ensureOpen();
       
   129             if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) {
       
   130                 // SO_REUSEADDR emulated when using exclusive bind
       
   131                 return (T)Boolean.valueOf(isReuseAddress);
       
   132             }
       
   133             // no options that require special handling
       
   134             return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
       
   135         }
       
   136     }
       
   137 
       
   138     private static class DefaultOptionsHolder {
       
   139         static final Set<SocketOption<?>> defaultOptions = defaultOptions();
       
   140 
       
   141         private static Set<SocketOption<?>> defaultOptions() {
       
   142             HashSet<SocketOption<?>> set = new HashSet<>();
       
   143             set.add(StandardSocketOptions.SO_RCVBUF);
       
   144             set.add(StandardSocketOptions.SO_REUSEADDR);
       
   145             if (Net.isReusePortAvailable()) {
       
   146                 set.add(StandardSocketOptions.SO_REUSEPORT);
       
   147             }
       
   148             set.addAll(ExtendedSocketOptions.serverSocketOptions());
       
   149             return Collections.unmodifiableSet(set);
       
   150         }
       
   151     }
       
   152 
       
   153     @Override
       
   154     public final Set<SocketOption<?>> supportedOptions() {
       
   155         return DefaultOptionsHolder.defaultOptions;
       
   156     }
       
   157 
       
   158     @Override
       
   159     public ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException {
       
   160         synchronized (stateLock) {
       
   161             ensureOpen();
       
   162             if (localAddress != null)
       
   163                 throw new AlreadyBoundException();
       
   164             InetSocketAddress isa = (local == null)
       
   165                                     ? new InetSocketAddress(0)
       
   166                                     : Net.checkAddress(local);
       
   167             SecurityManager sm = System.getSecurityManager();
       
   168             if (sm != null)
       
   169                 sm.checkListen(isa.getPort());
       
   170             NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort());
       
   171             Net.bind(fd, isa.getAddress(), isa.getPort());
       
   172             Net.listen(fd, backlog < 1 ? 50 : backlog);
       
   173             localAddress = Net.localAddress(fd);
       
   174         }
       
   175         return this;
       
   176     }
       
   177 
       
   178     protected int implAccept(FileDescriptor fd, FileDescriptor newfd, SocketAddress[] addrs)
       
   179         throws IOException
       
   180     {
       
   181         InetSocketAddress[] a = new InetSocketAddress[1];
       
   182         int n = Net.accept(fd, newfd, a);
       
   183         addrs[0] = a[0];
       
   184         return n;
       
   185     }
       
   186 
       
   187     protected SocketAddress getRevealedLocalAddress(SocketAddress addr) {
       
   188         return Net.getRevealedLocalAddress((InetSocketAddress)addr);
       
   189     }
       
   190 
       
   191     protected String getRevealedLocalAddressAsString(SocketAddress addr) {
       
   192         return Net.getRevealedLocalAddressAsString((InetSocketAddress)addr);
       
   193     }
       
   194 
       
   195 
       
   196     protected SocketChannel finishAccept(FileDescriptor newfd, SocketAddress sa)
       
   197         throws IOException
       
   198     {
       
   199         InetSocketAddress isa = (InetSocketAddress)sa;
       
   200         try {
       
   201             // newly accepted socket is initially in blocking mode
       
   202             IOUtil.configureBlocking(newfd, true);
       
   203 
       
   204             // check permitted to accept connections from the remote address
       
   205             SecurityManager sm = System.getSecurityManager();
       
   206             if (sm != null) {
       
   207                 sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort());
       
   208             }
       
   209             return new InetSocketChannelImpl(provider(), newfd, isa);
       
   210         } catch (Exception e) {
       
   211             nd.close(newfd);
       
   212             throw e;
       
   213         }
       
   214     }
       
   215 }