@@ -36,7 +36,7 @@ License (MIT license):
36
36
37
37
#include " ESP8266mDNS.h"
38
38
#include < functional>
39
-
39
+
40
40
#include " debug.h"
41
41
42
42
extern " C" {
@@ -88,28 +88,28 @@ static const int MDNS_PORT = 5353;
88
88
MDNSResponder::MDNSResponder () : _conn(0 ) { _services = 0 ; }
89
89
MDNSResponder::~MDNSResponder () {}
90
90
91
- bool MDNSResponder::begin (const char * domain){
91
+ bool MDNSResponder::begin (const char * domain){
92
92
// Open the MDNS socket if it isn't already open.
93
-
93
+
94
94
size_t n = strlen (domain);
95
95
if (n > 255 ) { // Can only handle domains that are 255 chars in length.
96
96
return false ;
97
97
}
98
-
98
+
99
99
// Copy in domain characters as lowercase
100
100
for (int i = 0 ; i < n; ++i)
101
101
_hostName[i] = tolower (domain[i]);
102
102
_hostName[n] = ' \0 ' ;
103
-
103
+
104
104
os_strcpy (_boardName, ARDUINO_BOARD);
105
-
105
+
106
106
// Open the MDNS socket if it isn't already open.
107
107
if (!_conn) {
108
108
uint32_t ourIp = _getOurIp ();
109
109
if (ourIp == 0 ){
110
110
return false ;
111
111
}
112
-
112
+
113
113
ip_addr_t ifaddr;
114
114
ifaddr.addr = ourIp;
115
115
ip_addr_t multicast_addr;
@@ -135,7 +135,7 @@ bool MDNSResponder::begin(const char* domain){
135
135
136
136
void MDNSResponder::update () {
137
137
if (!_conn->next ()) {
138
- return ;
138
+ return ;
139
139
}
140
140
_parsePacket ();
141
141
}
@@ -163,15 +163,21 @@ uint16_t MDNSResponder::_getServicePort(char *name, char *proto){
163
163
}
164
164
165
165
uint32_t MDNSResponder::_getOurIp (){
166
- if (wifi_get_opmode () & STATION_MODE){
166
+ int mode = wifi_get_opmode ();
167
+ if (mode & STATION_MODE){
167
168
struct ip_info staIpInfo;
168
169
wifi_get_ip_info (STATION_IF, &staIpInfo);
169
170
return staIpInfo.ip .addr ;
170
- }
171
+ } else if (mode & SOFTAP_MODE) {
172
+ struct ip_info staIpInfo;
173
+ wifi_get_ip_info (SOFTAP_IF, &staIpInfo);
174
+ return staIpInfo.ip .addr ;
175
+ } else {
171
176
#ifdef MDNS_DEBUG_ERR
172
177
os_printf (" ERR_NO_LOCAL_IP\n " );
173
178
#endif
174
- return 0 ;
179
+ return 0 ;
180
+ }
175
181
}
176
182
177
183
void MDNSResponder::_parsePacket (){
@@ -180,52 +186,52 @@ void MDNSResponder::_parsePacket(){
180
186
bool serviceParsed = false ;
181
187
bool protoParsed = false ;
182
188
bool localParsed = false ;
183
-
189
+
184
190
char hostName[255 ];
185
191
uint8_t hostNameLen;
186
-
192
+
187
193
char serviceName[32 ];
188
194
uint8_t serviceNameLen;
189
195
uint16_t servicePort;
190
-
196
+
191
197
char protoName[32 ];
192
198
uint8_t protoNameLen;
193
-
199
+
194
200
uint16_t packetHeader[6 ];
195
-
201
+
196
202
for (i=0 ; i<6 ; i++) packetHeader[i] = _conn_read16 ();
197
203
198
204
if ((packetHeader[1 ] & 0x8000 ) != 0 ){ // not parsing responses yet
199
205
_conn->flush ();
200
206
return ;
201
207
}
202
-
208
+
203
209
// PARSE REQUEST NAME
204
-
210
+
205
211
hostNameLen = _conn_read8 ();
206
212
_conn_readS (hostName, hostNameLen);
207
213
hostName[hostNameLen] = ' \0 ' ;
208
-
214
+
209
215
if (hostName[0 ] == ' _' ){
210
216
serviceParsed = true ;
211
217
memcpy (serviceName, hostName+1 , hostNameLen);
212
218
serviceNameLen = hostNameLen-1 ;
213
219
hostNameLen = 0 ;
214
220
}
215
-
221
+
216
222
if (hostNameLen > 0 && strcmp (_hostName, hostName) != 0 ){
217
223
#ifdef MDNS_DEBUG_ERR
218
224
os_printf (" ERR_NO_HOST: %s\n " , hostName);
219
225
#endif
220
226
_conn->flush ();
221
227
return ;
222
228
}
223
-
229
+
224
230
if (!serviceParsed){
225
231
serviceNameLen = _conn_read8 ();
226
232
_conn_readS (serviceName, serviceNameLen);
227
233
serviceName[serviceNameLen] = ' \0 ' ;
228
-
234
+
229
235
if (serviceName[0 ] == ' _' ){
230
236
memcpy (serviceName, serviceName+1 , serviceNameLen);
231
237
serviceNameLen--;
@@ -253,7 +259,7 @@ void MDNSResponder::_parsePacket(){
253
259
return ;
254
260
}
255
261
}
256
-
262
+
257
263
if (!protoParsed){
258
264
protoNameLen = _conn_read8 ();
259
265
_conn_readS (protoName, protoNameLen);
@@ -270,7 +276,7 @@ void MDNSResponder::_parsePacket(){
270
276
return ;
271
277
}
272
278
}
273
-
279
+
274
280
if (!localParsed){
275
281
char localName[32 ];
276
282
uint8_t localNameLen = _conn_read8 ();
@@ -287,7 +293,7 @@ void MDNSResponder::_parsePacket(){
287
293
return ;
288
294
}
289
295
}
290
-
296
+
291
297
if (serviceNameLen > 0 && protoNameLen > 0 ){
292
298
servicePort = _getServicePort (serviceName, protoName);
293
299
if (servicePort == 0 ){
@@ -304,16 +310,16 @@ void MDNSResponder::_parsePacket(){
304
310
_conn->flush ();
305
311
return ;
306
312
}
307
-
313
+
308
314
// RESPOND
309
-
315
+
310
316
#ifdef MDNS_DEBUG_RX
311
317
os_printf (" RX: REQ, ID:%u, Q:%u, A:%u, NS:%u, ADD:%u\n " , packetHeader[0 ], packetHeader[2 ], packetHeader[3 ], packetHeader[4 ], packetHeader[5 ]);
312
318
#endif
313
319
314
320
uint16_t currentType;
315
321
uint16_t currentClass;
316
-
322
+
317
323
int numQuestions = packetHeader[2 ];
318
324
if (numQuestions > 4 ) numQuestions = 4 ;
319
325
uint16_t questions[4 ];
@@ -326,21 +332,21 @@ void MDNSResponder::_parsePacket(){
326
332
}
327
333
currentClass = _conn_read16 ();
328
334
if (currentClass & MDNS_CLASS_IN) questions[question++] = currentType;
329
-
335
+
330
336
if (numQuestions > 0 ){
331
337
if (_conn_read16 () != 0xC00C ){// new question but for another host/service
332
338
_conn->flush ();
333
339
numQuestions = 0 ;
334
340
}
335
341
}
336
-
342
+
337
343
#ifdef MDNS_DEBUG_RX
338
344
os_printf (" REQ: " );
339
345
if (hostNameLen > 0 ) os_printf (" %s." , hostName);
340
346
if (serviceNameLen > 0 ) os_printf (" _%s." , serviceName);
341
347
if (protoNameLen > 0 ) os_printf (" _%s." , protoName);
342
348
os_printf (" local. " );
343
-
349
+
344
350
if (currentType == MDNS_TYPE_AAAA) os_printf (" AAAA " );
345
351
else if (currentType == MDNS_TYPE_A) os_printf (" A " );
346
352
else if (currentType == MDNS_TYPE_PTR) os_printf (" PTR " );
@@ -351,7 +357,7 @@ void MDNSResponder::_parsePacket(){
351
357
if (currentClass == MDNS_CLASS_IN) os_printf (" IN " );
352
358
else if (currentClass == MDNS_CLASS_IN_FLUSH_CACHE) os_printf (" IN[F] " );
353
359
else os_printf (" 0x%04X " , currentClass);
354
-
360
+
355
361
os_printf (" \n " );
356
362
#endif
357
363
}
@@ -362,7 +368,7 @@ void MDNSResponder::_parsePacket(){
362
368
else if (questions[i] == MDNS_TYPE_TXT) responseMask |= 0x4 ;
363
369
else if (questions[i] == MDNS_TYPE_PTR) responseMask |= 0xF ;
364
370
}
365
-
371
+
366
372
return _reply (responseMask, (serviceName), (protoName), servicePort);
367
373
}
368
374
@@ -371,14 +377,14 @@ void MDNSResponder::_parsePacket(){
371
377
void MDNSResponder::_reply (uint8_t replyMask, char * service, char *proto, uint16_t port){
372
378
int i;
373
379
if (replyMask == 0 ) return ;
374
-
380
+
375
381
#ifdef MDNS_DEBUG_TX
376
382
os_printf (" TX: mask:%01X, service:%s, proto:%s, port:%u\n " , replyMask, service, proto, port);
377
383
#endif
378
-
384
+
379
385
char nameLen = os_strlen (_hostName);
380
386
size_t serviceLen = os_strlen (service);
381
-
387
+
382
388
uint8_t answerCount = 0 ;
383
389
for (i=0 ;i<4 ;i++){
384
390
if (replyMask & (1 << i)) answerCount++;
@@ -394,12 +400,12 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
394
400
0x00 , 0x00 , // Additional records
395
401
};
396
402
_conn->append (reinterpret_cast <const char *>(head), 12 );
397
-
403
+
398
404
if ((replyMask & 0x8 ) == 0 ){
399
405
_conn->append (reinterpret_cast <const char *>(&nameLen), 1 );
400
406
_conn->append (reinterpret_cast <const char *>(_hostName), nameLen);
401
407
}
402
-
408
+
403
409
if (replyMask & 0xE ){
404
410
uint8_t servHead[2 ] = {(uint8_t )(serviceLen+1 ), ' _' };
405
411
uint8_t protoHead[2 ] = {0x4 , ' _' };
@@ -408,14 +414,14 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
408
414
_conn->append (reinterpret_cast <const char *>(protoHead), 2 );
409
415
_conn->append (reinterpret_cast <const char *>(proto), 3 );
410
416
}
411
-
417
+
412
418
uint8_t local[7 ] = {
413
419
0x05 , // strlen(_local)
414
420
0x6C , 0x6F , 0x63 , 0x61 , 0x6C , // local
415
421
0x00 , // End of domain
416
422
};
417
423
_conn->append (reinterpret_cast <const char *>(local), 7 );
418
-
424
+
419
425
// PTR Response
420
426
if (replyMask & 0x8 ){
421
427
uint8_t ptr[10 ] = {
@@ -430,16 +436,16 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
430
436
uint8_t ptrTail[2 ] = {0xC0 , 0x0C };
431
437
_conn->append (reinterpret_cast <const char *>(ptrTail), 2 );
432
438
}
433
-
439
+
434
440
// TXT Response
435
441
if (replyMask & 0x4 ){
436
442
if (replyMask & 0x8 ){// send the name
437
443
uint8_t txtHead[2 ] = {0xC0 , (uint8_t )(36 + serviceLen)};
438
444
_conn->append (reinterpret_cast <const char *>(txtHead), 2 );
439
445
}
440
-
446
+
441
447
uint8_t boardNameLen = os_strlen (_boardName);
442
-
448
+
443
449
uint8_t txt[24 ] = {
444
450
0x00 , 0x10 , // Type TXT
445
451
0x80 , 0x01 , // Class IN, with cache flush
@@ -450,9 +456,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
450
456
};
451
457
_conn->append (reinterpret_cast <const char *>(txt), 17 );
452
458
_conn->append (reinterpret_cast <const char *>(_boardName), boardNameLen);
453
-
459
+
454
460
}
455
-
461
+
456
462
// SRV Response
457
463
if (replyMask & 0x2 ){
458
464
if (replyMask & 0xC ){// send the name
@@ -461,7 +467,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
461
467
srvHead[1 ] = 36 + serviceLen;
462
468
_conn->append (reinterpret_cast <const char *>(srvHead), 2 );
463
469
}
464
-
470
+
465
471
uint8_t srv[16 ] = {
466
472
0x00 , 0x21 , // Type SRV
467
473
0x80 , 0x01 , // Class IN, with cache flush
@@ -479,7 +485,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
479
485
srvTail[1 ] = 19 + serviceLen;
480
486
_conn->append (reinterpret_cast <const char *>(srvTail), 2 );
481
487
}
482
-
488
+
483
489
// A Response
484
490
if (replyMask & 0x1 ){
485
491
uint32_t ip = _getOurIp ();
@@ -488,7 +494,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
488
494
_conn->append (reinterpret_cast <const char *>(_hostName), nameLen);
489
495
_conn->append (reinterpret_cast <const char *>(local), 7 );
490
496
}
491
-
497
+
492
498
uint8_t aaa[14 ] = {
493
499
0x00 , 0x01 , // TYPE A
494
500
0x80 , 0x01 , // Class IN, with cache flush
0 commit comments