Skip to content

Commit 758107f

Browse files
author
Eric Wilkison
committed
Add TXT support
1 parent f34f84b commit 758107f

File tree

2 files changed

+162
-34
lines changed

2 files changed

+162
-34
lines changed

libraries/ESP8266mDNS/ESP8266mDNS.cpp

Lines changed: 144 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static const IPAddress MDNS_MULTICAST_ADDR(224, 0, 0, 251);
8585
static const int MDNS_MULTICAST_TTL = 1;
8686
static const int MDNS_PORT = 5353;
8787

88-
MDNSResponder::MDNSResponder() : _conn(0) { _services = 0; _arduinoAuth = false; }
88+
MDNSResponder::MDNSResponder() : _conn(0) { _services = 0; }
8989
MDNSResponder::~MDNSResponder() {}
9090

9191
bool MDNSResponder::begin(const char* domain){
@@ -138,6 +138,58 @@ void MDNSResponder::update() {
138138
_parsePacket();
139139
}
140140

141+
142+
143+
144+
145+
146+
147+
148+
bool MDNSResponder::addServiceTxt(char *name, char *proto, char *txt){
149+
MDNSService* servicePtr;
150+
151+
//DEBUG Serial.printf("Starting addServiceTxt(name=%s,proto=%s,txt=%s)\n", name,proto,txt);
152+
//DEBUG delay(20);
153+
uint8_t txtLen = os_strlen(txt) + 1; //One accounts for length byte added when building the txt responce
154+
if ( txtLen > 128) return false;
155+
//Find the service
156+
for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) {
157+
//DEBUG Serial.printf("Checking service name=%s,proto=%s\n", servicePtr->_name,servicePtr->_proto);
158+
//DEBUG delay(20);
159+
if(strcmp(servicePtr->_name, name) == 0 && strcmp(servicePtr->_proto, proto) == 0){
160+
if (servicePtr->_txtLen + txtLen > 1300) return false; //max txt record size
161+
//DEBUG Serial.printf("found match service name=%s,proto=%s\n", servicePtr->_name,servicePtr->_proto);
162+
//DEBUG delay(20);
163+
struct MDNSTxt *newtxt = (struct MDNSTxt*)(os_malloc(sizeof(struct MDNSTxt)));
164+
os_strcpy(newtxt->_txt, txt);
165+
newtxt->_next = 0;
166+
if(servicePtr->_txts == 0) { //no services have been added
167+
servicePtr->_txts = newtxt;
168+
servicePtr->_txtLen += txtLen;
169+
//DEBUG Serial.printf("Adding First TXT of %s to service %s and proto %s\n", newtxt->_txt,servicePtr->_name,servicePtr->_proto);
170+
//DEBUG Serial.printf("New txt length: %d\n",servicePtr->_txtLen);
171+
return true;
172+
}
173+
else{
174+
MDNSTxt * txtPtr = servicePtr->_txts;
175+
while(txtPtr->_next !=0) {
176+
txtPtr = txtPtr->_next;
177+
//DEBUG Serial.println("Incramenting txt pointer");
178+
}
179+
txtPtr->_next = newtxt;
180+
servicePtr->_txtLen += txtLen;
181+
//DEBUG Serial.printf("Adding Another TXT of %s to service %s and proto %s\n", newtxt->_txt,servicePtr->_name,servicePtr->_proto);
182+
//DEBUG Serial.printf("New txt length: %d\n",servicePtr->_txtLen);
183+
return true;
184+
}
185+
/*
186+
*/
187+
}
188+
}
189+
return false;
190+
//DEBUG Serial.println("No Service Found When adding a txt");
191+
}
192+
141193
void MDNSResponder::addService(char *name, char *proto, uint16_t port){
142194
if(_getServicePort(name, proto) != 0) return;
143195
if(os_strlen(name) > 32 || os_strlen(proto) != 3) return; //bad arguments
@@ -146,15 +198,55 @@ void MDNSResponder::addService(char *name, char *proto, uint16_t port){
146198
os_strcpy(srv->_proto, proto);
147199
srv->_port = port;
148200
srv->_next = 0;
149-
201+
srv->_txts = 0;
202+
srv->_txtLen = 0;
203+
150204
if(_services == 0) _services = srv;
151205
else{
152206
MDNSService* servicePtr = _services;
153207
while(servicePtr->_next !=0) servicePtr = servicePtr->_next;
154208
servicePtr->_next = srv;
155209
}
210+
211+
}
212+
213+
MDNSTxt * MDNSResponder::_getServiceTxt(char *name, char *proto){
214+
MDNSService* servicePtr;
215+
for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) {
216+
if(servicePtr->_port > 0 && strcmp(servicePtr->_name, name) == 0 && strcmp(servicePtr->_proto, proto) == 0){
217+
if (servicePtr->_txts == 0) return false;
218+
else{
219+
return servicePtr->_txts;
220+
}
221+
}
222+
}
223+
return 0;
156224
}
157225

226+
uint16_t MDNSResponder::_getServiceTxtLen(char *name, char *proto){
227+
MDNSService* servicePtr;
228+
for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) {
229+
if(servicePtr->_port > 0 && strcmp(servicePtr->_name, name) == 0 && strcmp(servicePtr->_proto, proto) == 0){
230+
if (servicePtr->_txts == 0) return false;
231+
else{
232+
return servicePtr->_txtLen;
233+
}
234+
}
235+
}
236+
return 0;
237+
}
238+
239+
240+
241+
242+
243+
244+
245+
246+
247+
248+
249+
158250
uint16_t MDNSResponder::_getServicePort(char *name, char *proto){
159251
MDNSService* servicePtr;
160252
for (servicePtr = _services; servicePtr; servicePtr = servicePtr->_next) {
@@ -177,7 +269,7 @@ uint32_t MDNSResponder::_getOurIp(){
177269
return staIpInfo.ip.addr;
178270
} else {
179271
#ifdef MDNS_DEBUG_ERR
180-
os_printf("ERR_NO_LOCAL_IP\n");
272+
Serial.printf("ERR_NO_LOCAL_IP\n");
181273
#endif
182274
return 0;
183275
}
@@ -224,7 +316,7 @@ void MDNSResponder::_parsePacket(){
224316

225317
if(hostNameLen > 0 && strcmp(_hostName, hostName) != 0){
226318
#ifdef MDNS_DEBUG_ERR
227-
os_printf("ERR_NO_HOST: %s\n", hostName);
319+
Serial.printf("ERR_NO_HOST: %s\n", hostName);
228320
#endif
229321
_conn->flush();
230322
return;
@@ -249,14 +341,14 @@ void MDNSResponder::_parsePacket(){
249341
localParsed = true;
250342
} else {
251343
#ifdef MDNS_DEBUG_ERR
252-
os_printf("ERR_FQDN: %s\n", serviceName);
344+
Serial.printf("ERR_FQDN: %s\n", serviceName);
253345
#endif
254346
_conn->flush();
255347
return;
256348
}
257349
} else {
258350
#ifdef MDNS_DEBUG_ERR
259-
os_printf("ERR_SERVICE: %s\n", serviceName);
351+
Serial.printf("ERR_SERVICE: %s\n", serviceName);
260352
#endif
261353
_conn->flush();
262354
return;
@@ -273,7 +365,7 @@ void MDNSResponder::_parsePacket(){
273365
protoParsed = true;
274366
} else {
275367
#ifdef MDNS_DEBUG_ERR
276-
os_printf("ERR_PROTO: %s\n", protoName);
368+
Serial.printf("ERR_PROTO: %s\n", protoName);
277369
#endif
278370
_conn->flush();
279371
return;
@@ -290,7 +382,7 @@ void MDNSResponder::_parsePacket(){
290382
localParsed = true;
291383
} else {
292384
#ifdef MDNS_DEBUG_ERR
293-
os_printf("ERR_FQDN: %s\n", localName);
385+
Serial.printf("ERR_FQDN: %s\n", localName);
294386
#endif
295387
_conn->flush();
296388
return;
@@ -301,14 +393,14 @@ void MDNSResponder::_parsePacket(){
301393
servicePort = _getServicePort(serviceName, protoName);
302394
if(servicePort == 0){
303395
#ifdef MDNS_DEBUG_ERR
304-
os_printf("ERR_NO_SERVICE: %s\n", serviceName);
396+
Serial.printf("ERR_NO_SERVICE: %s\n", serviceName);
305397
#endif
306398
_conn->flush();
307399
return;
308400
}
309401
} else if(serviceNameLen > 0 || protoNameLen > 0){
310402
#ifdef MDNS_DEBUG_ERR
311-
os_printf("ERR_SERVICE_PROTO: %s\n", serviceName);
403+
Serial.printf("ERR_SERVICE_PROTO: %s\n", serviceName);
312404
#endif
313405
_conn->flush();
314406
return;
@@ -317,7 +409,7 @@ void MDNSResponder::_parsePacket(){
317409
// RESPOND
318410

319411
#ifdef MDNS_DEBUG_RX
320-
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]);
412+
Serial.printf("RX: REQ, ID:%u, Q:%u, A:%u, NS:%u, ADD:%u\n", packetHeader[0], packetHeader[2], packetHeader[3], packetHeader[4], packetHeader[5]);
321413
#endif
322414

323415
uint16_t currentType;
@@ -344,24 +436,24 @@ void MDNSResponder::_parsePacket(){
344436
}
345437

346438
#ifdef MDNS_DEBUG_RX
347-
os_printf("REQ: ");
348-
if(hostNameLen > 0) os_printf("%s.", hostName);
349-
if(serviceNameLen > 0) os_printf("_%s.", serviceName);
350-
if(protoNameLen > 0) os_printf("_%s.", protoName);
351-
os_printf("local. ");
352-
353-
if(currentType == MDNS_TYPE_AAAA) os_printf(" AAAA ");
354-
else if(currentType == MDNS_TYPE_A) os_printf(" A ");
355-
else if(currentType == MDNS_TYPE_PTR) os_printf(" PTR ");
356-
else if(currentType == MDNS_TYPE_SRV) os_printf(" SRV ");
357-
else if(currentType == MDNS_TYPE_TXT) os_printf(" TXT ");
358-
else os_printf(" 0x%04X ", currentType);
359-
360-
if(currentClass == MDNS_CLASS_IN) os_printf(" IN ");
361-
else if(currentClass == MDNS_CLASS_IN_FLUSH_CACHE) os_printf(" IN[F] ");
362-
else os_printf(" 0x%04X ", currentClass);
363-
364-
os_printf("\n");
439+
Serial.printf("REQ: ");
440+
if(hostNameLen > 0) Serial.printf("%s.", hostName);
441+
if(serviceNameLen > 0) Serial.printf("_%s.", serviceName);
442+
if(protoNameLen > 0) Serial.printf("_%s.", protoName);
443+
Serial.printf("local. ");
444+
445+
if(currentType == MDNS_TYPE_AAAA) Serial.printf(" AAAA ");
446+
else if(currentType == MDNS_TYPE_A) Serial.printf(" A ");
447+
else if(currentType == MDNS_TYPE_PTR) Serial.printf(" PTR ");
448+
else if(currentType == MDNS_TYPE_SRV) Serial.printf(" SRV ");
449+
else if(currentType == MDNS_TYPE_TXT) Serial.printf(" TXT ");
450+
else Serial.printf(" 0x%04X ", currentType);
451+
452+
if(currentClass == MDNS_CLASS_IN) Serial.printf(" IN ");
453+
else if(currentClass == MDNS_CLASS_IN_FLUSH_CACHE) Serial.printf(" IN[F] ");
454+
else Serial.printf(" 0x%04X ", currentClass);
455+
456+
Serial.printf("\n");
365457
#endif
366458
}
367459
uint8_t responseMask = 0;
@@ -376,8 +468,20 @@ void MDNSResponder::_parsePacket(){
376468
}
377469

378470
void MDNSResponder::enableArduino(uint16_t port, bool auth){
379-
_arduinoAuth = auth;
471+
472+
char boardName[64];
473+
const char *boardExtra = "board=";
474+
os_sprintf(boardName, "%s%s", boardExtra, ARDUINO_BOARD);
475+
476+
char authUpload[16];
477+
const char *authUploadExtra = "auth_upload=";
478+
os_sprintf(authUpload, "%s%s", authUploadExtra, (auth) ? "yes":"no");
479+
380480
addService("arduino", "tcp", port);
481+
addServiceTxt("arduino", "tcp", "tcp_check=no");
482+
addServiceTxt("arduino", "tcp", "ssh_upload=no");
483+
addServiceTxt("arduino", "tcp", (const char*)boardName);
484+
addServiceTxt("arduino", "tcp", (const char*)authUpload);
381485
}
382486

383487
void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint16_t port){
@@ -477,7 +581,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
477581
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
478582

479583
//Send the type, class, ttl and rdata length
480-
uint8_t txtDataLen = 0;
584+
uint8_t txtDataLen = _getServiceTxtLen(service,proto);
481585
uint8_t txtAttrs[10] = {
482586
0x00, 0x10, //TXT record query
483587
0x00, 0x01, //Class IN
@@ -487,7 +591,15 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
487591
_conn->append(reinterpret_cast<const char*>(txtAttrs), 10);
488592

489593
//Send the RData
490-
//TODO - Build TXT Redords
594+
MDNSTxt * txtPtr = _getServiceTxt(service,proto);
595+
while(txtPtr !=0){
596+
uint8_t txtLen = os_strlen(txtPtr->_txt);
597+
_conn->append(reinterpret_cast<const char*>(&txtLen), 1); // lenght of txt
598+
_conn->append(reinterpret_cast<const char*>(txtPtr->_txt), txtLen); // the txt
599+
//DEBUG Serial.print("We have txts: ");
600+
//DEBUG Serial.println(txtPtr->_txt);
601+
txtPtr = txtPtr->_next;
602+
}
491603
}
492604

493605

libraries/ESP8266mDNS/ESP8266mDNS.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ struct MDNSService {
5757
char _name[32];
5858
char _proto[3];
5959
uint16_t _port;
60+
struct MDNSTxt * _txts;
61+
uint16_t _txtLen; // length of all txts
62+
};
63+
64+
struct MDNSTxt{
65+
MDNSTxt * _next;
66+
char _txt[128];
6067
};
6168

6269
class MDNSResponder {
@@ -78,16 +85,25 @@ class MDNSResponder {
7885
addService(service.c_str(), proto.c_str(), port);
7986
}
8087

81-
void enableArduino(uint16_t port, bool auth=false);
88+
bool addServiceTxt(char *name, char *proto, char *txt);
89+
void addServiceTxt(const char *name, const char *proto, const char *txt){
90+
addServiceTxt((char *)name, (char *)proto, (char *)txt);
91+
}
92+
void addServiceTxt(String name, String proto, String txt){
93+
addServiceTxt(name.c_str(), proto.c_str(), txt.c_str());
94+
}
95+
96+
void enableArduino(uint16_t port, bool auth=false);
8297

8398
private:
8499
struct MDNSService * _services;
85100
UdpContext* _conn;
86101
char _hostName[128];
87-
bool _arduinoAuth;
88102

89103
uint32_t _getOurIp();
90104
uint16_t _getServicePort(char *service, char *proto);
105+
MDNSTxt * _getServiceTxt(char *name, char *proto);
106+
uint16_t _getServiceTxtLen(char *name, char *proto);
91107
void _parsePacket();
92108
void _reply(uint8_t replyMask, char * service, char *proto, uint16_t port);
93109
};

0 commit comments

Comments
 (0)