Skip to content
This repository was archived by the owner on Nov 20, 2020. It is now read-only.

Commit aca6d75

Browse files
horihirome-no-dev
authored andcommitted
add support for TXT records in mDNS query responses (#1480)
* add methods for getting TXTs. * add methods for getting TXTs.
1 parent 8fa02bd commit aca6d75

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/ESPmDNS.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,51 @@ uint16_t MDNSResponder::port(int idx) {
285285
return result->port;
286286
}
287287

288+
int MDNSResponder::numTxt(int idx) {
289+
mdns_result_t * result = _getResult(idx);
290+
if(!result){
291+
log_e("Result %d not found", idx);
292+
return 0;
293+
}
294+
return result->txt_count;
295+
}
296+
297+
bool MDNSResponder::hasTxt(int idx, const char * key) {
298+
mdns_result_t * result = _getResult(idx);
299+
if(!result){
300+
log_e("Result %d not found", idx);
301+
return false;
302+
}
303+
int i = 0;
304+
while(i < result->txt_count) {
305+
if (strcmp(result->txt[i].key, key) == 0) return true;
306+
i++;
307+
}
308+
return false;
309+
}
310+
311+
String MDNSResponder::txt(int idx, const char * key) {
312+
mdns_result_t * result = _getResult(idx);
313+
if(!result){
314+
log_e("Result %d not found", idx);
315+
return "";
316+
}
317+
int i = 0;
318+
while(i < result->txt_count) {
319+
if (strcmp(result->txt[i].key, key) == 0) return result->txt[i].value;
320+
i++;
321+
}
322+
return "";
323+
}
324+
325+
String MDNSResponder::txt(int idx, int txtIdx) {
326+
mdns_result_t * result = _getResult(idx);
327+
if(!result){
328+
log_e("Result %d not found", idx);
329+
return "";
330+
}
331+
if (txtIdx >= result->txt_count) return "";
332+
return result->txt[txtIdx].value;
333+
}
334+
288335
MDNSResponder MDNS;

src/ESPmDNS.h

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class MDNSResponder {
107107
IPAddress IP(int idx);
108108
IPv6Address IPv6(int idx);
109109
uint16_t port(int idx);
110+
int numTxt(int idx);
111+
bool hasTxt(int idx, const char * key);
112+
String txt(int idx, const char * key);
113+
String txt(int idx, int txtIdx);
110114

111115
private:
112116
String _hostname;

0 commit comments

Comments
 (0)