-
Notifications
You must be signed in to change notification settings - Fork 2
/
dnsquery.cpp
42 lines (35 loc) · 991 Bytes
/
dnsquery.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* Copyright (C) 2014 Stefan Helmert <stefan.helmert@gmx.net>
smartRNS do the DNS query
*/
#include "dnsquery.h"
#include <iostream>
// DNS query function; returns TXT records as a vector of strings; accepts domain as string
vector<string> getTXTrecs(string domain, uint32_t maxTXTs)
{
u_char nsbuf[NSBUFSIZE];
ns_msg msg;
ns_rr rr;
size_t len;
uint32_t l;
uint32_t i;
string TXT;
vector<string> TXTs;
res_init();
l = res_query(domain.c_str(), ns_c_in, ns_t_txt, nsbuf, sizeof(nsbuf));
ns_initparse(nsbuf, l, &msg);
l = ns_msg_count(msg, ns_s_an);
for (i = 0; (i < l) && (i < maxTXTs); i++)
{
ns_parserr(&msg, ns_s_an, i, &rr);
len = ns_rr_rdlen(rr);
if(0 == len) break;
u_char const* rdata = ns_rr_rdata(rr);
if(0==rdata) {
break;
}
TXT.assign((const char*) rdata, len);
TXT = TXT.substr(1);
TXTs.push_back(TXT);
}
return TXTs;
}