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.*;
29 import java.net.*;
30 import java.nio.channels.*;
31 import java.util.*;
32 import java.security.AccessController;
33 import java.security.PrivilegedAction;
34 import java.security.PrivilegedExceptionAction;
35
36
37 public class Net {
38
39 private Net() { }
40
41 // unspecified protocol family
42 static final ProtocolFamily UNSPEC = new ProtocolFamily() {
43 public String name() {
44 return "UNSPEC";
45 }
46 };
47
48 // set to true if exclusive binding is on for Windows
49 private static final boolean exclusiveBind;
50
51 static {
52 int availLevel = isExclusiveBindAvailable();
53 if (availLevel >= 0) {
54 String exclBindProp =
280 address[13] = ip4address[1];
281 address[14] = ip4address[2];
282 address[15] = ip4address[3];
283 return address;
284 }
285
286 throw new AssertionError("Should not reach here");
287 }
288
289 // -- Socket options
290
291 static void setSocketOption(FileDescriptor fd, ProtocolFamily family,
292 SocketOption<?> name, Object value)
293 throws IOException
294 {
295 if (value == null)
296 throw new IllegalArgumentException("Invalid option value");
297
298 // only simple values supported by this method
299 Class<?> type = name.type();
300 if (type != Integer.class && type != Boolean.class)
301 throw new AssertionError("Should not reach here");
302
303 // special handling
304 if (name == StandardSocketOptions.SO_RCVBUF ||
305 name == StandardSocketOptions.SO_SNDBUF)
306 {
307 int i = ((Integer)value).intValue();
308 if (i < 0)
309 throw new IllegalArgumentException("Invalid send/receive buffer size");
310 }
311 if (name == StandardSocketOptions.SO_LINGER) {
312 int i = ((Integer)value).intValue();
313 if (i < 0)
314 value = Integer.valueOf(-1);
315 if (i > 65535)
316 value = Integer.valueOf(65535);
317 }
318 if (name == StandardSocketOptions.IP_TOS) {
319 int i = ((Integer)value).intValue();
332 throw new AssertionError("Option not found");
333
334 int arg;
335 if (type == Integer.class) {
336 arg = ((Integer)value).intValue();
337 } else {
338 boolean b = ((Boolean)value).booleanValue();
339 arg = (b) ? 1 : 0;
340 }
341
342 boolean mayNeedConversion = (family == UNSPEC);
343 setIntOption0(fd, mayNeedConversion, key.level(), key.name(), arg);
344 }
345
346 static Object getSocketOption(FileDescriptor fd, ProtocolFamily family,
347 SocketOption<?> name)
348 throws IOException
349 {
350 Class<?> type = name.type();
351
352 // only simple values supported by this method
353 if (type != Integer.class && type != Boolean.class)
354 throw new AssertionError("Should not reach here");
355
356 // map option name to platform level/name
357 OptionKey key = SocketOptionRegistry.findOption(name, family);
358 if (key == null)
359 throw new AssertionError("Option not found");
360
361 boolean mayNeedConversion = (family == UNSPEC);
362 int value = getIntOption0(fd, mayNeedConversion, key.level(), key.name());
363
364 if (type == Integer.class) {
365 return Integer.valueOf(value);
366 } else {
367 return (value == 0) ? Boolean.FALSE : Boolean.TRUE;
368 }
369 }
370
371 // -- Socket operations --
|
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.*;
29 import java.net.*;
30 import jdk.net.*;
31 import java.nio.channels.*;
32 import java.util.*;
33 import java.security.AccessController;
34 import java.security.PrivilegedAction;
35 import java.security.PrivilegedExceptionAction;
36 import sun.net.ExtendedOptionsImpl;
37
38
39 public class Net {
40
41 private Net() { }
42
43 // unspecified protocol family
44 static final ProtocolFamily UNSPEC = new ProtocolFamily() {
45 public String name() {
46 return "UNSPEC";
47 }
48 };
49
50 // set to true if exclusive binding is on for Windows
51 private static final boolean exclusiveBind;
52
53 static {
54 int availLevel = isExclusiveBindAvailable();
55 if (availLevel >= 0) {
56 String exclBindProp =
282 address[13] = ip4address[1];
283 address[14] = ip4address[2];
284 address[15] = ip4address[3];
285 return address;
286 }
287
288 throw new AssertionError("Should not reach here");
289 }
290
291 // -- Socket options
292
293 static void setSocketOption(FileDescriptor fd, ProtocolFamily family,
294 SocketOption<?> name, Object value)
295 throws IOException
296 {
297 if (value == null)
298 throw new IllegalArgumentException("Invalid option value");
299
300 // only simple values supported by this method
301 Class<?> type = name.type();
302
303 if (type == SocketFlow.class) {
304 SecurityManager sm = System.getSecurityManager();
305 if (sm != null) {
306 sm.checkPermission(new NetworkPermission("setOption.SO_FLOW_SLA"));
307 }
308 ExtendedOptionsImpl.setFlowOption(fd, (SocketFlow)value);
309 return;
310 }
311
312 if (type != Integer.class && type != Boolean.class)
313 throw new AssertionError("Should not reach here");
314
315 // special handling
316 if (name == StandardSocketOptions.SO_RCVBUF ||
317 name == StandardSocketOptions.SO_SNDBUF)
318 {
319 int i = ((Integer)value).intValue();
320 if (i < 0)
321 throw new IllegalArgumentException("Invalid send/receive buffer size");
322 }
323 if (name == StandardSocketOptions.SO_LINGER) {
324 int i = ((Integer)value).intValue();
325 if (i < 0)
326 value = Integer.valueOf(-1);
327 if (i > 65535)
328 value = Integer.valueOf(65535);
329 }
330 if (name == StandardSocketOptions.IP_TOS) {
331 int i = ((Integer)value).intValue();
344 throw new AssertionError("Option not found");
345
346 int arg;
347 if (type == Integer.class) {
348 arg = ((Integer)value).intValue();
349 } else {
350 boolean b = ((Boolean)value).booleanValue();
351 arg = (b) ? 1 : 0;
352 }
353
354 boolean mayNeedConversion = (family == UNSPEC);
355 setIntOption0(fd, mayNeedConversion, key.level(), key.name(), arg);
356 }
357
358 static Object getSocketOption(FileDescriptor fd, ProtocolFamily family,
359 SocketOption<?> name)
360 throws IOException
361 {
362 Class<?> type = name.type();
363
364 if (type == SocketFlow.class) {
365 SecurityManager sm = System.getSecurityManager();
366 if (sm != null) {
367 sm.checkPermission(new NetworkPermission("getOption.SO_FLOW_SLA"));
368 }
369 SocketFlow flow = SocketFlow.create();
370 ExtendedOptionsImpl.getFlowOption(fd, flow);
371 return flow;
372 }
373
374 // only simple values supported by this method
375 if (type != Integer.class && type != Boolean.class)
376 throw new AssertionError("Should not reach here");
377
378 // map option name to platform level/name
379 OptionKey key = SocketOptionRegistry.findOption(name, family);
380 if (key == null)
381 throw new AssertionError("Option not found");
382
383 boolean mayNeedConversion = (family == UNSPEC);
384 int value = getIntOption0(fd, mayNeedConversion, key.level(), key.name());
385
386 if (type == Integer.class) {
387 return Integer.valueOf(value);
388 } else {
389 return (value == 0) ? Boolean.FALSE : Boolean.TRUE;
390 }
391 }
392
393 // -- Socket operations --
|