-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathESPmDNS.cpp
377 lines (328 loc) · 10.5 KB
/
ESPmDNS.cpp
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
ESP8266 Multicast DNS (port of CC3000 Multicast DNS library)
Version 1.1
Copyright (c) 2013 Tony DiCola ([email protected])
ESP8266 port (c) 2015 Ivan Grokhotkov ([email protected])
MDNS-SD Suport 2015 Hristo Gochkov ([email protected])
Extended MDNS-SD support 2016 Lars Englund ([email protected])
License (MIT license):
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Important RFC's for reference:
// - DNS request and response: http://www.ietf.org/rfc/rfc1035.txt
// - Multicast DNS: http://www.ietf.org/rfc/rfc6762.txt
// - MDNS-SD: https://tools.ietf.org/html/rfc6763
#ifndef LWIP_OPEN_SRC
#define LWIP_OPEN_SRC
#endif
#include "ESPmDNS.h"
#include <functional>
#include "esp_mac.h"
// Add quotes around defined value
#ifdef __IN_ECLIPSE__
#define STR_EXPAND(tok) #tok
#define STR(tok) STR_EXPAND(tok)
#else
#define STR(tok) tok
#endif
// static void addInterface(ESP_Network_Interface * iface){
// #if defined(CONFIG_MDNS_ADD_CUSTOM_NETIF) && !defined(CONFIG_MDNS_PREDEF_NETIF_STA) && !defined(CONFIG_MDNS_PREDEF_NETIF_ETH)
// /* Demonstration of adding a custom netif to mdns service, but we're adding the default example one,
// * so we must disable all predefined interfaces (PREDEF_NETIF_STA, AP and ETH) first
// */
// ESP_ERROR_CHECK(mdns_register_netif(iface->netif()));
// /* It is not enough to just register the interface, we have to enable is manually.
// * This is typically performed in "GOT_IP" event handler, but we call it here directly
// * since the `EXAMPLE_INTERFACE` netif is connected already, to keep the example simple.
// */
// ESP_ERROR_CHECK(mdns_netif_action(iface->netif(), MDNS_EVENT_ENABLE_IP4 | MDNS_EVENT_ENABLE_IP6));
// ESP_ERROR_CHECK(mdns_netif_action(iface->netif(), MDNS_EVENT_ANNOUNCE_IP4 | MDNS_EVENT_ANNOUNCE_IP6));
// #if defined(CONFIG_MDNS_RESPOND_REVERSE_QUERIES)
// ESP_ERROR_CHECK(mdns_netif_action(iface->netif(), MDNS_EVENT_IP4_REVERSE_LOOKUP | MDNS_EVENT_IP6_REVERSE_LOOKUP));
// #endif
// #endif // CONFIG_MDNS_ADD_CUSTOM_NETIF
// }
// static void _on_sys_event(arduino_event_t *event){
// mdns_handle_system_event(NULL, event);
// }
MDNSResponder::MDNSResponder() :results(NULL) {}
MDNSResponder::~MDNSResponder() {
end();
}
bool MDNSResponder::begin(const String& hostName){
if(mdns_init()){
log_e("Failed starting MDNS");
return false;
}
//WiFi.onEvent(_on_sys_event);
_hostname = hostName;
_hostname.toLowerCase();
if(mdns_hostname_set(hostName.c_str())) {
log_e("Failed setting MDNS hostname");
return false;
}
return true;
}
void MDNSResponder::end() {
mdns_free();
}
void MDNSResponder::setInstanceName(String name) {
if (name.length() > 63) return;
if(mdns_instance_name_set(name.c_str())){
log_e("Failed setting MDNS instance");
return;
}
}
void MDNSResponder::enableArduino(uint16_t port, bool auth){
mdns_txt_item_t arduTxtData[4] = {
{(char*)"board" ,(char*)STR(ARDUINO_VARIANT)},
{(char*)"tcp_check" ,(char*)"no"},
{(char*)"ssh_upload" ,(char*)"no"},
{(char*)"auth_upload" ,(char*)"no"}
};
if(mdns_service_add(NULL, "_arduino", "_tcp", port, arduTxtData, 4)) {
log_e("Failed adding Arduino service");
}
if(auth && mdns_service_txt_item_set("_arduino", "_tcp", "auth_upload", "yes")){
log_e("Failed setting Arduino txt item");
}
}
void MDNSResponder::disableArduino(){
if(mdns_service_remove("_arduino", "_tcp")) {
log_w("Failed removing Arduino service");
}
}
void MDNSResponder::enableWorkstation(esp_interface_t interface){
char winstance[21+_hostname.length()];
uint8_t mac[6];
esp_base_mac_addr_get(mac);
sprintf(winstance, "%s [%02x:%02x:%02x:%02x:%02x:%02x]", _hostname.c_str(), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
if(mdns_service_add(NULL, "_workstation", "_tcp", 9, NULL, 0)) {
log_e("Failed adding Workstation service");
} else if(mdns_service_instance_name_set("_workstation", "_tcp", winstance)) {
log_e("Failed setting Workstation service instance name");
}
}
void MDNSResponder::disableWorkstation(){
if(mdns_service_remove("_workstation", "_tcp")) {
log_w("Failed removing Workstation service");
}
}
bool MDNSResponder::addService(char *name, char *proto, uint16_t port){
char _name[strlen(name)+2];
char _proto[strlen(proto)+2];
if (name[0] == '_') {
sprintf(_name, "%s", name);
} else {
sprintf(_name, "_%s", name);
}
if (proto[0] == '_') {
sprintf(_proto, "%s", proto);
} else {
sprintf(_proto, "_%s", proto);
}
if(mdns_service_add(NULL, _name, _proto, port, NULL, 0)) {
log_e("Failed adding service %s.%s.\n", name, proto);
return false;
}
return true;
}
bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *value){
char _name[strlen(name)+2];
char _proto[strlen(proto)+2];
if (name[0] == '_') {
sprintf(_name, "%s", name);
} else {
sprintf(_name, "_%s", name);
}
if (proto[0] == '_') {
sprintf(_proto, "%s", proto);
} else {
sprintf(_proto, "_%s", proto);
}
if(mdns_service_txt_item_set(_name, _proto, key, value)) {
log_e("Failed setting service TXT");
return false;
}
return true;
}
IPAddress MDNSResponder::queryHost(char *host, uint32_t timeout){
esp_ip4_addr_t addr;
addr.addr = 0;
esp_err_t err = mdns_query_a(host, timeout, &addr);
if(err){
if(err == ESP_ERR_NOT_FOUND){
log_w("Host was not found!");
return IPAddress();
}
log_e("Query Failed");
return IPAddress();
}
return IPAddress(addr.addr);
}
int MDNSResponder::queryService(char *service, char *proto) {
if(!service || !service[0] || !proto || !proto[0]){
log_e("Bad Parameters");
return 0;
}
if(results){
mdns_query_results_free(results);
results = NULL;
}
char srv[strlen(service)+2];
char prt[strlen(proto)+2];
if (service[0] == '_') {
sprintf(srv, "%s", service);
} else {
sprintf(srv, "_%s", service);
}
if (proto[0] == '_') {
sprintf(prt, "%s", proto);
} else {
sprintf(prt, "_%s", proto);
}
esp_err_t err = mdns_query_ptr(srv, prt, 3000, 20, &results);
if(err){
log_e("Query Failed");
return 0;
}
if(!results){
log_w("No results found!");
return 0;
}
mdns_result_t * r = results;
int i = 0;
while(r){
i++;
r = r->next;
}
return i;
}
mdns_result_t * MDNSResponder::_getResult(int idx){
mdns_result_t * result = results;
int i = 0;
while(result){
if(i == idx){
break;
}
i++;
result = result->next;
}
return result;
}
mdns_txt_item_t * MDNSResponder::_getResultTxt(int idx, int txtIdx){
mdns_result_t * result = _getResult(idx);
if(!result){
log_e("Result %d not found", idx);
return NULL;
}
if (txtIdx >= result->txt_count) return NULL;
return &result->txt[txtIdx];
}
String MDNSResponder::hostname(int idx) {
mdns_result_t * result = _getResult(idx);
if(!result){
log_e("Result %d not found", idx);
return String();
}
return String(result->hostname);
}
IPAddress MDNSResponder::address(int idx) {
mdns_result_t * result = _getResult(idx);
if(!result){
log_e("Result %d not found", idx);
return IPAddress();
}
mdns_ip_addr_t * addr = result->addr;
while(addr){
if(addr->addr.type == MDNS_IP_PROTOCOL_V4){
return IPAddress(addr->addr.u_addr.ip4.addr);
}
addr = addr->next;
}
return IPAddress();
}
IPAddress MDNSResponder::addressV6(int idx) {
mdns_result_t * result = _getResult(idx);
if(!result){
log_e("Result %d not found", idx);
return IPAddress(IPv6);
}
mdns_ip_addr_t * addr = result->addr;
while(addr){
if(addr->addr.type == MDNS_IP_PROTOCOL_V6){
return IPAddress(IPv6, (const uint8_t *)addr->addr.u_addr.ip6.addr, addr->addr.u_addr.ip6.zone);
}
addr = addr->next;
}
return IPAddress(IPv6);
}
uint16_t MDNSResponder::port(int idx) {
mdns_result_t * result = _getResult(idx);
if(!result){
log_e("Result %d not found", idx);
return 0;
}
return result->port;
}
int MDNSResponder::numTxt(int idx) {
mdns_result_t * result = _getResult(idx);
if(!result){
log_e("Result %d not found", idx);
return 0;
}
return result->txt_count;
}
bool MDNSResponder::hasTxt(int idx, const char * key) {
mdns_result_t * result = _getResult(idx);
if(!result){
log_e("Result %d not found", idx);
return false;
}
int i = 0;
while(i < result->txt_count) {
if (strcmp(result->txt[i].key, key) == 0) return true;
i++;
}
return false;
}
String MDNSResponder::txt(int idx, const char * key) {
mdns_result_t * result = _getResult(idx);
if(!result){
log_e("Result %d not found", idx);
return "";
}
int i = 0;
while(i < result->txt_count) {
if (strcmp(result->txt[i].key, key) == 0) return result->txt[i].value;
i++;
}
return "";
}
String MDNSResponder::txt(int idx, int txtIdx) {
mdns_txt_item_t * resultTxt = _getResultTxt(idx, txtIdx);
return !resultTxt
? ""
: resultTxt->value;
}
String MDNSResponder::txtKey(int idx, int txtIdx) {
mdns_txt_item_t * resultTxt = _getResultTxt(idx, txtIdx);
return !resultTxt
? ""
: resultTxt->key;
}
MDNSResponder MDNS;