1 /*
   2  * Copyright (c) 2007, 2016, 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 package java.net;
  26 
  27 import java.io.IOException;
  28 import java.io.FileDescriptor;
  29 import java.util.Set;
  30 import java.util.HashSet;
  31 import sun.net.ext.ExtendedSocketOptions;
  32 
  33 /*
  34  * On Unix systems we simply delegate to native methods.
  35  *
  36  * @author Chris Hegarty
  37  */
  38 
  39 class PlainSocketImpl extends AbstractPlainSocketImpl
  40 {
  41     static {
  42         initProto();
  43     }
  44 
  45     /**
  46      * Constructs an empty instance.
  47      */
  48     PlainSocketImpl() { }
  49 
  50     /**
  51      * Constructs an instance with the given file descriptor.
  52      */
  53     PlainSocketImpl(FileDescriptor fd) {
  54         this.fd = fd;
  55     }
  56 
  57     static final ExtendedSocketOptions extendedOptions =
  58             ExtendedSocketOptions.getInstance();
  59 
  60     protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
  61         if (!extendedOptions.isOptionSupported(name)) {
  62             if (!name.equals(StandardSocketOptions.SO_REUSEPORT)) {
  63                 super.setOption(name, value);
  64             } else {
  65                 if (supportedOptions().contains(name)) {
  66                     super.setOption(name, value);
  67                 } else {
  68                     throw new UnsupportedOperationException("unsupported option");
  69                 }
  70             }
  71         } else {
  72             if (isClosedOrPending()) {
  73                 throw new SocketException("Socket closed");
  74             }
  75             extendedOptions.setOption(fd, name, value);
  76         }
  77     }
  78 
  79     @SuppressWarnings("unchecked")
  80     protected <T> T getOption(SocketOption<T> name) throws IOException {
  81         if (!extendedOptions.isOptionSupported(name)) {
  82             if (!name.equals(StandardSocketOptions.SO_REUSEPORT)) {
  83                 return super.getOption(name);
  84             } else {
  85                 if (supportedOptions().contains(name)) {
  86                     return super.getOption(name);
  87                 } else {
  88                     throw new UnsupportedOperationException("unsupported option");
  89                 }
  90             }
  91         } else {
  92             if (isClosedOrPending()) {
  93                 throw new SocketException("Socket closed");
  94             }
  95             return (T) extendedOptions.getOption(fd, name);
  96         }
  97     }
  98 
  99     protected Set<SocketOption<?>> supportedOptions() {
 100         HashSet<SocketOption<?>> options = new HashSet<>(super.supportedOptions());
 101         addExtSocketOptions(extendedOptions.options(), options);
 102         return options;
 103     }
 104 
 105     private void addExtSocketOptions(Set<SocketOption<?>> extOptions,
 106             Set<SocketOption<?>> options) {
 107         for (SocketOption<?> option : extOptions) {
 108             if (option.name().equals("SO_FLOW_SLA")) {
 109                 // SO_FLOW_SLA is Solaris specific option whis is not applicable
 110                 // for ServerSockets.
 111                 // getSocket() will always return null for server socket
 112                 if (getSocket() != null) {
 113                     options.add(option);
 114                 }
 115             }
 116             options.add(option);
 117         }
 118     }
 119     protected void socketSetOption(int opt, boolean b, Object val) throws SocketException {
 120         if (opt == SocketOptions.SO_REUSEPORT &&
 121             !supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
 122             throw new UnsupportedOperationException("unsupported option");
 123         }
 124         try {
 125             socketSetOption0(opt, b, val);
 126         } catch (SocketException se) {
 127             if (socket == null || !socket.isConnected())
 128                 throw se;
 129         }
 130     }
 131 
 132     native void socketCreate(boolean isServer) throws IOException;
 133 
 134     native void socketConnect(InetAddress address, int port, int timeout)
 135         throws IOException;
 136 
 137     native void socketBind(InetAddress address, int port)
 138         throws IOException;
 139 
 140     native void socketListen(int count) throws IOException;
 141 
 142     native void socketAccept(SocketImpl s) throws IOException;
 143 
 144     native int socketAvailable() throws IOException;
 145 
 146     native void socketClose0(boolean useDeferredClose) throws IOException;
 147 
 148     native void socketShutdown(int howto) throws IOException;
 149 
 150     static native void initProto();
 151 
 152     native void socketSetOption0(int cmd, boolean on, Object value)
 153         throws SocketException;
 154 
 155     native int socketGetOption(int opt, Object iaContainerObj) throws SocketException;
 156 
 157     native void socketSendUrgentData(int data) throws IOException;
 158 }