c++ - DnsQuery 无法获取某些特定 FQDN 上的有效地址

标签 c++ windows winapi dns

当我使用这段代码时 此处引用:http://support.microsoft.com/kb/831226

我可以编译成功,但是当我用它做一些 dns 查询时,返回地址很奇怪,例如:176.20.31.0(这不应该是一个有效地址)

这是我的输出:

C:\dnsq\Debug>dnsq.exe -n tw.media.blizzard.com -t A -s 8.8.8.8
The IP address of the host tw.media.blizzard.com is 176.20.31.0

但实际上tw.media.blizzard.com应该是:(我是通过nslookup查询的)

# nslookup tw.media.blizzard.com 8.8.8.8
Server:         8.8.8.8
Address:        8.8.8.8#53

Non-authoritative answer:
tw.media.blizzard.com   canonical name = tw.media.blizzard.com.edgesuite.net.
tw.media.blizzard.com.edgesuite.net     canonical name = a1479.g.akamai.net.
Name:   a1479.g.akamai.net
Address: 23.14.93.167
Name:   a1479.g.akamai.net
Address: 23.14.93.157

我的问题是为什么 dnsquery 在某些 FQDN 上不起作用? 任何建议将不胜感激:)

最佳答案

我发现了问题。

对于那些导致无效地址的FQDN,常见的是他们的DNS记录类型都是“DNS_TYPE_CNAME”,而不是DNS_TYPE_A。

所以我们需要解析整个 PDNS_RECORD 来获取 DNS_TYPE_A 信息。


我会在这里发布我的更改:

来自 MS 的原始代码:

    if(wType == DNS_TYPE_A) {
        //convert the Internet network address into a string
        //in Internet standard dotted format.
        ipaddr.S_un.S_addr = (pDnsRecord->Data.A.IpAddress);
        printf("The IP address of the host %s is %s \n", pOwnerName,inet_ntoa(ipaddr));

        // Free memory allocated for DNS records. 
        DnsRecordListFree(pDnsRecord, freetype);
    }

我的改变:

    if(wType == DNS_TYPE_A) {
        //convert the Internet network address into a string
        //in Internet standard dotted format.
        PDNS_RECORD cursor;

        for (cursor = pDnsRecord; cursor != NULL; cursor = cursor->pNext) {
            if (cursor->wType == DNS_TYPE_A) {
                ipaddr.S_un.S_addr = (cursor->Data.A.IpAddress);
                printf("The IP address of the host %s is %s \n", pOwnerName,inet_ntoa(ipaddr));                 
            }
        }

        // Free memory allocated for DNS records. 
        DnsRecordListFree(pDnsRecord, freetype);
    }       

关于c++ - DnsQuery 无法获取某些特定 FQDN 上的有效地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13837439/

相关文章:

c++ - 回调函数和常规函数有什么区别?

c++ - 如何从 C++ 中的资源文件加载游标组?

c++ - 除了 SetCursor 之外还有什么可以重置光标形状?

c++ - 从 ListView 控件丢失 WM_NOTIFY

c++ - 在 C++ 中使用文件时出现段错误

c++ - nvcc 和 BOOST 库的编译错误

windows - 适用于 Windows 64 位的 osm2pgsql 最新版本在哪里

C++:表达式必须是可修改的左值

windows - 在 QT5 (Windows) 中从 QPixmap 获取 HBITMAP

php - 使路径在 linux 和 Windows 上都有效