From 9632d6d9c7deb34916c6eca7bba78c93f2c4a100 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Sat, 4 Jul 2026 16:46:23 -0400 Subject: [PATCH] plutosdr: resolve ambiguous mDNS URIs using discovered endpoint Fix mDNS discovery ambiguity where multiple devices across different network segments/subnets can advertise the same hostname (e.g. "ip:pluto.local"), resulting in non-unique or misleading URIs. Replace unresolved mDNS hostnames with the actual discovered endpoint reported in the scan description. - Detect ".local" URIs on IP backend and substitute hostname with resolved endpoint from libiio scan results. - Preserve URI semantics: - Keep backend prefix ("ip:") - Preserve optional port suffix - Correctly format IPv6 literals with RFC2732 bracket notation when required - Add replaceHostnameWithIP() to perform URI normalization during scan: - Only applies to IP backend URIs - Only triggers for mDNS-based ".local" hostnames - Uses scan-provided endpoint (from description string) as authoritative address instead of advertised hostname - Extend scan() to emit both original and resolved URIs for debugging and verification during device discovery. This resolves inconsistent device addressing when multiple PlutoSDRs are accessible via mDNS across different subnets or network segments. No change to backend enumeration or device identity logic; this only normalizes .local IP URI resolution at scan time. Signed-off-by: Robin Getz --- devices/plutosdr/deviceplutosdrscan.cpp | 84 ++++++++++++++++++++++++- devices/plutosdr/deviceplutosdrscan.h | 3 + 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/devices/plutosdr/deviceplutosdrscan.cpp b/devices/plutosdr/deviceplutosdrscan.cpp index 794b98c40..e156d2de4 100644 --- a/devices/plutosdr/deviceplutosdrscan.cpp +++ b/devices/plutosdr/deviceplutosdrscan.cpp @@ -65,7 +65,10 @@ void DevicePlutoSDRScan::scan() continue; } - qDebug("PlutoSDRScan::scan: %d: %s [%s]", i, description, uri); + // For uri which are ip:*.local replace with the ip address + std::string fixedUri = replaceHostnameWithIP(uri, description); + + qDebug("PlutoSDRScan::scan: %d: %s [%s] [%s]", i, description, uri, fixedUri.c_str()); const std::string_view descriptionView = description ? std::string_view{description} : std::string_view{}; const bool isPlutoDescription = (descriptionView.find("PlutoSDR") != std::string_view::npos) || @@ -79,7 +82,7 @@ void DevicePlutoSDRScan::scan() DeviceScan({ std::string(description), std::string("TBD"), - std::string(uri) + fixedUri })); m_scans.push_back(dev_scan); m_urilMap[m_scans.back()->m_uri] = m_scans.back(); @@ -198,6 +201,83 @@ void DevicePlutoSDRScan::enumOriginDevices(const QString& hardwareId, PluginInte } +/** + * @brief Replaces an mDNS hostname with the discovered endpoint. + * + * Older libiio versions may return the advertised mDNS hostname (for example + * "ip:pluto.local") rather than the endpoint that was actually discovered. + * This becomes ambiguous when identical mDNS hostnames exist on different + * network segments, since multiple devices can legitimately advertise the + * same ".local" hostname. + * + * Since the scan description begins with the discovered endpoint (IPv4 or IPv6), + * followed by " (". For ".local" URIs, substitute the endpoint while + * preserving the backend and optional port number. + * + * Examples: + * URI: ip:pluto.local + * Description: 192.168.2.1 (...) + * Result: ip:192.168.2.1 + * + * URI: ip:pluto.local:30431 + * Description: fe80::205:f7ff:fe75:9c4f%eth1 (...) + * Result: ip:[fe80::205:f7ff:fe75:9c4f%eth1]:30431 + */ +std::string DevicePlutoSDRScan::replaceHostnameWithIP( + const std::string& uri, + const char *description) const +{ + if (!description) { + return uri; + } + + // Only applies to IP backends. + if (uri.rfind("ip:", 0) != 0) { + return uri; + } + + // Only rewrite mDNS hostnames. + if (uri.find(".local") == std::string::npos) { + return uri; + } + + // Description starts with " (". + const std::string desc(description); + const size_t endpointEnd = desc.find(" ("); + + if (endpointEnd == std::string::npos) { + return uri; + } + + const std::string endpoint = desc.substr(0, endpointEnd); + + // Preserve any optional port. + std::string port; + const size_t localPos = uri.find(".local"); + + if (localPos != std::string::npos) + { + const size_t afterLocal = localPos + 6; // strlen(".local") + + if (afterLocal < uri.size() && uri[afterLocal] == ':') { + port = uri.substr(afterLocal); + } + } + + std::string newUri = "ip:"; + + // RFC2732 requires brackets around IPv6 literals when a port is present. + if (!port.empty() && endpoint.find(':') != std::string::npos) { + newUri += "[" + endpoint + "]"; + } else { + newUri += endpoint; + } + + newUri += port; + + return newUri; +} + /** * @brief Creates a backend prefix from a libiio URI. * diff --git a/devices/plutosdr/deviceplutosdrscan.h b/devices/plutosdr/deviceplutosdrscan.h index dc0c4a790..eda8f79b9 100644 --- a/devices/plutosdr/deviceplutosdrscan.h +++ b/devices/plutosdr/deviceplutosdrscan.h @@ -52,6 +52,9 @@ public: private: static constexpr char BACKEND_SEPARATOR = '_'; + std::string replaceHostnameWithIP( + const std::string& uri, + const char *description) const; std::string createBackendSuffix(const char *uri) const; QString getBackendLabel(const std::string& serial) const; std::string getPhysicalSerial(const std::string& serial) const;