Skip to content

Linux + iPhone Continuity / iMessage / SMS

properly handle async communication in native host

Zack Barteld98a6d89f7f8
diff --git a/src/cli/main.cpp b/src/cli/main.cpp
index 1bb4e4b..dac1f83 100644
--- a/src/cli/main.cpp
+++ b/src/cli/main.cpp
@@ -1,25 +1,67 @@
+#include <atomic>
#include <string>
#include <tether/client.hpp>
#include <tether/crypto.hpp>
#include <tether/discovery.hpp>
#include <tether/log.hpp>
+#include <thread>
#include <unistd.h>
#include <vector>
void run_native_messaging_host(tether::Client& client) {
debug::log(INFO, "Starting Native Messaging Host loop...\n");
- while (true) {
+
+ // Subscribe to daemon broadcast events
+ client.send("{\"command\":\"subscribe\"}\n");
+
+ std::atomic<bool> running{true};
+
+ // read asynchronous events/responses from tetherd and send to browser
+ std::thread reader_thread([&]() {
+ std::string buffer;
+ char buf[8192];
+ int daemon_sock = client.get_socket();
+
+ while (running) {
+ ssize_t n = read(daemon_sock, buf, sizeof(buf));
+ if (n <= 0) {
+ running = false;
+ break;
+ }
+ buffer.append(buf, n);
+
+ size_t pos;
+ while ((pos = buffer.find('\n')) != std::string::npos) {
+ std::string msg = buffer.substr(0, pos);
+ buffer.erase(0, pos + 1);
+
+ if (msg.empty())
+ continue;
+
+ uint32_t out_length = msg.length();
+ std::cout.write(reinterpret_cast<const char*>(&out_length), sizeof(out_length));
+ std::cout.write(msg.data(), out_length);
+ std::cout.flush();
+ }
+ }
+ });
+
+ // main thread reads commands from browser and forwards to tetherd
+ while (running) {
uint32_t length = 0;
if (!std::cin.read(reinterpret_cast<char*>(&length), sizeof(length))) {
+ running = false;
break;
}
if (length == 0 || length > 10 * 1024 * 1024) {
+ running = false;
break;
}
std::vector<char> buffer(length);
if (!std::cin.read(buffer.data(), length)) {
+ running = false;
break;
}
@@ -28,16 +70,15 @@ void run_native_messaging_host(tether::Client& client) {
payload += "\n";
}
- std::string response = client.send_and_wait(payload);
-
- if (!response.empty() && response.back() == '\n') {
- response.pop_back();
+ if (!client.send(payload)) {
+ running = false;
+ break;
}
+ }
- uint32_t out_length = response.length();
- std::cout.write(reinterpret_cast<const char*>(&out_length), sizeof(out_length));
- std::cout.write(response.data(), out_length);
- std::cout.flush();
+ client.disconnect(); // unblock reader thread
+ if (reader_thread.joinable()) {
+ reader_thread.join();
}
}
diff --git a/src/core/include/tether/client.hpp b/src/core/include/tether/client.hpp
index 39870cf..af2ee5d 100644
--- a/src/core/include/tether/client.hpp
+++ b/src/core/include/tether/client.hpp
@@ -17,8 +17,12 @@ namespace tether {
bool is_connected() const;
// Base Level payload loop
+ bool send(const std::string& payload);
std::string send_and_wait(const std::string& payload);
+ bool send_file(const std::string& path, std::string& err_out);
+
std::string get_peer_fingerprint() const;
+ int get_socket() const;
// High Level Features
std::string get_clipboard(std::string& err_out);
@@ -28,8 +32,6 @@ namespace tether {
bool accept_device(const std::string& fingerprint, const std::string& name = "Paired Device");
std::string pair(const std::string& device_name, std::string& err_out);
- bool send_file(const std::string& path, std::string& err_out);
-
private:
int connect_unix(bool retry);
diff --git a/src/core/src/client.cpp b/src/core/src/client.cpp
index 98d6598..97fdf9d 100644
--- a/src/core/src/client.cpp
+++ b/src/core/src/client.cpp
@@ -152,6 +152,17 @@ namespace tether {
return "";
}
+ bool Client::send(const std::string& payload) {
+ if (ssl_) {
+ int w = SSL_write(ssl_, payload.c_str(), payload.size());
+ return w > 0;
+ } else {
+ return write(sock_, payload.c_str(), payload.size()) > 0;
+ }
+ }
+
+ int Client::get_socket() const { return sock_; }
+
std::string Client::get_clipboard(std::string& err_out) {
nlohmann::json j;
j["command"] = "clipboard_get";