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 private static final PlatformSocketOptions platformSocketOptions = 72 PlatformSocketOptions.get(); 73 74 private static final boolean flowSupported = 75 platformSocketOptions.flowSupported(); 76 77 private static final Set<SocketOption<?>> extendedOptions = options(); 78 79 static Set<SocketOption<?>> options() { 80 if (flowSupported) 81 return Set.of(SO_FLOW_SLA); 82 else 83 return Collections.<SocketOption<?>>emptySet(); 84 } 85 86 static { 87 // Registers the extended socket options with the base module. 88 sun.net.ext.ExtendedSocketOptions.register( 89 new sun.net.ext.ExtendedSocketOptions(extendedOptions) { 90 91 @Override 92 public void setOption(FileDescriptor fd, 93 SocketOption<?> option, 94 Object value) 95 throws SocketException 96 { 97 SecurityManager sm = System.getSecurityManager(); 98 if (sm != null) 99 sm.checkPermission(new NetworkPermission("setOption." + option.name())); 100 101 if (fd == null || !fd.valid()) 102 throw new SocketException("socket closed"); 103 104 if (option == SO_FLOW_SLA) { 105 assert flowSupported; 106 SocketFlow flow = checkValueType(value, option.type()); 107 setFlowOption(fd, flow); 108 } else { 109 throw new InternalError("Unexpected option " + option); 110 } 111 } 112 113 @Override 114 public Object getOption(FileDescriptor fd, 115 SocketOption<?> option) 116 throws SocketException 117 { 118 SecurityManager sm = System.getSecurityManager(); 119 if (sm != null) 120 sm.checkPermission(new NetworkPermission("getOption." + option.name())); 121 122 if (fd == null || !fd.valid()) 123 throw new SocketException("socket closed"); 124 125 if (option == SO_FLOW_SLA) { 126 assert flowSupported; 127 SocketFlow flow = SocketFlow.create(); 128 getFlowOption(fd, flow); 129 return flow; 130 } else { 131 throw new InternalError("Unexpected option " + option); 132 } 133 } 134 }); 135 } 136 137 @SuppressWarnings("unchecked") 138 private static <T> T checkValueType(Object value, Class<?> type) { 139 if (!type.isAssignableFrom(value.getClass())) { 140 String s = "Found: " + value.getClass() + ", Expected: " + type; 141 throw new IllegalArgumentException(s); 142 } 143 return (T) value; 144 } 145 146 private static final JavaIOFileDescriptorAccess fdAccess = 147 SharedSecrets.getJavaIOFileDescriptorAccess(); 148 149 private static void setFlowOption(FileDescriptor fd, SocketFlow f) 150 throws SocketException 151 { 152 int status = platformSocketOptions.setFlowOption(fdAccess.get(fd), 153 f.priority(), 154 f.bandwidth()); 155 f.status(status); // augment the given flow with the status 156 } 157 158 private static void getFlowOption(FileDescriptor fd, SocketFlow f) 159 throws SocketException 160 { 161 int status = platformSocketOptions.getFlowOption(fdAccess.get(fd), f); 162 f.status(status); // augment the given flow with the status 163 } 164 165 static class PlatformSocketOptions { 166 167 protected PlatformSocketOptions() {} 168 169 @SuppressWarnings("unchecked") 170 private static PlatformSocketOptions newInstance(String cn) { 171 Class<PlatformSocketOptions> c; 172 try { 173 c = (Class<PlatformSocketOptions>)Class.forName(cn); 174 return c.getConstructor(new Class<?>[] { }).newInstance(); 175 } catch (ReflectiveOperationException x) { 176 throw new AssertionError(x); 177 } 178 } 179 180 private static PlatformSocketOptions create() { 181 String osname = AccessController.doPrivileged( 182 new PrivilegedAction<String>() { 183 public String run() { 184 return System.getProperty("os.name"); 185 } 186 }); 187 if ("SunOS".equals(osname)) 188 return newInstance("jdk.net.SolarisSocketOptions"); 189 return new PlatformSocketOptions(); 190 } 191 192 private static final PlatformSocketOptions instance = create(); 193 194 static PlatformSocketOptions get() { 195 return instance; 196 } 197 198 int setFlowOption(int fd, int priority, long bandwidth) 199 throws SocketException 200 { 201 throw new UnsupportedOperationException("unsupported socket option"); 202 } 203 204 int getFlowOption(int fd, SocketFlow f) throws SocketException { 205 throw new UnsupportedOperationException("unsupported socket option"); 206 } 207 208 boolean flowSupported() { 209 return false; 210 } 211 } 212 } | 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 } |