-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDNS.cpp
More file actions
309 lines (281 loc) · 11.7 KB
/
Copy pathDNS.cpp
File metadata and controls
309 lines (281 loc) · 11.7 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* DNS.cpp
*
* Created on: 21/10/2010
* Author: Flavio
*/
#include "DNS.h"
DNS::DNS()
{
}
DNS::~DNS()
{
}
bool
DNS::GetMX(std::vector<string>& adds, const std::string& domain)
{
adds.clear(); // be safe in case of my utter stupidity
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(53); // default dns port
SOCKET s = socket(AF_INET, SOCK_DGRAM, 0);
hostent* host = 0;
#ifdef _WIN32
addr.sin_addr.S_un.S_addr = inet_addr(server.c_str());
if(addr.sin_addr.S_un.S_addr != INADDR_NONE) {
#else
if(inet_aton(server.c_str(), &addr.sin_addr)) {
#endif
host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr), AF_INET);
}
else
host = gethostbyname(server.c_str());
if(!host) { // couldn't get to dns, try to connect directly to 'server' instead.
////////////////////////////////////////////////////////////////////////////////
// just try to deliver mail directly to "server"
// as we didn't get an MX record.
addr.sin_family = AF_INET;
addr.sin_port = 25; // smtp port!! 25
#ifdef WIN32
addr.sin_addr.S_un.S_addr = inet_addr(server.c_str());
if(addr.sin_addr.S_un.S_addr != INADDR_NONE) {
#else
if(inet_aton(domain.c_str(), &addr.sin_addr)) {
#endif
host = gethostbyaddr((char*)&addr.sin_addr, sizeof(addr.sin_addr), AF_INET);
}
else
host = gethostbyname(domain.c_str());
if(!host) {
return false; // error!!!
}
memcpy((char*)&addr.sin_addr, host->h_addr, host->h_length);
char *p = host->h_addr;
while (*p) {
adds.push_back(p);
p++;
}
// adds.push_back(addr);
return true;
}
else
memcpy((char*)&addr.sin_addr, host->h_addr, host->h_length);
memset(&(addr.sin_zero), 0, 8);
#ifdef WIN32
if(connect(s, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) {
#else
if(connect(s, (sockaddr*)&addr, sizeof(addr))) {
#endif
return false; // dns connection unavailable
}
// dnsheader info id flags num queries
unsigned char dns[512] = {1,1, 1,0, 0,1, 0,0, 0,0, 0,0};
int dnspos = 12; // end of dns header
std::string::size_type stringpos(0);
std::string::size_type next(domain.find("."));
if(next != std::string::npos) { // multipart name e.g. "aserver.somewhere.net"
while(stringpos < domain.length()) {
std::string part(domain.substr(stringpos, next-stringpos));
dns[dnspos] = part.length();
++dnspos;
for(std::string::size_type i = 0; i < part.length(); ++i, ++dnspos) {
dns[dnspos] = part[i];
}
stringpos = ++next;
next = domain.find(".", stringpos);
if(next == std::string::npos) {
part = domain.substr(stringpos, domain.length() - stringpos);
dns[dnspos] = part.length();
++dnspos;
for(std::string::size_type i = 0; i < part.length(); ++i, ++dnspos) {
dns[dnspos] = part[i];
}
break;
}
}
}
else { // just a single part name. e.g. "aserver"
dns[dnspos] = domain.length();
++dnspos;
for(std::string::size_type i = 0; i < domain.length(); ++i, ++dnspos) {
dns[dnspos] = domain[i];
}
}
// in case the server string has a "." on the end
if(domain[domain.length()-1] == '.')
dns[dnspos] = 0;
else
dns[dnspos++] = 0;
// add the class & type
dns[dnspos++] = 0;
dns[dnspos++] = 15; // MX record.
dns[dnspos++] = 0;
dns[dnspos++] = 1;
dnspos = ::send(s, (char*)dns, dnspos, MSG_DONTROUTE);
dnspos = recv(s, (char*)dns, 512, 0);
#ifdef WIN32
closesocket(s);
#else
close(s);
#endif
#ifdef WIN32
if(dnspos != SOCKET_ERROR) {
#else
if(dnspos > -1) {
#endif
// now parse the data sent back from the dns for MX records
if(dnspos > 12) { // we got more than a dns header back
unsigned short numsitenames = ((unsigned short)dns[4]<<8) | dns[5];
unsigned short numanswerRR = ((unsigned short)dns[6]<<8) | dns[7];
unsigned short numauthorityRR = ((unsigned short)dns[8]<<8) | dns[9];
unsigned short numadditionalRR = ((unsigned short)dns[10]<<8) | dns[11];
if(!(dns[3] & 0x0F)) { // check for an error
// int auth((dns[2] & 0x04)); // AA bit. the nameserver has given authoritive answer.
int pos = 12; // start after the header.
std::string questionname;
if(numsitenames) {
parsename(pos, dns, questionname);
pos += 4; // move to the next RR
}
// This gives this warning in VC.
// bloody annoying, there is a way round it according to microsoft.
// The debugger basically cannot browse anything with a name
// longer than 256 characters, "get with the template program MS".
// #pragma warning( disable : 4786 )
// #pragma warning( default : 4786 )
std::vector<std::string> names;
//in_addr address;
std::string name;
// VC++ incompatability scoping
// num should be able to be declared in every for loop here
// not in VC
int num = 0;
for(; num < numanswerRR; ++num) {
name = "";
parseRR(pos, dns, name);
if(name.length())
names.push_back(name);
}
for(num = 0; num < numauthorityRR; ++num) {
name = "";
parseRR(pos, dns, name);
if(name.length())
names.push_back(name);
}
for(num = 0; num < numadditionalRR; ++num) {
name = "";
parseRR(pos, dns, name);
if(name.length())
names.push_back(name);
}
// now get all the MX records IP addresess
addr.sin_family = AF_INET;
addr.sin_port = 25; // smtp port!! 25
hostent* host = 0;
for(std::vector<std::string>::const_iterator it = names.begin(); it < names.end(); ++it) {
host = gethostbyname(it->c_str());
if(!host) {
#ifdef WIN32
addr.sin_addr.S_un.S_addr = 0;
#else
addr.sin_addr.s_addr = 0;
#endif
continue; // just skip it!!!
}
memcpy((char*)&addr.sin_addr, host->h_addr, host->h_length);
char *p = host->h_addr;
while (*p) {
adds.push_back(p);
p++;
}
// adds.push_back(addr);
}
// got the addresses
return true;
}
}
}
return false;
}
void
DNS::parsename(int& pos, const unsigned char dns[], std::string& name)
{
int len = dns[pos];
if(len >= 192) {
int pos1 = ++pos;
++pos;
parsename(pos1, dns, name);
}
else {
for(int i = 0; i < len; ++i)
name += dns[++pos];
len = dns[++pos];
if(len != 0)
name += ".";
if(len >= 192) {
int pos1 = dns[++pos];
++pos;
parsename(pos1, dns, name);
}
else if(len > 0) {
parsename(pos, dns, name);
//++pos;
}
else if(len == 0)
++pos;
}
}
bool
DNS::parseRR(int& pos, const unsigned char dns[], std::string& name)
{
if(pos < 12) // didn,t get more than a header.
return false;
if(pos > 512) // oops.
return false;
int len = dns[pos];
if(len >= 192) { // pointer
int pos1 = dns[++pos];
len = dns[pos1];
}
else { // not a pointer.
parsename(pos, dns, name);
}
// If I do not seperate getting the short values to different
// lines of code, the optimizer in VC++ only increments pos once!!!
unsigned short a = ((unsigned short)dns[++pos]<<8);
unsigned short b = dns[++pos];
unsigned short Type = a | b;
a = ((unsigned short)dns[++pos]<<8);
b = dns[++pos];
// unsigned short Class = a | b;
pos += 4; // ttl
a = ((unsigned short)dns[++pos]<<8);
b = dns[++pos];
unsigned short Datalen = a | b;
if(Type == 15) { // MX record
// first two bytes the precedence of the MX server
a = ((unsigned short)dns[++pos]<<8);
b = dns[++pos];
// unsigned short order = a | b; // we don't use this here
len = dns[++pos];
if(len >= 192) {
int pos1 = dns[++pos];
parsename(pos1, dns, name);
}
else
parsename(pos, dns, name);
}
else if(Type == 12) { // pointer record
pos += Datalen+1;
}
else if(Type == 2) { // nameserver
pos += Datalen+1;
}
else if(Type == 1) { // IP address, Datalen should be 4.
pos += Datalen+1;
}
else {
pos += Datalen+1;
}
return true;
}