Last active
December 23, 2015 02:29
-
-
Save scaryghost/6567395 to your computer and use it in GitHub Desktop.
How to do SRV and TXT query with windns.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Compiled on VS2012 | |
*/ | |
#include <winsock2.h> //winsock | |
#include <windns.h> //DNS api's | |
#include <Rpc.h> | |
#include <iostream> | |
#include <functional> | |
#include <map> | |
using namespace std; | |
#pragma comment(lib, "Ws2_32.lib") | |
#pragma comment(lib, "Dnsapi.lib") | |
int main(int argc, char *argv[]) { | |
if (argc < 3) { | |
cerr << "Usage: " << argv[0] << " [SRV|TXT] [service]" << endl; | |
return 1; | |
} | |
PDNS_RECORD pDnsRecord; //Pointer to DNS_RECORD structure. | |
{ | |
WORD type; | |
if (!strcmp(argv[1], "SRV")) { | |
type= DNS_TYPE_SRV; | |
} else if (!strcmp(argv[1], "TXT")) { | |
type= DNS_TYPE_TEXT; | |
} else { | |
cerr << "Invalid argument: '" << argv[1] << "'. Must be [SRV|TXT]" << endl; | |
return 1; | |
} | |
if (DnsQuery_A(argv[2], type, DNS_QUERY_BYPASS_CACHE, NULL, &pDnsRecord, NULL)) { | |
cerr << "Error querying: '" << argv[2] << "'" << endl; | |
return 2; | |
} | |
} | |
PDNS_RECORD it; | |
map<WORD, function<void (void)>> callbacks; | |
callbacks[DNS_TYPE_SRV]= [&it](void) -> void { | |
cout << it->dwTtl << endl; | |
cout << it->Data.SRV.wPriority << endl; | |
cout << it->Data.SRV.wWeight << endl; | |
cout << it->Data.SRV.wPort << endl; | |
cout << RPC_CSTR(it->Data.SRV.pNameTarget) << endl; | |
}; | |
callbacks[DNS_TYPE_TEXT]= [&it](void) -> void { | |
for(DWORD i= 0; i < it->Data.TXT.dwStringCount; i++) { | |
cout << RPC_CSTR(it->Data.TXT.pStringArray[i]) << endl;; | |
} | |
}; | |
for(it= pDnsRecord; it != NULL; it= it->pNext) { | |
if (callbacks.count(it->wType)) { | |
callbacks[it->wType](); | |
} | |
} | |
DnsRecordListFree(pDnsRecord, DnsFreeRecordListDeep); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment