Resource.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Resource.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "Name.h"
14 #include <IpAddress.h>
15 
16 struct Ip6Address {
17  uint8_t addr[16];
18 };
19 
25 #define MDNS_RESOURCE_TYPE_MAP(XX) \
26  XX(A, 0x0001, "32-bit IPv4 address") \
27  XX(SOA, 0x0006, "Authoritative DNS Zone information") \
28  XX(PTR, 0x000C, "Pointer to a canonical name") \
29  XX(HINFO, 0x000D, "Host Information") \
30  XX(TXT, 0x0010, "Arbitrary human-readable text") \
31  XX(AAAA, 0x001C, "128-bit IPv6 address") \
32  XX(SRV, 0x0021, "Server selection") \
33  XX(ANY, 0x00FF, "Matches any resource type in query")
34 
35 namespace mDNS
36 {
37 class Answer;
38 
39 namespace Resource
40 {
41 enum class Type : uint16_t {
42 #define XX(name, value, desc) name = value,
44 #undef XX
45 };
46 
50 class Record
51 {
52 public:
53  Record(const Answer& answer) : answer(const_cast<Answer&>(answer))
54  {
55  }
56 
57  String toString() const;
58 
60 
61 protected:
62  uint8_t* getRecord() const;
63  uint16_t getRecordSize() const;
64 };
65 
69 class A : public Record
70 {
71 public:
72  static constexpr Resource::Type type{Resource::Type::A};
73 
74  using Record::Record;
75 
77 
78  String toString() const
79  {
80  return getAddress().toString();
81  }
82 
83  // Writing
84  void init(IpAddress ipaddr);
85 };
86 
90 class PTR : public Record
91 {
92 public:
93  static constexpr Resource::Type type{Resource::Type::PTR};
94 
95  using Record::Record;
96 
97  Name getName() const;
98 
99  String toString() const
100  {
101  return getName();
102  }
103 
104  // Writing
105  void init(const String& name);
106 };
107 
111 class HINFO : public Record
112 {
113 public:
114  static constexpr Resource::Type type{Resource::Type::HINFO};
115 
116  using Record::Record;
117 };
118 
125 class TXT : public Record
126 {
127 public:
128  static constexpr Resource::Type type{Resource::Type::TXT};
129 
130  using Record::Record;
131 
132  uint8_t count() const;
133 
134  String operator[](uint8_t index) const;
135 
136  String operator[](const char* name) const
137  {
138  return getValue(name);
139  }
140 
141  String operator[](const String& name) const
142  {
143  return getValue(name.c_str());
144  }
145 
146  String toString(const String& sep = "; ") const;
147 
148  String getValue(const char* name, uint16_t namelen) const;
149 
150  String getValue(const char* name) const
151  {
152  return getValue(name, strlen(name));
153  }
154 
155  String getValue(const String& name) const
156  {
157  return getValue(name.c_str(), name.length());
158  }
159 
160  // Writing
161  void init()
162  {
163  }
164 
165  void add(const char* value, uint16_t len);
166 
167  void add(const String& value)
168  {
169  add(value.c_str(), value.length());
170  }
171 
172  TXT& operator+=(const char* value)
173  {
174  add(value, strlen(value));
175  return *this;
176  }
177 
178  TXT& operator+=(const String& value)
179  {
180  add(value);
181  return *this;
182  }
183 
184 private:
185  const char* get(uint8_t index, uint8_t& len) const;
186  mutable uint8_t mCount{0};
187 };
188 
192 class AAAA : public Record
193 {
194 public:
195  static constexpr Resource::Type type{Resource::Type::AAAA};
196 
197  using Record::Record;
198 
199  String toString() const;
200 
201  // Writing
202  void init(Ip6Address addr);
203 };
204 
208 class SRV : public Record
209 {
210 public:
211  static constexpr Resource::Type type{Resource::Type::SRV};
212 
213  using Record::Record;
214 
215  uint16_t getPriority() const;
216 
217  uint16_t getWeight() const;
218 
219  uint16_t getPort() const;
220 
221  Name getHost() const;
222 
223  String toString(const String& sep = "; ") const;
224 
225  // Writing
226  void init(uint16_t priority, uint16_t weight, uint16_t port, const String& host);
227 };
228 
229 } // namespace Resource
230 
232 
233 } // namespace mDNS
234 
String toString(mDNS::ResourceType type)
#define MDNS_RESOURCE_TYPE_MAP(XX)
MDNS resource type identifiers.
Definition: Resource.h:25
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:45
String toString() const
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
A single mDNS Answer.
Definition: Answer.h:26
Encoded DNS name.
Definition: Name.h:38
'AAAA' record containing 128-bit IPv6 address
Definition: Resource.h:193
static constexpr Resource::Type type
Definition: Resource.h:195
String toString() const
void init(Ip6Address addr)
'A' record containing IP4 address
Definition: Resource.h:70
void init(IpAddress ipaddr)
String toString() const
Definition: Resource.h:78
static constexpr Resource::Type type
Definition: Resource.h:72
IpAddress getAddress() const
'HINFO' record containing Host information
Definition: Resource.h:112
static constexpr Resource::Type type
Definition: Resource.h:114
'PTR' record containing pointer to a canonical name
Definition: Resource.h:91
static constexpr Resource::Type type
Definition: Resource.h:93
Name getName() const
void init(const String &name)
String toString() const
Definition: Resource.h:99
Resource Record with no specific type.
Definition: Resource.h:51
uint16_t getRecordSize() const
uint8_t * getRecord() const
Answer & answer
Definition: Resource.h:59
String toString() const
Record(const Answer &answer)
Definition: Resource.h:53
'SRV' Service Locator record
Definition: Resource.h:209
uint16_t getPort() const
static constexpr Resource::Type type
Definition: Resource.h:211
void init(uint16_t priority, uint16_t weight, uint16_t port, const String &host)
String toString(const String &sep="; ") const
uint16_t getWeight() const
uint16_t getPriority() const
Name getHost() const
'TXT' record containing attribute list
Definition: Resource.h:126
String operator[](uint8_t index) const
String toString(const String &sep="; ") const
String getValue(const char *name) const
Definition: Resource.h:150
String operator[](const char *name) const
Definition: Resource.h:136
TXT & operator+=(const String &value)
Definition: Resource.h:178
TXT & operator+=(const char *value)
Definition: Resource.h:172
String operator[](const String &name) const
Definition: Resource.h:141
void add(const String &value)
Definition: Resource.h:167
static constexpr Resource::Type type
Definition: Resource.h:128
void init()
Definition: Resource.h:161
void add(const char *value, uint16_t len)
String getValue(const String &name) const
Definition: Resource.h:155
uint8_t count() const
String getValue(const char *name, uint16_t namelen) const
Type
Definition: Resource.h:41
XX(name, value, desc)
Definition: Answer.h:18
Definition: Resource.h:16
uint8_t addr[16]
Definition: Resource.h:17