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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/dnsrr_resolver.h"
#if defined(OS_POSIX)
#include <resolv.h>
#endif
#include "base/message_loop.h"
#include "base/scoped_ptr.h"
#include "base/string_piece.h"
#include "base/task.h"
#include "base/worker_pool.h"
#include "net/base/dns_reload_timer.h"
#include "net/base/dns_util.h"
#include "net/base/net_errors.h"
namespace net {
static const uint16 kClassIN = 1;
namespace {
class CompletionCallbackTask : public Task,
public MessageLoop::DestructionObserver {
public:
explicit CompletionCallbackTask(CompletionCallback* callback,
MessageLoop* ml)
: callback_(callback),
rv_(OK),
posted_(false),
message_loop_(ml) {
ml->AddDestructionObserver(this);
}
void set_rv(int rv) {
rv_ = rv;
}
void Post() {
AutoLock locked(lock_);
DCHECK(!posted_);
if (!message_loop_) {
// MessageLoop got deleted, nothing to do.
delete this;
return;
}
posted_ = true;
message_loop_->PostTask(FROM_HERE, this);
}
// Task interface
void Run() {
message_loop_->RemoveDestructionObserver(this);
callback_->Run(rv_);
// We will be deleted by the message loop.
}
// DestructionObserver interface
virtual void WillDestroyCurrentMessageLoop() {
AutoLock locked(lock_);
message_loop_ = NULL;
}
private:
DISALLOW_COPY_AND_ASSIGN(CompletionCallbackTask);
CompletionCallback* callback_;
int rv_;
bool posted_;
Lock lock_; // covers |message_loop_|
MessageLoop* message_loop_;
};
#if defined(OS_POSIX)
class ResolveTask : public Task {
public:
ResolveTask(const std::string& name, uint16 rrtype,
uint16 flags, CompletionCallback* callback,
RRResponse* response, MessageLoop* ml)
: name_(name),
rrtype_(rrtype),
flags_(flags),
subtask_(new CompletionCallbackTask(callback, ml)),
response_(response) {
}
virtual void Run() {
// Runs on a worker thread.
bool r = true;
if ((_res.options & RES_INIT) == 0) {
if (res_ninit(&_res) != 0)
r = false;
}
if (r) {
unsigned long saved_options = _res.options;
r = Do();
#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD)
if (!r && DnsReloadTimerHasExpired()) {
res_nclose(&_res);
if (res_ninit(&_res) == 0)
r = Do();
}
#endif
_res.options = saved_options;
}
int error = r ? OK : ERR_NAME_NOT_RESOLVED;
subtask_->set_rv(error);
subtask_.release()->Post();
}
bool Do() {
// For DNSSEC, a 4K buffer is suggested
static const unsigned kMaxDNSPayload = 4096;
#ifndef RES_USE_DNSSEC
// Some versions of libresolv don't have support for the DO bit. In this
// case, we proceed without it.
static const int RES_USE_DNSSEC = 0;
#endif
#ifndef RES_USE_EDNS0
// Some versions of glibc are so old that they don't support EDNS0 either.
// http://code.google.com/p/chromium/issues/detail?id=51676
static const int RES_USE_EDNS0 = 0;
#endif
// We set the options explicitly. Note that this removes several default
// options: RES_DEFNAMES and RES_DNSRCH (see res_init(3)).
_res.options = RES_INIT | RES_RECURSE | RES_USE_EDNS0 | RES_USE_DNSSEC;
uint8 answer[kMaxDNSPayload];
int len = res_search(name_.c_str(), kClassIN, rrtype_, answer,
sizeof(answer));
if (len == -1)
return false;
return response_->ParseFromResponse(answer, len, rrtype_);
}
private:
DISALLOW_COPY_AND_ASSIGN(ResolveTask);
const std::string name_;
const uint16 rrtype_;
const uint16 flags_;
scoped_ptr<CompletionCallbackTask> subtask_;
RRResponse* const response_;
};
#else // OS_POSIX
// On non-Linux platforms we fail everything for now.
class ResolveTask : public Task {
public:
ResolveTask(const std::string& name, uint16 rrtype,
uint16 flags, CompletionCallback* callback,
RRResponse* response, MessageLoop* ml)
: subtask_(new CompletionCallbackTask(callback, ml)) {
}
virtual void Run() {
subtask_->set_rv(ERR_NAME_NOT_RESOLVED);
subtask_->Post();
}
private:
CompletionCallbackTask* const subtask_;
DISALLOW_COPY_AND_ASSIGN(ResolveTask);
};
#endif
// A Buffer is used for walking over a DNS packet.
class Buffer {
public:
Buffer(const uint8* p, unsigned len)
: p_(p),
packet_(p),
len_(len),
packet_len_(len) {
}
bool U8(uint8* v) {
if (len_ < 1)
return false;
*v = *p_;
p_++;
len_--;
return true;
}
bool U16(uint16* v) {
if (len_ < 2)
return false;
*v = static_cast<uint16>(p_[0]) << 8 |
static_cast<uint16>(p_[1]);
p_ += 2;
len_ -= 2;
return true;
}
bool U32(uint32* v) {
if (len_ < 4)
return false;
*v = static_cast<uint32>(p_[0]) << 24 |
static_cast<uint32>(p_[1]) << 16 |
static_cast<uint32>(p_[2]) << 8 |
static_cast<uint32>(p_[3]);
p_ += 4;
len_ -= 4;
return true;
}
bool Skip(unsigned n) {
if (len_ < n)
return false;
p_ += n;
len_ -= n;
return true;
}
bool Block(base::StringPiece* out, unsigned len) {
if (len_ < len)
return false;
*out = base::StringPiece(reinterpret_cast<const char*>(p_), len);
p_ += len;
len_ -= len;
return true;
}
// DNSName parses a (possibly compressed) DNS name from the packet. If |name|
// is not NULL, then the name is written into it. See RFC 1035 section 4.1.4.
bool DNSName(std::string* name) {
unsigned jumps = 0;
const uint8* p = p_;
unsigned len = len_;
if (name)
name->clear();
for (;;) {
if (len < 1)
return false;
uint8 d = *p;
p++;
len--;
// The two couple of bits of the length give the type of the length. It's
// either a direct length or a pointer to the remainder of the name.
if ((d & 0xc0) == 0xc0) {
// This limit matches the depth limit in djbdns.
if (jumps > 100)
return false;
if (len < 1)
return false;
uint16 offset = static_cast<uint16>(d) << 8 |
static_cast<uint16>(p[0]);
offset &= 0x3ff;
p++;
len--;
if (jumps == 0) {
p_ = p;
len_ = len;
}
jumps++;
if (offset >= packet_len_)
return false;
p = &packet_[offset];
} else if ((d & 0xc0) == 0) {
uint8 label_len = d;
if (len < label_len)
return false;
if (name && label_len) {
if (!name->empty())
name->append(".");
name->append(reinterpret_cast<const char*>(p), label_len);
}
p += label_len;
len -= label_len;
if (jumps == 0) {
p_ = p;
len_ = len;
}
if (label_len == 0)
break;
} else {
return false;
}
}
return true;
}
private:
DISALLOW_COPY_AND_ASSIGN(Buffer);
const uint8* p_;
const uint8* const packet_;
unsigned len_;
const unsigned packet_len_;
};
} // anonymous namespace
bool RRResponse::ParseFromResponse(const uint8* p, unsigned len,
uint16 rrtype_requested) {
#if defined(OS_POSIX)
name.clear();
ttl = 0;
dnssec = false;
rrdatas.clear();
signatures.clear();
// RFC 1035 section 4.4.1
uint8 flags2;
Buffer buf(p, len);
if (!buf.Skip(2) || // skip id
!buf.Skip(1) || // skip first flags byte
!buf.U8(&flags2)) {
return false;
}
// Bit 5 is the Authenticated Data (AD) bit. See
// http://tools.ietf.org/html/rfc2535#section-6.1
if (flags2 & 32) {
// AD flag is set. We'll trust it if it came from a local nameserver.
// Currently the resolv structure is IPv4 only, so we can't test for IPv6
// loopback addresses.
if (_res.nscount == 1 &&
memcmp(&_res.nsaddr_list[0].sin_addr,
"\x7f\x00\x00\x01" /* 127.0.0.1 */, 4) == 0) {
dnssec = true;
}
}
uint16 query_count, answer_count, authority_count, additional_count;
if (!buf.U16(&query_count) ||
!buf.U16(&answer_count) ||
!buf.U16(&authority_count) ||
!buf.U16(&additional_count)) {
return false;
}
if (query_count != 1)
return false;
uint16 type, klass;
if (!buf.DNSName(NULL) ||
!buf.U16(&type) ||
!buf.U16(&klass) ||
type != rrtype_requested ||
klass != kClassIN) {
return false;
}
if (answer_count < 1)
return false;
for (uint32 i = 0; i < answer_count; i++) {
std::string* name = NULL;
if (i == 0)
name = &this->name;
uint32 ttl;
uint16 rrdata_len;
if (!buf.DNSName(name) ||
!buf.U16(&type) ||
!buf.U16(&klass) ||
!buf.U32(&ttl) ||
!buf.U16(&rrdata_len)) {
return false;
}
base::StringPiece rrdata;
if (!buf.Block(&rrdata, rrdata_len))
return false;
if (klass == kClassIN && type == rrtype_requested) {
if (i == 0)
this->ttl = ttl;
rrdatas.push_back(std::string(rrdata.data(), rrdata.size()));
} else if (klass == kClassIN && type == kDNS_RRSIG) {
signatures.push_back(std::string(rrdata.data(), rrdata.size()));
}
}
#endif // defined(OS_POSIX)
return true;
}
// static
bool DnsRRResolver::Resolve(const std::string& name, uint16 rrtype,
uint16 flags, CompletionCallback* callback,
RRResponse* response) {
if (!callback || !response || name.empty())
return false;
// Don't allow queries of type ANY
if (rrtype == kDNS_ANY)
return false;
ResolveTask* task = new ResolveTask(name, rrtype, flags, callback, response,
MessageLoop::current());
return WorkerPool::PostTask(FROM_HERE, task, true /* task is slow */);
}
} // namespace net
|