Replace createDatagramSocket and createStreamSocket with one call.

(Continuing our policy of having native methods' names correspond to the
underlying syscall, this one sadly gets the ugly name of "socket".)

Change-Id: Icf08e4e0637ee3ae9eab673d350860181f547250
diff --git a/dalvik/src/main/java/dalvik/system/BlockGuard.java b/dalvik/src/main/java/dalvik/system/BlockGuard.java
index 0cf8a8a..0e8c892 100644
--- a/dalvik/src/main/java/dalvik/system/BlockGuard.java
+++ b/dalvik/src/main/java/dalvik/system/BlockGuard.java
@@ -332,8 +332,8 @@
             mNetwork.disconnectDatagram(aFD);
         }
 
-        public void createDatagramSocket(FileDescriptor aFD) throws SocketException {
-            mNetwork.createDatagramSocket(aFD);
+        public void socket(FileDescriptor aFD, boolean stream) throws SocketException {
+            mNetwork.socket(aFD, stream);
         }
 
         public void shutdownInput(FileDescriptor descriptor) throws IOException {
@@ -348,10 +348,6 @@
             mNetwork.sendUrgentData(fd, value);
         }
 
-        public void createStreamSocket(FileDescriptor aFD) throws SocketException {
-            mNetwork.createStreamSocket(aFD);
-        }
-
         public void listen(FileDescriptor aFD, int backlog) throws SocketException {
             mNetwork.listen(aFD, backlog);
         }
diff --git a/luni/src/main/java/java/net/InetAddress.java b/luni/src/main/java/java/net/InetAddress.java
index 59f88a0..b014a77 100644
--- a/luni/src/main/java/java/net/InetAddress.java
+++ b/luni/src/main/java/java/net/InetAddress.java
@@ -865,7 +865,7 @@
             throws IOException {
         FileDescriptor fd = new FileDescriptor();
         boolean reached = false;
-        NETIMPL.createStreamSocket(fd);
+        NETIMPL.socket(fd, true);
         try {
             if (null != source) {
                 NETIMPL.bind(fd, source, 0);
diff --git a/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java b/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java
index ce0a8c6..f4cf203 100644
--- a/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java
+++ b/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java
@@ -107,7 +107,7 @@
 
     @Override
     public void create() throws SocketException {
-        netImpl.createDatagramSocket(fd);
+        netImpl.socket(fd, false);
     }
 
     @Override
diff --git a/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java b/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
index 5eb241d..840496e 100644
--- a/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
+++ b/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
@@ -213,11 +213,7 @@
     @Override
     protected void create(boolean streaming) throws IOException {
         this.streaming = streaming;
-        if (streaming) {
-            netImpl.createStreamSocket(fd);
-        } else {
-            netImpl.createDatagramSocket(fd);
-        }
+        netImpl.socket(fd, streaming);
     }
 
     @Override
diff --git a/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java b/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java
index 152612d..24a42db 100644
--- a/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java
+++ b/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java
@@ -61,7 +61,7 @@
 
     public void disconnectDatagram(FileDescriptor fd) throws SocketException;
 
-    public void createDatagramSocket(FileDescriptor fd) throws SocketException;
+    public void socket(FileDescriptor fd, boolean stream) throws SocketException;
 
     public void shutdownInput(FileDescriptor descriptor) throws IOException;
 
@@ -69,8 +69,6 @@
 
     public void sendUrgentData(FileDescriptor fd, byte value);
 
-    public void createStreamSocket(FileDescriptor fd) throws SocketException;
-
     public void listen(FileDescriptor fd, int backlog) throws SocketException;
 
     public void connect(FileDescriptor fd, InetAddress inetAddress, int port, int timeout)
diff --git a/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java b/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
index d3d58e0..4be00f2 100644
--- a/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
+++ b/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
@@ -50,9 +50,7 @@
             throws IOException;
     public native boolean isConnected(FileDescriptor fd, int timeout) throws IOException;
 
-    public native void createDatagramSocket(FileDescriptor fd) throws SocketException;
-
-    public native void createStreamSocket(FileDescriptor fd) throws SocketException;
+    public native void socket(FileDescriptor fd, boolean stream) throws SocketException;
 
     public native void disconnectDatagram(FileDescriptor fd) throws SocketException;
 
diff --git a/luni/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java b/luni/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
index 868fad8..093551a 100644
--- a/luni/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
+++ b/luni/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
@@ -85,7 +85,7 @@
     protected DatagramChannelImpl(SelectorProvider selectorProvider) throws IOException {
         super(selectorProvider);
         fd = new FileDescriptor();
-        networkSystem.createDatagramSocket(fd);
+        networkSystem.socket(fd, false);
     }
 
     /*
diff --git a/luni/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java b/luni/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
index b67fa2b..529e00a 100644
--- a/luni/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
+++ b/luni/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
@@ -118,7 +118,7 @@
         fd = new FileDescriptor();
         status = SOCKET_STATUS_UNCONNECTED;
         if (connect) {
-            networkSystem.createStreamSocket(fd);
+            networkSystem.socket(fd, true);
         }
     }
 
diff --git a/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp b/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
index 45a1d00..f83ccc1 100644
--- a/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
+++ b/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
@@ -584,39 +584,33 @@
     return true;
 }
 
-static int createSocketFileDescriptor(JNIEnv* env, jobject fileDescriptor, int type) {
+static void osNetworkSystem_socket(JNIEnv* env, jobject, jobject fileDescriptor, jboolean stream) {
     if (fileDescriptor == NULL) {
         jniThrowNullPointerException(env, NULL);
         errno = EBADF;
-        return -1;
+        return;
     }
 
     // Try IPv6 but fall back to IPv4...
+    int type = stream ? SOCK_STREAM : SOCK_DGRAM;
     int fd = socket(AF_INET6, type, 0);
     if (fd == -1 && errno == EAFNOSUPPORT) {
         fd = socket(AF_INET, type, 0);
     }
     if (fd == -1) {
         jniThrowSocketException(env, errno);
+        return;
     } else {
         jniSetFileDescriptorOfFD(env, fileDescriptor, fd);
     }
-    return fd;
-}
 
-static void osNetworkSystem_createStreamSocket(JNIEnv* env, jobject, jobject fileDescriptor) {
-    createSocketFileDescriptor(env, fileDescriptor, SOCK_STREAM);
-}
-
-static void osNetworkSystem_createDatagramSocket(JNIEnv* env, jobject, jobject fileDescriptor) {
-    int fd = createSocketFileDescriptor(env, fileDescriptor, SOCK_DGRAM);
 #ifdef __linux__
     // The RFC (http://www.ietf.org/rfc/rfc3493.txt) says that IPV6_MULTICAST_HOPS defaults to 1.
     // The Linux kernel (at least up to 2.6.32) accidentally defaults to 64 (which would be correct
     // for the *unicast* hop limit). See http://www.spinics.net/lists/netdev/msg129022.html.
-    // When that's fixed, we can remove this code. Until then, we manually set the hop limit on
-    // IPv6 datagram sockets. (IPv4 is already correct.)
-    if (fd != -1 && getSocketAddressFamily(fd) == AF_INET6) {
+    // When that bug is fixed, we can remove this code. Until then, we manually set the hop
+    // limit on IPv6 datagram sockets. (IPv4 is already correct.)
+    if (type == SOCK_DGRAM && getSocketAddressFamily(fd) == AF_INET6) {
         int ttl = 1;
         setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(int));
     }
@@ -1523,8 +1517,6 @@
     { "close",                    "(Ljava/io/FileDescriptor;)V",                                              (void*) osNetworkSystem_close },
     { "connectNonBlocking",       "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)Z",                       (void*) osNetworkSystem_connectNonBlocking },
     { "connect",                  "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;II)V",                      (void*) osNetworkSystem_connect },
-    { "createDatagramSocket",     "(Ljava/io/FileDescriptor;)V",                                              (void*) osNetworkSystem_createDatagramSocket },
-    { "createStreamSocket",       "(Ljava/io/FileDescriptor;)V",                                              (void*) osNetworkSystem_createStreamSocket },
     { "disconnectDatagram",       "(Ljava/io/FileDescriptor;)V",                                              (void*) osNetworkSystem_disconnectDatagram },
     { "getSocketLocalAddress",    "(Ljava/io/FileDescriptor;)Ljava/net/InetAddress;",                         (void*) osNetworkSystem_getSocketLocalAddress },
     { "getSocketLocalPort",       "(Ljava/io/FileDescriptor;)I",                                              (void*) osNetworkSystem_getSocketLocalPort },
@@ -1543,6 +1535,7 @@
     { "setSocketOption",          "(Ljava/io/FileDescriptor;ILjava/lang/Object;)V",                           (void*) osNetworkSystem_setSocketOption },
     { "shutdownInput",            "(Ljava/io/FileDescriptor;)V",                                              (void*) osNetworkSystem_shutdownInput },
     { "shutdownOutput",           "(Ljava/io/FileDescriptor;)V",                                              (void*) osNetworkSystem_shutdownOutput },
+    { "socket",                   "(Ljava/io/FileDescriptor;Z)V",                                             (void*) osNetworkSystem_socket },
     { "write",                    "(Ljava/io/FileDescriptor;[BII)I",                                          (void*) osNetworkSystem_write },
     { "writeDirect",              "(Ljava/io/FileDescriptor;III)I",                                           (void*) osNetworkSystem_writeDirect },
 };