Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Unified Diff: net/base/dns_response.cc

Issue 7008021: Added DnsQuery class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added DnsResponse class Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/base/dns_response.cc
diff --git a/net/base/dns_response.cc b/net/base/dns_response.cc
new file mode 100644
index 0000000000000000000000000000000000000000..da41297b8de9239ecf00d796b1b939fe0ad49c84
--- /dev/null
+++ b/net/base/dns_response.cc
@@ -0,0 +1,109 @@
+// Copyright (c) 2011 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/dns_response.h"
+
+#include <netdb.h> // for EAI_NONAME
+
+#include "net/base/address_list.h"
+#include "net/base/dns_util.h"
+
+namespace net {
+
+// 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
+// bytes (not counting the IP nor UDP headers).
+static const int kMaxResponseSize = 512;
+
+// TODO(agayev): decide on |error_|, should we emulate getaddrinfo error
+// messages and continue with net_error and os_error scheme or should we
+// define more net_error codes and get rid of os_error, since there is no
+// "OS" anymore. Currently, |error_| is EAI_NONAME in case of an error, 0
+// otherwise.
+DnsResponse::DnsResponse(DnsQuery* query)
+ : error_(EAI_NONAME),
+ size_(kMaxResponseSize + 1),
+ query_(query),
+ io_buffer_(new IOBufferWithSize(size_)) {
+}
+
+bool DnsResponse::Parse(int nbytes, AddressList* results) {
+ DCHECK(query_->IsValid());
+
+ // Response includes query, it should be at least that size.
+ if (nbytes < query_->size() || nbytes > kMaxResponseSize)
+ return false;
+
+ size_ = nbytes;
+ DnsResponseBuffer response(reinterpret_cast<uint8*>(io_buffer_->data()),
+ size_);
+
+ uint16 id;
+ if (!response.U16(&id) || id != query_->id()) // Make sure IDs match.
+ return false;
+
+ uint8 flags, rcode;
+ if (!response.U8(&flags) || !response.U8(&rcode))
+ return false;
+
+ if (flags & 2) // TC is set -- server wants TCP, we don't support it (yet?).
+ return false;
+
+ rcode &= 0x0f;
+ if (rcode && (rcode != 3)) // 3 means NXDOMAIN, the rest means server failed.
+ return false;
+
+ 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))
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.
+ return false;
+
+ if (query_count != 1) // Sent a single question, shouldn't have changed.
+ return false;
+
+ std::string hostname;
+ uint16 qtype, qclass;
+ if (!response.DNSName(&hostname) ||
+ !response.U16(&qtype) ||
+ !response.U16(&qclass) ||
+ hostname != query_->hostname() || // Make sure Question section
+ qtype != query_->qtype() || // echoed back.
+ qclass != query_->qclass())
+ return false;
agl 2011/05/30 18:35:30 ditto
agayev 2011/05/31 15:19:06 Will do.
+
+ 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
+ return false;
+
+ std::vector<IPAddressNumber> rdatas;
+ while (answer_count--) {
+ uint32 ttl;
+ uint16 rdlength;
+ if (!response.DNSName(NULL) ||
+ !response.U16(&qtype) ||
+ !response.U16(&qclass) ||
+ !response.U32(&ttl) ||
+ !response.U16(&rdlength))
+ return false;
agl 2011/05/30 18:35:30 ditto
agayev 2011/05/31 15:19:06 Will do.
+
+ if (qtype == query_->qtype() &&
+ qclass == query_->qclass() &&
+ (rdlength == kIPv4AddressSize || rdlength == kIPv6AddressSize)) {
+ base::StringPiece rdata;
+ if (!response.Block(&rdata, rdlength))
+ return false;
+ rdatas.push_back(IPAddressNumber(rdata.begin(), rdata.end()));
+ } else if (!response.Skip(rdlength))
+ return false;
+ }
+
+ if (rdatas.empty())
+ return false;
+
+ *results = AddressList::CreateFromIPAddressList(rdatas, query_->port());
+ error_ = 0;
+ return true;
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698