Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix: improve mdns discovery

Zack Barteleb60a5815c02
diff --git a/src/core/include/tether/net.hpp b/src/core/include/tether/net.hpp
index 4c0a1d3..13d4b73 100644
--- a/src/core/include/tether/net.hpp
+++ b/src/core/include/tether/net.hpp
@@ -32,6 +32,9 @@ namespace tether {
void unregister_local_subscriber(int fd);
void broadcast_local_event(const std::string& msg, int exclude_fd = -1);
+ // The mDNS peers the daemon currently knows of.
+ void set_discovered_devices(const nlohmann::json& devices);
+
// Normalizes stored pairing requests to {fingerprint: {name, ts}} and drops
// the ones older than the TTL.
nlohmann::json prune_pending_pairs(const nlohmann::json& raw, int64_t now);
diff --git a/src/core/src/net.cpp b/src/core/src/net.cpp
index 11bcb42..8c89534 100644
--- a/src/core/src/net.cpp
+++ b/src/core/src/net.cpp
@@ -66,6 +66,10 @@ namespace tether {
static std::set<int> local_subscribers;
static std::mutex g_subscribers_mutex;
+ // last known mdns peers.
+ static std::mutex g_discovered_mutex;
+ static nlohmann::json g_discovered_devices = nlohmann::json::array();
+
struct ReceivedFileInfo {
std::string path;
std::string filename;
@@ -164,6 +168,11 @@ namespace tether {
broadcast_local_event(make_otp_event({code, sender_domain, id}).dump(), exclude_fd);
}
+ void set_discovered_devices(const nlohmann::json& devices) {
+ std::lock_guard<std::mutex> lock(g_discovered_mutex);
+ g_discovered_devices = devices.is_array() ? devices : nlohmann::json::array();
+ }
+
void register_local_subscriber(int fd) {
std::lock_guard<std::mutex> lock(g_subscribers_mutex);
local_subscribers.insert(fd);
@@ -826,6 +835,11 @@ namespace tether {
snapshot["recent_received_files"].push_back(item);
}
+ {
+ std::lock_guard<std::mutex> lock(g_discovered_mutex);
+ snapshot["discovered_devices"] = g_discovered_devices;
+ }
+
snapshot["mdns_available"] = g_mdns_available.load();
snapshot["clipboard_available"] = g_wayland && g_wayland->clipboard_available();
snapshot["firewall_active"] = firewall_active();
@@ -1589,6 +1603,7 @@ namespace tether {
}
payload["devices"].push_back(d);
}
+ set_discovered_devices(payload["devices"]);
broadcast_local_event(payload.dump());
}).detach();
} else if (j.contains("command") && j["command"] == "send_file" && j.contains("path")) {
diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp
index 9408e0d..a9ff3a4 100644
--- a/src/daemon/main.cpp
+++ b/src/daemon/main.cpp
@@ -145,6 +145,7 @@ int main(int argc, char** argv) {
}
payload["devices"].push_back(d);
}
+ tether::set_discovered_devices(payload["devices"]);
tether::broadcast_local_event(payload.dump());
});
diff --git a/src/gtk/devices_view.cpp b/src/gtk/devices_view.cpp
index 64ad254..e95b14f 100644
--- a/src/gtk/devices_view.cpp
+++ b/src/gtk/devices_view.cpp
@@ -837,6 +837,21 @@ namespace tether::ui {
g_devices.mdns_ok = j.value("mdns_available", true);
g_devices.clipboard_ok = j.value("clipboard_available", true);
g_devices.firewall_active = j.value("firewall_active", false);
+
+ if (j.contains("discovered_devices") && j["discovered_devices"].is_array()) {
+ g_devices.discovered_devices.clear();
+ for (const auto& d : j["discovered_devices"]) {
+ tether::DiscoveredDevice dev;
+ dev.name = d.value("name", "");
+ dev.fingerprint = d.value("fingerprint", "");
+ if (d.contains("addresses") && d["addresses"].is_array()) {
+ for (const auto& a : d["addresses"])
+ dev.addresses.push_back({a.value("address", ""), a.value<uint16_t>("port", 5134)});
+ }
+ g_devices.discovered_devices.push_back(dev);
+ }
+ }
+
g_devices.connected_fps.clear();
g_devices.unpaired_clients.clear();
if (j.contains("connected_clients") && j["connected_clients"].is_array()) {