It will be seem a weird question to some. but I've searched and didn't find any answer.
When I want a dual stack server, I need to listen on INADDR_ANY
for IPv4 and to in6addr_any
for IPv6.
If I have more than one network card then I need to choose if I want to listen to all, or to specify which card to listen.
For this exact propose I'm using getaddrinfo
method with configurable host_name. If the host_name
had not configured, then I call getaddrinfo
with NULL, and get the two "ANY" addresses. If I configured it with an IP (v6 or v4) I get only one address, which is also fine.
But when I'm using my hostname
as the configured host_name, on a Windows machine I'm getting from getaddrinfo
3 address: one IPv4 address, and two IPv6 address. the first is seen by ipconfig as "Link-local IPv6 Address" the second is seen as "IPv6 Address" under the section "Tunnel adapter 6TO4 Adapter:".
The addresses ordered like this:
- IPv6 Link Local
- IPv6 Address
- IPv4
So, if I'm listening to all the addresses the dual stack is actually triple stack. If I take the first IPv6 address, (as it was the convention in IPv4 server with configured host_name) I'm listening only on the "Link-local IPv6 Address" which is less accessible than the "IPv6 Address" and many client can't connect to it, while they can connect to the IPv4 address.
Now I'm trying to complicate it further. I'm connected my cellphone to the USB and activate the USB Tethering. when I resolve addresses by getaddrinfo
I'm getting 5 addresses:
by this order:
- USB IPv6 Link Local
- Ethernet IPv6 Link Local
- IPv6 Address
- USB IPv4
- Ethernet IPv4
So my questions are:
If it was IPv4 only I would say I take only the first IPv4. and don't care about the rest. but when using IPv6, it look like the last IPv6 is the most appropriate. is there any convention for it?
If I have multi-network machine I need to choose the first network, and listen on both IPv4 and IPv6, but here the results are mixed. again, is there any convention?
Do I need to listen to all IPv6 addresses? in that case I will listen to an IPv6 address which I don't listen to the corresponding IPv4. and I hope to avoid from it.
Thanks for any help or comment. But please don't advice to listen only on "ANY" since I can't.
Old post, i know, how did you resolve it finally? I am really interested to know.
For this I would recommend the option you avoid, bind to ANY, wildcard
"::"
,bind(.., "::", ..)
and use some firewall or pack filter rules to rule out the connections you don't want to.