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 "net/base/address_list.h" | |
8 #include "net/base/dns_util.h" | |
9 #include "net/base/net_errors.h" | |
10 | |
11 namespace net { | |
12 | |
13 // RFC 1035, section 4.2.1: Messages carried by UDP are restricted to 512 | |
14 // bytes (not counting the IP nor UDP headers). | |
15 static const int kMaxResponseSize = 512; | |
16 | |
17 DnsResponse::DnsResponse(DnsQuery* query) | |
18 : query_(query), | |
19 io_buffer_(new IOBufferWithSize(kMaxResponseSize + 1)) { | |
20 DCHECK(query_); | |
21 DCHECK(query_->IsValid()); | |
22 } | |
23 | |
24 int DnsResponse::Parse(int nbytes, AddressList* results) { | |
25 DCHECK(query_->IsValid()); | |
26 | |
27 // Response includes query, it should be at least that size. | |
28 if (nbytes < query_->io_buffer()->size() || nbytes > kMaxResponseSize) | |
29 return ERR_DNS_MALFORMED_RESPONSE; | |
30 | |
31 DnsResponseBuffer response(reinterpret_cast<uint8*>(io_buffer_->data()), | |
32 io_buffer_->size()); | |
33 uint16 id; | |
34 if (!response.U16(&id) || id != query_->id()) // Make sure IDs match. | |
35 return ERR_DNS_MALFORMED_RESPONSE; | |
36 | |
37 uint8 flags, rcode; | |
38 if (!response.U8(&flags) || !response.U8(&rcode)) | |
39 return ERR_DNS_MALFORMED_RESPONSE; | |
40 | |
41 if (flags & 2) // TC is set -- server wants TCP, we don't support it (yet?). | |
42 return ERR_DNS_SERVER_REQUIRES_TCP; | |
43 | |
44 rcode &= 0x0f; // 3 means NXDOMAIN, the rest means server failed. | |
45 if (rcode && (rcode != 3)) | |
46 return ERR_DNS_SERVER_FAILED; | |
47 | |
48 uint16 query_count, answer_count, authority_count, additional_count; | |
49 if (!response.U16(&query_count) || | |
50 !response.U16(&answer_count) || | |
51 !response.U16(&authority_count) || | |
52 !response.U16(&additional_count)) { | |
53 return ERR_DNS_MALFORMED_RESPONSE; | |
54 } | |
55 | |
56 if (query_count != 1) // Sent a single question, shouldn't have changed. | |
57 return ERR_DNS_MALFORMED_RESPONSE; | |
58 | |
59 std::string hostname; | |
60 uint16 qtype, qclass; | |
61 if (!response.DNSName(&hostname) || | |
62 !response.U16(&qtype) || | |
63 !response.U16(&qclass) || | |
64 hostname != query_->hostname() || // Make sure Question section | |
65 qtype != query_->qtype() || // echoed back. | |
66 qclass != kClassIN) { | |
67 return ERR_DNS_MALFORMED_RESPONSE; | |
68 } | |
69 | |
70 if (answer_count < 1) | |
71 return ERR_NAME_NOT_RESOLVED; | |
72 | |
73 std::vector<IPAddressNumber> rdatas; | |
74 while (answer_count--) { | |
75 uint32 ttl; | |
76 uint16 rdlength; | |
77 if (!response.DNSName(NULL) || | |
78 !response.U16(&qtype) || | |
79 !response.U16(&qclass) || | |
80 !response.U32(&ttl) || | |
81 !response.U16(&rdlength)) { | |
82 return ERR_DNS_MALFORMED_RESPONSE; | |
83 } | |
84 | |
85 if (qtype == query_->qtype() && | |
86 qclass == kClassIN && | |
87 (rdlength == kIPv4AddressSize || rdlength == kIPv6AddressSize)) { | |
88 base::StringPiece rdata; | |
89 if (!response.Block(&rdata, rdlength)) | |
90 return ERR_DNS_MALFORMED_RESPONSE; | |
91 rdatas.push_back(IPAddressNumber(rdata.begin(), rdata.end())); | |
92 } else if (!response.Skip(rdlength)) | |
93 return ERR_DNS_MALFORMED_RESPONSE; | |
94 } | |
95 | |
96 if (rdatas.empty()) | |
97 return ERR_NAME_NOT_RESOLVED; | |
98 | |
99 *results = AddressList::CreateFromIPAddressList(rdatas, query_->port()); | |
100 return OK; | |
101 } | |
102 | |
103 } // namespace net | |
OLD | NEW |