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 
76  IpAddress getAddress() const;
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 
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:43
Resource Record with no specific type.
Definition: Resource.h:50
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:641
uint8_t addr[16]
Definition: Resource.h:17
TXT & operator+=(const char *value)
Definition: Resource.h:172
void add(const String &value)
Definition: Resource.h:167
TXT & operator+=(const String &value)
Definition: Resource.h:178
Record(const Answer &answer)
Definition: Resource.h:53
#define MDNS_RESOURCE_TYPE_MAP(XX)
MDNS resource type identifiers.
Definition: Resource.h:25
&#39;A&#39; record containing IP4 address
Definition: Resource.h:69
void init()
Definition: Resource.h:161
The String class.
Definition: WString.h:136
&#39;TXT&#39; record containing attribute list
Definition: Resource.h:125
A single mDNS Answer.
Definition: Answer.h:25
bool getValue(const TSource &source, TDest &dest)
Copies a Json data value to a variable, but only if it exists.
Definition: ArduinoJson.h:80
Definition: Answer.h:17
&#39;SRV&#39; Service Locator record
Definition: Resource.h:208
String getValue(const String &name) const
Definition: Resource.h:155
String operator[](const String &name) const
Definition: Resource.h:141
String operator[](const char *name) const
Definition: Resource.h:136
Answer & answer
Definition: Resource.h:59
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:237
String getValue(const char *name) const
Definition: Resource.h:150
&#39;PTR&#39; record containing pointer to a canonical name
Definition: Resource.h:90
&#39;AAAA&#39; record containing 128-bit IPv6 address
Definition: Resource.h:192
String toString() const
String toString() const
Definition: Resource.h:78
XX(name, extensionStart, mime)
Type
Definition: Resource.h:41
String toString(mDNS::ResourceType type)
String toString() const
Definition: Resource.h:99
Definition: Resource.h:16
&#39;HINFO&#39; record containing Host information
Definition: Resource.h:111
Encoded DNS name.
Definition: Name.h:37