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

Unified Diff: cloud_print/gcp20/prototype/dns_response_builder.cc

Issue 16975004: Finished DNS-SD server. Finished Privet-specified DNS-SD server. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed Lint errors. Created 7 years, 6 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: cloud_print/gcp20/prototype/dns_response_builder.cc
diff --git a/cloud_print/gcp20/prototype/dns_response_builder.cc b/cloud_print/gcp20/prototype/dns_response_builder.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c2e678529a8558e31beb06398ddc118aae74938b
--- /dev/null
+++ b/cloud_print/gcp20/prototype/dns_response_builder.cc
@@ -0,0 +1,138 @@
+// Copyright 2013 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 "cloud_print/gcp20/prototype/dns_response_builder.h"
+
+#include "base/logging.h"
+#include "net/base/big_endian.h"
+#include "net/base/dns_util.h"
+#include "net/base/ip_endpoint.h"
+
+namespace {
+
+const uint16 kSrvPriority = 0;
+const uint16 kSrvWeight = 0;
+
+uint16 klass = net::dns_protocol::kClassIN;
+
+} // namespace
+
+DnsResponseBuilder::DnsResponseBuilder(uint16 id) {
+ header_.id = id;
+ // TODO(maksymb): Check do we need AA flag enabled.
+ header_.flags = net::dns_protocol::kFlagResponse |
+ net::dns_protocol::kFlagAA;
+ header_.qdcount = 0;
+ header_.ancount = 0;
+ header_.nscount = 0;
+ header_.arcount = 0;
+}
+
+void DnsResponseBuilder::AppendPtr(uint32 ttl,
+ const ServiceParameters& serv_params) {
+ std::string rdata;
+ bool success = net::DNSDomainFromDot(serv_params.service_name_,
+ &rdata);
+ DCHECK(success);
+
+ AddResponse(serv_params.service_type_, net::dns_protocol::kTypePTR, ttl,
+ rdata);
+}
+
+void DnsResponseBuilder::AppendSrv(uint32 ttl,
+ const ServiceParameters& serv_params) {
+ std::string domain_name;
+ bool success = net::DNSDomainFromDot(serv_params.service_domain_name_,
+ &domain_name);
+ DCHECK(success);
+
+ int rdata_size = 2 + 2 + 2 + domain_name.size();
+ scoped_ptr<char[]> rdata(new char[rdata_size]);
+ net::BigEndianWriter writer(rdata.get(), rdata_size);
+
+ success = writer.WriteU16(kSrvPriority) &&
+ writer.WriteU16(kSrvWeight) &&
+ writer.WriteU16(serv_params.http_port_) &&
+ writer.WriteBytes(domain_name.data(), domain_name.size());
+ DCHECK(success);
+ DCHECK_EQ(writer.remaining(), 0); // For warranty of correct size allocation.
+
+ base::StringPiece rdata_sp;
+ rdata_sp.set(rdata.get(), rdata_size);
+ std::string rdata_string;
+ rdata_sp.CopyToString(&rdata_string);
+
+ AddResponse(serv_params.service_name_, net::dns_protocol::kTypeSRV, ttl,
+ rdata_string);
+}
+
+void DnsResponseBuilder::AppendA(uint32 ttl,
+ const ServiceParameters& serv_params) {
+ if (serv_params.http_ip_.empty()) {
+ LOG(ERROR) << "Invalid IP";
+ return;
+ }
+
+ std::string rdata(serv_params.http_ip_.begin(), serv_params.http_ip_.end());
+
+ AddResponse(serv_params.service_domain_name_, net::dns_protocol::kTypeA, ttl,
+ rdata);
+}
+
+void DnsResponseBuilder::AppendTxt(uint32 ttl,
+ const ServiceParameters& serv_params,
+ const std::string& txt) {
+ AddResponse(serv_params.service_name_, net::dns_protocol::kTypeTXT, ttl, txt);
+}
+
+scoped_refptr<net::IOBufferWithSize> DnsResponseBuilder::Build() const {
+ scoped_refptr<net::IOBufferWithSize> message(
+ new net::IOBufferWithSize(sizeof(header_) + responses_.size()));
+
+ if (responses_.size() == 0)
+ return NULL; // No answer.
+
+ net::BigEndianWriter writer(message->data(), message->size());
+ bool success = writer.WriteU16(header_.id) &&
+ writer.WriteU16(header_.flags) &&
+ writer.WriteU16(header_.qdcount) &&
+ writer.WriteU16(header_.ancount) &&
+ writer.WriteU16(header_.nscount) &&
+ writer.WriteU16(header_.arcount) &&
+ writer.WriteBytes(responses_.data(), responses_.size());
+ DCHECK(success);
+ DCHECK_EQ(writer.remaining(), 0); // For warranty of correct size allocation.
+
+ return message;
+}
+
+void DnsResponseBuilder::AddResponse(const std::string& name, uint16 type,
+ uint32 ttl, const std::string& rdata) {
gene 2013/06/19 05:40:51 It looks through the code, it might be better to p
maksymb 2013/06/19 21:47:21 Done. But still I store rdata as std::vector<uint8
+ ++header_.ancount;
+
+ std::string name_in_dns_format;
+ bool success = net::DNSDomainFromDot(name, &name_in_dns_format);
+ DCHECK(success);
+
+ std::vector<uint8> current_respond(name_in_dns_format.size() + sizeof(type) +
+ sizeof(klass) + sizeof(ttl) +
+ 2 + // sizeof(RDLENGTH)
+ rdata.size());
+
+ net::BigEndianWriter writer(current_respond.data(), current_respond.size());
+ success = writer.WriteBytes(name_in_dns_format.data(),
+ name_in_dns_format.size()) &&
+ writer.WriteU16(type) &&
+ writer.WriteU16(klass) &&
+ writer.WriteU32(ttl) &&
+ writer.WriteU16(static_cast<uint16>(rdata.size())) &&
+ writer.WriteBytes(rdata.data(), rdata.size());
+ DCHECK(success);
+ DCHECK_EQ(writer.remaining(), 0); // For warranty of correct size allocation.
+
+ // Append current respond to all responds.
+ responses_.insert(responses_.end(), current_respond.begin(),
+ current_respond.end());
+}
+

Powered by Google App Engine
This is Rietveld 408576698