Winsock - getaddrinfo does not return ipv6 adress

Andreyka_ 136 Reputation points
2021-04-28T09:12:48.76+00:00

Hello,

Please help with the question: I am making a request to getadresinfo:

my_ADDRINFOA = {0};                  
my_ADDRINFOA.ai_family = AF_UNSPEC;         
my_ADDRINFOA.ai_socktype = SOCK_STREAM;    
my_ADDRINFOA.ai_protocol = IPPROTO_TCP;


ADDRINFOA* my_servinfo;  

INT my_getaddrinfo = getaddrinfo("facebook.com", NULL, &my_ADDRINFOA, &my_servinfo);

However, DNS only returns ipv4 addresses! Although Facebook definitely has ipv6 adress!
And yes, I looked through this linked list of the structure my_servinfo, there is no AFINET6, only AF_INET.

What could it be ? Function getaddrinfo implementation error in Windows?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,429 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,544 questions
Windows 10 Network
Windows 10 Network
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Network: A group of devices that communicate either wirelessly or via a physical connection.
2,276 questions
{count} votes

Accepted answer
  1. Andreyka_ 136 Reputation points
    2021-05-03T10:32:14.32+00:00

    In general, I can to call getaddrinfo with the following parameters:

    my_ADDRINFOA = {0};
    
    my_ADDRINFOA.ai_family = AF_UNSPEC;      
    my_ADDRINFOA.ai_socktype = SOCK_STREAM;     
    my_ADDRINFOA.ai_protocol = IPPROTO_TCP;
    
    my_ADDRINFOA.ai_flags = AI_V4MAPPED | AI_ALL;
    

    In this case, both IPv4 and IPv6 are returned.

    Although rfc3493 says the following about the AI_V4MAPPED and AI_ALL flags:

    If the AI_V4MAPPED flag is specified along with an ai_family ofAF_INET6, then getaddrinfo() shall return IPv4-mapped IPv6 addresses on finding no matching IPv6 addresses

    In this case, in this case, both the mapping from IPv4 to IPv6 and real IPv6 are returned, although the RFC seems to clearly say that the mapping from IPv4 to IPv6 should be returned only if there are no suitable IPv6 addresses, but they are.

    If the AI_ALL flag is used with the AI_V4MAPPED flag, then getaddrinfo() shall return all matching IPv6 and IPv4 addresses.
    For example, when using the DNS, queries are made for both AAAArecords and A records, and getaddrinfo() returns the combinedresults of both queries. Any IPv4 addresses found are returned as IPv4-mapped IPv6 addresses.

    In this case, I have IPv4 - they are returned exactly as 32-bit regular IPv4.
    And IPv6 is like 128 bit.

    The AI_ALL flag without the AI_V4MAPPED flag is ignored. When ai_family is not specified (AF_UNSPEC), AI_V4MAPPED and AI_ALL flags will only be used if AF_INET6 is supported.

    In general, on the one hand, it worked, on the other, if I understood everything correctly, then not quite clearly following the RFC.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Song Zhu - MSFT 906 Reputation points
    2021-04-29T03:12:29.593+00:00

    This is not an implementation error of getaddrinfo.

    In fact, even if you set AF_UNSPEC, there is no guarantee that getaddrinfo will return two detailed information to you at the same time. It returns IP address type that is associated with given host name.

    So if you need the ipv6 address, please set ai_family to AF_INET6, this will work for you.

    More references: getaddrinfo not returning ipv6 details, GetAddrInfo cannot resolve ipv6.google.com


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Andreyka_ 136 Reputation points
    2021-04-29T05:58:07.54+00:00

    SongZhu-MSFT,

    rfc3493:

    If the ai_family field to which hints points has the value AF_UNSPEC,
    addresses shall be returned for use with any address family that can
    be used with the specified nodename and/or servname.

    only AF_INET6 - return error:

    WSANO_DATA
    11004
    Valid name, no data record of requested type.
    The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for. The usual example for this is a host name-to-address translation attempt (using gethostbyname or WSAAsyncGetHostByName) which uses the DNS (Domain Name Server). An MX record is returned but no A record—indicating the host itself exists, but is not directly reachable.


  3. Cheong00 3,471 Reputation points
    2021-05-03T01:25:20.377+00:00

    I checked and seems there is a relevant bug for Win2016. Will you try and see if the PowerShell command at the end of thread can fix your problem?

    0 comments No comments