Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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