order of IPv4 & IPv6 addresses returned by getaddrinfo() when AI_PASSIVE is set

Godmar Back godmar@gmail.com
Thu Dec 8 02:39:00 GMT 2011


on Ulrich Drepper's site on protocol-independent socket programming
[1], he claims the RFC 3483 mandates that IPv6 addresses be returned
before IPv4 addresses when getaddrinfo is invoked with AI_PASSIVE and
a port number to obtain local addresses to bind a listening socket to.
He provides code that relies on this behavior.

We have observed shifting behavior in different GNU libc versions.
None of the systems had any setting in /etc/gai.conf that could have
affected this.

On a CentOS 5.3 system with the oldest libc version (2.5), IPv6 is
returned before IPv4. His code works.
On a CentOS 6.0 system with libc 2.11, IPv4 is returned before IPv6.
Same on libc 2.12 on Ubuntu 10.04 LTS.

I briefly looked at the fully uncommented code of getaddrinfo() in
sysdeps/posix/getaddrinfo.c, and couldn't make out how this preference
is implemented (rfc3484_sort?)

The behavior is, of course, important for applications that wish to
take advantage of the dual-bind feature in Linux to simplify accepting
both IPv4 and IPv6 clients. Instead of simply binding to the first
port, they'll have to now scan the list twice, or specify an address
family in the hints passed to getaddrinfo.

Does anybody have any insight into what's going on here?


 - Godmar

[1] http://www.akkadia.org/drepper/userapi-ipv6.html

More information about the Libc-help mailing list