IPv6: Is `::' equivalent to `0.0.0.0' when listening for connections?

Xharlie picture Xharlie · Dec 15, 2014 · Viewed 25.7k times · Source

When I examine the output of IPGlobalProperties.GetActiveTcpListeners(), I see listeners on 0.0.0.0 as well as ::.

I believe that listening on a port on 0.0.0.0 is equivalent to listening on a port on any network adapter, at least my memory of the Windows socket API says that this is so.

It also makes sense to me that :: would mean the equivalent in IPv6 parlance so a listener on [::]:49156 would be listening to port 49156 on all IPv6 network adapters where as [::1]:1434 would be port 1434 on only the IPv6 loopback adapter.

Is this correct?

I assume that IPv6 listen end-points only apply to IPv6 adapters. That is, if an adapter only had an IPv4 address, connections to it port 49156 would not be received by a listener on [::]:49156?

Also, has anyone noticed that the MSDN article for GetActiveTcpListeners() incorrectly declares that the returned objects "include listeners in all TCP states except the Listen state."?

Answer

Remy Lebeau picture Remy Lebeau · Jun 28, 2016

I believe that listening on a port on 0.0.0.0 is equivalent to listening on a port on any network adapter, at least my memory of the Windows socket API says that this is so.

That is correct. 0.0.0.0 is defined as INADDR_ANY and can be used to listen on all local IPv4 adapters.

It also makes sense to me that :: would mean the equivalent in IPv6 parlance so a listener on :::49156 would be listening to port 49156 on all IPv6 network adapters where as ::1:1434 would be port 1434 on only the IPv6 loopback adapter.

From the perspective of listening, yes. :: is defined as INADDR6_ANY and can be used to listen on all local IPv6 adapters. ::1 is defined as INADDR6_LOOPBACK.

I assume that IPv6 listen end-points only apply to IPv6 adapters. That is, if an adapter only had an IPv4 address, connections to it port 49156 would not be received by a listener on :::49156?

That depends on the listener. An IPv6-only listener cannot listen on an IPv4 adapter and cannot accept IPv4 clients. However, a dual-stack listener bound to INADDR6_ANY can bind to IPv4 and IPv6 adapters and accept both IPv4 and IPv6 clients, where IPv4 addresses are reported by accept(), WSAAccept(), and getpeername() as IPv4-mapped IPv6 addresses.