Note that there are some explanatory texts on larger screens.

plurals
  1. POUsage of getaddrinfo() with AI_PASSIVE
    primarykey
    data
    text
    <p>The <code>getaddrinfo()</code> function not only allows for client programs to efficiently find the correct data for creating a socket to a given host, it also allows for servers to bind to the correct socket - in theory.</p> <p>I just <a href="https://stackoverflow.com/questions/8111044/c-sockets-recv-syscall-returning-1/8111252#comment9950077_8111252">learned about that</a> and started to play around with it via Python:</p> <pre><code>from socket import * for i in getaddrinfo(None, 22, AF_UNSPEC, SOCK_STREAM, IPPROTO_IP, AI_PASSIVE): i </code></pre> <p>yields</p> <pre><code>(2, 1, 6, '', ('0.0.0.0', 22)) (10, 1, 6, '', ('::', 22, 0, 0)) </code></pre> <p>what makes me wonder about if there is something wrong.</p> <p>What exactly am I supposed to do with these answers? Should I</p> <ul> <li>make a <code>listen()</code>ing socket of all of these answers, or should I</li> <li>just pick the first one which really works? </li> </ul> <p>The example in the <a href="http://linux.die.net/man/3/getaddrinfo" rel="nofollow noreferrer">manpage</a> suggests me to only take the first one and be happy with it if it is error-free, but then I only get a connection via IPv4 n my example.</p> <p>But if I try all of them, I have to worry with 2 server sockets, which is unnecessary due to the fact that IPv6 server sockets also listen to IPv4 if certain conditions are met (OS, socket flags etc.).</p> <p>Where am I thinking wrong?</p> <hr> <p>EDIT: Obviously, I'm not thinking wrong, but my PC does the wrong thing. I use the default <code>/etc/gai.conf</code> shipped with OpenSUSE. It would be nice if anyone could point me towards the right direction.</p> <p>EDIT 2: In the given case, <code>strace</code> gives the following calls made internally after reading <code>/etc/gai.conf</code> (now with port 54321, as I thought that using port 22 might have some bad influence, which was not the case):</p> <pre><code>socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 3 connect(3, {sa_family=AF_INET6, sin6_port=htons(54321), inet_pton(AF_INET6, "::", &amp;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", &amp;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", &amp;sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [28]) = 0 close(3) = 0 </code></pre> <p>Obviously, the decision is intended to take place according to the results of the <code>getsockname()</code> calls...</p> <p>BTW: <a href="https://bugs.launchpad.net/ubuntu/+source/eglibc/+bug/673708" rel="nofollow noreferrer">https://bugs.launchpad.net/ubuntu/+source/eglibc/+bug/673708</a> and the other bug reports mentionned there confirm my observations. Several people there claim that the new behaviour is correct, so I'm obviously stuck to using <code>AF_INET6</code>... :-(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload