| Index: net/dns/dns_response.cc
|
| ===================================================================
|
| --- net/dns/dns_response.cc (revision 113383)
|
| +++ net/dns/dns_response.cc (working copy)
|
| @@ -4,185 +4,97 @@
|
|
|
| #include "net/dns/dns_response.h"
|
|
|
| -#include "net/base/big_endian.h"
|
| +#include "net/base/dns_util.h"
|
| #include "net/base/io_buffer.h"
|
| #include "net/base/net_errors.h"
|
| -#include "net/base/sys_byteorder.h"
|
| -#include "net/dns/dns_protocol.h"
|
| #include "net/dns/dns_query.h"
|
|
|
| namespace net {
|
|
|
| -DnsRecordParser::DnsRecordParser() : packet_(NULL), length_(0), cur_(0) {
|
| +// RFC 1035, section 4.2.1: Messages carried by UDP are restricted to 512
|
| +// bytes (not counting the IP nor UDP headers).
|
| +static const int kMaxResponseSize = 512;
|
| +
|
| +DnsResponse::DnsResponse(DnsQuery* query)
|
| + : query_(query),
|
| + io_buffer_(new IOBufferWithSize(kMaxResponseSize + 1)) {
|
| + DCHECK(query_);
|
| }
|
|
|
| -DnsRecordParser::DnsRecordParser(const void* packet,
|
| - size_t length,
|
| - size_t offset)
|
| - : packet_(reinterpret_cast<const char*>(packet)),
|
| - length_(length),
|
| - cur_(packet_ + offset) {
|
| - DCHECK_LE(offset, length);
|
| +DnsResponse::~DnsResponse() {
|
| }
|
|
|
| -int DnsRecordParser::ParseName(const void* const vpos, std::string* out) const {
|
| - const char* const pos = reinterpret_cast<const char*>(vpos);
|
| - DCHECK(packet_);
|
| - DCHECK_LE(packet_, pos);
|
| - DCHECK_LE(pos, packet_ + length_);
|
| +int DnsResponse::Parse(int nbytes, IPAddressList* ip_addresses) {
|
| + // Response includes query, it should be at least that size.
|
| + if (nbytes < query_->io_buffer()->size() || nbytes > kMaxResponseSize)
|
| + return ERR_DNS_MALFORMED_RESPONSE;
|
|
|
| - const char* p = pos;
|
| - const char* end = packet_ + length_;
|
| - // Count number of seen bytes to detect loops.
|
| - size_t seen = 0;
|
| - // Remember how many bytes were consumed before first jump.
|
| - size_t consumed = 0;
|
| + DnsResponseBuffer response(reinterpret_cast<uint8*>(io_buffer_->data()),
|
| + io_buffer_->size());
|
| + uint16 id;
|
| + if (!response.U16(&id) || id != query_->id()) // Make sure IDs match.
|
| + return ERR_DNS_MALFORMED_RESPONSE;
|
|
|
| - if (pos >= end)
|
| - return 0;
|
| + uint8 flags, rcode;
|
| + if (!response.U8(&flags) || !response.U8(&rcode))
|
| + return ERR_DNS_MALFORMED_RESPONSE;
|
|
|
| - if (out) {
|
| - out->clear();
|
| - out->reserve(dns_protocol::kMaxNameLength);
|
| - }
|
| + if (flags & 2) // TC is set -- server wants TCP, we don't support it (yet?).
|
| + return ERR_DNS_SERVER_REQUIRES_TCP;
|
|
|
| - for (;;) {
|
| - // 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.
|
| - switch (*p & dns_protocol::kLabelMask) {
|
| - case dns_protocol::kLabelPointer: {
|
| - if (p + sizeof(uint16) > end)
|
| - return 0;
|
| - if (consumed == 0) {
|
| - consumed = p - pos + sizeof(uint16);
|
| - if (!out)
|
| - return consumed; // If name is not stored, that's all we need.
|
| - }
|
| - seen += sizeof(uint16);
|
| - // If seen the whole packet, then we must be in a loop.
|
| - if (seen > length_)
|
| - return 0;
|
| - uint16 offset;
|
| - ReadBigEndian<uint16>(p, &offset);
|
| - offset &= dns_protocol::kOffsetMask;
|
| - p = packet_ + offset;
|
| - if (p >= end)
|
| - return 0;
|
| - break;
|
| - }
|
| - case dns_protocol::kLabelDirect: {
|
| - uint8 label_len = *p;
|
| - ++p;
|
| - // Note: root domain (".") is NOT included.
|
| - if (label_len == 0) {
|
| - if (consumed == 0) {
|
| - consumed = p - pos;
|
| - } // else we set |consumed| before first jump
|
| - return consumed;
|
| - }
|
| - if (p + label_len >= end)
|
| - return 0; // Truncated or missing label.
|
| - if (out) {
|
| - if (!out->empty())
|
| - out->append(".");
|
| - out->append(p, label_len);
|
| - }
|
| - p += label_len;
|
| - seen += 1 + label_len;
|
| - break;
|
| - }
|
| - default:
|
| - // unhandled label type
|
| - return 0;
|
| - }
|
| - }
|
| -}
|
| + rcode &= 0x0f; // 3 means NXDOMAIN, the rest means server failed.
|
| + if (rcode && (rcode != 3))
|
| + return ERR_DNS_SERVER_FAILED;
|
|
|
| -bool DnsRecordParser::ParseRecord(DnsResourceRecord* out) {
|
| - DCHECK(packet_);
|
| - size_t consumed = ParseName(cur_, &out->name);
|
| - if (!consumed)
|
| - return false;
|
| - BigEndianReader reader(cur_ + consumed,
|
| - packet_ + length_ - (cur_ + consumed));
|
| - uint16 rdlen;
|
| - if (reader.ReadU16(&out->type) &&
|
| - reader.ReadU16(&out->klass) &&
|
| - reader.ReadU32(&out->ttl) &&
|
| - reader.ReadU16(&rdlen) &&
|
| - reader.ReadPiece(&out->rdata, rdlen)) {
|
| - cur_ = reader.ptr();
|
| - return true;
|
| + uint16 query_count, answer_count, authority_count, additional_count;
|
| + if (!response.U16(&query_count) ||
|
| + !response.U16(&answer_count) ||
|
| + !response.U16(&authority_count) ||
|
| + !response.U16(&additional_count)) {
|
| + return ERR_DNS_MALFORMED_RESPONSE;
|
| }
|
| - return false;
|
| -}
|
|
|
| -DnsResponse::DnsResponse()
|
| - : io_buffer_(new IOBufferWithSize(dns_protocol::kMaxUDPSize + 1)) {
|
| -}
|
| + if (query_count != 1) // Sent a single question, shouldn't have changed.
|
| + return ERR_DNS_MALFORMED_RESPONSE;
|
|
|
| -DnsResponse::DnsResponse(const void* data,
|
| - size_t length,
|
| - size_t answer_offset)
|
| - : io_buffer_(new IOBufferWithSize(length)),
|
| - parser_(io_buffer_->data(), length, answer_offset) {
|
| - memcpy(io_buffer_->data(), data, length);
|
| -}
|
| + base::StringPiece question; // Make sure question section is echoed back.
|
| + if (!response.Block(&question, query_->question_size()) ||
|
| + memcmp(question.data(), query_->question_data(),
|
| + query_->question_size())) {
|
| + return ERR_DNS_MALFORMED_RESPONSE;
|
| + }
|
|
|
| -DnsResponse::~DnsResponse() {
|
| -}
|
| + if (answer_count < 1)
|
| + return ERR_NAME_NOT_RESOLVED;
|
|
|
| -bool DnsResponse::InitParse(int nbytes, const DnsQuery& query) {
|
| - // Response includes query, it should be at least that size.
|
| - if (nbytes < query.io_buffer()->size() || nbytes > dns_protocol::kMaxUDPSize)
|
| - return false;
|
| -
|
| - // Match the query id.
|
| - if (ntohs(header()->id) != query.id())
|
| - return false;
|
| -
|
| - // Match question count.
|
| - if (ntohs(header()->qdcount) != 1)
|
| - return false;
|
| -
|
| - // Match the question section.
|
| - const size_t hdr_size = sizeof(dns_protocol::Header);
|
| - const base::StringPiece question = query.question();
|
| - if (question != base::StringPiece(io_buffer_->data() + hdr_size,
|
| - question.size())) {
|
| - return false;
|
| + IPAddressList rdatas;
|
| + while (answer_count--) {
|
| + uint32 ttl;
|
| + uint16 rdlength, qtype, qclass;
|
| + if (!response.DNSName(NULL) ||
|
| + !response.U16(&qtype) ||
|
| + !response.U16(&qclass) ||
|
| + !response.U32(&ttl) ||
|
| + !response.U16(&rdlength)) {
|
| + return ERR_DNS_MALFORMED_RESPONSE;
|
| + }
|
| + if (qtype == query_->qtype() &&
|
| + qclass == kClassIN &&
|
| + (rdlength == kIPv4AddressSize || rdlength == kIPv6AddressSize)) {
|
| + base::StringPiece rdata;
|
| + if (!response.Block(&rdata, rdlength))
|
| + return ERR_DNS_MALFORMED_RESPONSE;
|
| + rdatas.push_back(IPAddressNumber(rdata.begin(), rdata.end()));
|
| + } else if (!response.Skip(rdlength))
|
| + return ERR_DNS_MALFORMED_RESPONSE;
|
| }
|
|
|
| - // Construct the parser.
|
| - parser_ = DnsRecordParser(io_buffer_->data(),
|
| - nbytes,
|
| - hdr_size + question.size());
|
| - return true;
|
| -}
|
| + if (rdatas.empty())
|
| + return ERR_NAME_NOT_RESOLVED;
|
|
|
| -uint8 DnsResponse::flags0() const {
|
| - return header()->flags[0];
|
| + if (ip_addresses)
|
| + ip_addresses->swap(rdatas);
|
| + return OK;
|
| }
|
|
|
| -uint8 DnsResponse::flags1() const {
|
| - return header()->flags[1] & ~(dns_protocol::kRcodeMask);
|
| -}
|
| -
|
| -uint8 DnsResponse::rcode() const {
|
| - return header()->flags[1] & dns_protocol::kRcodeMask;
|
| -}
|
| -
|
| -int DnsResponse::answer_count() const {
|
| - return ntohs(header()->ancount);
|
| -}
|
| -
|
| -DnsRecordParser DnsResponse::Parser() const {
|
| - DCHECK(parser_.IsValid());
|
| - return parser_;
|
| -}
|
| -
|
| -const dns_protocol::Header* DnsResponse::header() const {
|
| - return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
|
| -}
|
| -
|
| } // namespace net
|
|
|