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 * SO_QUICKACK. 73 * 74 * The value of this socket option is a {@code Boolean} that represents 75 * whether the option is enabled or disabled. The socket option is specific 76 * to stream-oriented sockets using the TCP/IP protocol. 77 * 78 * The exact semantics of this socket option are socket type and system 79 * dependent. 80 * 81 * This socket option usually enable TCP_QUICKACK mode if set or disable 82 * TCP_QUICKACK mode if cleared. 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 10 93 */ 94 public static final SocketOption<Boolean> SO_QUICKACK 95 = new ExtSocketOption<Boolean>("SO_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, SO_QUICKACK); 111 } else { 112 return Set.of(SO_FLOW_SLA); 113 } 114 } else if (quickAckSupported) { 115 return Set.of(SO_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 == SO_QUICKACK) { 144 assert quickAckSupported; 145 setQuickAckOption(fd, (boolean)value); 146 } else { 147 throw new InternalError("Unexpected option " + option); 148 } 149 } 150 151 @Override 152 public Object getOption(FileDescriptor fd, 153 SocketOption<?> option) 154 throws SocketException 155 { 156 SecurityManager sm = System.getSecurityManager(); 157 if (sm != null) 158 sm.checkPermission(new NetworkPermission("getOption." + option.name())); 159 160 if (fd == null || !fd.valid()) 161 throw new SocketException("socket closed"); 162 163 if (option == SO_FLOW_SLA) { 164 assert flowSupported; 165 SocketFlow flow = SocketFlow.create(); 166 getFlowOption(fd, flow); 167 return flow; 168 } else if (option == SO_QUICKACK) { 169 return getQuickAckOption(fd); 170 } else { 171 throw new InternalError("Unexpected option " + option); 172 } 173 } 174 }); 175 } 176 177 @SuppressWarnings("unchecked") 178 private static <T> T checkValueType(Object value, Class<?> type) { 179 if (!type.isAssignableFrom(value.getClass())) { 180 String s = "Found: " + value.getClass() + ", Expected: " + type; 181 throw new IllegalArgumentException(s); 182 } 183 return (T) value; 184 } 185 186 private static final JavaIOFileDescriptorAccess fdAccess = 187 SharedSecrets.getJavaIOFileDescriptorAccess(); 188 189 private static void setFlowOption(FileDescriptor fd, SocketFlow f) 190 throws SocketException 191 { 192 int status = platformSocketOptions.setFlowOption(fdAccess.get(fd), 193 f.priority(), 194 f.bandwidth()); 195 f.status(status); // augment the given flow with the status 196 } 197 198 private static void getFlowOption(FileDescriptor fd, SocketFlow f) 199 throws SocketException { 200 int status = platformSocketOptions.getFlowOption(fdAccess.get(fd), f); 201 f.status(status); // augment the given flow with the status 202 } 203 204 private static void setQuickAckOption(FileDescriptor fd, boolean on) 205 throws SocketException { 206 platformSocketOptions.setQuickAck(fdAccess.get(fd), on == true ? 1 : 0); 207 } 208 209 private static Object getQuickAckOption(FileDescriptor fd) 210 throws SocketException { 211 return platformSocketOptions.getQuickAck(fdAccess.get(fd)); 212 } 213 214 static class PlatformSocketOptions { 215 216 protected PlatformSocketOptions() {} 217 218 @SuppressWarnings("unchecked") 219 private static PlatformSocketOptions newInstance(String cn) { 220 Class<PlatformSocketOptions> c; 221 try { 222 c = (Class<PlatformSocketOptions>)Class.forName(cn); 223 return c.getConstructor(new Class<?>[] { }).newInstance(); 224 } catch (ReflectiveOperationException x) { 225 throw new AssertionError(x); 226 } 227 } 228 229 private static PlatformSocketOptions create() { 230 String osname = AccessController.doPrivileged( 231 new PrivilegedAction<String>() { 232 public String run() { 233 return System.getProperty("os.name"); 234 } 235 }); 236 if ("SunOS".equals(osname)) { 237 return newInstance("jdk.net.SolarisSocketOptions"); 238 } else if ("Linux".equals(osname)) { 239 return newInstance("jdk.net.LinuxSocketOptions"); 240 } else { 241 return new PlatformSocketOptions(); 242 } 243 } 244 245 private static final PlatformSocketOptions instance = create(); 246 247 static PlatformSocketOptions get() { 248 return instance; 249 } 250 251 int setFlowOption(int fd, int priority, long bandwidth) 252 throws SocketException 253 { 254 throw new UnsupportedOperationException("unsupported socket option"); 255 } 256 257 int getFlowOption(int fd, SocketFlow f) throws SocketException { 258 throw new UnsupportedOperationException("unsupported socket option"); 259 } 260 261 boolean flowSupported() { 262 return false; 263 } 264 265 void setQuickAck(int fd, int on) throws SocketException { 266 throw new UnsupportedOperationException("unsupported socket option"); 267 } 268 269 Object getQuickAck(int fd) throws SocketException { 270 throw new UnsupportedOperationException("unsupported socket option"); 271 } 272 273 boolean quickAckSupported() { 274 return false; 275 } 276 } 277 }