Chromium Code Reviews| 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 | |
| 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_)) { | |
|
cbentzel
2011/06/01 17:17:03
DCHECK(query_) in the constructor [and maybe query
agayev
2011/06/01 19:15:01
Done.
| |
| 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)) { | |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 if (query_count != 1) // Sent a single question, shouldn't have changed. | |
| 65 return false; | |
| 66 | |
| 67 std::string hostname; | |
| 68 uint16 qtype, qclass; | |
| 69 if (!response.DNSName(&hostname) || | |
| 70 !response.U16(&qtype) || | |
| 71 !response.U16(&qclass) || | |
| 72 hostname != query_->hostname() || // Make sure Question section | |
| 73 qtype != query_->qtype() || // echoed back. | |
| 74 qclass != kClassIN) { | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 if (answer_count < 1) | |
| 79 return false; | |
| 80 | |
| 81 std::vector<IPAddressNumber> rdatas; | |
| 82 while (answer_count--) { | |
| 83 uint32 ttl; | |
| 84 uint16 rdlength; | |
| 85 if (!response.DNSName(NULL) || | |
|
cbentzel
2011/06/01 17:17:03
Why are you passing in NULL DNS name? Don't you wa
cbentzel
2011/06/01 17:17:03
Does DNSResponseBuffer handle the compressed form
agayev
2011/06/01 19:15:01
It does.
agayev
2011/06/01 19:15:01
djbdns skips names there; I assume legal name serv
| |
| 86 !response.U16(&qtype) || | |
| 87 !response.U16(&qclass) || | |
| 88 !response.U32(&ttl) || | |
| 89 !response.U16(&rdlength)) { | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 if (qtype == query_->qtype() && | |
| 94 qclass == kClassIN && | |
| 95 (rdlength == kIPv4AddressSize || rdlength == kIPv6AddressSize)) { | |
| 96 base::StringPiece rdata; | |
| 97 if (!response.Block(&rdata, rdlength)) | |
| 98 return false; | |
| 99 rdatas.push_back(IPAddressNumber(rdata.begin(), rdata.end())); | |
|
cbentzel
2011/06/01 17:17:03
At some point we'll want to preserve TTLs as well.
agayev
2011/06/01 19:15:01
Yep, will amend it then.
| |
| 100 } else if (!response.Skip(rdlength)) | |
|
cbentzel
2011/06/01 17:17:03
In the future, might be nice to keep track of CNAM
agayev
2011/06/01 19:15:01
Okay.
| |
| 101 return false; | |
| 102 } | |
| 103 | |
| 104 if (rdatas.empty()) | |
| 105 return false; | |
|
cbentzel
2011/06/01 17:17:03
This should distinguish invalid DNS responses from
agayev
2011/06/01 19:15:01
This is something agl pointed out too, I'm waiting
| |
| 106 | |
| 107 *results = AddressList::CreateFromIPAddressList(rdatas, query_->port()); | |
| 108 error_ = 0; | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 } // namespace net | |
| OLD | NEW |