Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one specify a specific DNS server to query using DnsQuery?

DnsQuery doesn't have a parameter to specify the server one wants to query. On the other hand, I've seen this sample, which seems to have passed an IP in the parameter marked as "reserved and must be 0" in the MSDN documentation.

Is there some way to query a specific server (as nslookup does?) Or am I going to have to write my own DNS client (or find a library of course)?

like image 279
Billy ONeal Avatar asked Jan 19 '12 19:01

Billy ONeal


1 Answers

The 4th argument of DnsQuery PVOID pExtra accepts a PIP4_ARRAY containing the specific DNS servers to be queried. This is combined with the 3rd argument DWORD Options of DNS_QUERY_BYPASS_CACHE , in order to bypass the resolver cache.

This has worked since Windows 2000, and in Windows XP DnsQuery calls the function called privateNarrowToWideQuery (in dnsapi.dll) and takes the pExtra argument is as the PIP4_ARRAY.

The MSDN is inaccurate in this regard , resulting in this being an undocumented feature. In older versions of the DnsQuery API Call pExtra used to be called aipServers.

Regarding the DNS of IPV6 AAAA records, you can try the function with the second argument WORD wType as DNS_TYPE_AAAA and the fifth argument PDNS_RECORD *ppQueryResultsSet as a pointer to DNS_AAAA_DATA. Though this still forces you to pass in IPV4 IP address array and not IPV6.

Regarding Windows Version support for the IPV6 queries see the following references

  • Domain Name Service (DNS)
  • Domain Name System Client Behavior in Windows Vista
  • Changes to IPv6 in Windows Vista and Windows Server 2008

In future Windows versions, I believe the correct way to do this for IPV6 will be using DnsQueryEx , with its first argument of PDNS_QUERY_REQUEST pQueryRequest which contains a member PDNS_ADDR_ARRAY pDnsServerList;, which contains member WORD Family; which specifies which type of IP address the DNS server is.

Whether DnsQuery already supports a pointer to PDNS_ADDR_ARRAY as an argument to PVOID pExtra or will be modified in future Windows Versions to do so, I am not sure, but you are welcome to try and see.

See How to use the DnsQuery function to resolve host names and host addresses with Visual C++ .NET for sample code, as you have already discovered.

Also see the following similar SO Question.

like image 175
Appleman1234 Avatar answered Oct 20 '22 20:10

Appleman1234