Chromium Code Reviews| Index: cloud_print/gcp20/prototype/dns_sd_server.cc |
| diff --git a/cloud_print/gcp20/prototype/dns_sd_server.cc b/cloud_print/gcp20/prototype/dns_sd_server.cc |
| index 0ca8b4395f508f3225067019951632252f9f338c..a228ecc5b52c5149324797ff5a2ce203f233bc4e 100644 |
| --- a/cloud_print/gcp20/prototype/dns_sd_server.cc |
| +++ b/cloud_print/gcp20/prototype/dns_sd_server.cc |
| @@ -7,7 +7,12 @@ |
| #include <string.h> |
| #include "base/basictypes.h" |
| +#include "base/command_line.h" |
| +#include "base/message_loop.h" |
| +#include "cloud_print/gcp20/prototype/dns_packet_parser.h" |
| #include "net/base/big_endian.h" |
| +#include "net/base/dns_util.h" |
| +#include "net/base/net_errors.h" |
| #include "net/base/net_util.h" |
| #include "net/dns/dns_protocol.h" |
| @@ -16,29 +21,49 @@ namespace { |
| const char* kDefaultIpAddressMulticast = "224.0.0.251"; |
| const uint16 kDefaultPortMulticast = 5353; |
| -// TODO(maksymb): Add possibility to set constants via command line arguments |
| -const uint32 kDefaultTTL = 60*60; // in seconds |
| +const double kTimeToNextAnnouncement = 0.8; // relatively to TTL |
| +const int kDnsBufSize = 65537; |
| + |
| +void DoNothingAfterSendToSocket(int /*val*/) { |
| + // TODO(maksymb): This function is not reached and never used. It is callback |
|
gene
2013/06/15 02:13:23
If this should never be reached, use NOTREACHED()
maksymb
2013/06/18 01:14:48
Done.
|
| + // for SendTo() method of UDPSocket. It SHOULD be reached if used. Understand |
| + // why we don't use it but still have |DCHECK(!write_callabck_.is_null())| in |
| + // UDPSocket implementation. |
| +} |
| } // namespace |
| -DnsSdServer::DnsSdServer() : is_online_(false) { |
| - // Do nothing |
| +DnsSdServer::DnsSdServer() |
| + : is_online_(false), |
| + recv_buf_(new net::IOBufferWithSize(kDnsBufSize)) { |
| } |
| DnsSdServer::~DnsSdServer() { |
| Shutdown(); |
| + |
| + delete response_builder_factory_; |
|
gene
2013/06/15 02:13:23
This may crash :), if I construct and delete the i
maksymb
2013/06/18 01:14:48
Factory was deleted.
|
| } |
| -bool DnsSdServer::Start() { |
| +bool DnsSdServer::Start( |
| + DnsResponseBuilderFactoryInterface* response_builder_factory, |
| + uint32 full_ttl) { |
| if (is_online_) |
| return true; |
| if (!CreateSocket()) |
| return false; |
| + // Initializing server with parameters from arguments. |
| + response_builder_factory_ = response_builder_factory; |
| + full_ttl_ = full_ttl; |
| + |
| LOG(INFO) << "DNS server started"; |
| + LOG(WARNING) << "DNS server does not support probing"; |
| - SendAnnouncement(kDefaultTTL); |
| + SendAnnouncement(full_ttl_); |
| + base::MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&DnsSdServer::OnDatagramReceived, base::Unretained(this))); |
| is_online_ = true; |
| return true; |
| @@ -48,23 +73,19 @@ void DnsSdServer::Update() { |
| if (!is_online_) |
| return; |
| - SendAnnouncement(kDefaultTTL); |
| + SendAnnouncement(full_ttl_); |
| } |
| void DnsSdServer::Shutdown() { |
| if (!is_online_) |
| return; |
| - SendAnnouncement(0); // ttl is 0 |
| + SendAnnouncement(0); // TTL is 0 |
| socket_->Close(); |
| is_online_ = false; |
| LOG(INFO) << "DNS server stopped"; |
| } |
| -void DnsSdServer::ProcessMessages() { |
| - NOTIMPLEMENTED(); // implement this |
| -} |
| - |
| bool DnsSdServer::CreateSocket() { |
| net::IPAddressNumber local_ip_any; |
| bool success = net::ParseIPLiteralToNumber("0.0.0.0", &local_ip_any); |
| @@ -103,35 +124,150 @@ bool DnsSdServer::CreateSocket() { |
| return true; |
| } |
| -bool DnsSdServer::CheckPendingQueries() { |
| - NOTIMPLEMENTED(); // implement this |
| - return false; |
| -} |
| +void DnsSdServer::ProcessMessage(int len, net::IOBufferWithSize* buf) { |
| + VLOG(1) << "Received new message with length: " << len; |
| -void DoNothing(int /*var*/) { |
| - // Do nothing |
| -} |
| + // Parse the message |
| + DnsPacketParser parser(buf->data(), |
|
gene
2013/06/15 02:13:23
suggestion: since DnsPacketParser understands the
maksymb
2013/06/18 01:14:48
Done.
|
| + len, |
| + sizeof(net::dns_protocol::Header)); |
| -void DnsSdServer::SendAnnouncement(uint32 ttl) { |
| - // Create a message with allocated space for header. |
| - // DNS header is temporary empty. |
| - scoped_ptr<std::vector<uint8> > message( |
| - new std::vector<uint8>(sizeof(net::dns_protocol::Header), 0)); // all is 0 |
| + net::dns_protocol::Header header; |
| + net::BigEndianReader reader(buf->data(), len); |
| + bool success = reader.ReadU16(&header.id) && |
|
gene
2013/06/15 02:13:23
I am probably missing something. Where are you che
maksymb
2013/06/18 01:14:48
Done.
|
| + reader.ReadU16(&header.flags) && |
| + reader.ReadU16(&header.qdcount) && |
| + reader.ReadU16(&header.ancount) && |
| + reader.ReadU16(&header.nscount) && |
| + reader.ReadU16(&header.arcount); |
| - // TODO(maksymb): Create and implement DnsResponse class |
| + if (!success) |
| + return; |
| - // Preparing for sending |
| - scoped_refptr<net::IOBufferWithSize> buffer = |
| - new net::IOBufferWithSize(static_cast<int>(message.get()->size())); |
| - memcpy(buffer.get()->data(), message.get()->data(), message.get()->size()); |
| + scoped_refptr<net::IOBufferWithSize> buffer( |
| + CreateResponsePacket(header, &parser)); |
| - // Create empty callback (we don't need it at all) and send packet |
| - net::CompletionCallback callback = base::Bind(DoNothing); |
| + if (buffer.get() == NULL) |
| + return; // No answers. |
| + |
| + bool multicast_respond = |
| + CommandLine::ForCurrentProcess()->HasSwitch("multicast-respond"); |
| socket_->SendTo(buffer.get(), |
| buffer.get()->size(), |
| - multicast_address_, |
| - callback); |
| + multicast_respond ? multicast_address_ : recv_address_, |
| + base::Bind(&DoNothingAfterSendToSocket)); |
| + VLOG(1) << "Responded to " |
| + << (multicast_respond ? multicast_address_ : recv_address_).ToString(); |
| +} |
| - LOG(INFO) << "Announcement was sent with TTL: " << ttl; |
| +scoped_refptr<net::IOBufferWithSize> DnsSdServer::CreateResponsePacket( |
| + const net::dns_protocol::Header& header, |
| + DnsPacketParser* parser) const { |
| + scoped_ptr<DnsResponseBuilderInterface> builder( |
| + response_builder_factory_->Create(header.id)); |
| + |
| + // TODO(maksymb): Handle truncated messages. |
| + |
| + uint32 current_ttl; |
| + if (time_until_live_ < base::Time::Now()) { |
| + // This should not be reachable. But still we don't need to fail. |
| + current_ttl = 0; |
|
gene
2013/06/15 02:13:23
That is usually a bad idea (client will treat this
maksymb
2013/06/18 01:14:48
Done.
|
| + LOG(ERROR) << "|current_ttl| equals to zero."; |
| + } else { |
| + current_ttl = (time_until_live_ - base::Time::Now()).InSeconds(); |
| + } |
| + |
| + DnsQueryRecord query; |
| + // TODO(maksymb): Check known answers. |
| + for (int query_idx = 0; query_idx < header.qdcount; ++query_idx) { |
| + bool success = parser->ReadRecord(&query); |
| + if (success) { |
|
gene
2013/06/15 02:13:23
This whole if() is asking to go to a different fun
maksymb
2013/06/18 01:14:48
Done.
|
| + std::string log; |
| + bool responded = false; |
| + switch (query.qtype) { |
| + // TODO(maksymb): Add IPv6 support. |
| + case net::dns_protocol::kTypePTR: |
| + log = "Processing PTR query"; |
| + if ((responded = builder->IsSamePtr(query.qname))) |
|
gene
2013/06/15 02:13:23
Usually, it is property of the DnsServer to serve
maksymb
2013/06/18 01:14:48
Done.
|
| + builder->AppendPtr(current_ttl); |
| + break; |
| + case net::dns_protocol::kTypeSRV: |
| + log = "Processing SRV query"; |
| + if ((responded = builder->IsSameSrv(query.qname))) |
| + builder->AppendSrv(current_ttl); |
| + break; |
| + case net::dns_protocol::kTypeA: |
| + log = "Processing A query"; |
| + if ((responded = builder->IsSameA(query.qname))) |
| + builder->AppendA(current_ttl); |
| + break; |
| + case net::dns_protocol::kTypeTXT: |
| + log = "Processing TXT query"; |
| + if ((responded = builder->IsSameTxt(query.qname))) |
| + builder->AppendTxt(current_ttl); |
| + break; |
| + default: |
| + log = "Unknown query type"; |
|
gene
2013/06/15 02:13:23
if you want this log, please add query type as num
maksymb
2013/06/18 01:14:48
Done.
|
| + } |
| + log += responded ? ": responded" : ": ignored"; |
| + VLOG(1) << log; |
| + } else { // if (success) |
| + LOG(INFO) << "Broken package"; |
| + break; |
| + } |
| + } |
| + |
| + if (builder->HaveAnswers()) { |
| + VLOG(1) << "Current TTL for respond: " << current_ttl; |
| + return builder->Build(); |
|
gene
2013/06/15 02:13:23
Builder may return an empty buffer if it has no an
maksymb
2013/06/18 01:14:48
Done.
|
| + } |
| + return NULL; |
| +} |
| + |
| +void DnsSdServer::DoLoop(int rv) { |
| + // TODO(maksymb): Check what happened if buffer will be overflowed |
| + do { |
| + if (rv > 0) |
| + ProcessMessage(rv, recv_buf_); |
| + rv = socket_->RecvFrom(recv_buf_, |
| + recv_buf_->size(), |
| + &recv_address_, |
| + base::Bind(&DnsSdServer::DoLoop, |
| + base::Unretained(this))); |
| + } while (rv > 0); |
| + |
| + // TODO(maksymb): Add handler for errors |
| + DCHECK(rv == net::ERR_IO_PENDING); |
| +} |
| + |
| +void DnsSdServer::OnDatagramReceived() { |
| + DoLoop(0); |
| +} |
| + |
| +void DnsSdServer::SendAnnouncement(uint32 ttl) { |
| + if (!CommandLine::ForCurrentProcess()->HasSwitch("no-announcement")) { |
| + scoped_ptr<DnsResponseBuilderInterface> builder( |
| + response_builder_factory_->Create(0)); |
| + scoped_refptr<net::IOBufferWithSize> buffer( |
| + builder->BuildAnnouncement(ttl)); |
| + |
| + socket_->SendTo(buffer.get(), |
| + buffer.get()->size(), |
| + multicast_address_, |
| + base::Bind(&DoNothingAfterSendToSocket)); |
| + |
| + VLOG(1) << "Announcement was sent with TTL: " << ttl; |
| + } |
| + |
| + time_until_live_ = base::Time::Now() + |
| + base::TimeDelta::FromSeconds(full_ttl_); |
| + |
| + // Schedule next announcement. |
| + |
| + base::MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&DnsSdServer::Update, base::Unretained(this)), |
| + base::TimeDelta::FromSeconds(static_cast<int64>( |
| + kTimeToNextAnnouncement*full_ttl_))); |
| } |