< prev index next >

src/java.base/share/classes/sun/nio/ch/Net.java

Print this page
rev 55750 : UDS support, temporary commit


  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.FileDescriptor;
  29 import java.io.IOException;
  30 import java.net.Inet4Address;
  31 import java.net.Inet6Address;
  32 import java.net.InetAddress;
  33 import java.net.InetSocketAddress;
  34 import java.net.NetworkInterface;
  35 import java.net.ProtocolFamily;
  36 import java.net.SocketAddress;
  37 import java.net.SocketException;
  38 import java.net.SocketOption;
  39 import java.net.StandardProtocolFamily;
  40 import java.net.StandardSocketOptions;

  41 import java.net.UnknownHostException;
  42 import java.nio.channels.AlreadyBoundException;
  43 import java.nio.channels.ClosedChannelException;
  44 import java.nio.channels.NotYetBoundException;
  45 import java.nio.channels.NotYetConnectedException;
  46 import java.nio.channels.UnresolvedAddressException;
  47 import java.nio.channels.UnsupportedAddressTypeException;
  48 import java.security.AccessController;
  49 import java.security.PrivilegedAction;
  50 import java.util.Enumeration;
  51 
  52 import sun.net.ext.ExtendedSocketOptions;
  53 import sun.net.util.IPAddressUtil;
  54 import sun.security.action.GetPropertyAction;
  55 
  56 public class Net {
  57 
  58     private Net() { }
  59 
  60     // unspecified protocol family


 422     // -- Socket operations --
 423 
 424     private static native boolean isIPv6Available0();
 425 
 426     private static native boolean isReusePortAvailable0();
 427 
 428     /*
 429      * Returns 1 for Windows and -1 for Solaris/Linux/Mac OS
 430      */
 431     private static native int isExclusiveBindAvailable();
 432 
 433     private static native boolean canIPv6SocketJoinIPv4Group0();
 434 
 435     private static native boolean canJoin6WithIPv4Group0();
 436 
 437     static FileDescriptor socket(boolean stream) throws IOException {
 438         return socket(UNSPEC, stream);
 439     }
 440 
 441     static FileDescriptor socket(ProtocolFamily family, boolean stream) throws IOException {



 442         boolean preferIPv6 = isIPv6Available() &&
 443             (family != StandardProtocolFamily.INET);
 444         return IOUtil.newFD(socket0(preferIPv6, stream, false, fastLoopback));
 445     }









 446 
 447     static FileDescriptor serverSocket(boolean stream) {
 448         return IOUtil.newFD(socket0(isIPv6Available(), stream, true, fastLoopback));
 449     }
 450 
 451     // Due to oddities SO_REUSEADDR on windows reuse is ignored
 452     private static native int socket0(boolean preferIPv6, boolean stream, boolean reuse,
 453                                       boolean fastLoopback);
 454 












 455     public static void bind(FileDescriptor fd, InetAddress addr, int port)
 456         throws IOException
 457     {
 458         bind(UNSPEC, fd, addr, port);
 459     }
 460 
 461     static void bind(ProtocolFamily family, FileDescriptor fd,
 462                      InetAddress addr, int port) throws IOException
 463     {
 464         boolean preferIPv6 = isIPv6Available() &&
 465             (family != StandardProtocolFamily.INET);
 466         if (addr.isLinkLocalAddress()) {
 467             addr = IPAddressUtil.toScopedAddress(addr);
 468         }
 469         bind0(fd, preferIPv6, exclusiveBind, addr, port);
 470     }
 471 
 472     private static native void bind0(FileDescriptor fd, boolean preferIPv6,
 473                                      boolean useExclBind, InetAddress addr,
 474                                      int port)
 475         throws IOException;
 476 
 477     static native void listen(FileDescriptor fd, int backlog) throws IOException;
 478 
 479     static int connect(FileDescriptor fd, InetAddress remote, int remotePort)
 480         throws IOException
 481     {
 482         return connect(UNSPEC, fd, remote, remotePort);
 483     }
 484 






 485     static int connect(ProtocolFamily family, FileDescriptor fd, InetAddress remote, int remotePort)
 486         throws IOException
 487     {
 488         if (remote.isLinkLocalAddress()) {
 489             remote = IPAddressUtil.toScopedAddress(remote);
 490         }
 491         boolean preferIPv6 = isIPv6Available() &&
 492             (family != StandardProtocolFamily.INET);
 493         return connect0(preferIPv6, fd, remote, remotePort);
 494     }
 495 
 496     private static native int connect0(boolean preferIPv6,
 497                                        FileDescriptor fd,
 498                                        InetAddress remote,
 499                                        int remotePort)
 500         throws IOException;
 501 
 502     public static native int accept(FileDescriptor fd,



 503                                     FileDescriptor newfd,
 504                                     InetSocketAddress[] isaa)













 505         throws IOException;
 506 
 507     public static final int SHUT_RD = 0;
 508     public static final int SHUT_WR = 1;
 509     public static final int SHUT_RDWR = 2;
 510 
 511     static native void shutdown(FileDescriptor fd, int how) throws IOException;
 512 
 513     private static native int localPort(FileDescriptor fd)
 514         throws IOException;
 515 
 516     private static native InetAddress localInetAddress(FileDescriptor fd)
 517         throws IOException;
 518 
 519     public static InetSocketAddress localAddress(FileDescriptor fd)
 520         throws IOException
 521     {
 522         return new InetSocketAddress(localInetAddress(fd), localPort(fd));
 523     }
 524 



 525     private static native int remotePort(FileDescriptor fd)
 526         throws IOException;
 527 
 528     private static native InetAddress remoteInetAddress(FileDescriptor fd)
 529         throws IOException;
 530 
 531     static InetSocketAddress remoteAddress(FileDescriptor fd)
 532         throws IOException
 533     {
 534         return new InetSocketAddress(remoteInetAddress(fd), remotePort(fd));
 535     }
 536 
 537     private static native int getIntOption0(FileDescriptor fd, boolean mayNeedConversion,
 538                                             int level, int opt)
 539         throws IOException;
 540 
 541     private static native void setIntOption0(FileDescriptor fd, boolean mayNeedConversion,
 542                                              int level, int opt, int arg, boolean isIPv6)
 543         throws IOException;
 544 




  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.FileDescriptor;
  29 import java.io.IOException;
  30 import java.net.Inet4Address;
  31 import java.net.Inet6Address;
  32 import java.net.InetAddress;
  33 import java.net.InetSocketAddress;
  34 import java.net.NetworkInterface;
  35 import java.net.ProtocolFamily;
  36 import java.net.SocketAddress;
  37 import java.net.SocketException;
  38 import java.net.SocketOption;
  39 import java.net.StandardProtocolFamily;
  40 import java.net.StandardSocketOptions;
  41 import java.net.UnixSocketAddress;
  42 import java.net.UnknownHostException;
  43 import java.nio.channels.AlreadyBoundException;
  44 import java.nio.channels.ClosedChannelException;
  45 import java.nio.channels.NotYetBoundException;
  46 import java.nio.channels.NotYetConnectedException;
  47 import java.nio.channels.UnresolvedAddressException;
  48 import java.nio.channels.UnsupportedAddressTypeException;
  49 import java.security.AccessController;
  50 import java.security.PrivilegedAction;
  51 import java.util.Enumeration;
  52 
  53 import sun.net.ext.ExtendedSocketOptions;
  54 import sun.net.util.IPAddressUtil;
  55 import sun.security.action.GetPropertyAction;
  56 
  57 public class Net {
  58 
  59     private Net() { }
  60 
  61     // unspecified protocol family


 423     // -- Socket operations --
 424 
 425     private static native boolean isIPv6Available0();
 426 
 427     private static native boolean isReusePortAvailable0();
 428 
 429     /*
 430      * Returns 1 for Windows and -1 for Solaris/Linux/Mac OS
 431      */
 432     private static native int isExclusiveBindAvailable();
 433 
 434     private static native boolean canIPv6SocketJoinIPv4Group0();
 435 
 436     private static native boolean canJoin6WithIPv4Group0();
 437 
 438     static FileDescriptor socket(boolean stream) throws IOException {
 439         return socket(UNSPEC, stream);
 440     }
 441 
 442     static FileDescriptor socket(ProtocolFamily family, boolean stream) throws IOException {
 443         if (family == StandardProtocolFamily.UNIX) {
 444             return IOUtil.newFD(socket1(stream));
 445         } else {
 446             boolean preferIPv6 = isIPv6Available() &&
 447                 (family != StandardProtocolFamily.INET);
 448             return IOUtil.newFD(socket0(preferIPv6, stream, false, fastLoopback));
 449         }
 450     }
 451 
 452     static FileDescriptor serverSocket(boolean stream, ProtocolFamily family) {
 453         if (family == StandardProtocolFamily.UNIX) {
 454             return IOUtil.newFD(socket1(stream));
 455         } else {
 456             return serverSocket(stream);
 457         }
 458     }
 459     
 460     static FileDescriptor serverSocket(boolean stream) {
 461         return IOUtil.newFD(socket0(isIPv6Available(), stream, true, fastLoopback));
 462     }
 463     
 464     // Due to oddities SO_REUSEADDR on windows reuse is ignored
 465     private static native int socket0(boolean preferIPv6, boolean stream, boolean reuse,
 466                                       boolean fastLoopback);
 467     
 468     // TODO: rename, refactor, support maybe also SOCK_SEQPACKET, support reuse (delete socket file if exists)?
 469     private static native int socket1(boolean stream);
 470 
 471     public static void bind(FileDescriptor fd, UnixSocketAddress addr)
 472         throws IOException
 473     {
 474         bind1(fd, addr.getPathBytes());
 475     }
 476     
 477     // TODO: rename, UnixSocketAddress object instead of byte array?
 478     private static native void bind1(FileDescriptor fd, byte[] addr) throws IOException;
 479     
 480     public static void bind(FileDescriptor fd, InetAddress addr, int port)
 481         throws IOException
 482     {
 483         bind(UNSPEC, fd, addr, port);
 484     }
 485 
 486     static void bind(ProtocolFamily family, FileDescriptor fd,
 487                      InetAddress addr, int port) throws IOException
 488     {
 489         boolean preferIPv6 = isIPv6Available() &&
 490             (family != StandardProtocolFamily.INET);
 491         if (addr.isLinkLocalAddress()) {
 492             addr = IPAddressUtil.toScopedAddress(addr);
 493         }
 494         bind0(fd, preferIPv6, exclusiveBind, addr, port);
 495     }
 496 
 497     private static native void bind0(FileDescriptor fd, boolean preferIPv6,
 498                                      boolean useExclBind, InetAddress addr,
 499                                      int port)
 500         throws IOException;
 501 
 502     static native void listen(FileDescriptor fd, int backlog) throws IOException;
 503 
 504     static int connect(FileDescriptor fd, InetAddress remote, int remotePort)
 505         throws IOException
 506     {
 507         return connect(UNSPEC, fd, remote, remotePort);
 508     }
 509     
 510     static int connect(FileDescriptor fd, UnixSocketAddress remote)
 511         throws IOException
 512     {
 513         return connect1(fd, remote.getPathBytes());
 514     }
 515 
 516     static int connect(ProtocolFamily family, FileDescriptor fd, InetAddress remote, int remotePort)
 517         throws IOException
 518     {
 519         if (remote.isLinkLocalAddress()) {
 520             remote = IPAddressUtil.toScopedAddress(remote);
 521         }
 522         boolean preferIPv6 = isIPv6Available() &&
 523             (family != StandardProtocolFamily.INET);
 524         return connect0(preferIPv6, fd, remote, remotePort);
 525     }
 526 
 527     private static native int connect0(boolean preferIPv6,
 528                                        FileDescriptor fd,
 529                                        InetAddress remote,
 530                                        int remotePort)
 531         throws IOException;
 532     
 533     private static native int connect1(FileDescriptor fd, byte[] remote)
 534         throws IOException;
 535 
 536     public static int accept(FileDescriptor fd,
 537                                     FileDescriptor newfd,
 538                                     InetSocketAddress[] isaa)
 539         throws IOException {
 540         
 541         SocketAddress[] array = new SocketAddress[isaa.length];
 542         int result = accept(fd, newfd, array);
 543         for (int i = 0; i < isaa.length; i++) {
 544             isaa[i] = (InetSocketAddress) array[i];
 545         }
 546         return result;
 547     }
 548     
 549     public static native int accept(FileDescriptor fd,
 550                                     FileDescriptor newfd,
 551                                     SocketAddress[] isaa)
 552         throws IOException;
 553 
 554     public static final int SHUT_RD = 0;
 555     public static final int SHUT_WR = 1;
 556     public static final int SHUT_RDWR = 2;
 557 
 558     static native void shutdown(FileDescriptor fd, int how) throws IOException;
 559 
 560     private static native int localPort(FileDescriptor fd)
 561         throws IOException;
 562 
 563     private static native InetAddress localInetAddress(FileDescriptor fd)
 564         throws IOException;
 565 
 566     public static InetSocketAddress localAddress(FileDescriptor fd)
 567         throws IOException
 568     {
 569         return new InetSocketAddress(localInetAddress(fd), localPort(fd));
 570     }
 571     
 572     public static native SocketAddress localSocketAddress(FileDescriptor fd)
 573         throws IOException;
 574     
 575     private static native int remotePort(FileDescriptor fd)
 576         throws IOException;
 577 
 578     private static native InetAddress remoteInetAddress(FileDescriptor fd)
 579         throws IOException;
 580 
 581     static InetSocketAddress remoteAddress(FileDescriptor fd)
 582         throws IOException
 583     {
 584         return new InetSocketAddress(remoteInetAddress(fd), remotePort(fd));
 585     }
 586 
 587     private static native int getIntOption0(FileDescriptor fd, boolean mayNeedConversion,
 588                                             int level, int opt)
 589         throws IOException;
 590 
 591     private static native void setIntOption0(FileDescriptor fd, boolean mayNeedConversion,
 592                                              int level, int opt, int arg, boolean isIPv6)
 593         throws IOException;
 594 


< prev index next >