• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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 DISCOVERY_DNSSD_PUBLIC_DNS_SD_INSTANCE_H_
6 #define DISCOVERY_DNSSD_PUBLIC_DNS_SD_INSTANCE_H_
7 
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 #include "discovery/dnssd/public/dns_sd_txt_record.h"
13 #include "platform/base/ip_address.h"
14 #include "util/std_util.h"
15 
16 namespace openscreen {
17 namespace discovery {
18 
19 bool IsInstanceValid(const std::string& instance);
20 bool IsServiceValid(const std::string& service);
21 bool IsDomainValid(const std::string& domain);
22 
23 // Represents the data stored in DNS records of types SRV, TXT, A, and AAAA
24 class DnsSdInstance {
25  public:
26   using Subtype = std::string;
27 
28   // These ctors expect valid input, and will cause a crash if they are not.
29   DnsSdInstance(std::string instance_id,
30                 std::string service_id,
31                 std::string domain_id,
32                 DnsSdTxtRecord txt,
33                 uint16_t port,
34                 std::vector<Subtype> subtypes);
35 
36   template <typename... T>
DnsSdInstance(std::string instance_id,std::string service_id,std::string domain_id,DnsSdTxtRecord txt,uint16_t port,T...subtypes)37   DnsSdInstance(std::string instance_id,
38                 std::string service_id,
39                 std::string domain_id,
40                 DnsSdTxtRecord txt,
41                 uint16_t port,
42                 T... subtypes)
43       : DnsSdInstance(std::move(instance_id),
44                       std::move(service_id),
45                       std::move(domain_id),
46                       std::move(txt),
47                       port,
48                       std::vector<Subtype>{std::move(subtypes)...}) {}
49 
50   DnsSdInstance(const DnsSdInstance& other);
51   DnsSdInstance(DnsSdInstance&& other);
52 
53   virtual ~DnsSdInstance();
54 
55   DnsSdInstance& operator=(const DnsSdInstance& rhs);
56   DnsSdInstance& operator=(DnsSdInstance&& rhs);
57 
58   // Returns the instance name for this DNS-SD record.
instance_id()59   const std::string& instance_id() const { return instance_id_; }
60 
61   // Returns the service id for this DNS-SD record.
service_id()62   const std::string& service_id() const { return service_id_; }
63 
64   // Returns the domain id for this DNS-SD record.
domain_id()65   const std::string& domain_id() const { return domain_id_; }
66 
67   // Returns the TXT record associated with this DNS-SD record
txt()68   const DnsSdTxtRecord& txt() const { return txt_; }
69 
70   // Returns the port associated with this instance record.
port()71   uint16_t port() const { return port_; }
72 
73   // The set of subtypes for this instance.
subtypes()74   const std::vector<Subtype>& subtypes() { return subtypes_; }
75 
76  private:
77   std::string instance_id_;
78   std::string service_id_;
79   std::string domain_id_;
80   DnsSdTxtRecord txt_;
81   uint16_t port_;
82 
83   // Subtypes of this instance which have been received so far.
84   // NOTE: Subtypes are stored in sorted order to simplify comparison.
85   // NOTE: This vector will always be empty for incoming queries and will not be
86   // respected for publications. It is only present for future use.
87   //
88   // TODO(issuetracker.google.com/158533407): Implement use of this field.
89   std::vector<Subtype> subtypes_;
90 
91   friend bool operator<(const DnsSdInstance& lhs, const DnsSdInstance& rhs);
92 };
93 
94 bool IsSubtypeValid(const DnsSdInstance::Subtype& subtype);
95 
96 bool IsValid(const DnsSdInstance::Subtype& subtype);
97 
98 bool operator<(const DnsSdInstance& lhs, const DnsSdInstance& rhs);
99 
100 inline bool operator>(const DnsSdInstance& lhs, const DnsSdInstance& rhs) {
101   return rhs < lhs;
102 }
103 
104 inline bool operator<=(const DnsSdInstance& lhs, const DnsSdInstance& rhs) {
105   return !(lhs > rhs);
106 }
107 
108 inline bool operator>=(const DnsSdInstance& lhs, const DnsSdInstance& rhs) {
109   return !(lhs < rhs);
110 }
111 
112 inline bool operator==(const DnsSdInstance& lhs, const DnsSdInstance& rhs) {
113   return lhs <= rhs && lhs >= rhs;
114 }
115 
116 inline bool operator!=(const DnsSdInstance& lhs, const DnsSdInstance& rhs) {
117   return !(lhs == rhs);
118 }
119 
120 }  // namespace discovery
121 }  // namespace openscreen
122 
123 #endif  // DISCOVERY_DNSSD_PUBLIC_DNS_SD_INSTANCE_H_
124