Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix: daemons not dialing eachother

Zack Bartel28246b5c8e77
diff --git a/src/core/include/tether/discovery.hpp b/src/core/include/tether/discovery.hpp
index 0500ba4..3014f8a 100644
--- a/src/core/include/tether/discovery.hpp
+++ b/src/core/include/tether/discovery.hpp
@@ -78,6 +78,9 @@ namespace tether {
// Only one continuous browse session can run per Discovery instance.
void start_continuous_browse(std::function<void(const std::vector<DiscoveredDevice>&)> callback);
+ // Re-run the browse callback against the current cache.
+ void refresh();
+
// Stop continuous background discovery.
void stop_continuous_browse();
diff --git a/src/core/include/tether/net.hpp b/src/core/include/tether/net.hpp
index 9b17022..6041ad1 100644
--- a/src/core/include/tether/net.hpp
+++ b/src/core/include/tether/net.hpp
@@ -2,6 +2,7 @@
#include "tether/event_loop.hpp"
#include <filesystem>
+#include <functional>
#include <map>
#include <nlohmann/json.hpp>
#include <openssl/ssl.h>
@@ -92,12 +93,18 @@ namespace tether {
bool accept_device(const std::string& fingerprint, const std::string& fallback_name = "Paired Device");
// Dial a peer and add the session into this server's client tables
- bool connect_peer(const std::string& host, int port, const std::string& peer_name = "");
+ bool connect_peer(const std::string& host,
+ int port,
+ const std::string& peer_name = "",
+ const std::string& expected_fingerprint = "");
// Drops a pairing: unpins the fingerprint and closes any live session with it.
// Returns true when the fingerprint was pinned.
bool forget_device(const std::string& fingerprint);
+ // Invoked when the set of trusted peers gains a member with no live session.
+ void set_peers_changed_callback(std::function<void()> callback);
+
private:
struct ConnectedClientInfo {
std::string address;
@@ -108,7 +115,10 @@ namespace tether {
void handle_accept(int fd);
void handle_client(int client_fd);
- void adopt_peer(int fd, const std::string& host, const std::string& peer_name);
+ void adopt_peer(int fd,
+ const std::string& host,
+ const std::string& peer_name,
+ const std::string& expected_fingerprint);
void spawn_pair_dialog(int client_fd, SSL* ssl, const std::string& fingerprint, const std::string& device_name);
// Marks a session trusted and wires it into the broadcast registry.
void promote_session(int client_fd, const std::string& fingerprint, const std::string& device_name);
@@ -125,8 +135,11 @@ namespace tether {
std::map<int, ConnectedClientInfo> client_info_;
// fds we dialled
std::map<int, bool> client_initiated_;
+
std::set<std::string> dialing_;
+ std::function<void()> peers_changed_;
+
// Track spawned dialog child processes
struct PendingPairDialog {
pid_t pid;
diff --git a/src/core/src/discovery.cpp b/src/core/src/discovery.cpp
index 30e6a82..919eb71 100644
--- a/src/core/src/discovery.cpp
+++ b/src/core/src/discovery.cpp
@@ -598,6 +598,19 @@ namespace tether {
avahi_threaded_poll_start(impl_->browse_poll);
}
+ void Discovery::refresh() {
+ std::function<void(const std::vector<DiscoveredDevice>&)> cb;
+ std::vector<DiscoveredDevice> grouped;
+ {
+ std::lock_guard<std::mutex> lock(impl_->results_mutex);
+ if (!impl_->browse_callback)
+ return;
+ cb = impl_->browse_callback;
+ grouped = group_discovered_hosts(impl_->results, impl_->pub_fingerprint);
+ }
+ cb(grouped);
+ }
+
void Discovery::stop_continuous_browse() {
if (!impl_->browse_poll)
return;
diff --git a/src/core/src/net.cpp b/src/core/src/net.cpp
index 68f1596..9af8d11 100644
--- a/src/core/src/net.cpp
+++ b/src/core/src/net.cpp
@@ -1484,27 +1484,38 @@ namespace tether {
return my_fingerprint < peer_fingerprint;
}
- bool TcpServer::connect_peer(const std::string& host, int port, const std::string& peer_name) {
+ // A dial is identified by the peer's fingerprint when mDNS gave us one, and by address otherwise.
+ static std::string dial_key_for(const std::string& host, const std::string& fingerprint) {
+ return fingerprint.empty() ? host : fingerprint;
+ }
+
+ bool TcpServer::connect_peer(const std::string& host,
+ int port,
+ const std::string& peer_name,
+ const std::string& expected_fingerprint) {
if (host.empty())
return false;
+ const bool by_fingerprint = !expected_fingerprint.empty();
for (auto const& [fd, remote] : connected_remote_clients) {
(void)fd;
- if (remote.address == host) {
+ const bool match = by_fingerprint ? remote.fingerprint == expected_fingerprint : remote.address == host;
+ if (match) {
debug::log(INFO, "TcpServer: Already holding a session with {}", host);
return true;
}
}
- if (!dialing_.insert(host).second) {
+ const std::string dial_key = dial_key_for(host, expected_fingerprint);
+ if (!dialing_.insert(dial_key).second) {
debug::log(INFO, "TcpServer: Dial to {} already in flight", host);
return true;
}
// connect() blocks, and the loop watches EPOLLIN only
- std::thread([this, host, port, peer_name]() {
+ std::thread([this, host, port, peer_name, expected_fingerprint, dial_key]() {
// adopt_peer clears it on the success path, once the fd is in the client tables.
- auto give_up = [&] { loop_.post([this, host]() { dialing_.erase(host); }); };
+ auto give_up = [&] { loop_.post([this, dial_key]() { dialing_.erase(dial_key); }); };
auto fail = [&](const char* reason) {
give_up();
@@ -1571,14 +1582,19 @@ namespace tether {
timeval none{0, 0};
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &none, sizeof(none));
- loop_.post([this, sock, host, peer_name]() { adopt_peer(sock, host, peer_name); });
+ loop_.post([this, sock, host, peer_name, expected_fingerprint]() {
+ adopt_peer(sock, host, peer_name, expected_fingerprint);
+ });
}).detach();
return true;
}
- void TcpServer::adopt_peer(int fd, const std::string& host, const std::string& peer_name) {
- dialing_.erase(host);
+ void TcpServer::adopt_peer(int fd,
+ const std::string& host,
+ const std::string& peer_name,
+ const std::string& expected_fingerprint) {
+ dialing_.erase(dial_key_for(host, expected_fingerprint));
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
@@ -1597,6 +1613,7 @@ namespace tether {
client_info_[fd].device_name = peer_name;
connected_remote_clients[fd].address = host;
connected_remote_clients[fd].device_name = peer_name;
+ connected_remote_clients[fd].fingerprint = expected_fingerprint;
loop_.addFd(fd, [this](int cfd) { handle_client(cfd); });
@@ -1905,6 +1922,8 @@ namespace tether {
broadcast_local_event(connected_event.dump());
}
+ void TcpServer::set_peers_changed_callback(std::function<void()> callback) { peers_changed_ = std::move(callback); }
+
bool TcpServer::forget_device(const std::string& fingerprint) {
if (fingerprint.empty())
return false;
@@ -1977,6 +1996,9 @@ namespace tether {
{"connected", promoted}};
broadcast_local_event(event.dump());
+ if (!promoted && peers_changed_)
+ peers_changed_();
+
// An external acceptance supersedes any dialog for the same request.
// Its exit callback observes the promoted session and ignores the stale
// non-zero result rather than sending pair_rejected to the phone.
diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp
index 7b0ef40..864ed15 100644
--- a/src/daemon/main.cpp
+++ b/src/daemon/main.cpp
@@ -106,8 +106,8 @@ int main(int argc, char** argv) {
// on avahi's poll thread
const auto& addr = dev.addresses.front();
- loop.post([&tcp_srv, host = addr.address, port = addr.port, name = dev.name]() {
- tcp_srv.connect_peer(host, port, name);
+ loop.post([&tcp_srv, host = addr.address, port = addr.port, name = dev.name, fp = dev.fingerprint]() {
+ tcp_srv.connect_peer(host, port, name, fp);
});
}
@@ -129,6 +129,8 @@ int main(int argc, char** argv) {
}
tether::broadcast_local_event(payload.dump());
});
+
+ tcp_srv.set_peers_changed_callback([&discovery] { discovery.refresh(); });
}
tether::WaylandContext wayland_srv(loop);
diff --git a/test/test_discovery.cpp b/test/test_discovery.cpp
index f3a831f..a8fa298 100644
--- a/test/test_discovery.cpp
+++ b/test/test_discovery.cpp
@@ -127,3 +127,56 @@ TEST(DiscoveryTest, StateCallbackReportsOnlyChanges) {
EXPECT_NE(reports[i], reports[i - 1]) << "duplicate report at index " << i;
}
}
+
+// refresh() is called from the daemon whenever a pairing is accepted, which can happen
+// before any browse is running.
+TEST(DiscoveryTest, RefreshWithoutBrowseIsNoop) {
+ tether::Discovery discovery;
+ discovery.refresh();
+}
+
+// The dial sweep lives in the browse callback, and Avahi only announces a service once.
+// refresh() is what gives a newly trusted peer a second look.
+TEST(DiscoveryTest, RefreshReinvokesBrowseCallback) {
+ tether::Discovery discovery;
+
+ std::mutex mutex;
+ int calls = 0;
+ discovery.start_continuous_browse([&](const std::vector<tether::DiscoveredDevice>&) {
+ std::lock_guard<std::mutex> lock(mutex);
+ ++calls;
+ });
+
+ {
+ std::lock_guard<std::mutex> lock(mutex);
+ calls = 0; // ignore anything Avahi announced on its own
+ }
+
+ discovery.refresh();
+
+ std::lock_guard<std::mutex> lock(mutex);
+ EXPECT_EQ(calls, 1);
+}
+
+// stop_continuous_browse drops the callback, so a late refresh must not reach it.
+TEST(DiscoveryTest, RefreshAfterStopIsNoop) {
+ tether::Discovery discovery;
+
+ std::mutex mutex;
+ int calls = 0;
+ discovery.start_continuous_browse([&](const std::vector<tether::DiscoveredDevice>&) {
+ std::lock_guard<std::mutex> lock(mutex);
+ ++calls;
+ });
+ discovery.stop_continuous_browse();
+
+ {
+ std::lock_guard<std::mutex> lock(mutex);
+ calls = 0;
+ }
+
+ discovery.refresh();
+
+ std::lock_guard<std::mutex> lock(mutex);
+ EXPECT_EQ(calls, 0);
+}