Linux + iPhone Continuity / iMessage / SMS
group discovered logical devices (not by interface)
Zack Bartel5a0414fb3803
diff --git a/src/cli/main.cpp b/src/cli/main.cppindex 4c00106..25937c7 100644--- a/src/cli/main.cpp+++ b/src/cli/main.cpp@@ -80,23 +80,26 @@ int main(int argc, char* argv[]) {tether::Discovery discovery;auto hosts = discovery.discover(timeout_ms);+ auto devices = tether::group_discovered_hosts(hosts);- if (hosts.empty()) {+ if (devices.empty()) {std::cout << " No tetherd instances found.\n";} else {tether::Crypto::instance().init();- for (const auto& h : hosts) {+ for (const auto& dev : devices) {std::string status = "[new]";- if (!h.fingerprint.empty() && tether::Crypto::instance().is_host_known(h.fingerprint)) {+ if (!dev.fingerprint.empty() && tether::Crypto::instance().is_host_known(dev.fingerprint)) {status = "[known]";}- // Pad columns for clean output- printf(" %-20s %s:%-5d %s\n",- h.name.c_str(), h.address.c_str(), h.port, status.c_str());+ printf(" %-20s %s\n", dev.name.c_str(), status.c_str());+ for (const auto& addr : dev.addresses) {+ printf(" %s:%d\n", addr.address.c_str(), addr.port);+ }+ printf("\n");}}- printf("\nFound %zu device(s) in %.1fs\n", hosts.size(), timeout_ms / 1000.0);+ printf("Found %zu device(s) in %.1fs\n", devices.size(), timeout_ms / 1000.0);return 0;}diff --git a/src/core/include/tether/discovery.hpp b/src/core/include/tether/discovery.hppindex 3786980..69e6e0c 100644--- a/src/core/include/tether/discovery.hpp+++ b/src/core/include/tether/discovery.hpp@@ -7,7 +7,13 @@namespace tether {- /// Represents a tetherd instance discovered via mDNS/DNS-SD on the local network.+ /// A single network address where a tetherd instance is reachable.+ struct DiscoveredAddress {+ std::string address; ///< IPv4 or IPv6 address+ uint16_t port = 0; ///< TCP port+ };++ /// Raw per-interface mDNS result (one entry per resolved address).struct DiscoveredHost {std::string name; ///< Advertised service name (e.g. hostname)std::string address; ///< Resolved IPv4/IPv6 address@@ -15,6 +21,17 @@ namespace tether {std::string fingerprint; ///< TLS certificate SHA-256 fingerprint from TXT record};+ /// A logical device, grouping all network interfaces that share the same+ /// TLS fingerprint (i.e. the same tetherd instance).+ struct DiscoveredDevice {+ std::string name; ///< Advertised service name+ std::string fingerprint; ///< Shared TLS fingerprint+ std::vector<DiscoveredAddress> addresses; ///< All reachable addresses+ };++ /// Group raw per-interface results into logical devices by fingerprint.+ std::vector<DiscoveredDevice> group_discovered_hosts(const std::vector<DiscoveredHost>& hosts);+/// mDNS service discovery using Avahi.////// The daemon calls publish() to advertise itself as `_tether._tcp`.@@ -44,7 +61,7 @@ namespace tether {/// Scan the local network for tetherd instances./// Blocks for up to @p timeout_ms milliseconds and returns all hosts found./// @param timeout_ms How long to listen for mDNS responses (default 3 s).- /// @return A list of discovered hosts, possibly empty.+ /// @return A list of discovered hosts (one per interface), possibly empty.std::vector<DiscoveredHost> discover(int timeout_ms = 3000);private:@@ -53,3 +70,4 @@ namespace tether {};} // namespace tether+diff --git a/src/core/src/discovery.cpp b/src/core/src/discovery.cppindex 649c033..28c17f6 100644--- a/src/core/src/discovery.cpp+++ b/src/core/src/discovery.cpp@@ -8,9 +8,11 @@#include <avahi-common/simple-watch.h>#include <avahi-common/thread-watch.h>+#include <algorithm>#include <chrono>#include <cstring>#include <iostream>+#include <map>#include <mutex>#include <thread>@@ -346,5 +348,40 @@ namespace tether {return impl_->results;}-} // namespace tether+ // ─── Grouping helper ────────────────────────────────────────────++ std::vector<DiscoveredDevice> group_discovered_hosts(const std::vector<DiscoveredHost>& hosts) {+ // Use fingerprint as the grouping key. Maintain insertion order.+ std::vector<DiscoveredDevice> devices;+ std::map<std::string, size_t> fp_index; // fingerprint -> index into devices++ for (const auto& h : hosts) {+ std::string key = h.fingerprint.empty() ? ("__nokey__" + h.name) : h.fingerprint;++ auto it = fp_index.find(key);+ if (it == fp_index.end()) {+ fp_index[key] = devices.size();+ DiscoveredDevice dev;+ dev.name = h.name;+ dev.fingerprint = h.fingerprint;+ dev.addresses.push_back({h.address, h.port});+ devices.push_back(std::move(dev));+ } else {+ devices[it->second].addresses.push_back({h.address, h.port});+ }+ }++ // Sort addresses within each device: IPv4 first (no ':'), then IPv6+ for (auto& dev : devices) {+ std::stable_sort(dev.addresses.begin(), dev.addresses.end(),+ [](const DiscoveredAddress& a, const DiscoveredAddress& b) {+ bool a_v4 = a.address.find(':') == std::string::npos;+ bool b_v4 = b.address.find(':') == std::string::npos;+ return a_v4 > b_v4; // true (v4) sorts before false (v6)+ });+ }++ return devices;+ }+} // namespace tetherdiff --git a/src/gtk/main.cpp b/src/gtk/main.cppindex 34716ac..2890475 100644--- a/src/gtk/main.cpp+++ b/src/gtk/main.cpp@@ -188,59 +188,70 @@ static gboolean on_discover_complete(gpointer data) {gtk_widget_destroy(GTK_WIDGET(iter->data));g_list_free(children);- if (result->hosts.empty()) {+ auto devices = tether::group_discovered_hosts(result->hosts);++ if (devices.empty()) {GtkWidget* row = gtk_label_new("No tetherd instances found on the network.");gtk_widget_set_halign(row, GTK_ALIGN_START);gtk_container_add(GTK_CONTAINER(discovered_list_box), row);} else {tether::Crypto::instance().init();- for (const auto& h : result->hosts) {- bool known = !h.fingerprint.empty() &&- tether::Crypto::instance().is_host_known(h.fingerprint);+ for (const auto& dev : devices) {+ bool known = !dev.fingerprint.empty() &&+ tether::Crypto::instance().is_host_known(dev.fingerprint);std::string status_str = known ? "paired" : "new";- std::string addr_str = h.address + ":" + std::to_string(h.port);- std::string markup = "<b>" + h.name + "</b> <small>" + addr_str +- "</small> <span foreground='" +- (known ? "#8ec07c" : "#fabd2f") + "'>[" +- status_str + "]</span>";+ std::string status_color = known ? "#8ec07c" : "#fabd2f";++ // Build address summary string+ std::string addr_summary;+ for (const auto& a : dev.addresses) {+ if (!addr_summary.empty()) addr_summary += " · ";+ addr_summary += a.address + ":" + std::to_string(a.port);+ }++ std::string markup =+ "<b>" + dev.name + "</b> <span foreground='" + status_color ++ "'>[" + status_str + "]</span>\n<small>" + addr_summary + "</small>";GtkWidget* row_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);gtk_container_set_border_width(GTK_CONTAINER(row_box), 6);const char* icon_name = known ? "network-wired-symbolic" : "network-wireless-symbolic";GtkWidget* icon = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR);+ gtk_widget_set_valign(icon, GTK_ALIGN_CENTER);gtk_box_pack_start(GTK_BOX(row_box), icon, FALSE, FALSE, 0);GtkWidget* label = gtk_label_new(NULL);gtk_label_set_markup(GTK_LABEL(label), markup.c_str());gtk_label_set_xalign(GTK_LABEL(label), 0.0);+ gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);gtk_box_pack_start(GTK_BOX(row_box), label, TRUE, TRUE, 0);- // "Connect" button auto-fills the connection bar- std::string* host_copy = new std::string(h.address);- int port_val = h.port;- GtkWidget* btn_use = gtk_button_new_with_label("Connect");- g_signal_connect(btn_use, "clicked",- G_CALLBACK(+[](GtkWidget*, gpointer data) {- auto* host_str = static_cast<std::string*>(data);- gtk_entry_set_text(GTK_ENTRY(entry_connect_host), host_str->c_str());- // Trigger the connect action- on_btn_connect_clicked(btn_connect, nullptr);- }),- host_copy);- // Clean up the heap string when the button is destroyed- g_signal_connect(btn_use, "destroy",- G_CALLBACK(+[](GtkWidget*, gpointer data) {- delete static_cast<std::string*>(data);- }),- host_copy);- gtk_box_pack_end(GTK_BOX(row_box), btn_use, FALSE, FALSE, 0);+ // "Connect" button uses the first address (IPv4 preferred, already sorted)+ if (!dev.addresses.empty()) {+ std::string* host_copy = new std::string(dev.addresses[0].address);+ GtkWidget* btn_use = gtk_button_new_with_label("Connect");+ gtk_widget_set_valign(btn_use, GTK_ALIGN_CENTER);+ g_signal_connect(btn_use, "clicked",+ G_CALLBACK(+[](GtkWidget*, gpointer data) {+ auto* host_str = static_cast<std::string*>(data);+ gtk_entry_set_text(GTK_ENTRY(entry_connect_host), host_str->c_str());+ on_btn_connect_clicked(btn_connect, nullptr);+ }),+ host_copy);+ g_signal_connect(btn_use, "destroy",+ G_CALLBACK(+[](GtkWidget*, gpointer data) {+ delete static_cast<std::string*>(data);+ }),+ host_copy);+ gtk_box_pack_end(GTK_BOX(row_box), btn_use, FALSE, FALSE, 0);+ }gtk_list_box_insert(GTK_LIST_BOX(discovered_list_box), row_box, -1);}}- std::string status = "Found " + std::to_string(result->hosts.size()) + " device(s)";+ std::string status = "Found " + std::to_string(devices.size()) + " device(s)";gtk_label_set_text(GTK_LABEL(lbl_discover_status), status.c_str());gtk_widget_show_all(discovered_list_box);