From 72ed50d1e9048b79efdb5b9e4ffb07c99a302d84 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Fri, 16 Jan 2015 15:12:40 +0900 Subject: Use the IPv6 instead of the IPv4 wildcard address in sockets. A few places in libcore consider the local address of an unbound socket to be Inet4Address.ANY. For example, calling getLocalAddress on an uncreated Socket will return 0.0.0.0, and when creating a DatagramSocket, libcore attempts to bind it to 0.0.0.0 unless the caller specifies another address. On Android, this is incorrect. All native socket filedescriptors that underpin Java socket objects are created by IoBridge.socket, and are dual-stack AF_INET6 sockets, not AF_INET sockets. When such a socket is created, its local address is ::, not 0.0.0.0. Thus, for example, calling getLocalAddress on a just-created ServerSocket object will return ::. Binding to 0.0.0.0 is not even allowed by os.bind, which silently converts it to :: instead (in inetAddresstoSockaddr). Therefore, accept reality and use :: instead of 0.0.0.0 in the Java layer as well. Specifically: 1. Change DatagramSocket's constructors to bind to :: instead of 0.0.0.0. This is a complete no-op, because os.bind() silently converts 0.0.0.0 into ::. Add a test for the other of the two codepaths. 2. Change InetSocketAddress so that an uninitialized object has an IP address of :: and not 0.0.0.0, and update its test. This is unlikely to break anything short of an app that explicitly depends on this behaviour, because os.bind() converts 0.0.0.0 to ::, and because any SocketAddress returned by any real socket will never contain 0.0.0.0 anyway. 3. Change Socket so that calling getLocalAddress() when there is no underlying socket file descriptor is will return :: instead of 0.0.0.0. This is more correct, because it's consistent with sockets that have been created, which will never have a local address of 0.0.0.0. Tested: vogar $(find libcore/*Socket*Test*) all passes on device. Bug: 18094870 Change-Id: I9d60710fe945a99d6a5e65430248a889008ef4b1 --- .../apache/harmony/tests/java/net/DatagramSocketTest.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'harmony-tests/src/test') diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java index e585b14..d9f3d91 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java @@ -745,12 +745,13 @@ public class DatagramSocketTest extends junit.framework.TestCase { public void test_getLocalSocketAddress_ANY() throws Exception { DatagramSocket s = new DatagramSocket(0); - try { - assertTrue("ANY address not IPv6: " + s.getLocalSocketAddress(), - ((InetSocketAddress) s.getLocalSocketAddress()).getAddress() instanceof Inet6Address); - } finally { - s.close(); - } + assertEquals("ANY address not IPv6: " + s.getLocalSocketAddress(), + Inet6Address.ANY, s.getLocalAddress()); + s.close(); + s = new DatagramSocket(0, null); + assertEquals(Inet6Address.ANY, s.getLocalAddress()); + assertFalse(0 == s.getLocalPort()); + s.close(); } public void test_setReuseAddressZ() throws Exception { -- cgit v1.1