Bug 14415

Summary: AI_V4MAPPED is useless with AI_UNSPEC but they're used together as defaults when hints=NULL
Product: glibc Reporter: Pavel Šimerda <psimerda>
Component: networkAssignee: Pavel Šimerda <psimerda>
Status: WAITING ---    
Severity: minor CC: carlos, neleai, tore
Priority: P2 Flags: fweimer: security-
Version: 2.15   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:
Attachments: patch to remove useless AI_V4MAPPED from default with AF_UNSPEC
getaddrinfo: use zero ai_flags by default

Description Pavel Šimerda 2012-07-28 09:38:50 UTC
From 'man getaddrinfo':

  Specifying hints as NULL is equivalent to  setting  ai_socktype and
  ai_protocol to 0; ai_family to AF_UNSPEC; and ai_flags to
  (AI_V4MAPPED | AI_ADDRCONFIG).

  ...

  If hint.ai_flags specifies the AI_V4MAPPED flag, and hints.ai_family
  was specified as  AF_INET6, and no  matching IPv6 addresses could be
  found, then return IPv4-mapped IPv6 addresses in the list pointed to
  by res. If both AI_V4MAPPED and AI_ALL are specified in hints.ai_flags,
  then return both IPv6 and IPv4-mapped IPv6 addresses in the list pointed
  to by res. AI_ALL is ignored if AI_V4MAPPED is not also specified.

As I read it, AI_V4MAPPED is useless without AF_INET6. So either it is wrong,
or I miss something about AI_V4MAPPED that should be documented in the manpage.
Comment 1 Carlos O'Donell 2012-08-03 04:03:51 UTC
My understanding is that because the default uses AF_UNSPEC that the caller accepts *any* address family thus AI_V4MAPPED is not useless because AF_UNSPEC can be treated as anything including AF_INET6.

Does that clarify why we would set AI_V4MAPPED in the default?
Comment 2 Carlos O'Donell 2012-08-03 04:06:03 UTC
Please note that the man pages are maintained by man-pages project here:
http://www.kernel.org/doc/man-pages/

Would a clarification on the man page about the uses of AF_UNSPEC help?
Comment 3 Pavel Šimerda 2012-08-03 16:12:06 UTC
> My understanding is that because the default uses AF_UNSPEC that the caller
> accepts *any* address family thus AI_V4MAPPED is not useless because AF_UNSPEC
> can be treated as anything including AF_INET6.

This is not the case. My tests say that AF_UNSPEC with AI_V4MAPPED returns
regular IPv4 addresses, regular IPv6 addresses but *no* IPv4-mapped IPv6
addresses. So it actually behaves exactly like AF_UNSPEC without AI_V4MAPPED.

> Does that clarify why we would set AI_V4MAPPED in the default?

Unfortunately not.
Comment 4 Pavel Šimerda 2012-09-22 15:45:24 UTC
Created attachment 6649 [details]
patch to remove useless AI_V4MAPPED from default with AF_UNSPEC
Comment 5 Pavel Šimerda 2013-01-03 14:53:57 UTC
Created attachment 6794 [details]
getaddrinfo: use zero ai_flags by default

    According to POSIX1-2008, AI_V4MAPPED flag shall be ignored unless ai_family
    equals AF_INET6. In the default hints, ai_family is AF_UNSPEC. Therefore
    AI_V4MAPPED is redundant.
    
    AI_ADDRCONFIG can potentially be beneficial to applications that use
    getaddrinfo() results to immediately call connect(), sendto() and
    similar functions to avoid querying redundant DNS records according
    to the current address configuration.
    
    Those applications will also need to specify ai_socktype and/or
    ai_protocol and therefore will not use hints=NULL. As AI_DEFAULT is not
    a part of the public API, such applications will not be able to use the
    default flags at all.
    
    Please note that POSIX1-2008 doesn't define any default flags at all.
    
    Resolves: #14415, #14965
Comment 6 Ondrej Bilka 2013-10-20 20:54:40 UTC
Could you send these patches to libc-alpha? In second one wouldn't be defining AI_DEFAULT as 0 more reasonable?