Skip to content

MDNSResponder: Add method to get TXT key values. #5135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions libraries/ESPmDNS/src/ESPmDNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ mdns_result_t * MDNSResponder::_getResult(int idx){
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){
Expand Down Expand Up @@ -333,13 +343,17 @@ String MDNSResponder::txt(int idx, const char * key) {
}

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;
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;
2 changes: 2 additions & 0 deletions libraries/ESPmDNS/src/ESPmDNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ class MDNSResponder {
bool hasTxt(int idx, const char * key);
String txt(int idx, const char * key);
String txt(int idx, int txtIdx);
String txtKey(int idx, int txtIdx);

private:
String _hostname;
mdns_result_t * results;
mdns_result_t * _getResult(int idx);
mdns_txt_item_t * _getResultTxt(int idx, int txtIdx);
};

extern MDNSResponder MDNS;
Expand Down