Skip to content

Linux + iPhone Continuity / iMessage / SMS

mdns discovery

Zack Bartelab83abdb1e2c
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6e77cbe..df3c7bd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(PkgConfig REQUIRED)
pkg_check_modules(WAYLAND REQUIRED IMPORTED_TARGET wayland-client)
+pkg_check_modules(AVAHI REQUIRED IMPORTED_TARGET avahi-client)
find_package(OpenSSL REQUIRED)
include(FetchContent)
diff --git a/src/cli/main.cpp b/src/cli/main.cpp
index 0cc2bd5..117b649 100644
--- a/src/cli/main.cpp
+++ b/src/cli/main.cpp
@@ -1,6 +1,8 @@
#include <iostream>
#include <string>
#include <tether/client.hpp>
+#include <tether/discovery.hpp>
+#include <tether/crypto.hpp>
void print_help() {
std::cout << "tether - Wayland companion CLI\n\n"
@@ -9,8 +11,10 @@ void print_help() {
<< " -g, --get-clipboard Retrieve the current Wayland clipboard text.\n"
<< " -s, --set-clipboard Take string input and copy it to the local Wayland clipboard.\n"
<< " -f, --send-file Send a file securely to the local Wayland download directory.\n"
+ << " -d, --discover Scan the local network for tetherd instances via mDNS.\n"
<< " --host <ip> Connect over TCP to daemon ip instead of UNIX Socket.\n"
<< " --port <num> Connect over TCP port (default 5134).\n"
+ << " --timeout <ms> Discovery scan duration in milliseconds (default 3000).\n"
<< " --list-devices List all paired connection devices.\n"
<< " --accept <fingerprint> Accept a pending pairing request locally.\n"
<< " --pair Send a pair_request over TCP to the daemon.\n\n"
@@ -18,6 +22,8 @@ void print_help() {
<< " tether -g\n"
<< " tether -g --host 127.0.0.1\n"
<< " echo \"pipe\" | tether -s\n"
+ << " tether --discover\n"
+ << " tether --discover --timeout 5000\n"
<< " tether --accept 9a4f21...\n";
}
@@ -29,6 +35,7 @@ int main(int argc, char* argv[]) {
std::string action, arg_val, host = "";
int port = 5134;
+ int timeout_ms = 3000;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
@@ -36,11 +43,13 @@ int main(int argc, char* argv[]) {
print_help();
return 0;
} else if (arg == "-g" || arg == "--get-clipboard") { action = "get"; }
+ else if (arg == "-d" || arg == "--discover") { action = "discover"; }
else if (arg == "--list-devices") { action = "list"; }
else if (arg == "--pair") { action = "pair"; }
else if (arg == "--accept") { action = "accept"; if (i + 1 < argc && argv[i + 1][0] != '-') arg_val = argv[++i]; }
else if (arg == "--host") { if (i + 1 < argc && argv[i + 1][0] != '-') host = argv[++i]; }
else if (arg == "--port") { if (i + 1 < argc && argv[i + 1][0] != '-') port = std::stoi(argv[++i]); }
+ else if (arg == "--timeout") { if (i + 1 < argc && argv[i + 1][0] != '-') timeout_ms = std::stoi(argv[++i]); }
else if (arg == "-f" || arg == "--send-file") {
action = "file";
if (i + 1 < argc && argv[i + 1][0] != '-') arg_val = argv[++i];
@@ -65,6 +74,29 @@ int main(int argc, char* argv[]) {
client.accept_device(arg_val);
std::cout << "Successfully paired device: " << arg_val << "\n";
return 0;
+ } else if (action == "discover") {
+ std::cout << "Scanning for tetherd instances on the local network...\n\n";
+
+ tether::Discovery discovery;
+ auto hosts = discovery.discover(timeout_ms);
+
+ if (hosts.empty()) {
+ std::cout << " No tetherd instances found.\n";
+ } else {
+ tether::Crypto::instance().init();
+ for (const auto& h : hosts) {
+ std::string status = "[new]";
+ if (!h.fingerprint.empty() && tether::Crypto::instance().is_host_known(h.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("\nFound %zu device(s) in %.1fs\n", hosts.size(), timeout_ms / 1000.0);
+ return 0;
}
if (action == "set" && arg_val.empty()) {
@@ -110,3 +142,4 @@ int main(int argc, char* argv[]) {
return 0;
}
+
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index aba1750..3c970d8 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -7,6 +7,7 @@ add_library(tether STATIC
src/file_transfer.cpp
src/crypto.cpp
src/client.cpp
+ src/discovery.cpp
src/wayland.cpp
src/wayland_protocols/wayland.cpp
src/wayland_protocols/wlr-data-control-unstable-v1.cpp
@@ -21,4 +22,5 @@ target_link_libraries(tether PUBLIC
PkgConfig::WAYLAND
OpenSSL::SSL
OpenSSL::Crypto
+ PkgConfig::AVAHI
)
diff --git a/src/core/include/tether/discovery.hpp b/src/core/include/tether/discovery.hpp
new file mode 100644
index 0000000..3786980
--- /dev/null
+++ b/src/core/include/tether/discovery.hpp
@@ -0,0 +1,55 @@
+#pragma once
+
+#include <cstdint>
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace tether {
+
+ /// Represents a tetherd instance discovered via mDNS/DNS-SD on the local network.
+ struct DiscoveredHost {
+ std::string name; ///< Advertised service name (e.g. hostname)
+ std::string address; ///< Resolved IPv4/IPv6 address
+ uint16_t port = 0; ///< TCP port the daemon is listening on
+ std::string fingerprint; ///< TLS certificate SHA-256 fingerprint from TXT record
+ };
+
+ /// mDNS service discovery using Avahi.
+ ///
+ /// The daemon calls publish() to advertise itself as `_tether._tcp`.
+ /// Clients call discover() to find daemon instances on the LAN.
+ class Discovery {
+ public:
+ Discovery();
+ ~Discovery();
+
+ // Non-copyable, movable
+ Discovery(const Discovery&) = delete;
+ Discovery& operator=(const Discovery&) = delete;
+ Discovery(Discovery&&) noexcept;
+ Discovery& operator=(Discovery&&) noexcept;
+
+ /// Publish this tetherd instance on the local network.
+ /// @param name Human-readable service name (typically the hostname).
+ /// @param port TCP port the daemon is listening on.
+ /// @param fingerprint SHA-256 fingerprint of the daemon's TLS certificate.
+ /// @return true if the service was successfully registered.
+ bool publish(const std::string& name, uint16_t port,
+ const std::string& fingerprint);
+
+ /// Stop advertising this instance. Called automatically by the destructor.
+ void unpublish();
+
+ /// 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.
+ std::vector<DiscoveredHost> discover(int timeout_ms = 3000);
+
+ private:
+ struct Impl;
+ std::unique_ptr<Impl> impl_;
+ };
+
+} // namespace tether
diff --git a/src/core/src/discovery.cpp b/src/core/src/discovery.cpp
new file mode 100644
index 0000000..649c033
--- /dev/null
+++ b/src/core/src/discovery.cpp
@@ -0,0 +1,350 @@
+#include "tether/discovery.hpp"
+
+#include <avahi-client/client.h>
+#include <avahi-client/lookup.h>
+#include <avahi-client/publish.h>
+#include <avahi-common/error.h>
+#include <avahi-common/malloc.h>
+#include <avahi-common/simple-watch.h>
+#include <avahi-common/thread-watch.h>
+
+#include <chrono>
+#include <cstring>
+#include <iostream>
+#include <mutex>
+#include <thread>
+
+namespace tether {
+
+ static constexpr const char* SERVICE_TYPE = "_tether._tcp";
+
+ // ─── Impl ───────────────────────────────────────────────────────
+ // Defined at file scope so C-style Avahi callbacks can access it.
+
+ struct DiscoveryImpl {
+ // Publishing state
+ AvahiThreadedPoll* pub_poll = nullptr;
+ AvahiClient* pub_client = nullptr;
+ AvahiEntryGroup* pub_group = nullptr;
+ std::string pub_name;
+ uint16_t pub_port = 0;
+ std::string pub_fingerprint;
+
+ // Discovery state (per-call, not persistent)
+ std::mutex results_mutex;
+ std::vector<DiscoveredHost> results;
+ };
+
+ // Wire the pimpl to use DiscoveryImpl
+ struct Discovery::Impl : DiscoveryImpl {};
+
+ // ─── Publishing callbacks ───────────────────────────────────────
+
+ static void entry_group_callback(AvahiEntryGroup* g, AvahiEntryGroupState state, void* userdata) {
+ auto* impl = static_cast<DiscoveryImpl*>(userdata);
+ switch (state) {
+ case AVAHI_ENTRY_GROUP_ESTABLISHED:
+ std::cout << "mDNS: Published \"" << SERVICE_TYPE << "\" as \"" << impl->pub_name
+ << "\" on port " << impl->pub_port << std::endl;
+ break;
+ case AVAHI_ENTRY_GROUP_COLLISION:
+ std::cerr << "mDNS: Service name collision for \"" << impl->pub_name << "\"" << std::endl;
+ break;
+ case AVAHI_ENTRY_GROUP_FAILURE:
+ std::cerr << "mDNS: Entry group failure: "
+ << avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))) << std::endl;
+ break;
+ default:
+ break;
+ }
+ }
+
+ static void create_services(DiscoveryImpl* impl) {
+ if (!impl->pub_client) return;
+
+ if (!impl->pub_group) {
+ impl->pub_group = avahi_entry_group_new(impl->pub_client, entry_group_callback, impl);
+ if (!impl->pub_group) {
+ std::cerr << "mDNS: avahi_entry_group_new() failed: "
+ << avahi_strerror(avahi_client_errno(impl->pub_client)) << std::endl;
+ return;
+ }
+ }
+
+ if (avahi_entry_group_is_empty(impl->pub_group)) {
+ std::string txt_fp = "fp=" + impl->pub_fingerprint;
+ std::string txt_ver = "v=1";
+
+ int ret = avahi_entry_group_add_service(
+ impl->pub_group,
+ AVAHI_IF_UNSPEC,
+ AVAHI_PROTO_UNSPEC,
+ (AvahiPublishFlags)0,
+ impl->pub_name.c_str(),
+ SERVICE_TYPE,
+ nullptr, // domain
+ nullptr, // host
+ impl->pub_port,
+ txt_fp.c_str(),
+ txt_ver.c_str(),
+ nullptr // sentinel
+ );
+
+ if (ret < 0) {
+ std::cerr << "mDNS: Failed to add service: " << avahi_strerror(ret) << std::endl;
+ return;
+ }
+
+ ret = avahi_entry_group_commit(impl->pub_group);
+ if (ret < 0) {
+ std::cerr << "mDNS: Failed to commit entry group: " << avahi_strerror(ret) << std::endl;
+ }
+ }
+ }
+
+ static void pub_client_callback(AvahiClient* c, AvahiClientState state, void* userdata) {
+ auto* impl = static_cast<DiscoveryImpl*>(userdata);
+ impl->pub_client = c;
+
+ switch (state) {
+ case AVAHI_CLIENT_S_RUNNING:
+ create_services(impl);
+ break;
+ case AVAHI_CLIENT_FAILURE:
+ std::cerr << "mDNS: Client failure: " << avahi_strerror(avahi_client_errno(c)) << std::endl;
+ break;
+ case AVAHI_CLIENT_S_COLLISION:
+ case AVAHI_CLIENT_S_REGISTERING:
+ if (impl->pub_group) {
+ avahi_entry_group_reset(impl->pub_group);
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ // ─── Discovery callbacks ────────────────────────────────────────
+
+ struct BrowseContext {
+ DiscoveryImpl* impl;
+ AvahiClient* client;
+ };
+
+ static void resolve_callback(
+ AvahiServiceResolver* r,
+ AvahiIfIndex /*interface*/,
+ AvahiProtocol /*protocol*/,
+ AvahiResolverEvent event,
+ const char* name,
+ const char* /*type*/,
+ const char* /*domain*/,
+ const char* /*host_name*/,
+ const AvahiAddress* address,
+ uint16_t port,
+ AvahiStringList* txt,
+ AvahiLookupResultFlags /*flags*/,
+ void* userdata) {
+
+ auto* ctx = static_cast<BrowseContext*>(userdata);
+
+ if (event == AVAHI_RESOLVER_FOUND && address) {
+ char addr_str[AVAHI_ADDRESS_STR_MAX];
+ avahi_address_snprint(addr_str, sizeof(addr_str), address);
+
+ DiscoveredHost host;
+ host.name = name ? name : "";
+ host.address = addr_str;
+ host.port = port;
+
+ // Extract fingerprint from TXT records
+ for (AvahiStringList* item = txt; item; item = avahi_string_list_get_next(item)) {
+ char* key = nullptr;
+ char* value = nullptr;
+ if (avahi_string_list_get_pair(item, &key, &value, nullptr) == 0) {
+ if (key && value && std::strcmp(key, "fp") == 0) {
+ host.fingerprint = value;
+ }
+ avahi_free(key);
+ avahi_free(value);
+ }
+ }
+
+ {
+ std::lock_guard<std::mutex> lock(ctx->impl->results_mutex);
+ // Deduplicate by address+port
+ bool exists = false;
+ for (const auto& h : ctx->impl->results) {
+ if (h.address == host.address && h.port == host.port) {
+ exists = true;
+ break;
+ }
+ }
+ if (!exists) {
+ ctx->impl->results.push_back(std::move(host));
+ }
+ }
+ }
+
+ avahi_service_resolver_free(r);
+ }
+
+ static void browse_callback(
+ AvahiServiceBrowser* /*b*/,
+ AvahiIfIndex interface,
+ AvahiProtocol protocol,
+ AvahiBrowserEvent event,
+ const char* name,
+ const char* type,
+ const char* domain,
+ AvahiLookupResultFlags /*flags*/,
+ void* userdata) {
+
+ auto* ctx = static_cast<BrowseContext*>(userdata);
+
+ if (event == AVAHI_BROWSER_NEW) {
+ avahi_service_resolver_new(
+ ctx->client,
+ interface,
+ protocol,
+ name,
+ type,
+ domain,
+ AVAHI_PROTO_UNSPEC,
+ (AvahiLookupFlags)0,
+ resolve_callback,
+ userdata);
+ }
+ }
+
+ // ─── Discovery class ────────────────────────────────────────────
+
+ Discovery::Discovery() : impl_(std::make_unique<Impl>()) {}
+
+ Discovery::~Discovery() {
+ unpublish();
+ }
+
+ Discovery::Discovery(Discovery&&) noexcept = default;
+ Discovery& Discovery::operator=(Discovery&&) noexcept = default;
+
+ bool Discovery::publish(const std::string& name, uint16_t port, const std::string& fingerprint) {
+ unpublish();
+
+ impl_->pub_name = name;
+ impl_->pub_port = port;
+ impl_->pub_fingerprint = fingerprint;
+
+ impl_->pub_poll = avahi_threaded_poll_new();
+ if (!impl_->pub_poll) {
+ std::cerr << "mDNS: Failed to create threaded poll" << std::endl;
+ return false;
+ }
+
+ int error = 0;
+ impl_->pub_client = avahi_client_new(
+ avahi_threaded_poll_get(impl_->pub_poll),
+ (AvahiClientFlags)0,
+ pub_client_callback,
+ impl_.get(),
+ &error);
+
+ if (!impl_->pub_client) {
+ std::cerr << "mDNS: Failed to create client: " << avahi_strerror(error) << std::endl;
+ avahi_threaded_poll_free(impl_->pub_poll);
+ impl_->pub_poll = nullptr;
+ return false;
+ }
+
+ if (avahi_threaded_poll_start(impl_->pub_poll) < 0) {
+ std::cerr << "mDNS: Failed to start threaded poll" << std::endl;
+ avahi_client_free(impl_->pub_client);
+ impl_->pub_client = nullptr;
+ avahi_threaded_poll_free(impl_->pub_poll);
+ impl_->pub_poll = nullptr;
+ return false;
+ }
+
+ return true;
+ }
+
+ void Discovery::unpublish() {
+ if (impl_->pub_poll) {
+ avahi_threaded_poll_stop(impl_->pub_poll);
+ }
+ if (impl_->pub_group) {
+ avahi_entry_group_free(impl_->pub_group);
+ impl_->pub_group = nullptr;
+ }
+ if (impl_->pub_client) {
+ avahi_client_free(impl_->pub_client);
+ impl_->pub_client = nullptr;
+ }
+ if (impl_->pub_poll) {
+ avahi_threaded_poll_free(impl_->pub_poll);
+ impl_->pub_poll = nullptr;
+ }
+ }
+
+ std::vector<DiscoveredHost> Discovery::discover(int timeout_ms) {
+ AvahiThreadedPoll* poll = avahi_threaded_poll_new();
+ if (!poll) {
+ std::cerr << "mDNS: Failed to create browse poll" << std::endl;
+ return {};
+ }
+
+ // Clear previous results
+ {
+ std::lock_guard<std::mutex> lock(impl_->results_mutex);
+ impl_->results.clear();
+ }
+
+ int error = 0;
+ AvahiClient* client = avahi_client_new(
+ avahi_threaded_poll_get(poll),
+ (AvahiClientFlags)0,
+ [](AvahiClient*, AvahiClientState, void*) {}, // no-op callback
+ nullptr,
+ &error);
+
+ if (!client) {
+ std::cerr << "mDNS: Failed to create browse client: " << avahi_strerror(error) << std::endl;
+ avahi_threaded_poll_free(poll);
+ return {};
+ }
+
+ BrowseContext ctx{impl_.get(), client};
+
+ AvahiServiceBrowser* browser = avahi_service_browser_new(
+ client,
+ AVAHI_IF_UNSPEC,
+ AVAHI_PROTO_UNSPEC,
+ SERVICE_TYPE,
+ nullptr, // domain (default .local)
+ (AvahiLookupFlags)0,
+ browse_callback,
+ &ctx);
+
+ if (!browser) {
+ std::cerr << "mDNS: Failed to create browser: "
+ << avahi_strerror(avahi_client_errno(client)) << std::endl;
+ avahi_client_free(client);
+ avahi_threaded_poll_free(poll);
+ return {};
+ }
+
+ // Run the browse loop for the requested duration
+ avahi_threaded_poll_start(poll);
+ std::this_thread::sleep_for(std::chrono::milliseconds(timeout_ms));
+ avahi_threaded_poll_stop(poll);
+
+ avahi_service_browser_free(browser);
+ avahi_client_free(client);
+ avahi_threaded_poll_free(poll);
+
+ std::lock_guard<std::mutex> lock(impl_->results_mutex);
+ return impl_->results;
+ }
+
+} // namespace tether
+
diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp
index 88e83a4..6cb8f95 100644
--- a/src/daemon/main.cpp
+++ b/src/daemon/main.cpp
@@ -5,8 +5,10 @@
#include <tether/wayland.hpp>
#include <tether/file_transfer.hpp>
#include <tether/crypto.hpp>
+#include <tether/discovery.hpp>
#include <nlohmann/json.hpp>
#include <csignal>
+#include <unistd.h>
tether::EpollEventLoop* g_loop = nullptr;
@@ -51,6 +53,17 @@ int main(int argc, char** argv) {
return 1;
}
+ // Advertise this daemon on the local network via mDNS
+ tether::Discovery discovery;
+ {
+ char hostname[256] = {};
+ gethostname(hostname, sizeof(hostname) - 1);
+ std::string my_fp = tether::Crypto::instance().get_my_fingerprint();
+ if (!discovery.publish(hostname, 5134, my_fp)) {
+ std::cerr << "Warning: mDNS advertisement failed (is avahi-daemon running?)" << std::endl;
+ }
+ }
+
tether::WaylandContext wayland_srv(loop);
tether::g_wayland = &wayland_srv;
if (wayland_srv.init()) {
@@ -68,6 +81,7 @@ int main(int argc, char** argv) {
std::cout << "tetherd is running. Press Ctrl-C to stop." << std::endl;
loop.run();
+ // discovery destructor calls unpublish() automatically
std::cout << "tetherd shutdown complete." << std::endl;
return 0;
}
diff --git a/src/gtk/main.cpp b/src/gtk/main.cpp
index 33ab228..96e2742 100644
--- a/src/gtk/main.cpp
+++ b/src/gtk/main.cpp
@@ -1,8 +1,10 @@
#include <gtk/gtk.h>
#include <tether/client.hpp>
#include <tether/crypto.hpp>
+#include <tether/discovery.hpp>
#include <iostream>
#include <string>
+#include <thread>
#include <nlohmann/json.hpp>
// Always-connected local unix socket client.
@@ -15,6 +17,8 @@ static tether::Client* g_remote = nullptr;
static GtkWidget* text_view_clip;
static GtkWidget* lbl_status;
static GtkWidget* devices_list_box;
+static GtkWidget* discovered_list_box;
+static GtkWidget* lbl_discover_status;
static GtkWidget* entry_fingerprint;
static GtkWidget* entry_pair_host;
static GtkWidget* entry_pair_port;
@@ -168,6 +172,105 @@ static void refresh_device_list() {
gtk_widget_show_all(devices_list_box);
}
+// ─── mDNS Discovery ───
+
+struct DiscoverResult {
+ std::vector<tether::DiscoveredHost> hosts;
+};
+
+static gboolean on_discover_complete(gpointer data) {
+ auto* result = static_cast<DiscoverResult*>(data);
+
+ // Clear previous discovered list
+ GList* children = gtk_container_get_children(GTK_CONTAINER(discovered_list_box));
+ for (GList* iter = children; iter; iter = g_list_next(iter))
+ gtk_widget_destroy(GTK_WIDGET(iter->data));
+ g_list_free(children);
+
+ if (result->hosts.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);
+ 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>";
+
+ 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_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_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);
+
+ 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)";
+ gtk_label_set_text(GTK_LABEL(lbl_discover_status), status.c_str());
+ gtk_widget_show_all(discovered_list_box);
+
+ delete result;
+ return G_SOURCE_REMOVE;
+}
+
+static void on_btn_scan_clicked(GtkWidget*, gpointer) {
+ gtk_label_set_text(GTK_LABEL(lbl_discover_status), "Scanning...");
+
+ // Clear the list immediately to show scanning state
+ GList* children = gtk_container_get_children(GTK_CONTAINER(discovered_list_box));
+ for (GList* iter = children; iter; iter = g_list_next(iter))
+ gtk_widget_destroy(GTK_WIDGET(iter->data));
+ g_list_free(children);
+
+ GtkWidget* spinner_row = gtk_label_new("Scanning the local network...");
+ gtk_widget_set_halign(spinner_row, GTK_ALIGN_START);
+ gtk_container_add(GTK_CONTAINER(discovered_list_box), spinner_row);
+ gtk_widget_show_all(discovered_list_box);
+
+ // Run discovery in a background thread to avoid blocking the GTK main loop
+ std::thread([]() {
+ tether::Discovery discovery;
+ auto hosts = discovery.discover(3000);
+
+ auto* result = new DiscoverResult{std::move(hosts)};
+ g_idle_add(on_discover_complete, result);
+ }).detach();
+}
+
static void on_btn_accept_clicked(GtkWidget*, gpointer) {
const gchar* fp = gtk_entry_get_text(GTK_ENTRY(entry_fingerprint));
if (!fp || strlen(fp) == 0) return;
@@ -213,6 +316,37 @@ static GtkWidget* build_devices_tab() {
GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_container_set_border_width(GTK_CONTAINER(box), 16);
+ // ── Discovered on Network ──
+ GtkWidget* discover_header_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
+ GtkWidget* discover_lbl = gtk_label_new(NULL);
+ gtk_label_set_markup(GTK_LABEL(discover_lbl), "<b>Discovered on Network</b>");
+ gtk_widget_set_halign(discover_lbl, GTK_ALIGN_START);
+ gtk_box_pack_start(GTK_BOX(discover_header_row), discover_lbl, TRUE, TRUE, 0);
+
+ lbl_discover_status = gtk_label_new("");
+ gtk_style_context_add_class(gtk_widget_get_style_context(lbl_discover_status), "dim-label");
+ gtk_box_pack_start(GTK_BOX(discover_header_row), lbl_discover_status, FALSE, FALSE, 0);
+
+ GtkWidget* btn_scan = gtk_button_new_with_label("Scan");
+ g_signal_connect(btn_scan, "clicked", G_CALLBACK(on_btn_scan_clicked), NULL);
+ gtk_box_pack_end(GTK_BOX(discover_header_row), btn_scan, FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(box), discover_header_row, FALSE, FALSE, 0);
+
+ GtkWidget* discover_frame = gtk_frame_new(NULL);
+ GtkWidget* discover_scroll = gtk_scrolled_window_new(NULL, NULL);
+ gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(discover_scroll), 100);
+ discovered_list_box = gtk_list_box_new();
+ gtk_list_box_set_selection_mode(GTK_LIST_BOX(discovered_list_box), GTK_SELECTION_NONE);
+ GtkWidget* placeholder = gtk_label_new("Click Scan to discover devices.");
+ gtk_container_add(GTK_CONTAINER(discovered_list_box), placeholder);
+ gtk_container_add(GTK_CONTAINER(discover_scroll), discovered_list_box);
+ gtk_container_add(GTK_CONTAINER(discover_frame), discover_scroll);
+ gtk_box_pack_start(GTK_BOX(box), discover_frame, FALSE, FALSE, 0);
+
+ // ── Trusted Devices ──
+ GtkWidget* sep0 = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
+ gtk_box_pack_start(GTK_BOX(box), sep0, FALSE, FALSE, 2);
+
GtkWidget* header = gtk_label_new(NULL);
gtk_label_set_markup(GTK_LABEL(header), "<b>Trusted Devices</b>");
gtk_widget_set_halign(header, GTK_ALIGN_START);