-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLightMDNS.hpp
325 lines (274 loc) · 10.3 KB
/
LightMDNS.hpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// mgream 2024
// - significantly refactored
// Copyright (C) 2010 Georg Kaindl
// http://gkaindl.com
//
// This file is part of Arduino EthernetBonjour.
//
// EthernetBonjour is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// EthernetBonjour is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with EthernetBonjour. If not, see
// <http://www.gnu.org/licenses/>.
//
#if ! defined(__MDNS_H__)
#define __MDNS_H__ 1
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
#include <Arduino.h>
#include <vector>
#include <set>
// #define DEBUG_MDNS
// #define DEBUG_MDNS_UDP_READ
// #define DEBUG_MDNS_UDP_WRITE
#if ! defined(DEBUG_PRINTF)
#ifdef DEBUG_MDNS
#define DEBUG_PRINTF Serial.printf
#else
#define DEBUG_PRINTF(...) \
do { \
} while (0)
#endif
#endif
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
class MDNSTXTBuilder;
class MDNSTXT {
public:
static constexpr size_t KEY_LENGTH_MAX = 9; // RFC recommendation
static constexpr size_t TOTAL_LENGTH_MAX = 255; // Per TXT string
struct _Entry {
String key;
std::vector<uint8_t> value;
bool binary;
};
using Entries = std::vector<_Entry>;
using Builder = MDNSTXTBuilder;
private:
Entries _entries;
mutable uint16_t cached_length { 0 };
mutable bool length_valid { false };
bool validate (const String &key) const;
public:
bool insert (const String &key, const void *value, const size_t length, const bool is_binary);
inline const Entries &entries () const {
return _entries;
}
inline size_t size () const {
return _entries.size ();
}
uint16_t length () const;
String toString () const;
};
// -----------------------------------------------------------------------------------------------
class MDNSServiceBuilder;
struct MDNSService {
using TXT = MDNSTXT;
struct Config {
uint16_t priority = 0x0000;
uint16_t weight = 0x0000;
// std::vector<String> subtypes;
};
enum class Protocol {
TCP,
UDP
};
static String toString (const Protocol protocol) {
if (protocol == Protocol::TCP)
return "TCP";
else if (protocol == Protocol::UDP)
return "UDP";
else
return "Unknown";
}
uint16_t port;
Protocol proto;
String name;
Config config {};
TXT text {};
String _serv {}, _fqsn {};
using Builder = MDNSServiceBuilder;
};
// -----------------------------------------------------------------------------------------------
class MDNS {
public:
struct TTLConfig {
uint32_t announce = 120; // Default announcement TTL
uint32_t probe = 0; // Probe TTL always 0
uint32_t goodbye = 0; // Goodbye/release TTL always 0
uint32_t shared_max = 10; // Maximum TTL for shared records per RFC
};
enum class Status {
TryLater = 2,
Success = 1,
Failure = 0,
InvalidArgument = -1,
OutOfMemory = -2,
ServerError = -3,
PacketBad = -4,
NameConflict = -5,
};
// clang-format off
static String toString(const Status status) {
switch (status) {
case Status::TryLater: return "TryLater";
case Status::Success: return "Success";
case Status::Failure: return "Failure";
case Status::InvalidArgument: return "InvalidArgument";
case Status::OutOfMemory: return "OutOfMemory";
case Status::ServerError: return "ServerError";
case Status::PacketBad: return "PacketBad";
case Status::NameConflict: return "NameConflict";
default: return "Unknown";
}
}
// clang-format on
using Service = MDNSService;
using Services = std::vector<Service>;
using ServiceTypes = std::set<String>;
private:
UDP* _udp;
IPAddress _addr;
String _name, _fqhn, _arpa;
TTLConfig _ttls;
bool _enabled{ false };
Status _messageRecv(void);
Status _messageSend(const uint16_t xid, const int type, const Service* service = nullptr);
unsigned long _announced{ 0 };
Status _announce(void);
Status _conflicted(void);
Services _services;
ServiceTypes _serviceTypes;
void _writeAddressRecord(const uint32_t ttl) const;
void _writeReverseRecord(const uint32_t ttl) const;
void _writeServiceRecord(const Service& service, const uint32_t ttl) const;
void _writeCompleteRecord(const uint32_t ttl) const;
void _writeProbeRecord(const uint32_t ttl) const;
void _writeNextSecureRecord(const String& name, const std::initializer_list<uint8_t>& types, const uint32_t ttl, const bool includeAdditional = false) const;
inline uint32_t _announceTime() const {
return ((_ttls.announce / 2) + (_ttls.announce / 4)) * static_cast<uint32_t>(1000);
}
Status serviceRecordInsert(const Service::Protocol proto, const uint16_t port, const String& name, const Service::Config& config = Service::Config(), const Service::TXT& text = Service::TXT()); // deprecated
Status serviceRecordRemove(const Service::Protocol proto, const uint16_t port, const String& name); // deprecated
Status serviceRecordRemove(const String& name); // deprecated
Status serviceRecordClear(void); // deprecated
public:
explicit MDNS(UDP& udp);
virtual ~MDNS();
Status begin(void);
Status start(const IPAddress& addr, const String& name = String(), const bool checkForConflicts = false);
Status process(void);
Status stop(void);
inline Status serviceInsert(const Service& service) {
return serviceRecordInsert(service.proto, service.port, service.name, service.config, service.text);
}
inline Status serviceRemove(const Service& service) {
return serviceRecordRemove(service.proto, service.port, service.name);
}
inline Status serviceRemove(const String& name) {
return name.isEmpty () ? Status::InvalidArgument : serviceRecordRemove(name);
}
inline Status serviceClear(void) {
return serviceRecordClear();
}
};
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
#include <stdexcept>
class MDNSTXTBuilder {
private:
MDNSTXT _record;
inline MDNSTXTBuilder& _add(const String& key, const void* value, const size_t length, const bool is_binary) {
if (!_record.insert(key, value, length, is_binary)) throw std::runtime_error("TXT insert failed");
return *this;
}
public:
MDNSTXTBuilder()
: _record{} {}
inline MDNSTXT build() const {
return _record;
}
inline operator MDNSTXT() const {
return build();
}
inline MDNSTXTBuilder& add(const String& key) {
return _add(key, nullptr, 0, false);
}
inline MDNSTXTBuilder& add(const String& key, const String& value) {
return _add(key, reinterpret_cast<const uint8_t*>(value.c_str()), value.length(), false);
}
inline MDNSTXTBuilder& add(const String& key, const char* value) {
return add(key, String(value));
}
inline MDNSTXTBuilder& add(const String& key, const bool value) {
return add(key, String(value ? "true" : "false"));
}
inline MDNSTXTBuilder& add(const String& key, const int value) {
return add(key, String(value));
}
inline MDNSTXTBuilder& add(const String& key, const uint8_t* value, const size_t length) {
return _add(key, value, length, true);
}
};
// -----------------------------------------------------------------------------------------------
class MDNSServiceBuilder {
private:
using Service = MDNS::Service;
Service _service;
bool _hasName{ false }, _hasPort{ false }, _hasProtocol{ false };
inline bool validate() const {
return (_hasName && _hasPort && _hasProtocol);
}
public:
MDNSServiceBuilder()
: _service{} {}
MDNS::Service build() const {
if (!validate())
throw std::runtime_error("Invalid service configuration: missing required fields");
return _service;
}
operator MDNS::Service() const {
return build();
}
MDNSServiceBuilder& withName(const String& name) {
_service.name = name;
_hasName = true;
return *this;
}
MDNSServiceBuilder& withPort(uint16_t port) {
_service.port = port;
_hasPort = true;
return *this;
}
MDNSServiceBuilder& withProtocol(Service::Protocol proto) {
_service.proto = proto;
_hasProtocol = true;
return *this;
}
MDNSServiceBuilder& withConfig(const Service::Config& config) {
_service.config = config;
return *this;
}
MDNSServiceBuilder& withPriority(uint16_t priority) {
_service.config.priority = priority;
return *this;
}
MDNSServiceBuilder& withWeight(uint16_t weight) {
_service.config.weight = weight;
return *this;
}
MDNSServiceBuilder& withTXT(const Service::TXT& txt) {
_service.text = txt;
return *this;
}
};
// -----------------------------------------------------------------------------------------------
#endif // __MDNS_H__