1 /*
   2  * Copyright (c) 2014, 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 jdk.net;
  27 
  28 import java.io.FileDescriptor;
  29 import java.net.SocketException;
  30 import java.net.SocketOption;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.util.Collections;
  34 import java.util.Set;
  35 import jdk.internal.misc.JavaIOFileDescriptorAccess;
  36 import jdk.internal.misc.SharedSecrets;
  37 
  38 /**
  39  * Defines extended socket options, beyond those defined in
  40  * {@link java.net.StandardSocketOptions}. These options may be platform
  41  * specific.
  42  *
  43  * @since 1.8
  44  */
  45 public final class ExtendedSocketOptions {
  46 
  47     private static class ExtSocketOption<T> implements SocketOption<T> {
  48         private final String name;
  49         private final Class<T> type;
  50         ExtSocketOption(String name, Class<T> type) {
  51             this.name = name;
  52             this.type = type;
  53         }
  54         @Override public String name() { return name; }
  55         @Override public Class<T> type() { return type; }
  56         @Override public String toString() { return name; }
  57     }
  58 
  59     private ExtendedSocketOptions() { }
  60 
  61     /**
  62      * Service level properties. When a security manager is installed,
  63      * setting or getting this option requires a {@link NetworkPermission}
  64      * {@code ("setOption.SO_FLOW_SLA")} or {@code "getOption.SO_FLOW_SLA"}
  65      * respectively.
  66      */
  67     public static final SocketOption<SocketFlow> SO_FLOW_SLA = new
  68         ExtSocketOption<SocketFlow>("SO_FLOW_SLA", SocketFlow.class);
  69 
  70 
  71     /**
  72      * TCP_QUICKACK.
  73      *
  74      * This socket option usually enable TCP_QUICKACK mode if set or disable
  75      * TCP_QUICKACK mode if cleared.
  76      *
  77      * The value of this socket option is a {@code Boolean} that represents
  78      * whether the option is enabled or disabled. The socket option is specific
  79      * to stream-oriented sockets using the TCP/IP protocol.
  80      *
  81      * The exact semantics of this socket option are socket type and system
  82      * dependent.
  83      *
  84      * When TCP_QUICKACK is enabled, acks are sent immediately, rather than
  85      * delayed if needed in accordance to normal TCP operation.This flag is not
  86      * permanent, it only enables a switch to or from TCP_QUICKACK mode.
  87      *
  88      * Subsequent operation of the TCP protocol will once again enter/leave
  89      * TCP_QUICKACK mode depending on internal protocol processing and factors
  90      * such as delayed ack timeouts occurring and data transfer.
  91      *
  92      * @since 18.3
  93      */
  94     public static final SocketOption<Boolean> TCP_QUICKACK
  95             = new ExtSocketOption<Boolean>("TCP_QUICKACK", Boolean.class);
  96     
  97     private static final PlatformSocketOptions platformSocketOptions =
  98             PlatformSocketOptions.get();
  99 
 100     private static final boolean flowSupported =
 101             platformSocketOptions.flowSupported();
 102     private static final boolean quickAckSupported =
 103             platformSocketOptions.quickAckSupported();
 104 
 105     private static final Set<SocketOption<?>> extendedOptions = options();
 106 
 107     static Set<SocketOption<?>> options() {
 108         if (flowSupported) {
 109             if (quickAckSupported) {
 110                 return Set.of(SO_FLOW_SLA, TCP_QUICKACK);
 111             } else {
 112                 return Set.of(SO_FLOW_SLA);
 113             }
 114         } else if (quickAckSupported) {
 115             return Set.of(TCP_QUICKACK);
 116         } else {
 117             return Collections.<SocketOption<?>>emptySet();
 118         }
 119     }
 120 
 121     static {
 122         // Registers the extended socket options with the base module.
 123         sun.net.ext.ExtendedSocketOptions.register(
 124                 new sun.net.ext.ExtendedSocketOptions(extendedOptions) {
 125 
 126             @Override
 127             public void setOption(FileDescriptor fd,
 128                                   SocketOption<?> option,
 129                                   Object value)
 130                 throws SocketException
 131             {
 132                 SecurityManager sm = System.getSecurityManager();
 133                 if (sm != null)
 134                     sm.checkPermission(new NetworkPermission("setOption." + option.name()));
 135 
 136                 if (fd == null || !fd.valid())
 137                     throw new SocketException("socket closed");
 138 
 139                 if (option == SO_FLOW_SLA) {
 140                     assert flowSupported;
 141                     SocketFlow flow = checkValueType(value, option.type());
 142                     setFlowOption(fd, flow);
 143                 } else if (option == TCP_QUICKACK) {
 144                     setQuickAckOption(fd, (boolean) value);
 145                 } else {
 146                     throw new InternalError("Unexpected option " + option);
 147                 }
 148             }
 149 
 150             @Override
 151             public Object getOption(FileDescriptor fd,
 152                                     SocketOption<?> option)
 153                 throws SocketException
 154             {
 155                 SecurityManager sm = System.getSecurityManager();
 156                 if (sm != null)
 157                     sm.checkPermission(new NetworkPermission("getOption." + option.name()));
 158 
 159                 if (fd == null || !fd.valid())
 160                     throw new SocketException("socket closed");
 161 
 162                 if (option == SO_FLOW_SLA) {
 163                     assert flowSupported;
 164                     SocketFlow flow = SocketFlow.create();
 165                     getFlowOption(fd, flow);
 166                     return flow;
 167                 } else if (option == TCP_QUICKACK) {
 168                     return getQuickAckOption(fd);
 169                 } else {
 170                     throw new InternalError("Unexpected option " + option);
 171                 }
 172             }
 173         });
 174     }
 175 
 176     @SuppressWarnings("unchecked")
 177     private static <T> T checkValueType(Object value, Class<?> type) {
 178         if (!type.isAssignableFrom(value.getClass())) {
 179             String s = "Found: " + value.getClass() + ", Expected: " + type;
 180             throw new IllegalArgumentException(s);
 181         }
 182         return (T) value;
 183     }
 184 
 185     private static final JavaIOFileDescriptorAccess fdAccess =
 186             SharedSecrets.getJavaIOFileDescriptorAccess();
 187 
 188     private static void setFlowOption(FileDescriptor fd, SocketFlow f)
 189         throws SocketException
 190     {
 191         int status = platformSocketOptions.setFlowOption(fdAccess.get(fd),
 192                                                          f.priority(),
 193                                                          f.bandwidth());
 194         f.status(status);  // augment the given flow with the status
 195     }
 196 
 197     private static void getFlowOption(FileDescriptor fd, SocketFlow f)
 198             throws SocketException {
 199         int status = platformSocketOptions.getFlowOption(fdAccess.get(fd), f);
 200         f.status(status);  // augment the given flow with the status
 201     }
 202 
 203     private static void setQuickAckOption(FileDescriptor fd, boolean enable)
 204             throws SocketException {
 205         platformSocketOptions.setQuickAck(fdAccess.get(fd), enable == true ? 1 : 0);
 206     }
 207 
 208     private static Object getQuickAckOption(FileDescriptor fd)
 209             throws SocketException {
 210         return platformSocketOptions.getQuickAck(fdAccess.get(fd));
 211     }
 212     
 213     static class PlatformSocketOptions {
 214 
 215         protected PlatformSocketOptions() {}
 216 
 217         @SuppressWarnings("unchecked")
 218         private static PlatformSocketOptions newInstance(String cn) {
 219             Class<PlatformSocketOptions> c;
 220             try {
 221                 c = (Class<PlatformSocketOptions>)Class.forName(cn);
 222                 return c.getConstructor(new Class<?>[] { }).newInstance();
 223             } catch (ReflectiveOperationException x) {
 224                 throw new AssertionError(x);
 225             }
 226         }
 227 
 228         private static PlatformSocketOptions create() {
 229             String osname = AccessController.doPrivileged(
 230                     new PrivilegedAction<String>() {
 231                         public String run() {
 232                             return System.getProperty("os.name");
 233                         }
 234                     });
 235             if ("SunOS".equals(osname)) {
 236                 return newInstance("jdk.net.SolarisSocketOptions");
 237             } else if ("Linux".equals(osname)) {
 238                 return newInstance("jdk.net.LinuxSocketOptions");
 239             } else {
 240                 return new PlatformSocketOptions();
 241             }
 242         }
 243 
 244         private static final PlatformSocketOptions instance = create();
 245 
 246         static PlatformSocketOptions get() {
 247             return instance;
 248         }
 249 
 250         int setFlowOption(int fd, int priority, long bandwidth)
 251             throws SocketException
 252         {
 253             throw new UnsupportedOperationException("unsupported socket option");
 254         }
 255 
 256         int getFlowOption(int fd, SocketFlow f) throws SocketException {
 257             throw new UnsupportedOperationException("unsupported socket option");
 258         }
 259 
 260         boolean flowSupported() {
 261             return false;
 262         }
 263         
 264         void setQuickAck(int fd, int on) throws SocketException {
 265             throw new UnsupportedOperationException("unsupported TCP_QUICKACK option");
 266         }
 267 
 268         boolean getQuickAck(int fd) throws SocketException {
 269             throw new UnsupportedOperationException("unsupported TCP_QUICKACK option");
 270         }
 271 
 272         boolean quickAckSupported() {
 273             return false;
 274         }
 275     }
 276 }