-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathresolver.cpp
More file actions
289 lines (253 loc) · 6.66 KB
/
Copy pathresolver.cpp
File metadata and controls
289 lines (253 loc) · 6.66 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
SMTP PING
Copyright (C) 2011 Halon Security <support@halon.se>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "resolver.hpp"
#include <map>
#include <algorithm>
#if defined(__WIN32__)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windns.h>
#else
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
#include <memory.h>
/*
* initialize thread-safe m_res structure
*/
Resolver::Resolver()
{
#ifdef __WIN32__
m_hDnsInst = LoadLibrary("DNSAPI.DLL");
if (m_hDnsInst)
{
m_lpfnDnsRecordListFree = reinterpret_cast<LPDNSRECORDLISTFREE>(GetProcAddress(m_hDnsInst, "DnsRecordListFree"));
m_lpfnDnsQuery = reinterpret_cast<LPDNSQUERY>(GetProcAddress(m_hDnsInst, "DnsQuery_A"));
}
#else
memset((void*)&m_res, 0, sizeof m_res);
res_ninit(&m_res);
#endif
}
/*
* close thread-safe m_res structure
*/
Resolver::~Resolver()
{
#ifdef __WIN32__
if (m_hDnsInst)
{
FreeLibrary(m_hDnsInst);
m_hDnsInst = NULL;
}
#else
res_nclose(&m_res);
#endif
}
bool Resolver::Lookup(const std::string& domain, RecordType recordType, std::vector<std::string>& result)
{
std::map<unsigned int, std::vector<std::string> > prioMap;
#ifdef __WIN32__
if (!m_lpfnDnsRecordListFree || !m_lpfnDnsQuery)
return false;
int req_rec_type;
#ifndef DNS_TYPE_AAAA
# define DNS_TYPE_AAAA (28)
#endif
switch(recordType)
{
case RR_A:
req_rec_type = DNS_TYPE_A;
break;
case RR_AAAA:
req_rec_type = DNS_TYPE_AAAA;
break;
case RR_MX:
req_rec_type = DNS_TYPE_MX;
break;
default:
return false;
}
PDNS_RECORD pRec = NULL;
if (m_lpfnDnsQuery(domain.c_str(), req_rec_type, DNS_QUERY_STANDARD, NULL, &pRec, NULL) != ERROR_SUCCESS)
return false;
PDNS_RECORD pRecFirst = pRec;
while (pRec)
{
if (req_rec_type == pRec->wType && pRec->Flags.S.Section == DNSREC_ANSWER)
{
if (pRec->wType == DNS_TYPE_MX)
{
prioMap[(int)pRec->Data.MX.wPreference].push_back(pRec->Data.MX.pNameExchange);
}
if (pRec->wType == DNS_TYPE_AAAA)
{
SOCKADDR_IN6 addr;
memset(&addr, 0, sizeof addr);
addr.sin6_family = AF_INET6;
addr.sin6_addr = *((in_addr6*)&(pRec->Data.AAAA.Ip6Address));
char buf[128];
DWORD bufsize = sizeof buf;
if (WSAAddressToStringA((sockaddr*)&addr, sizeof addr, NULL, buf, &bufsize) == 0)
{
prioMap[0].push_back(buf);
}
}
if (pRec->wType == DNS_TYPE_A)
{
SOCKADDR_IN addr;
memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
addr.sin_addr = *((in_addr*)&(pRec->Data.A.IpAddress));
char buf[128];
DWORD bufsize = sizeof buf;
if (WSAAddressToStringA((sockaddr*)&addr, sizeof addr, NULL, buf, &bufsize) == 0)
{
prioMap[0].push_back(buf);
}
}
}
pRec = pRec->pNext;
}
m_lpfnDnsRecordListFree(pRecFirst, DnsFreeRecordList);
#else
unsigned char response[64 * 1024];
memset(response, 0, sizeof response);
unsigned char *resData, *resEnd;
unsigned short rec_len, rec_pref;
unsigned short rec_type;
HEADER* header;
int req_rec_type;
switch(recordType)
{
case RR_A:
req_rec_type = T_A;
break;
case RR_AAAA:
req_rec_type = T_AAAA;
break;
case RR_MX:
req_rec_type = T_MX;
break;
default:
return false;
}
int len = res_nquery(&m_res, domain.c_str(), C_IN, req_rec_type, (unsigned char*)&response, sizeof response);
if (len < 0)
{
if (m_res.res_h_errno == NO_DATA)
return true;
return false;
}
if (len > (int)sizeof response) {
return false;
}
/* a valid response must at least contain the fixed header */
if (len < HFIXEDSZ)
return false;
header = (HEADER*)&response;
resData = (unsigned char*)&response + HFIXEDSZ;
resEnd = (unsigned char*)&response + len;
int answer_count = ntohs((unsigned short)header->ancount);
int query_count = ntohs((unsigned short)header->qdcount);
for (int i = 0; i < query_count; i++) {
if ((len = dn_skipname(resData, resEnd)) < 0)
return false;
resData += len;
/* each question is followed by QTYPE + QCLASS */
if (resData + QFIXEDSZ > resEnd)
return false;
resData += QFIXEDSZ;
}
char buf[MAXDNAME + 1];
for (int i = 0; i < answer_count; i++) {
len = dn_expand((unsigned char*)&response, resEnd, resData, (char*)&buf, sizeof buf - 1);
if (len < 0)
return false;
resData += len;
/* fixed RR header: TYPE(2) + CLASS(2) + TTL(4) + RDLENGTH(2) */
if (resData + 3 * INT16SZ + INT32SZ > resEnd)
return false;
GETSHORT(rec_type, resData);
resData += INT16SZ + INT32SZ;
GETSHORT(rec_len, resData);
/* the record data must lie within the response buffer */
if (rec_len > resEnd - resData)
return false;
/* parse rdata through a separate cursor so that advancing
resData below always uses the untouched RDLENGTH */
unsigned char* rdata = resData;
unsigned short rdata_len = rec_len;
switch(rec_type)
{
case T_MX:
/* MX rdata is PREFERENCE(2) + exchange name */
if (rdata_len < INT16SZ)
return false;
GETSHORT(rec_pref, rdata);
rdata_len -= INT16SZ;
break;
default:
rec_pref = 0;
break;
}
if (rec_type == req_rec_type) {
switch(rec_type)
{
case T_A:
if (rdata_len >= sizeof(struct in_addr))
{
char abuf[INET_ADDRSTRLEN];
if (inet_ntop(AF_INET,
rdata, abuf, sizeof abuf)) {
prioMap[rec_pref].push_back(abuf);
}
}
break;
case T_AAAA:
if (rdata_len >= sizeof(struct in6_addr))
{
char abuf[INET6_ADDRSTRLEN];
if (inet_ntop(AF_INET6,
rdata, abuf, sizeof abuf)) {
prioMap[rec_pref].push_back(abuf);
}
}
break;
case T_MX:
{
char mbuf[MAXDNAME + 1];
if (dn_expand((unsigned char*)&response, resEnd, rdata, (char*)&mbuf, sizeof mbuf - 1) < 0)
return false;
prioMap[rec_pref].push_back(mbuf);
}
break;
}
}
resData += rec_len;
}
#endif
// merge map
std::map<unsigned int, std::vector<std::string> >::iterator i;
for(i = prioMap.begin(); i != prioMap.end(); ++i)
{
std::sort(i->second.begin(), i->second.end());
result.insert(result.end(), i->second.begin(), i->second.end());
}
return true;
}