Skip to content

Commit

Permalink
fix(windows): Fixed how we handle FQDN and TXT records on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyost committed Nov 20, 2023
1 parent f2e7d5b commit 1e7d545
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
6 changes: 4 additions & 2 deletions packages/bonsoir_windows/windows/bonsoir_broadcast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ namespace bonsoir_windows {
if (!(broadcast->isRunning())) {
return;
}
auto parts = split(toUtf8(instance->pszInstanceName), '.');
std::string name = parts[0];
std::string name = std::get<0>(parseBonjourFqdn(toUtf8(instance->pszInstanceName)));
if (name == "") {
return;
}
BonsoirService service = broadcast->service;
if (service.name != name) {
std::string oldName = service.name;
Expand Down
23 changes: 12 additions & 11 deletions packages/bonsoir_windows/windows/bonsoir_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ namespace bonsoir_windows {
return;
}

std::string nameHost = toUtf8(dnsRecord->Data.PTR.pNameHost);
auto parts = split(nameHost, '.');
std::string name = parts[0];
std::string type = parts[1] + "." + parts[2];
auto serviceData = parseBonjourFqdn(toUtf8(dnsRecord->Data.PTR.pNameHost));
std::string name = std::get<0>(serviceData);
std::string type = std::get<1>(serviceData);

BonsoirService *service = discovery->findService(name, type);
if (dnsRecord->dwTtl <= 0) {
Expand All @@ -118,11 +117,15 @@ namespace bonsoir_windows {
DNS_TXT_DATAW *pData = &txtRecord->Data.TXT;
for (DWORD s = 0; s < pData->dwStringCount; s++) {
std::string record = toUtf8(std::wstring(pData->pStringArray[s]));
std::string key = record;
std::string value = "";
int splitIndex = static_cast<int>(record.find("="));
if (splitIndex != std::string::npos) {
newService.attributes.insert(
{record.substr(0, splitIndex), record.substr(splitIndex + 1, record.length())}
);
key = record.substr(0, splitIndex);
value = record.substr(splitIndex + 1, record.length());
}
if (key.rfind("=", 0) == std::string::npos && newService.attributes.find(key) == newService.attributes.end()) {
newService.attributes.insert({key, value});
}
}
}
Expand All @@ -140,10 +143,8 @@ namespace bonsoir_windows {
std::string name = "";
if (serviceInstance && serviceInstance->pszInstanceName) {
std::string nameHost = toUtf8(serviceInstance->pszInstanceName);
auto parts = split(nameHost, '.');
name = parts[0];
std::string type = parts[1] + "." + parts[2];
service = discovery->findService(name, type);
auto serviceData = parseBonjourFqdn(nameHost);
service = discovery->findService(std::get<0>(serviceData), std::get<1>(serviceData));
}
if (status != ERROR_SUCCESS) {
if (service) {
Expand Down
29 changes: 15 additions & 14 deletions packages/bonsoir_windows/windows/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,23 @@ namespace bonsoir_windows {
return result;
}

std::vector<std::string> split(const std::string text, const char splitter) {
std::vector<std::string> result;
std::string current = "";
for (int i = 0; i < text.size(); i++) {
if (text[i] == splitter) {
if (current != "") {
result.push_back(current);
current = "";
}
continue;
std::tuple<std::string, std::string> parseBonjourFqdn(const std::string fqdn) {
std::regex regexPattern("^(.*?)\\._(.*?)\\.?(?:local)?\\.?$");
std::smatch match;

if (std::regex_search(fqdn, match, regexPattern)) {
std::string serviceName = match[1].str();
size_t pos = serviceName.find_last_not_of(" \t\r\n");
if (pos != std::string::npos) {
serviceName.erase(pos + 1);
}
current += text[i];

std::string serviceType = "_" + match[2].str();

return {serviceName, serviceType};
}
if (current.size() != 0)
result.push_back(current);
return result;

return {"", ""};
}

std::wstring getComputerName() {
Expand Down
2 changes: 1 addition & 1 deletion packages/bonsoir_windows/windows/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace bonsoir_windows {

std::string toUtf8(const std::wstring wide_string);

std::vector<std::string> split(const std::string text, const char delimiter);
std::tuple<std::string, std::string> parseBonjourFqdn(const std::string fqdn);

std::wstring getComputerName();

Expand Down

0 comments on commit 1e7d545

Please sign in to comment.