Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my service always bind to ipv6 localhost instead of ipv4?

I have a service that creates a ServerSocket and binds to localhost:7060. When I did "netstat -an" on my android device, I see it is using ipV6 localhost instead of ipv4 localhost interface.

The output is like this:
tcp6 0 0 ::ffff:127.0.0.1:7060 :::* LISTEN

The ports that use ipV4 are listed like this:
tcp 0 0 127.0.0.1:5060 0.0.0.0:* LISTEN

What is the trick to force it to use IPv4 always? I am setting up a port forward rule using iptables. The version I have supports ipv4 destination addresses.

This is how I am creating my Java code for listening on the port.

InetAddress localAddress = Inet4Address.getByName("localhost"); //InetAddress localAddress = Inet4Address.getLocalHost(); sockServer = new ServerSocket(nPort, 20, localAddress);

I followed other advice like setting system property to prefer ipV4 in the startup of my service. That didn't make any difference.

System.setProperty("java.net.preferIPv4Stack", "true");

I am running this on Android 2.3 built for an embedded device.

Update: I checked InetAddress.java sources in android tree. It is reading the above flag with a line like below.

static boolean preferIPv6Addresses() {
        String propertyName = "java.net.preferIPv6Addresses";
        String propertyValue = AccessController.doPrivileged(new PriviAction<String>(propertyName));
        return Boolean.parseBoolean(propertyValue);
    }

Now I am not sure System.setProperty() call is really changing the value read by above code.

like image 353
videoguy Avatar asked Nov 29 '11 23:11

videoguy


People also ask

Is localhost IPv4 or IPv6?

The loopback address, also called localhost, is probably familiar to you. It is an internal address that routes back to the local system. The loopback address in IPv4 is 127.0. 01.

Does localhost work with IPv6?

The name localhost normally resolves to the IPv4 loopback address 127.0. 0.1, and to the IPv6 loopback address ::1.

What is the equivalent of 0.0 0.0 in IPv6?

The IPv6 equivalent of IPv4's 0.0. 0.0 is ::/0 . Save this answer.

What is the IPv6 loopback address of the local host?

For IPv4, this class A network address is 127.0. 0.1 . For IPv6, the reserved loopback address is ::1 . You can also specify loopback as the host name.


1 Answers

In theory a IPv6 server listens to IPv4 as well, since IPv4 address space is a subset of IPv6, is this causing real problems to you?

A trick that might work is to use "127.0.0.1" instead of "localhost", which has IPv4 and IPv6 addresses associated.

like image 86
epx Avatar answered Oct 11 '22 17:10

epx