diff options
author | Lorenzo Colitti <lorenzo@google.com> | 2015-03-16 10:58:14 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-03-16 10:58:14 +0000 |
commit | 71f075469264b2ae9fbfae18d4601e1675f00956 (patch) | |
tree | 90213d1885f463be7544df0a28ba3c4b1d52fa85 | |
parent | 7d6411b79ecf1a6e2ed0e9a5af2113826e32030f (diff) | |
parent | f5fc4ade3ab082f545e2aafdb1942c1f14e0df5d (diff) | |
download | libcore-71f075469264b2ae9fbfae18d4601e1675f00956.zip libcore-71f075469264b2ae9fbfae18d4601e1675f00956.tar.gz libcore-71f075469264b2ae9fbfae18d4601e1675f00956.tar.bz2 |
Merge "Also support IPv4 fallback in the SocketAddress version of sendto."
-rw-r--r-- | luni/src/main/native/libcore_io_Posix.cpp | 27 | ||||
-rw-r--r-- | luni/src/test/java/libcore/io/OsTest.java | 18 |
2 files changed, 34 insertions, 11 deletions
diff --git a/luni/src/main/native/libcore_io_Posix.cpp b/luni/src/main/native/libcore_io_Posix.cpp index 12ac3f0..88990a5 100644 --- a/luni/src/main/native/libcore_io_Posix.cpp +++ b/luni/src/main/native/libcore_io_Posix.cpp @@ -464,15 +464,21 @@ static bool fillInetSocketAddress(JNIEnv* env, jint rc, jobject javaInetSocketAd return true; } -static bool javaInetSocketAddressToSockaddr( - JNIEnv* env, jobject javaSocketAddress, sockaddr_storage& ss, socklen_t& sa_len) { +static void javaInetSocketAddressToInetAddressAndPort( + JNIEnv* env, jobject javaInetSocketAddress, jobject& javaInetAddress, jint& port) { static jfieldID addressFid = env->GetFieldID( JniConstants::inetSocketAddressClass, "addr", "Ljava/net/InetAddress;"); static jfieldID portFid = env->GetFieldID(JniConstants::inetSocketAddressClass, "port", "I"); - return inetAddressToSockaddr(env, - env->GetObjectField(javaSocketAddress, addressFid), - env->GetIntField(javaSocketAddress, portFid), - ss, sa_len); + javaInetAddress = env->GetObjectField(javaInetSocketAddress, addressFid); + port = env->GetIntField(javaInetSocketAddress, portFid); +} + +static bool javaInetSocketAddressToSockaddr( + JNIEnv* env, jobject javaSocketAddress, sockaddr_storage& ss, socklen_t& sa_len) { + jobject javaInetAddress; + jint port; + javaInetSocketAddressToInetAddressAndPort(env, javaSocketAddress, javaInetAddress, port); + return inetAddressToSockaddr(env, javaInetAddress, port, ss, sa_len); } static bool javaNetlinkSocketAddressToSockaddr( @@ -1459,6 +1465,15 @@ static jint Posix_sendtoBytes(JNIEnv* env, jobject, jobject javaFd, jobject java } static jint Posix_sendtoBytesSocketAddress(JNIEnv* env, jobject, jobject javaFd, jobject javaBytes, jint byteOffset, jint byteCount, jint flags, jobject javaSocketAddress) { + if (env->IsInstanceOf(javaSocketAddress, JniConstants::inetSocketAddressClass)) { + // Use the InetAddress version so we get the benefit of NET_IPV4_FALLBACK. + jobject javaInetAddress; + jint port; + javaInetSocketAddressToInetAddressAndPort(env, javaSocketAddress, javaInetAddress, port); + return Posix_sendtoBytes(env, NULL, javaFd, javaBytes, byteOffset, byteCount, flags, + javaInetAddress, port); + } + ScopedBytesRO bytes(env, javaBytes); if (bytes.get() == NULL) { return -1; diff --git a/luni/src/test/java/libcore/io/OsTest.java b/luni/src/test/java/libcore/io/OsTest.java index 3ebbfcd..dce08f0 100644 --- a/luni/src/test/java/libcore/io/OsTest.java +++ b/luni/src/test/java/libcore/io/OsTest.java @@ -321,14 +321,14 @@ public class OsTest extends TestCase { checkByteBufferPositions_sendto_recvfrom(AF_INET6, InetAddress.getByName("::1")); } - public void test_sendtoSocketAddress() throws Exception { - FileDescriptor recvFd = Libcore.os.socket(AF_INET6, SOCK_DGRAM, 0); - Libcore.os.bind(recvFd, InetAddress.getLoopbackAddress(), 0); + private void checkSendToSocketAddress(int family, InetAddress loopback) throws Exception { + FileDescriptor recvFd = Libcore.os.socket(family, SOCK_DGRAM, 0); + Libcore.os.bind(recvFd, loopback, 0); StructTimeval tv = StructTimeval.fromMillis(20); Libcore.os.setsockoptTimeval(recvFd, SOL_SOCKET, SO_RCVTIMEO, tv); InetSocketAddress to = ((InetSocketAddress) Libcore.os.getsockname(recvFd)); - FileDescriptor sendFd = Libcore.os.socket(AF_INET6, SOCK_DGRAM, 0); + FileDescriptor sendFd = Libcore.os.socket(family, SOCK_DGRAM, 0); byte[] msg = ("Hello, I'm going to a socket address: " + to.toString()).getBytes("UTF-8"); int len = msg.length; @@ -336,7 +336,15 @@ public class OsTest extends TestCase { byte[] received = new byte[msg.length + 42]; InetSocketAddress from = new InetSocketAddress(); assertEquals(len, Libcore.os.recvfrom(recvFd, received, 0, received.length, 0, from)); - assertEquals(InetAddress.getLoopbackAddress(), from.getAddress()); + assertEquals(loopback, from.getAddress()); + } + + public void test_sendtoSocketAddress_af_inet() throws Exception { + checkSendToSocketAddress(AF_INET, InetAddress.getByName("127.0.0.1")); + } + + public void test_sendtoSocketAddress_af_inet6() throws Exception { + checkSendToSocketAddress(AF_INET6, InetAddress.getByName("::1")); } public void test_socketFamilies() throws Exception { |