socket.getaddrinfo: flags |= AI_ADDRCONFIG

Karl Chen quarl at cs.berkeley.edu
Wed Nov 5 07:16:45 EST 2008


I've discovered that since glibc 2.3.2, getaddrinfo(3) supports a
useful flag called AI_ADDRCONFIG.  It turns off AAAA lookups if
the machine isn't configured for IPv6 (and similarly for IPv4,
theoretically).  This is especially important when behind gateways
whose DNS forwarder silently filter AAAA requests.  Without
AI_ADDRCONFIG, every DNS request has to wait for 4 AAAA request
timeouts before it even attempts an A request.

Passing hints=NULL to getaddrinfo (the C function) means
hints->flags = AI_V4MAPPED | AI_ADDRCONFIG.  I.e., not the same as
hints->hints->flags = 0.  (Don't ask me why the default isn't 0, I
don't like this API either.  It means if you wish to specify only
some hint parameters you have to carefully get the flags right.)

Python's socketmodule.c, especially socket_getaddrinfo, uses 0 as
the default flags.  So AI_ADDRCONFIG is turned off by default.

I propose:
1) mimic glibc default behavior, i.e. if flags is unspecified or
   None, treat it as the default value of AI_V4MAPPED |
   AI_ADDRCONFIG).

Alternatively:
2) add these flags to callers of socket.getaddrinfo in various
   standard libs, e.g. httplib.


(AI_V4MAPPED isn't as important since it only applies if you
explicitly choose AF_INET6, a conscious decision.)

Karl



More information about the Python-list mailing list