src/java.base/share/classes/sun/nio/ch/UnixDomainSocketChannelImpl.java
branchunixdomainchannels
changeset 58801 119ac9128c1b
child 58847 692de65ab293
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.io.UncheckedIOException;
       
    31 import java.net.ProtocolFamily;
       
    32 import java.net.Socket;
       
    33 import java.net.SocketAddress;
       
    34 import java.net.SocketException;
       
    35 import java.net.SocketOption;
       
    36 import java.net.SocketTimeoutException;
       
    37 import java.net.StandardProtocolFamily;
       
    38 import java.net.StandardSocketOptions;
       
    39 import java.nio.ByteBuffer;
       
    40 import java.nio.channels.AlreadyBoundException;
       
    41 import java.nio.channels.AlreadyConnectedException;
       
    42 import java.nio.channels.AsynchronousCloseException;
       
    43 import java.nio.channels.ClosedChannelException;
       
    44 import java.nio.channels.ConnectionPendingException;
       
    45 import java.nio.channels.IllegalBlockingModeException;
       
    46 import java.nio.channels.NoConnectionPendingException;
       
    47 import java.nio.channels.NotYetConnectedException;
       
    48 import java.nio.channels.SelectionKey;
       
    49 import java.nio.channels.SocketChannel;
       
    50 import java.nio.channels.UnixDomainSocketAddress;
       
    51 import java.nio.channels.spi.SelectorProvider;
       
    52 import java.security.AccessController;
       
    53 import java.util.Collections;
       
    54 import java.util.HashSet;
       
    55 import java.util.Objects;
       
    56 import java.util.Set;
       
    57 import java.util.concurrent.locks.ReentrantLock;
       
    58 
       
    59 import sun.net.ConnectionResetException;
       
    60 import sun.net.NetHooks;
       
    61 import sun.net.ext.ExtendedSocketOptions;
       
    62 import sun.net.util.SocketExceptions;
       
    63 
       
    64 /**
       
    65  * An implementation of SocketChannels
       
    66  */
       
    67 
       
    68 public class UnixDomainSocketChannelImpl extends SocketChannelImpl
       
    69 {
       
    70     public UnixDomainSocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound)
       
    71         throws IOException
       
    72     {
       
    73         super(sp, fd);
       
    74         if (bound) {
       
    75             synchronized (stateLock) {
       
    76                 this.localAddress = Net.localUnixAddress(fd);
       
    77             }
       
    78         }
       
    79     }
       
    80 
       
    81 /*
       
    82     public static AbstractSocketChannelImpl create(SelectorProvider sp, FileDescriptor fd) {
       
    83         try {
       
    84             return new UnixDomainSocketChannelImpl(sp, fd, false);
       
    85         } catch (IOException e) {
       
    86             throw new UncheckedIOException(e);
       
    87         }
       
    88     }
       
    89 */
       
    90 
       
    91     // Constructor for sockets obtained from server sockets
       
    92     //
       
    93     UnixDomainSocketChannelImpl(SelectorProvider sp, FileDescriptor fd, SocketAddress isa)
       
    94         throws IOException
       
    95     {
       
    96         super(sp, fd);
       
    97         synchronized (stateLock) {
       
    98             this.localAddress = Net.localUnixAddress(fd);
       
    99             this.remoteAddress = isa;
       
   100             this.state = ST_CONNECTED;
       
   101         }
       
   102     }
       
   103 
       
   104     @Override
       
   105     public SocketAddress getLocalAddress() throws IOException {
       
   106         synchronized (stateLock) {
       
   107             ensureOpen();
       
   108             return localAddress; // TODO: revealed local address?
       
   109         }
       
   110     }
       
   111 
       
   112     @Override
       
   113     public SocketAddress getRemoteAddress() throws IOException {
       
   114         synchronized (stateLock) {
       
   115             ensureOpen();
       
   116             return remoteAddress;
       
   117         }
       
   118     }
       
   119 
       
   120     @Override
       
   121     public <T> SocketChannel setOption(SocketOption<T> name, T value)
       
   122         throws IOException
       
   123     {
       
   124         Objects.requireNonNull(name);
       
   125         if (!supportedOptions().contains(name))
       
   126             throw new UnsupportedOperationException("'" + name + "' not supported");
       
   127         if (!name.type().isInstance(value))
       
   128             throw new IllegalArgumentException("Invalid value '" + value + "'");
       
   129 
       
   130         synchronized (stateLock) {
       
   131             ensureOpen();
       
   132 
       
   133             // no options that require special handling
       
   134             Net.setSocketOption(fd, name, value);
       
   135             return this;
       
   136         }
       
   137     }
       
   138 
       
   139     @Override
       
   140     @SuppressWarnings("unchecked")
       
   141     public <T> T getOption(SocketOption<T> name)
       
   142         throws IOException
       
   143     {
       
   144         Objects.requireNonNull(name);
       
   145         if (!supportedOptions().contains(name))
       
   146             throw new UnsupportedOperationException("'" + name + "' not supported");
       
   147 
       
   148         synchronized (stateLock) {
       
   149             ensureOpen();
       
   150             // no options that require special handling
       
   151             return (T) Net.getSocketOption(fd, name); // AF_UNIX
       
   152         }
       
   153     }
       
   154 
       
   155     private static class DefaultOptionsHolder {
       
   156         static final Set<SocketOption<?>> defaultOptions = defaultOptions();
       
   157 
       
   158         private static Set<SocketOption<?>> defaultOptions() {
       
   159             HashSet<SocketOption<?>> set = new HashSet<>();
       
   160             set.add(StandardSocketOptions.SO_SNDBUF);
       
   161             set.add(StandardSocketOptions.SO_RCVBUF);
       
   162             set.add(StandardSocketOptions.SO_REUSEADDR); // TODO: DELETE ME
       
   163             set.add(StandardSocketOptions.SO_KEEPALIVE);
       
   164             set.add(StandardSocketOptions.SO_LINGER);
       
   165             return Collections.unmodifiableSet(set);
       
   166         }
       
   167     }
       
   168 
       
   169     @Override
       
   170     public final Set<SocketOption<?>> supportedOptions() {
       
   171         return DefaultOptionsHolder.defaultOptions;
       
   172     }
       
   173 
       
   174     @Override
       
   175     public SocketChannel bind(SocketAddress local) throws IOException {
       
   176         readLock.lock();
       
   177         try {
       
   178             writeLock.lock();
       
   179             try {
       
   180                 synchronized (stateLock) {
       
   181                     ensureOpen();
       
   182                     if (state == ST_CONNECTIONPENDING)
       
   183                         throw new ConnectionPendingException();
       
   184                     if (localAddress != null)
       
   185                         throw new AlreadyBoundException();
       
   186                     if (local == null)
       
   187                         throw new NullPointerException(); // TODO: ??
       
   188                     UnixDomainSocketAddress usa = Net.checkUnixAddress(local);
       
   189                     Net.unixDomainBind(fd, usa);
       
   190                     localAddress = Net.localUnixAddress(fd);
       
   191                 }
       
   192             } finally {
       
   193                 writeLock.unlock();
       
   194             }
       
   195         } finally {
       
   196             readLock.unlock();
       
   197         }
       
   198         return this;
       
   199     }
       
   200 
       
   201     public Socket socket() {
       
   202         throw new UnsupportedOperationException("socket not supported");
       
   203     }
       
   204 
       
   205     /**
       
   206      * Checks the remote address to which this channel is to be connected.
       
   207      */
       
   208     @Override
       
   209     SocketAddress checkRemote(SocketAddress sa) throws IOException {
       
   210         SocketAddress isa = Net.checkUnixAddress(sa);
       
   211         return isa; // TODO
       
   212     }
       
   213 
       
   214     @Override
       
   215     int connectImpl(FileDescriptor fd, SocketAddress sa) throws IOException {
       
   216         UnixDomainSocketAddress isa = (UnixDomainSocketAddress)sa;
       
   217         return Net.unixDomainConnect(fd, isa);
       
   218     }
       
   219 
       
   220     @Override
       
   221     SocketAddress localAddressImpl(FileDescriptor fd) throws IOException {
       
   222         return Net.localUnixAddress(fd);
       
   223     }
       
   224 
       
   225     String getRevealedLocalAddressAsString(SocketAddress sa) {
       
   226         return sa.toString(); // TODO
       
   227     }
       
   228 
       
   229 }