diff options
author | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-12 10:59:46 +0000 |
---|---|---|
committer | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-12 10:59:46 +0000 |
commit | 0d6b4faaa5e99c53534818df933fdc3523242e17 (patch) | |
tree | 33f812fcc45ded5b3107faa19bc328fda4c1d0b3 /cloud_print | |
parent | 1f0803412c2cfd7cf96ad9f0b857ff9cbcfb2905 (diff) | |
download | chromium_src-0d6b4faaa5e99c53534818df933fdc3523242e17.zip chromium_src-0d6b4faaa5e99c53534818df933fdc3523242e17.tar.gz chromium_src-0d6b4faaa5e99c53534818df933fdc3523242e17.tar.bz2 |
Added --extended-response to put additional information into PTR responces.
Shortened service name.
Added AAAA response.
Review URL: https://codereview.chromium.org/131103028
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@250673 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'cloud_print')
-rw-r--r-- | cloud_print/gcp20/prototype/dns_response_builder.cc | 68 | ||||
-rw-r--r-- | cloud_print/gcp20/prototype/dns_response_builder.h | 40 | ||||
-rw-r--r-- | cloud_print/gcp20/prototype/dns_sd_server.cc | 49 | ||||
-rw-r--r-- | cloud_print/gcp20/prototype/gcp20_device.cc | 1 | ||||
-rw-r--r-- | cloud_print/gcp20/prototype/printer.cc | 10 | ||||
-rw-r--r-- | cloud_print/gcp20/prototype/service_parameters.cc | 2 | ||||
-rw-r--r-- | cloud_print/gcp20/prototype/service_parameters.h | 2 |
7 files changed, 128 insertions, 44 deletions
diff --git a/cloud_print/gcp20/prototype/dns_response_builder.cc b/cloud_print/gcp20/prototype/dns_response_builder.cc index c5ad602..23cc866 100644 --- a/cloud_print/gcp20/prototype/dns_response_builder.cc +++ b/cloud_print/gcp20/prototype/dns_response_builder.cc @@ -37,19 +37,24 @@ DnsResponseBuilder::DnsResponseBuilder(uint16 id) { DnsResponseBuilder::~DnsResponseBuilder() { } -void DnsResponseBuilder::AppendPtr(const std::string& service_type, uint32 ttl, - const std::string& service_name) { +void DnsResponseBuilder::AppendPtr(const std::string& service_type, + uint32 ttl, + const std::string& service_name, + bool answer) { std::string rdata; bool success = net::DNSDomainFromDot(service_name, &rdata); DCHECK(success); - AddResponse(service_type, net::dns_protocol::kTypePTR, ttl, rdata); + AddResponse(service_type, net::dns_protocol::kTypePTR, ttl, rdata, answer); } -void DnsResponseBuilder::AppendSrv(const std::string& service_name, uint32 ttl, - uint16 priority, uint16 weight, +void DnsResponseBuilder::AppendSrv(const std::string& service_name, + uint32 ttl, + uint16 priority, + uint16 weight, uint16 http_port, - const std::string& service_domain_name) { + const std::string& service_domain_name, + bool answer) { std::string domain_name; bool success = net::DNSDomainFromDot(service_domain_name, &domain_name); DCHECK(success); @@ -65,11 +70,13 @@ void DnsResponseBuilder::AppendSrv(const std::string& service_name, uint32 ttl, DCHECK_EQ(writer.remaining(), 0); // For warranty of correct size allocation. AddResponse(service_name, net::dns_protocol::kTypeSRV, ttl, - std::string(rdata.begin(), rdata.end())); + std::string(rdata.begin(), rdata.end()), answer); } void DnsResponseBuilder::AppendA(const std::string& service_domain_name, - uint32 ttl, net::IPAddressNumber http_ipv4) { + uint32 ttl, + net::IPAddressNumber http_ipv4, + bool answer) { // TODO(maksymb): IP to send must depends on interface from where query was // received. if (http_ipv4.empty()) { @@ -78,11 +85,28 @@ void DnsResponseBuilder::AppendA(const std::string& service_domain_name, } AddResponse(service_domain_name, net::dns_protocol::kTypeA, ttl, - std::string(http_ipv4.begin(), http_ipv4.end())); + std::string(http_ipv4.begin(), http_ipv4.end()), answer); } -void DnsResponseBuilder::AppendTxt(const std::string& service_name, uint32 ttl, - const std::vector<std::string>& metadata) { +void DnsResponseBuilder::AppendAAAA(const std::string& service_domain_name, + uint32 ttl, + net::IPAddressNumber http_ipv6, + bool answer) { + // TODO(maksymb): IP to send must depends on interface from where query was + // received. + if (http_ipv6.empty()) { + LOG(ERROR) << "Invalid IP"; + return; + } + + AddResponse(service_domain_name, net::dns_protocol::kTypeAAAA, ttl, + std::string(http_ipv6.begin(), http_ipv6.end()), answer); +} + +void DnsResponseBuilder::AppendTxt(const std::string& service_name, + uint32 ttl, + const std::vector<std::string>& metadata, + bool answer) { std::string rdata; for (std::vector<std::string>::const_iterator str = metadata.begin(); str != metadata.end(); ++str) { @@ -92,7 +116,7 @@ void DnsResponseBuilder::AppendTxt(const std::string& service_name, uint32 ttl, rdata += *str; } - AddResponse(service_name, net::dns_protocol::kTypeTXT, ttl, rdata); + AddResponse(service_name, net::dns_protocol::kTypeTXT, ttl, rdata, answer); } scoped_refptr<net::IOBufferWithSize> DnsResponseBuilder::Build() { @@ -108,8 +132,8 @@ scoped_refptr<net::IOBufferWithSize> DnsResponseBuilder::Build() { if (responses_.empty()) return NULL; // No answer. - header_.ancount = static_cast<uint16>(responses_.size()); - + DCHECK_EQ(static_cast<size_t>(header_.ancount + header_.arcount), + responses_.size()); scoped_refptr<net::IOBufferWithSize> message( new net::IOBufferWithSize(static_cast<int>(size))); net::BigEndianWriter writer(message->data(), message->size()); @@ -143,14 +167,24 @@ scoped_refptr<net::IOBufferWithSize> DnsResponseBuilder::Build() { return message; } -void DnsResponseBuilder::AddResponse(const std::string& name, uint16 type, - uint32 ttl, const std::string& rdata) { +void DnsResponseBuilder::AddResponse(const std::string& name, + uint16 type, + uint32 ttl, + const std::string& rdata, + bool answer) { DnsResponseRecord response; response.name = name; response.klass = klass; response.ttl = ttl; response.type = type; response.rdata = rdata; - responses_.push_back(response); + + if (answer) { + responses_.insert(responses_.begin() + header_.ancount, response); + ++header_.ancount; + } else { + responses_.push_back(response); + ++header_.arcount; + } } diff --git a/cloud_print/gcp20/prototype/dns_response_builder.h b/cloud_print/gcp20/prototype/dns_response_builder.h index 0d4a3eb..b574ddf 100644 --- a/cloud_print/gcp20/prototype/dns_response_builder.h +++ b/cloud_print/gcp20/prototype/dns_response_builder.h @@ -40,23 +40,43 @@ class DnsResponseBuilder { ~DnsResponseBuilder(); // Methods for appending different types of responses to packet. - void AppendPtr(const std::string& service_type, uint32 ttl, - const std::string& service_name); - void AppendSrv(const std::string& service_name, uint32 ttl, uint16 priority, + void AppendPtr(const std::string& service_type, + uint32 ttl, + const std::string& service_name, + bool answer); + + void AppendSrv(const std::string& service_name, + uint32 ttl, + uint16 priority, uint16 weight, uint16 http_port, - const std::string& service_domain_name); - void AppendA(const std::string& service_domain_name, uint32 ttl, - net::IPAddressNumber http_ipv4); - void AppendTxt(const std::string& service_name, uint32 ttl, - const std::vector<std::string>& metadata); + const std::string& service_domain_name, + bool answer); + + void AppendA(const std::string& service_domain_name, + uint32 ttl, + net::IPAddressNumber http_ipv4, + bool answer); + + void AppendAAAA(const std::string& service_domain_name, + uint32 ttl, + net::IPAddressNumber http_ipv6, + bool answer); + + void AppendTxt(const std::string& service_name, + uint32 ttl, + const std::vector<std::string>& metadata, + bool answer); // Serializes packet to byte sequence. scoped_refptr<net::IOBufferWithSize> Build(); private: // Appends response to packet. - void AddResponse(const std::string& name, uint16 type, uint32 ttl, - const std::string& rdata); + void AddResponse(const std::string& name, + uint16 type, + uint32 ttl, + const std::string& rdata, + bool answer); std::vector<DnsResponseRecord> responses_; diff --git a/cloud_print/gcp20/prototype/dns_sd_server.cc b/cloud_print/gcp20/prototype/dns_sd_server.cc index 85b3f87..f5500de 100644 --- a/cloud_print/gcp20/prototype/dns_sd_server.cc +++ b/cloud_print/gcp20/prototype/dns_sd_server.cc @@ -101,7 +101,7 @@ void DnsSdServer::UpdateMetadata(const std::vector<std::string>& metadata) { if (!CommandLine::ForCurrentProcess()->HasSwitch("no-announcement")) { DnsResponseBuilder builder(current_ttl); - builder.AppendTxt(serv_params_.service_name_, current_ttl, metadata_); + builder.AppendTxt(serv_params_.service_name_, current_ttl, metadata_, true); scoped_refptr<net::IOBufferWithSize> buffer(builder.Build()); DCHECK(buffer.get() != NULL); @@ -203,7 +203,20 @@ void DnsSdServer::ProccessQuery(uint32 current_ttl, const DnsQueryRecord& query, if (query.qname == serv_params_.service_type_ || query.qname == serv_params_.secondary_service_type_) { builder->AppendPtr(query.qname, current_ttl, - serv_params_.service_name_); + serv_params_.service_name_, true); + + if (CommandLine::ForCurrentProcess()->HasSwitch("extended-response")) { + builder->AppendSrv(serv_params_.service_name_, current_ttl, + kSrvPriority, kSrvWeight, serv_params_.http_port_, + serv_params_.service_domain_name_, false); + builder->AppendA(serv_params_.service_domain_name_, current_ttl, + serv_params_.http_ipv4_, false); + builder->AppendAAAA(serv_params_.service_domain_name_, current_ttl, + serv_params_.http_ipv6_, false); + builder->AppendTxt(serv_params_.service_name_, current_ttl, metadata_, + false); + } + responded = true; } @@ -213,7 +226,7 @@ void DnsSdServer::ProccessQuery(uint32 current_ttl, const DnsQueryRecord& query, if (query.qname == serv_params_.service_name_) { builder->AppendSrv(serv_params_.service_name_, current_ttl, kSrvPriority, kSrvWeight, serv_params_.http_port_, - serv_params_.service_domain_name_); + serv_params_.service_domain_name_, true); responded = true; } break; @@ -221,14 +234,23 @@ void DnsSdServer::ProccessQuery(uint32 current_ttl, const DnsQueryRecord& query, log = "Processing A query"; if (query.qname == serv_params_.service_domain_name_) { builder->AppendA(serv_params_.service_domain_name_, current_ttl, - serv_params_.http_ipv4_); + serv_params_.http_ipv4_, true); + responded = true; + } + break; + case net::dns_protocol::kTypeAAAA: + log = "Processing AAAA query"; + if (query.qname == serv_params_.service_domain_name_) { + builder->AppendAAAA(serv_params_.service_domain_name_, current_ttl, + serv_params_.http_ipv6_, true); responded = true; } break; case net::dns_protocol::kTypeTXT: log = "Processing TXT query"; if (query.qname == serv_params_.service_name_) { - builder->AppendTxt(serv_params_.service_name_, current_ttl, metadata_); + builder->AppendTxt(serv_params_.service_name_, current_ttl, metadata_, + true); responded = true; } break; @@ -261,15 +283,18 @@ void DnsSdServer::SendAnnouncement(uint32 ttl) { DnsResponseBuilder builder(ttl); builder.AppendPtr(serv_params_.service_type_, ttl, - serv_params_.service_name_); + serv_params_.service_name_, true); builder.AppendPtr(serv_params_.secondary_service_type_, ttl, - serv_params_.service_name_); - builder.AppendSrv(serv_params_.service_name_, ttl, kSrvPriority, kSrvWeight, - serv_params_.http_port_, - serv_params_.service_domain_name_); + serv_params_.service_name_, true); + builder.AppendSrv(serv_params_.service_name_, ttl, kSrvPriority, + kSrvWeight, serv_params_.http_port_, + serv_params_.service_domain_name_, true); builder.AppendA(serv_params_.service_domain_name_, ttl, - serv_params_.http_ipv4_); - builder.AppendTxt(serv_params_.service_name_, ttl, metadata_); + serv_params_.http_ipv4_, true); + builder.AppendAAAA(serv_params_.service_domain_name_, ttl, + serv_params_.http_ipv6_, true); + builder.AppendTxt(serv_params_.service_name_, ttl, metadata_, true); + scoped_refptr<net::IOBufferWithSize> buffer(builder.Build()); DCHECK(buffer.get() != NULL); diff --git a/cloud_print/gcp20/prototype/gcp20_device.cc b/cloud_print/gcp20/prototype/gcp20_device.cc index 74040bd0..2b38a9a 100644 --- a/cloud_print/gcp20/prototype/gcp20_device.cc +++ b/cloud_print/gcp20/prototype/gcp20_device.cc @@ -26,6 +26,7 @@ const char kHelpMessage[] = "HTTP header\n" " -h, --help prints this message\n" " --no-announcement disables DNS announcements\n" + " --extended-response responds to PTR with additional records\n" " --simulate-printing-errors simulates some errors for local printing\n" " --unicast-respond DNS responses will be sent in unicast " "instead of multicast\n" diff --git a/cloud_print/gcp20/prototype/printer.cc b/cloud_print/gcp20/prototype/printer.cc index 4765198..fdbdf87 100644 --- a/cloud_print/gcp20/prototype/printer.cc +++ b/cloud_print/gcp20/prototype/printer.cc @@ -38,7 +38,7 @@ const uint32 kTtlDefault = 60*60; // in seconds const char kServiceType[] = "_privet._tcp.local"; const char kSecondaryServiceType[] = "_printer._sub._privet._tcp.local"; -const char kServiceNamePrefixDefault[] = "first_gcp20_device"; +const char kServiceNamePrefixDefault[] = "gcp20_device_"; const char kServiceDomainNameFormatDefault[] = "my-privet-device%d.local"; const char kPrinterName[] = "Google GCP2.0 Prototype"; @@ -843,8 +843,8 @@ bool Printer::StartDnsServer() { uint16 port = command_line_reader::ReadHttpPort(kHttpPortDefault); std::string service_name_prefix = - command_line_reader::ReadServiceNamePrefix(net::IPAddressToString(ip) + - kServiceNamePrefixDefault); + command_line_reader::ReadServiceNamePrefix(kServiceNamePrefixDefault + + net::IPAddressToString(ip)); std::replace(service_name_prefix .begin(), service_name_prefix .end(), '.', '_'); @@ -854,8 +854,8 @@ bool Printer::StartDnsServer() { base::RandInt(0, INT_MAX))); ServiceParameters params(kServiceType, kSecondaryServiceType, - service_name_prefix, - service_domain_name, ip, port); + service_name_prefix, service_domain_name, + ip, GetLocalIp("", true), port); return dns_server_.Start(params, command_line_reader::ReadTtl(kTtlDefault), diff --git a/cloud_print/gcp20/prototype/service_parameters.cc b/cloud_print/gcp20/prototype/service_parameters.cc index 85ba83c..c15be13 100644 --- a/cloud_print/gcp20/prototype/service_parameters.cc +++ b/cloud_print/gcp20/prototype/service_parameters.cc @@ -15,11 +15,13 @@ ServiceParameters::ServiceParameters(const std::string& service_type, const std::string& service_name_prefix, const std::string& service_domain_name, const net::IPAddressNumber& http_ipv4, + const net::IPAddressNumber& http_ipv6, uint16 http_port) : service_type_(service_type), secondary_service_type_(secondary_service_type), service_name_(service_name_prefix + "." + service_type), service_domain_name_(service_domain_name), http_ipv4_(http_ipv4), + http_ipv6_(http_ipv6), http_port_(http_port) { } diff --git a/cloud_print/gcp20/prototype/service_parameters.h b/cloud_print/gcp20/prototype/service_parameters.h index 881e15f..b22b3dd 100644 --- a/cloud_print/gcp20/prototype/service_parameters.h +++ b/cloud_print/gcp20/prototype/service_parameters.h @@ -20,6 +20,7 @@ struct ServiceParameters { const std::string& service_name_prefix, const std::string& service_domain_name, const net::IPAddressNumber& http_ipv4, + const net::IPAddressNumber& http_ipv6, uint16 http_port); std::string service_type_; @@ -27,6 +28,7 @@ struct ServiceParameters { std::string service_name_; std::string service_domain_name_; net::IPAddressNumber http_ipv4_; + net::IPAddressNumber http_ipv6_; uint16 http_port_; }; |