Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cloud_print/gcp20/prototype/dns_sd_server.h" | |
| 6 | |
| 7 #include <string.h> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "net/base/big_endian.h" | |
| 11 #include "net/base/net_util.h" | |
| 12 #include "net/dns/dns_protocol.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const char* kDefaultIpAddressMulticast = "224.0.0.251"; | |
| 17 const uint16 kDefaultPortMulticast = 5353; | |
| 18 | |
| 19 // TODO(maksymb): Add possibility to set constants via command line arguments | |
| 20 const uint32 kDefaultTTL = 60*60; // in seconds | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 DnsSdServer::DnsSdServer(): is_online_(false) { | |
| 25 // Do nothing | |
| 26 } | |
| 27 | |
| 28 DnsSdServer::~DnsSdServer() { | |
| 29 Shutdown(); | |
| 30 } | |
| 31 | |
| 32 bool DnsSdServer::Start() { | |
| 33 if (is_online_) | |
| 34 return true; | |
| 35 | |
| 36 if (!CreateSocket()) | |
| 37 return false; | |
| 38 | |
| 39 LOG(INFO) << "DNS server started"; | |
| 40 | |
| 41 SendAnnouncement(kDefaultTTL); | |
| 42 | |
| 43 is_online_ = true; | |
| 44 return true; | |
| 45 } | |
| 46 | |
| 47 void DnsSdServer::Update() { | |
| 48 if (!is_online_) | |
| 49 return; | |
| 50 | |
| 51 SendAnnouncement(kDefaultTTL); | |
| 52 } | |
| 53 | |
| 54 void DnsSdServer::Shutdown() { | |
| 55 if (!is_online_) | |
| 56 return; | |
| 57 | |
| 58 SendAnnouncement(0); // ttl is 0 | |
| 59 socket_->Close(); | |
| 60 is_online_ = false; | |
| 61 LOG(INFO) << "DNS server stopped"; | |
| 62 } | |
| 63 | |
| 64 void DnsSdServer::ProcessMessages() { | |
| 65 NOTIMPLEMENTED(); // implement this | |
| 66 } | |
| 67 | |
| 68 bool DnsSdServer::CreateSocket() { | |
| 69 net::IPAddressNumber local_ip_any; | |
| 70 bool success = net::ParseIPLiteralToNumber("0.0.0.0", &local_ip_any); | |
| 71 DCHECK(success); | |
| 72 | |
| 73 net::IPAddressNumber multicast_dns_ip_address; | |
| 74 success = net::ParseIPLiteralToNumber(kDefaultIpAddressMulticast, | |
| 75 &multicast_dns_ip_address); | |
| 76 DCHECK(success); | |
| 77 | |
| 78 | |
| 79 socket_.reset(new net::UDPSocket(net::DatagramSocket::DEFAULT_BIND, | |
| 80 net::RandIntCallback(), | |
| 81 NULL, | |
| 82 net::NetLog::Source())); | |
| 83 | |
| 84 net::IPEndPoint local_address = net::IPEndPoint(local_ip_any, | |
| 85 kDefaultPortMulticast); | |
| 86 multicast_address_ = net::IPEndPoint(multicast_dns_ip_address, | |
| 87 kDefaultPortMulticast); | |
| 88 | |
| 89 socket_->AllowAddressReuse(); | |
| 90 | |
| 91 int status = socket_->Bind(local_address); | |
| 92 if (status < 0) | |
| 93 return false; | |
| 94 | |
| 95 socket_->SetMulticastLoopbackMode(false); | |
| 96 status = socket_->JoinGroup(multicast_dns_ip_address); | |
| 97 | |
| 98 if (status < 0) | |
| 99 return false; | |
| 100 | |
| 101 DCHECK(socket_->is_connected()); | |
| 102 | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 bool DnsSdServer::CheckPendingQueries() { | |
| 107 NOTIMPLEMENTED(); // implement this | |
| 108 return false; | |
| 109 } | |
| 110 | |
| 111 void DoNothing(int /*var*/) { | |
| 112 // Do nothing | |
| 113 } | |
| 114 | |
| 115 void DnsSdServer::SendAnnouncement(uint32 ttl) { | |
| 116 // Create a message with allocated space for header. | |
| 117 // DNS header is temporary empty. | |
| 118 scoped_ptr<std::vector<uint8> > message( | |
| 119 new std::vector<uint8>(sizeof(net::dns_protocol::Header), 0)); // all is 0 | |
| 120 | |
| 121 // TODO(maksymb): Create and implement DnsResponce class | |
|
gene
2013/06/06 02:41:20
DnsResponce -> DnsResponse
maksymb
2013/06/06 16:43:34
Done.
| |
| 122 | |
| 123 // Preparing for sending | |
| 124 scoped_refptr<net::IOBufferWithSize> buffer = | |
| 125 new net::IOBufferWithSize(message.get()->size()); | |
| 126 memcpy(buffer.get()->data(), message.get()->data(), message.get()->size()); | |
| 127 | |
| 128 // Create empty callback (we don't need it at all) and send packet | |
| 129 net::CompletionCallback callback = base::Bind(DoNothing); | |
| 130 socket_->SendTo(buffer.get(), | |
| 131 buffer.get()->size(), | |
| 132 multicast_address_, | |
| 133 callback); | |
| 134 | |
| 135 LOG(INFO) << "Announcement was sent with TTL: " << ttl; | |
| 136 } | |
| 137 | |
| OLD | NEW |