src/java.base/share/classes/sun/nio/ch/Net.java
branchunixdomainchannels
changeset 58801 119ac9128c1b
parent 55375 96c7427456f9
child 58847 692de65ab293
--- a/src/java.base/share/classes/sun/nio/ch/Net.java	Fri Oct 25 14:50:16 2019 +0100
+++ b/src/java.base/share/classes/sun/nio/ch/Net.java	Fri Oct 25 15:56:35 2019 +0100
@@ -43,6 +43,7 @@
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.NotYetBoundException;
 import java.nio.channels.NotYetConnectedException;
+import java.nio.channels.UnixDomainSocketAddress;
 import java.nio.channels.UnresolvedAddressException;
 import java.nio.channels.UnsupportedAddressTypeException;
 import java.security.AccessController;
@@ -53,17 +54,31 @@
 import sun.net.util.IPAddressUtil;
 import sun.security.action.GetPropertyAction;
 
+
 public class Net {
 
-    private Net() { }
+    public Net() { }
+
+    private static final boolean unixDomainSupported;
+    //private static final boolean abstractNamesSupported;
 
     // unspecified protocol family
-    static final ProtocolFamily UNSPEC = new ProtocolFamily() {
+    public static final ProtocolFamily UNSPEC = new ProtocolFamily() {
         public String name() {
             return "UNSPEC";
         }
     };
 
+    static {
+        unixDomainSupported = unixDomainSocketSupported();
+        String name = GetPropertyAction.privilegedGetProperty("os.name")
+            .toLowerCase();
+
+        // Windows claims to support it but doesn't. Disable for now
+        //abstractNamesSupported = name.startsWith("linux");
+            //  || name.startsWith("windows");
+    }
+
     // set to true if exclusive binding is on for Windows
     private static final boolean exclusiveBind;
 
@@ -102,7 +117,7 @@
     /**
      * Returns true if exclusive binding is on
      */
-    static boolean useExclusiveBind() {
+    public static boolean useExclusiveBind() {
         return exclusiveBind;
     }
 
@@ -311,13 +326,13 @@
     static final ExtendedSocketOptions extendedOptions =
             ExtendedSocketOptions.getInstance();
 
-    static void setSocketOption(FileDescriptor fd, SocketOption<?> name, Object value)
+    public static void setSocketOption(FileDescriptor fd, SocketOption<?> name, Object value)
         throws IOException
     {
         setSocketOption(fd, Net.UNSPEC, name, value);
     }
 
-    static void setSocketOption(FileDescriptor fd, ProtocolFamily family,
+    public static void setSocketOption(FileDescriptor fd, ProtocolFamily family,
                                 SocketOption<?> name, Object value)
         throws IOException
     {
@@ -379,13 +394,13 @@
         setIntOption0(fd, mayNeedConversion, key.level(), key.name(), arg, isIPv6);
     }
 
-    static Object getSocketOption(FileDescriptor fd, SocketOption<?> name)
+    public static Object getSocketOption(FileDescriptor fd, SocketOption<?> name)
         throws IOException
     {
         return getSocketOption(fd, Net.UNSPEC, name);
     }
 
-    static Object getSocketOption(FileDescriptor fd, ProtocolFamily family, SocketOption<?> name)
+    public static Object getSocketOption(FileDescriptor fd, ProtocolFamily family, SocketOption<?> name)
         throws IOException
     {
         Class<?> type = name.type();
@@ -444,7 +459,7 @@
         return IOUtil.newFD(socket0(preferIPv6, stream, false, fastLoopback));
     }
 
-    static FileDescriptor serverSocket(boolean stream) {
+    public static FileDescriptor serverSocket(boolean stream) {
         return IOUtil.newFD(socket0(isIPv6Available(), stream, true, fastLoopback));
     }
 
@@ -474,7 +489,7 @@
                                      int port)
         throws IOException;
 
-    static native void listen(FileDescriptor fd, int backlog) throws IOException;
+    public static native void listen(FileDescriptor fd, int backlog) throws IOException;
 
     static int connect(FileDescriptor fd, InetAddress remote, int remotePort)
         throws IOException
@@ -508,7 +523,7 @@
     public static final int SHUT_WR = 1;
     public static final int SHUT_RDWR = 2;
 
-    static native void shutdown(FileDescriptor fd, int how) throws IOException;
+    public static native void shutdown(FileDescriptor fd, int how) throws IOException;
 
     private static native int localPort(FileDescriptor fd)
         throws IOException;
@@ -575,14 +590,14 @@
      *
      * @return true if connected
      */
-    static boolean pollConnectNow(FileDescriptor fd) throws IOException {
+    public static boolean pollConnectNow(FileDescriptor fd) throws IOException {
         return pollConnect(fd, 0);
     }
 
     /**
      * Return the number of bytes in the socket input buffer.
      */
-    static native int available(FileDescriptor fd) throws IOException;
+    public static native int available(FileDescriptor fd) throws IOException;
 
     /**
      * Send one byte of urgent data (MSG_OOB) on the socket.
@@ -693,6 +708,56 @@
      */
     public static final short POLLIN;
     public static final short POLLOUT;
+
+    public static boolean isUnixDomainSupported() {
+        return unixDomainSupported;
+    }
+
+    public static UnixDomainSocketAddress checkUnixAddress(SocketAddress sa) {
+        if (sa == null)
+            throw new NullPointerException();
+        if (!(sa instanceof UnixDomainSocketAddress))
+            throw new UnsupportedAddressTypeException();
+        UnixDomainSocketAddress usa = (UnixDomainSocketAddress)sa;
+        //if (usa.isAbstract() && !abstractNamesSupported())
+            //throw new UnsupportedAddressTypeException();
+        return usa;
+    }
+
+    /**
+     * 2 methods to be implemented if fine-grained security to be used
+     */
+    static UnixDomainSocketAddress getRevealedLocalAddress(UnixDomainSocketAddress addr) {
+        return addr;
+    }
+
+    static String getRevealedLocalAddressAsString(UnixDomainSocketAddress addr) {
+        return addr.toString();
+    }
+
+    // -- Socket operations --
+
+    public static FileDescriptor unixDomainSocket() throws IOException {
+        return IOUtil.newFD(unixDomainSocket0());
+    }
+
+    public static native boolean unixDomainSocketSupported();
+
+    private static native int unixDomainSocket0();
+
+    static native void unixDomainBind(FileDescriptor fd, UnixDomainSocketAddress addr)
+        throws IOException;
+
+    static native int unixDomainConnect(FileDescriptor fd, UnixDomainSocketAddress remote)
+        throws IOException;
+
+    static native int unixDomainAccept(FileDescriptor fd,
+                                     FileDescriptor newfd,
+                                     SocketAddress[] isaa)
+        throws IOException;
+
+    public static native UnixDomainSocketAddress localUnixAddress(FileDescriptor fd)
+        throws IOException;
     public static final short POLLERR;
     public static final short POLLHUP;
     public static final short POLLNVAL;
@@ -736,4 +801,6 @@
 
         fastLoopback = isFastTcpLoopbackRequested();
     }
+
+
 }