The getaddrinfo() function not only allows client programs to efficiently find the correct data to create a socket for a given host, but also allows servers to bind to the correct socket - theoretically.
I just found out about it and started playing with it through Python:
from socket import * for i in getaddrinfo(None, 22, AF_UNSPEC, SOCK_STREAM, IPPROTO_IP, AI_PASSIVE): i
gives
(2, 1, 6, '', ('0.0.0.0', 22)) (10, 1, 6, '', ('::', 22, 0, 0))
which makes me think that something is wrong.
What exactly should I do with these answers? Should I
- do
listen() ing the socket of all these answers, or should I - just pick the first one that really works?
The example in the manpage suggests that I only take the first one and be satisfied if it is error free, but then I only get the connection through IPv4 in my example.
But if I try all of them, I have to worry with 2 server sockets, which is not necessary because IPv6 server sockets also listen on IPv4 if certain conditions are met (OS, socket flags, etc.) .
Where am I thinking wrong?
EDIT: Obviously, I'm not mistaken, but my computer is doing the wrong thing. I use the default /etc/gai.conf that ships with OpenSUSE. It would be nice if someone could point me in the right direction.
EDIT 2: In this case, strace issues internal calls after reading /etc/gai.conf (now with port 54321, since I thought using port 22 might have some bad effect, which was not)
socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 3 connect(3, {sa_family=AF_INET6, sin6_port=htons(54321), inet_pton(AF_INET6, "::", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = 0 getsockname(3, {sa_family=AF_INET6, sin6_port=htons(38289), inet_pton(AF_INET6, "::1", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [28]) = 0 connect(3, {sa_family=AF_UNSPEC, sa_data="\0\0\0\0\0\0\0\0\0\0\0\0\0\0"}, 16) = 0 connect(3, {sa_family=AF_INET, sin_port=htons(54321), sin_addr=inet_addr("0.0.0.0")}, 16) = 0 getsockname(3, {sa_family=AF_INET6, sin6_port=htons(60866), inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [28]) = 0 close(3) = 0
Obviously, the solution should be in accordance with the results of getsockname() ...
BTW: https://bugs.launchpad.net/ubuntu/+source/eglibc/+bug/673708 , and the other error messages mentioned there confirm my observations. Some people claim that the new behavior is correct, so I obviously stick to using AF_INET6 ... AF_INET6