41 import java.net.UnknownHostException; |
41 import java.net.UnknownHostException; |
42 import java.nio.channels.AlreadyBoundException; |
42 import java.nio.channels.AlreadyBoundException; |
43 import java.nio.channels.ClosedChannelException; |
43 import java.nio.channels.ClosedChannelException; |
44 import java.nio.channels.NotYetBoundException; |
44 import java.nio.channels.NotYetBoundException; |
45 import java.nio.channels.NotYetConnectedException; |
45 import java.nio.channels.NotYetConnectedException; |
|
46 import java.nio.channels.UnixDomainSocketAddress; |
46 import java.nio.channels.UnresolvedAddressException; |
47 import java.nio.channels.UnresolvedAddressException; |
47 import java.nio.channels.UnsupportedAddressTypeException; |
48 import java.nio.channels.UnsupportedAddressTypeException; |
48 import java.security.AccessController; |
49 import java.security.AccessController; |
49 import java.security.PrivilegedAction; |
50 import java.security.PrivilegedAction; |
50 import java.util.Enumeration; |
51 import java.util.Enumeration; |
51 |
52 |
52 import sun.net.ext.ExtendedSocketOptions; |
53 import sun.net.ext.ExtendedSocketOptions; |
53 import sun.net.util.IPAddressUtil; |
54 import sun.net.util.IPAddressUtil; |
54 import sun.security.action.GetPropertyAction; |
55 import sun.security.action.GetPropertyAction; |
55 |
56 |
|
57 |
56 public class Net { |
58 public class Net { |
57 |
59 |
58 private Net() { } |
60 public Net() { } |
|
61 |
|
62 private static final boolean unixDomainSupported; |
|
63 //private static final boolean abstractNamesSupported; |
59 |
64 |
60 // unspecified protocol family |
65 // unspecified protocol family |
61 static final ProtocolFamily UNSPEC = new ProtocolFamily() { |
66 public static final ProtocolFamily UNSPEC = new ProtocolFamily() { |
62 public String name() { |
67 public String name() { |
63 return "UNSPEC"; |
68 return "UNSPEC"; |
64 } |
69 } |
65 }; |
70 }; |
|
71 |
|
72 static { |
|
73 unixDomainSupported = unixDomainSocketSupported(); |
|
74 String name = GetPropertyAction.privilegedGetProperty("os.name") |
|
75 .toLowerCase(); |
|
76 |
|
77 // Windows claims to support it but doesn't. Disable for now |
|
78 //abstractNamesSupported = name.startsWith("linux"); |
|
79 // || name.startsWith("windows"); |
|
80 } |
66 |
81 |
67 // set to true if exclusive binding is on for Windows |
82 // set to true if exclusive binding is on for Windows |
68 private static final boolean exclusiveBind; |
83 private static final boolean exclusiveBind; |
69 |
84 |
70 // set to true if the fast tcp loopback should be enabled on Windows |
85 // set to true if the fast tcp loopback should be enabled on Windows |
309 // -- Socket options |
324 // -- Socket options |
310 |
325 |
311 static final ExtendedSocketOptions extendedOptions = |
326 static final ExtendedSocketOptions extendedOptions = |
312 ExtendedSocketOptions.getInstance(); |
327 ExtendedSocketOptions.getInstance(); |
313 |
328 |
314 static void setSocketOption(FileDescriptor fd, SocketOption<?> name, Object value) |
329 public static void setSocketOption(FileDescriptor fd, SocketOption<?> name, Object value) |
315 throws IOException |
330 throws IOException |
316 { |
331 { |
317 setSocketOption(fd, Net.UNSPEC, name, value); |
332 setSocketOption(fd, Net.UNSPEC, name, value); |
318 } |
333 } |
319 |
334 |
320 static void setSocketOption(FileDescriptor fd, ProtocolFamily family, |
335 public static void setSocketOption(FileDescriptor fd, ProtocolFamily family, |
321 SocketOption<?> name, Object value) |
336 SocketOption<?> name, Object value) |
322 throws IOException |
337 throws IOException |
323 { |
338 { |
324 if (value == null) |
339 if (value == null) |
325 throw new IllegalArgumentException("Invalid option value"); |
340 throw new IllegalArgumentException("Invalid option value"); |
377 boolean mayNeedConversion = (family == UNSPEC); |
392 boolean mayNeedConversion = (family == UNSPEC); |
378 boolean isIPv6 = (family == StandardProtocolFamily.INET6); |
393 boolean isIPv6 = (family == StandardProtocolFamily.INET6); |
379 setIntOption0(fd, mayNeedConversion, key.level(), key.name(), arg, isIPv6); |
394 setIntOption0(fd, mayNeedConversion, key.level(), key.name(), arg, isIPv6); |
380 } |
395 } |
381 |
396 |
382 static Object getSocketOption(FileDescriptor fd, SocketOption<?> name) |
397 public static Object getSocketOption(FileDescriptor fd, SocketOption<?> name) |
383 throws IOException |
398 throws IOException |
384 { |
399 { |
385 return getSocketOption(fd, Net.UNSPEC, name); |
400 return getSocketOption(fd, Net.UNSPEC, name); |
386 } |
401 } |
387 |
402 |
388 static Object getSocketOption(FileDescriptor fd, ProtocolFamily family, SocketOption<?> name) |
403 public static Object getSocketOption(FileDescriptor fd, ProtocolFamily family, SocketOption<?> name) |
389 throws IOException |
404 throws IOException |
390 { |
405 { |
391 Class<?> type = name.type(); |
406 Class<?> type = name.type(); |
392 |
407 |
393 if (extendedOptions.isOptionSupported(name)) { |
408 if (extendedOptions.isOptionSupported(name)) { |
442 boolean preferIPv6 = isIPv6Available() && |
457 boolean preferIPv6 = isIPv6Available() && |
443 (family != StandardProtocolFamily.INET); |
458 (family != StandardProtocolFamily.INET); |
444 return IOUtil.newFD(socket0(preferIPv6, stream, false, fastLoopback)); |
459 return IOUtil.newFD(socket0(preferIPv6, stream, false, fastLoopback)); |
445 } |
460 } |
446 |
461 |
447 static FileDescriptor serverSocket(boolean stream) { |
462 public static FileDescriptor serverSocket(boolean stream) { |
448 return IOUtil.newFD(socket0(isIPv6Available(), stream, true, fastLoopback)); |
463 return IOUtil.newFD(socket0(isIPv6Available(), stream, true, fastLoopback)); |
449 } |
464 } |
450 |
465 |
451 // Due to oddities SO_REUSEADDR on windows reuse is ignored |
466 // Due to oddities SO_REUSEADDR on windows reuse is ignored |
452 private static native int socket0(boolean preferIPv6, boolean stream, boolean reuse, |
467 private static native int socket0(boolean preferIPv6, boolean stream, boolean reuse, |
472 private static native void bind0(FileDescriptor fd, boolean preferIPv6, |
487 private static native void bind0(FileDescriptor fd, boolean preferIPv6, |
473 boolean useExclBind, InetAddress addr, |
488 boolean useExclBind, InetAddress addr, |
474 int port) |
489 int port) |
475 throws IOException; |
490 throws IOException; |
476 |
491 |
477 static native void listen(FileDescriptor fd, int backlog) throws IOException; |
492 public static native void listen(FileDescriptor fd, int backlog) throws IOException; |
478 |
493 |
479 static int connect(FileDescriptor fd, InetAddress remote, int remotePort) |
494 static int connect(FileDescriptor fd, InetAddress remote, int remotePort) |
480 throws IOException |
495 throws IOException |
481 { |
496 { |
482 return connect(UNSPEC, fd, remote, remotePort); |
497 return connect(UNSPEC, fd, remote, remotePort); |
506 |
521 |
507 public static final int SHUT_RD = 0; |
522 public static final int SHUT_RD = 0; |
508 public static final int SHUT_WR = 1; |
523 public static final int SHUT_WR = 1; |
509 public static final int SHUT_RDWR = 2; |
524 public static final int SHUT_RDWR = 2; |
510 |
525 |
511 static native void shutdown(FileDescriptor fd, int how) throws IOException; |
526 public static native void shutdown(FileDescriptor fd, int how) throws IOException; |
512 |
527 |
513 private static native int localPort(FileDescriptor fd) |
528 private static native int localPort(FileDescriptor fd) |
514 throws IOException; |
529 throws IOException; |
515 |
530 |
516 private static native InetAddress localInetAddress(FileDescriptor fd) |
531 private static native InetAddress localInetAddress(FileDescriptor fd) |
573 * Performs a non-blocking poll of a connecting socket to test if the |
588 * Performs a non-blocking poll of a connecting socket to test if the |
574 * connection has been established. |
589 * connection has been established. |
575 * |
590 * |
576 * @return true if connected |
591 * @return true if connected |
577 */ |
592 */ |
578 static boolean pollConnectNow(FileDescriptor fd) throws IOException { |
593 public static boolean pollConnectNow(FileDescriptor fd) throws IOException { |
579 return pollConnect(fd, 0); |
594 return pollConnect(fd, 0); |
580 } |
595 } |
581 |
596 |
582 /** |
597 /** |
583 * Return the number of bytes in the socket input buffer. |
598 * Return the number of bytes in the socket input buffer. |
584 */ |
599 */ |
585 static native int available(FileDescriptor fd) throws IOException; |
600 public static native int available(FileDescriptor fd) throws IOException; |
586 |
601 |
587 /** |
602 /** |
588 * Send one byte of urgent data (MSG_OOB) on the socket. |
603 * Send one byte of urgent data (MSG_OOB) on the socket. |
589 */ |
604 */ |
590 static native int sendOOB(FileDescriptor fd, byte data) throws IOException; |
605 static native int sendOOB(FileDescriptor fd, byte data) throws IOException; |
691 * Event masks for the various poll system calls. |
706 * Event masks for the various poll system calls. |
692 * They will be set platform dependent in the static initializer below. |
707 * They will be set platform dependent in the static initializer below. |
693 */ |
708 */ |
694 public static final short POLLIN; |
709 public static final short POLLIN; |
695 public static final short POLLOUT; |
710 public static final short POLLOUT; |
|
711 |
|
712 public static boolean isUnixDomainSupported() { |
|
713 return unixDomainSupported; |
|
714 } |
|
715 |
|
716 public static UnixDomainSocketAddress checkUnixAddress(SocketAddress sa) { |
|
717 if (sa == null) |
|
718 throw new NullPointerException(); |
|
719 if (!(sa instanceof UnixDomainSocketAddress)) |
|
720 throw new UnsupportedAddressTypeException(); |
|
721 UnixDomainSocketAddress usa = (UnixDomainSocketAddress)sa; |
|
722 //if (usa.isAbstract() && !abstractNamesSupported()) |
|
723 //throw new UnsupportedAddressTypeException(); |
|
724 return usa; |
|
725 } |
|
726 |
|
727 /** |
|
728 * 2 methods to be implemented if fine-grained security to be used |
|
729 */ |
|
730 static UnixDomainSocketAddress getRevealedLocalAddress(UnixDomainSocketAddress addr) { |
|
731 return addr; |
|
732 } |
|
733 |
|
734 static String getRevealedLocalAddressAsString(UnixDomainSocketAddress addr) { |
|
735 return addr.toString(); |
|
736 } |
|
737 |
|
738 // -- Socket operations -- |
|
739 |
|
740 public static FileDescriptor unixDomainSocket() throws IOException { |
|
741 return IOUtil.newFD(unixDomainSocket0()); |
|
742 } |
|
743 |
|
744 public static native boolean unixDomainSocketSupported(); |
|
745 |
|
746 private static native int unixDomainSocket0(); |
|
747 |
|
748 static native void unixDomainBind(FileDescriptor fd, UnixDomainSocketAddress addr) |
|
749 throws IOException; |
|
750 |
|
751 static native int unixDomainConnect(FileDescriptor fd, UnixDomainSocketAddress remote) |
|
752 throws IOException; |
|
753 |
|
754 static native int unixDomainAccept(FileDescriptor fd, |
|
755 FileDescriptor newfd, |
|
756 SocketAddress[] isaa) |
|
757 throws IOException; |
|
758 |
|
759 public static native UnixDomainSocketAddress localUnixAddress(FileDescriptor fd) |
|
760 throws IOException; |
696 public static final short POLLERR; |
761 public static final short POLLERR; |
697 public static final short POLLHUP; |
762 public static final short POLLHUP; |
698 public static final short POLLNVAL; |
763 public static final short POLLNVAL; |
699 public static final short POLLCONN; |
764 public static final short POLLCONN; |
700 |
765 |