From 83810fa1563f77145272583e36dfb076d3a92018 Mon Sep 17 00:00:00 2001 From: horihiro Date: Tue, 19 Jun 2018 00:18:50 +0900 Subject: [PATCH] add support for TXT records in mDNS query responses (#1480) * add methods for getting TXTs. * add methods for getting TXTs. --- libraries/ESPmDNS/src/ESPmDNS.cpp | 47 +++++++++++++++++++++++++++++++ libraries/ESPmDNS/src/ESPmDNS.h | 4 +++ 2 files changed, 51 insertions(+) diff --git a/libraries/ESPmDNS/src/ESPmDNS.cpp b/libraries/ESPmDNS/src/ESPmDNS.cpp index e7b05c9d..42a4ad50 100644 --- a/libraries/ESPmDNS/src/ESPmDNS.cpp +++ b/libraries/ESPmDNS/src/ESPmDNS.cpp @@ -285,4 +285,51 @@ uint16_t MDNSResponder::port(int idx) { 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_result_t * result = _getResult(idx); + if(!result){ + log_e("Result %d not found", idx); + return ""; + } + if (txtIdx >= result->txt_count) return ""; + return result->txt[txtIdx].value; +} + MDNSResponder MDNS; diff --git a/libraries/ESPmDNS/src/ESPmDNS.h b/libraries/ESPmDNS/src/ESPmDNS.h index 083ec17e..e66fbdb7 100644 --- a/libraries/ESPmDNS/src/ESPmDNS.h +++ b/libraries/ESPmDNS/src/ESPmDNS.h @@ -107,6 +107,10 @@ public: IPAddress IP(int idx); IPv6Address IPv6(int idx); uint16_t port(int idx); + int numTxt(int idx); + bool hasTxt(int idx, const char * key); + String txt(int idx, const char * key); + String txt(int idx, int txtIdx); private: String _hostname;