The man page says:

The getaddrinfo(3) function is not limited to creating IPv4 socket address structures; IPv6 socket address structures can be created if IPv6 support is available. These socket address structures can be used directly by bind(2) or connect(2), to prepare a client or a server socket.

What should I do to force getaddrinfo to not create IPv6 socket address structures?

  • Is this for a program you're writing? Or are you trying to get an application that calls getaddrinfo with AF_UNSPEC not to use IPv6? – Gilles Aug 16 '12 at 22:58

According to the manpage for getaddrinfo(), you can pass the address family in the hints parameter, so something like

struct addrinfo hints, *result;
int s;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;        /* or AF_INET6 for ipv6 addresses */
s = getaddrinfo(NULL, "ftp", &hints, &result);

I haven't tried this, but the approach seems to be right.


Interestingly enough, the first question I ever asked on this site turned out to have an answer you might find useful.

To summarize, the file /etc/gai.conf is used by the getaddrinfo() system call to determine how to respond. For your particular case, adding

precedence ::ffff:0:0/96  100

to the end of the config file should be sufficient.

  • Thanks for your answer, but in my CentOS 5.5 final, there is no /etc/gai.conf, do I need to create one? – Derui Si Aug 16 '12 at 9:55
  • Possibly. According to dpkg the file is part of the libc-bin package, which is pretty deep down the essential packages list in Debian... – Shadur Aug 16 '12 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.