diff options
-rw-r--r-- | adb_listeners.cpp | 20 | ||||
-rw-r--r-- | adb_listeners.h | 2 | ||||
-rw-r--r-- | client/main.cpp | 2 |
3 files changed, 14 insertions, 10 deletions
diff --git a/adb_listeners.cpp b/adb_listeners.cpp index 6f7f3438..c24016fb 100644 --- a/adb_listeners.cpp +++ b/adb_listeners.cpp @@ -33,15 +33,18 @@ #include "transport.h" // A listener is an entity which binds to a local port and, upon receiving a connection on that -// port, creates an asocket to connect the new local connection to a specific remote service. +// port, creates an asocket to connect the new local connection to a specific remote service. They +// are mostly used to implement forward and reverse-forward. // -// TODO: some listeners read from the new connection to determine what exact service to connect to -// on the far side. +// Some listeners, called "smartsockets" read from the new connection to determine what exact +// service to connect to on the far side. This is implemented with a different fdevent handler. class alistener { public: alistener(const std::string& _local_name, const std::string& _connect_to); ~alistener(); + bool isSmartSocket() { return connect_to == kSmartSocketConnectTo; } + fdevent* fde = nullptr; int fd = -1; @@ -127,8 +130,7 @@ std::string format_listeners() EXCLUDES(listener_list_mutex) { std::lock_guard<std::mutex> lock(listener_list_mutex); std::string result; for (auto& l : listener_list) { - // Ignore special listeners like those for *smartsocket* - if (l->connect_to[0] == '*') { + if (l->isSmartSocket()) { continue; } // <device-serial> " " <local-name> " " <remote-name> "\n" @@ -170,7 +172,7 @@ void remove_all_listeners() EXCLUDES(listener_list_mutex) { void enable_server_sockets() EXCLUDES(listener_list_mutex) { std::lock_guard<std::mutex> lock(listener_list_mutex); for (auto& l : listener_list) { - if (l->connect_to == "*smartsocket*") { + if (l->isSmartSocket()) { fdevent_set(l->fde, FDE_READ); } } @@ -179,7 +181,7 @@ void enable_server_sockets() EXCLUDES(listener_list_mutex) { void close_smartsockets() EXCLUDES(listener_list_mutex) { std::lock_guard<std::mutex> lock(listener_list_mutex); auto pred = [](const std::unique_ptr<alistener>& listener) { - return listener->connect_to == "*smartsocket*"; + return listener->isSmartSocket(); }; listener_list.remove_if(pred); } @@ -192,7 +194,7 @@ InstallStatus install_listener(const std::string& local_name, const char* connec for (auto& l : listener_list) { if (local_name == l->local_name) { // Can't repurpose a smartsocket. - if (l->connect_to[0] == '*') { + if (l->isSmartSocket()) { *error = "cannot repurpose smartsocket"; return INSTALL_STATUS_INTERNAL_ERROR; } @@ -230,7 +232,7 @@ InstallStatus install_listener(const std::string& local_name, const char* connec } close_on_exec(listener->fd); - if (listener->connect_to == "*smartsocket*") { + if (listener->isSmartSocket()) { #if ADB_HOST listener->fde = fdevent_create(listener->fd, ss_listener_event_func, listener.get()); #else diff --git a/adb_listeners.h b/adb_listeners.h index 0aa774a7..7fd46f02 100644 --- a/adb_listeners.h +++ b/adb_listeners.h @@ -22,6 +22,8 @@ #include <android-base/macros.h> +inline constexpr const char* kSmartSocketConnectTo = "*smartsocket*"; + // error/status codes for install_listener. enum InstallStatus { INSTALL_STATUS_OK = 0, diff --git a/client/main.cpp b/client/main.cpp index 27e7badd..818d305a 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -157,7 +157,7 @@ int adb_server_main(int is_daemon, const std::string& socket_spec, const char* o // If we told a previous adb server to quit because of version mismatch, we can get to this // point before it's finished exiting. Retry for a while to give it some time. Don't actually // accept any connections until adb_wait_for_device_initialization finishes below. - while (install_listener(socket_spec, "*smartsocket*", nullptr, INSTALL_LISTENER_DISABLED, + while (install_listener(socket_spec, kSmartSocketConnectTo, nullptr, INSTALL_LISTENER_DISABLED, nullptr, &error) != INSTALL_STATUS_OK) { if (std::chrono::steady_clock::now() - start > 0.5s) { LOG(FATAL) << "could not install *smartsocket* listener: " << error; |