summaryrefslogtreecommitdiffstats
path: root/cloud_print/gcp20/prototype/dns_sd_server.h
blob: f9aa540d737353f5bb77b141161667c84b6fc778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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.

#ifndef CLOUD_PRINT_GCP20_PROTOTYPE_DNS_SD_SERVER_H_
#define CLOUD_PRINT_GCP20_PROTOTYPE_DNS_SD_SERVER_H_

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "cloud_print/gcp20/prototype/service_parameters.h"
#include "net/base/ip_endpoint.h"
#include "net/udp/udp_socket.h"

namespace net {

class IOBufferWithSize;

}  // namespace net

struct DnsQueryRecord;
class DnsResponseBuilder;

// Class for sending multicast announcements, receiving queries and answering on
// them.
// TODO(maksymb): Implement probing.
class DnsSdServer : public base::SupportsWeakPtr<DnsSdServer> {
 public:
  // Constructor does not start server.
  DnsSdServer();

  // Stops the server and destroys the object.
  ~DnsSdServer();

  // Starts the server. Returns |true| if server works. Also sends
  // announcement.
  bool Start(const ServiceParameters& serv_params,
             uint32 full_ttl,
             const std::vector<std::string>& metadata) WARN_UNUSED_RESULT;

  // Sends announcement if server works.
  void Update();

  // Stops server with announcement.
  void Shutdown();

  // Returns |true| if server works.
  bool IsOnline() { return !!socket_; }

  // Updates data for TXT respond.
  void UpdateMetadata(const std::vector<std::string>& metadata);

 private:
  // Binds a socket to multicast address. Returns |true| on success.
  bool CreateSocket();

  // Processes single query.
  void ProccessQuery(uint32 current_ttl,
                     const DnsQueryRecord& query,
                     DnsResponseBuilder* builder) const;

  // Processes DNS message.
  void ProcessMessage(int len, net::IOBufferWithSize* buf);

  // CompletionCallback for receiving data from DNS.
  void DoLoop(int rv);

  // Function to start listening to socket (delegate to DoLoop function).
  void OnDatagramReceived();

  // Sends announcement.
  void SendAnnouncement(uint32 ttl);

  // Calculates and returns current TTL (with accordance to last send
  // announcement time.
  uint32 GetCurrentTLL() const;

  // Stores socket to multicast address.
  scoped_ptr<net::UDPSocket> socket_;

  // Stores multicast address end point.
  net::IPEndPoint multicast_address_;

  // Stores time until last announcement is live.
  base::Time time_until_live_;

  // Stores service parameters (like service-name and service-type etc.)
  ServiceParameters serv_params_;

  // Stores the buffer for receiving messages.
  scoped_refptr<net::IOBufferWithSize> recv_buf_;

  // Stores address from where last message was sent.
  net::IPEndPoint recv_address_;

  // Stores information for TXT respond.
  std::vector<std::string> metadata_;

  // TTL for announcements
  uint32 full_ttl_;

  DISALLOW_COPY_AND_ASSIGN(DnsSdServer);
};

#endif  // CLOUD_PRINT_GCP20_PROTOTYPE_DNS_SD_SERVER_H_