Skip to content

Commit

Permalink
feat: TXT record in MDNSBrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
feelfreelinux committed Jun 5, 2024
1 parent a8c87fa commit c0310ee
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
49 changes: 44 additions & 5 deletions main/platform/MDNSBrowser.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include <functional> // for function
#include <map> // for map
#include <memory> // for unique_ptr
#include <string> // for string
#include <vector> // for vector
#include <BellLogger.h>
#include <functional> // for function
#include <memory> // for unique_ptr
#include <string> // for string
#include <unordered_map> // for unordered_map
#include <vector> // for vector

namespace bell {

Expand All @@ -17,6 +18,7 @@ class MDNSBrowser {
std::string name, type, domain, hostname;

std::vector<std::string> ipv4, ipv6;
std::unordered_map<std::string, std::string> txtRecords;
uint16_t port = 0;

bool operator==(const DiscoveredRecord& rhs) const {
Expand All @@ -43,6 +45,43 @@ class MDNSBrowser {
// Starts discovery of passed type and returns implemented browser instance
static std::unique_ptr<MDNSBrowser> startDiscovery(
std::string type, RecordsUpdatedCallback callback);

protected:
std::unordered_map<std::string, std::string> parseMDNSTXTRecord(
int txtLen, const unsigned char* txtRecord) {
std::unordered_map<std::string, std::string> result;
int i = 0;

while (i < txtLen) {
// Get the length of the current key-value pair
int length = txtRecord[i];
i++;

if (i + length > txtLen) {
// This should not happen if the input is well-formed
BELL_LOG(error, "MDNSBrowser", "Cannot parse ill-formed txt record");
break;
}

// Extract the key-value pair
std::string pair(reinterpret_cast<const char*>(txtRecord + i), length);
i += length;

// Find the position of the '=' character
size_t pos = pair.find('=');
if (pos != std::string::npos) {
// Split into key and value
std::string key = pair.substr(0, pos);
std::string value = pair.substr(pos + 1);
result[key] = value;
} else {
// If there's no '=', then it's just a key with an empty value
result[pair] = "";
}
}

return result;
}
};

} // namespace bell
4 changes: 4 additions & 0 deletions main/platform/apple/MDNSBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <unistd.h>
#include <atomic> // for function
#include <stdexcept>
#include <string>
#include <utility> // for pair
#include "BellLogger.h"
#include "dns_sd.h" // for DNSServiceRef, DNSServiceRefDeallocate, DNS...
Expand All @@ -28,6 +29,7 @@ class implMDNSBrowser : public MDNSBrowser {
struct AddrResolvReference {
implMDNSBrowser* parentPtr;
std::string name, type, domain;
std::unordered_map<std::string, std::string> txtRecords;
int port = 0;
};

Expand Down Expand Up @@ -63,6 +65,7 @@ class implMDNSBrowser : public MDNSBrowser {
for (auto& record : discoveredRecords) {
if (record.name == ctx->name && record.domain == ctx->domain &&
record.type == ctx->type) {
record.txtRecords = ctx->txtRecords;
record.hostname = hostname;
record.ipv4.push_back(addrStr);
record.port = ctx->port;
Expand Down Expand Up @@ -98,6 +101,7 @@ class implMDNSBrowser : public MDNSBrowser {
const unsigned char* txtRecord) {
auto refCopy = service;
if (ctx->parentPtr != NULL) {
ctx->txtRecords = parseMDNSTXTRecord(txtLen, txtRecord);
auto error = DNSServiceGetAddrInfo(
&refCopy, kDNSServiceFlagsShareConnection, 0,
kDNSServiceProtocol_IPv4, hostTarget, getAddrInfoReply, ctx);
Expand Down
5 changes: 5 additions & 0 deletions main/platform/esp/MDNSBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class implMDNSBrowser : public MDNSBrowser {
a = a->next;
}

// copy txt records int std::unordered_map
for (int x = 0; x < r->txt_count; x++) {
record.txtRecords.insert({std::string(r->txt[x].key), std::string(&r->txt[x].value[0], &r->txt[x].value[r->txt_value_len[x]])});
}

discoveredRecords.push_back(record);
r = r->next;
}
Expand Down

0 comments on commit c0310ee

Please sign in to comment.