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#ifndef CLOUD_PRINT_GCP20_PROTOTYPE_DNS_SD_SERVER_H_ 6#define CLOUD_PRINT_GCP20_PROTOTYPE_DNS_SD_SERVER_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/basictypes.h" 12#include "base/memory/scoped_ptr.h" 13#include "base/memory/weak_ptr.h" 14#include "cloud_print/gcp20/prototype/service_parameters.h" 15#include "net/base/ip_endpoint.h" 16#include "net/udp/udp_socket.h" 17 18namespace net { 19 20class IOBufferWithSize; 21 22} // namespace net 23 24struct DnsQueryRecord; 25class DnsResponseBuilder; 26 27// Class for sending multicast announcements, receiving queries and answering on 28// them. 29// TODO(maksymb): Implement probing. 30class DnsSdServer : public base::SupportsWeakPtr<DnsSdServer> { 31 public: 32 // Constructor does not start server. 33 DnsSdServer(); 34 35 // Stops the server and destroys the object. 36 ~DnsSdServer(); 37 38 // Starts the server. Returns |true| if server works. Also sends 39 // announcement. 40 bool Start(const ServiceParameters& serv_params, 41 uint32 full_ttl, 42 const std::vector<std::string>& metadata) WARN_UNUSED_RESULT; 43 44 // Sends announcement if server works. 45 void Update(); 46 47 // Stops server with announcement. 48 void Shutdown(); 49 50 // Returns |true| if server works. 51 bool IsOnline() { return !!socket_; } 52 53 // Updates data for TXT respond. 54 void UpdateMetadata(const std::vector<std::string>& metadata); 55 56 private: 57 // Binds a socket to multicast address. Returns |true| on success. 58 bool CreateSocket(); 59 60 // Processes single query. 61 void ProccessQuery(uint32 current_ttl, 62 const DnsQueryRecord& query, 63 DnsResponseBuilder* builder) const; 64 65 // Processes DNS message. 66 void ProcessMessage(int len, net::IOBufferWithSize* buf); 67 68 // CompletionCallback for receiving data from DNS. 69 void DoLoop(int rv); 70 71 // Function to start listening to socket (delegate to DoLoop function). 72 void OnDatagramReceived(); 73 74 // Sends announcement. 75 void SendAnnouncement(uint32 ttl); 76 77 // Calculates and returns current TTL (with accordance to last send 78 // announcement time. 79 uint32 GetCurrentTLL() const; 80 81 // Stores socket to multicast address. 82 scoped_ptr<net::UDPSocket> socket_; 83 84 // Stores multicast address end point. 85 net::IPEndPoint multicast_address_; 86 87 // Stores time until last announcement is live. 88 base::Time time_until_live_; 89 90 // Stores service parameters (like service-name and service-type etc.) 91 ServiceParameters serv_params_; 92 93 // Stores the buffer for receiving messages. 94 scoped_refptr<net::IOBufferWithSize> recv_buf_; 95 96 // Stores address from where last message was sent. 97 net::IPEndPoint recv_address_; 98 99 // Stores information for TXT respond. 100 std::vector<std::string> metadata_; 101 102 // TTL for announcements 103 uint32 full_ttl_; 104 105 DISALLOW_COPY_AND_ASSIGN(DnsSdServer); 106}; 107 108#endif // CLOUD_PRINT_GCP20_PROTOTYPE_DNS_SD_SERVER_H_ 109 110