OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/base/dns_response.h" | |
6 | |
7 #include <netdb.h> // for EAI_NONAME | |
8 | |
9 #include "net/base/address_list.h" | |
10 #include "net/base/dns_util.h" | |
11 | |
12 namespace net { | |
13 | |
14 // RFC 1035, section 4.2.1: Messages carried by UDP are restricted to 512 | |
agl
2011/05/30 18:35:30
This is no longer true. The current recommendation
agayev
2011/05/31 15:19:06
Right. I was thinking of adding EDNS0 support in
| |
15 // bytes (not counting the IP nor UDP headers). | |
16 static const int kMaxResponseSize = 512; | |
17 | |
18 // TODO(agayev): decide on |error_|, should we emulate getaddrinfo error | |
19 // messages and continue with net_error and os_error scheme or should we | |
20 // define more net_error codes and get rid of os_error, since there is no | |
21 // "OS" anymore. Currently, |error_| is EAI_NONAME in case of an error, 0 | |
22 // otherwise. | |
23 DnsResponse::DnsResponse(DnsQuery* query) | |
24 : error_(EAI_NONAME), | |
25 size_(kMaxResponseSize + 1), | |
26 query_(query), | |
27 io_buffer_(new IOBufferWithSize(size_)) { | |
28 } | |
29 | |
30 bool DnsResponse::Parse(int nbytes, AddressList* results) { | |
31 DCHECK(query_->IsValid()); | |
32 | |
33 // Response includes query, it should be at least that size. | |
34 if (nbytes < query_->size() || nbytes > kMaxResponseSize) | |
35 return false; | |
36 | |
37 size_ = nbytes; | |
38 DnsResponseBuffer response(reinterpret_cast<uint8*>(io_buffer_->data()), | |
39 size_); | |
40 | |
41 uint16 id; | |
42 if (!response.U16(&id) || id != query_->id()) // Make sure IDs match. | |
43 return false; | |
44 | |
45 uint8 flags, rcode; | |
46 if (!response.U8(&flags) || !response.U8(&rcode)) | |
47 return false; | |
48 | |
49 if (flags & 2) // TC is set -- server wants TCP, we don't support it (yet?). | |
50 return false; | |
51 | |
52 rcode &= 0x0f; | |
53 if (rcode && (rcode != 3)) // 3 means NXDOMAIN, the rest means server failed. | |
54 return false; | |
55 | |
56 uint16 query_count, answer_count, authority_count, additional_count; | |
57 if (!response.U16(&query_count) || | |
58 !response.U16(&answer_count) || | |
59 !response.U16(&authority_count) || | |
60 !response.U16(&additional_count)) | |
agl
2011/05/30 18:35:30
{ } around the body if the conditional is multi-li
agayev
2011/05/31 15:19:06
Will do.
| |
61 return false; | |
62 | |
63 if (query_count != 1) // Sent a single question, shouldn't have changed. | |
64 return false; | |
65 | |
66 std::string hostname; | |
67 uint16 qtype, qclass; | |
68 if (!response.DNSName(&hostname) || | |
69 !response.U16(&qtype) || | |
70 !response.U16(&qclass) || | |
71 hostname != query_->hostname() || // Make sure Question section | |
72 qtype != query_->qtype() || // echoed back. | |
73 qclass != query_->qclass()) | |
74 return false; | |
agl
2011/05/30 18:35:30
ditto
agayev
2011/05/31 15:19:06
Will do.
| |
75 | |
76 if (answer_count < 1) | |
agl
2011/05/30 18:35:30
This isn't a parse error of course. I feel that yo
agayev
2011/05/31 15:19:06
Thanks for these. As I've written in the comment
| |
77 return false; | |
78 | |
79 std::vector<IPAddressNumber> rdatas; | |
80 while (answer_count--) { | |
81 uint32 ttl; | |
82 uint16 rdlength; | |
83 if (!response.DNSName(NULL) || | |
84 !response.U16(&qtype) || | |
85 !response.U16(&qclass) || | |
86 !response.U32(&ttl) || | |
87 !response.U16(&rdlength)) | |
88 return false; | |
agl
2011/05/30 18:35:30
ditto
agayev
2011/05/31 15:19:06
Will do.
| |
89 | |
90 if (qtype == query_->qtype() && | |
91 qclass == query_->qclass() && | |
92 (rdlength == kIPv4AddressSize || rdlength == kIPv6AddressSize)) { | |
93 base::StringPiece rdata; | |
94 if (!response.Block(&rdata, rdlength)) | |
95 return false; | |
96 rdatas.push_back(IPAddressNumber(rdata.begin(), rdata.end())); | |
97 } else if (!response.Skip(rdlength)) | |
98 return false; | |
99 } | |
100 | |
101 if (rdatas.empty()) | |
102 return false; | |
103 | |
104 *results = AddressList::CreateFromIPAddressList(rdatas, query_->port()); | |
105 error_ = 0; | |
106 return true; | |
107 } | |
108 | |
109 } // namespace net | |
OLD | NEW |