GetAddrInfo无法解析ipv6.google.com(但nslookup可以)

时间:2012-05-26 14:33:18

标签: winapi sockets dns ipv6

我可以尝试使用GetAddrInfo来解析ipv6.google.com

wsaError = getaddrinfo("ipv6.google.com", null, null, ref addrInfo);

返回的套接字错误代码为11001(不知道这样的主机)。

  

注意:不推荐使用的旧版功能GetHostByName不支持IPv6。它已被GetAddrInfo取代。

奇怪的是,我可以使用nslookup,它可以很好地找到地址:

问题

SendRequest(), len 33
    HEADER:
        opcode = QUERY, id = 4, rcode = NOERROR
        header flags:  query, want recursion
        questions = 1,  answers = 0,  authority records = 0,  additional = 0

    QUESTIONS:
        ipv6.google.com, type = A, class = IN

权威答案

Got answer (106 bytes):
    HEADER:
        opcode = QUERY, id = 4, rcode = NOERROR
        header flags:  response, want recursion, recursion avail.
        questions = 1,  answers = 1,  authority records = 1,  additional = 0

    QUESTIONS:
        ipv6.google.com, type = A, class = IN
    ANSWERS:
    ->  ipv6.google.com
        type = CNAME, class = IN, dlen = 9
        canonical name = ipv6.l.google.com
        ttl = 21743 (6 hours 2 mins 23 secs)
    AUTHORITY RECORDS:
    ->  l.google.com
        type = SOA, class = IN, dlen = 38
        ttl = 30 (30 secs)
        primary name server = ns4.google.com
        responsible mail addr = dns-admin.google.com
        serial  = 1486713
        refresh = 900 (15 mins)
        retry   = 900 (15 mins)
        expire  = 1800 (30 mins)
        default TTL = 60 (1 min)

非权威性问题

SendRequest(), len 33
    HEADER:
        opcode = QUERY, id = 5, rcode = NOERROR
        header flags:  query, want recursion
        questions = 1,  answers = 0,  authority records = 0,  additional = 0

    QUESTIONS:
        ipv6.google.com, type = AAAA, class = IN

非权威性回答

Got answer (82 bytes):
    HEADER:
        opcode = QUERY, id = 5, rcode = NOERROR
        header flags:  response, want recursion, recursion avail.
        questions = 1,  answers = 2,  authority records = 0,  additional = 0

    QUESTIONS:
        ipv6.google.com, type = AAAA, class = IN
    ANSWERS:
    ->  ipv6.google.com
        type = CNAME, class = IN, dlen = 9
        canonical name = ipv6.l.google.com
        ttl = 21743 (6 hours 2 mins 23 secs)
    ->  ipv6.l.google.com
        type = AAAA, class = IN, dlen = 16
        AAAA IPv6 address = 2607:f8b0:4009:801::1012
        ttl = 270 (4 mins 30 secs)

------------
Name:    ipv6.l.google.com
Address:  2607:f8b0:4009:801::1012
Aliases:  ipv6.google.com

GetAddrInfo无法解决时,nslookup能够解决地址的原因是什么?我可以用GetAddrInfo做些什么,所以它有效吗?

2 个答案:

答案 0 :(得分:2)

尝试在AF_INET6参数中传递pHints以使用IPV6地址。这似乎对我有用:

struct addrinfo *result = NULL;
struct addrinfo hints;

ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;

dwRetval = getaddrinfo("ipv6.google.com", NULL, &hints, &result);
// check your dwRetval here ...

答案 1 :(得分:1)

我有类似的问题,代码不是问题,而是操作系统。以下命令意味着Windows正确解析了IPv6地址,而不是给出WSANO_DATA错误:

Netsh interface teredo set state enterpriseclient

来源:

http://netscantools.blogspot.co.uk/2011/06/ipv6-teredo-problems-and-solutions-on.html