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
6 files changed