0

Php curl uses getaddrinfo() to resolve hostnames to IP addresses. By setting an option it is easy to make it lookup IPV4 addresses but that will take quite some doing and will be quite difficult to undo when we get IPv6 up and running properly on our networks.

Is there any way to set getaddrinfo() to default to IPv4 lookups for our entire Ubuntu server?

Thanks Wayne

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
Wayne
  • 149
  • 3
  • 14
  • Are you interested in a solution in C or php? – askmish Dec 11 '12 at 09:51
  • Neither. I'd like to set a default setting somewhere like in sys or dev that defaults the lookups to IPv4. – Wayne Dec 11 '12 at 10:20
  • There doesn't seem to be such a setting. Some info here: http://askubuntu.com/questions/32298/prefer-a-ipv4-dns-lookups-before-aaaaipv6-lookups though. – nos Dec 11 '12 at 13:22

2 Answers2

3

This solution is for C, not sure about php. When calling getaddrinfo, your pass a struct addrinfo providing 'hints' as to what you want to lookup. Example below

  struct addrinfo *res;
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_INET;
  hints.ai_flags = AI_PASSIVE;
  hints.ai_socktype = SOCK_STREAM;

  getaddrinfo("localhost", "80", &hints, &res);

Copied below from my comment as it seemed applicable to the answer:

Just create a wrapper function for getaddrinfo that provides your 'defaults'. Then use the wrapper function throughout your application. When ipv6 is up and running change the function's internal 'default'.

goji
  • 6,911
  • 3
  • 42
  • 59
  • Thanks, I'm looking for a global solution though. Like a default setting. – Wayne Dec 11 '12 at 10:51
  • 2
    Just create a wrapper function for getaddrinfo that provides your 'defaults'. Then use the wrapper function throughout your application. When ipv6 is up and running change the function's internal 'default'. – goji Dec 11 '12 at 10:55
1

On Linux, the behaviour of getaddrinfo() can be tweaked with /etc/gai.conf. You cannot use this file to prevent IPv6 addresses being returned, but you can use it to prioritise IPv4 addresses over IPv6 addresses.

Really though, you just want PHP/Curl to pass the AI_ADDRCONFIG flag to getaddrinfo(), which will cause it to return IPv6 addresses only if the machine currently has an IPv6 address configured.

caf
  • 233,326
  • 40
  • 323
  • 462