The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRANSPORT |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 18 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 19 | #include "sysdeps.h" |
Josh Gao | 31b5be6 | 2018-03-07 16:51:08 -0800 | [diff] [blame] | 20 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 21 | #include "transport.h" |
| 22 | |
Dan Albert | 055f1aa | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 23 | #include <ctype.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 24 | #include <errno.h> |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 25 | #include <inttypes.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #include <string.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 31 | #include <algorithm> |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 32 | #include <list> |
Pirama Arumuga Nainar | 29e3dd8 | 2018-08-08 10:33:24 -0700 | [diff] [blame] | 33 | #include <memory> |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 34 | #include <mutex> |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 35 | #include <set> |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 36 | #include <thread> |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 37 | |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 38 | #include <adb/crypto/rsa_2048_key.h> |
| 39 | #include <adb/crypto/x509_generator.h> |
| 40 | #include <adb/tls/tls_connection.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 41 | #include <android-base/logging.h> |
Yurii Zubrytskyi | 6fc26df | 2020-03-26 18:19:28 -0700 | [diff] [blame] | 42 | #include <android-base/no_destructor.h> |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 43 | #include <android-base/parsenetaddress.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 44 | #include <android-base/stringprintf.h> |
| 45 | #include <android-base/strings.h> |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 46 | #include <android-base/thread_annotations.h> |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 47 | |
Josh Gao | 2776845 | 2018-01-02 12:01:43 -0800 | [diff] [blame] | 48 | #include <diagnose_usb.h> |
| 49 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | #include "adb.h" |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 51 | #include "adb_auth.h" |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 52 | #include "adb_io.h" |
Josh Gao | cfe72e2 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 53 | #include "adb_trace.h" |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 54 | #include "adb_utils.h" |
Josh Gao | 57e09b1 | 2019-06-28 13:50:37 -0700 | [diff] [blame] | 55 | #include "fdevent/fdevent.h" |
Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 56 | #include "sysdeps/chrono.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 58 | using namespace adb::crypto; |
| 59 | using namespace adb::tls; |
Josh Gao | 982f7bd | 2019-02-12 13:59:03 -0800 | [diff] [blame] | 60 | using android::base::ScopedLockAssertion; |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 61 | using TlsError = TlsConnection::TlsError; |
Josh Gao | 982f7bd | 2019-02-12 13:59:03 -0800 | [diff] [blame] | 62 | |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 63 | static void remove_transport(atransport* transport); |
Josh Gao | 9a8366b | 2019-12-09 13:45:31 -0800 | [diff] [blame] | 64 | static void transport_destroy(atransport* transport); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 66 | // TODO: unordered_map<TransportId, atransport*> |
Josh Gao | b7b1edf | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 67 | static auto& transport_list = *new std::list<atransport*>(); |
| 68 | static auto& pending_list = *new std::list<atransport*>(); |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 69 | |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 70 | static auto& transport_lock = *new std::recursive_mutex(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | |
Todd Kennedy | 51c05ec | 2015-11-10 00:03:25 +0000 | [diff] [blame] | 72 | const char* const kFeatureShell2 = "shell_v2"; |
| 73 | const char* const kFeatureCmd = "cmd"; |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 74 | const char* const kFeatureStat2 = "stat_v2"; |
Josh Gao | 34a478f | 2019-08-07 14:23:17 -0700 | [diff] [blame] | 75 | const char* const kFeatureLs2 = "ls_v2"; |
Josh Gao | 5d1756c | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 76 | const char* const kFeatureLibusb = "libusb"; |
Dan Albert | 5176df8 | 2017-05-23 14:30:00 -0700 | [diff] [blame] | 77 | const char* const kFeaturePushSync = "push_sync"; |
Dario Freni | 29814de | 2018-10-04 16:26:40 +0100 | [diff] [blame] | 78 | const char* const kFeatureApex = "apex"; |
Josh Gao | fb08510 | 2018-10-22 13:00:05 -0700 | [diff] [blame] | 79 | const char* const kFeatureFixedPushMkdir = "fixed_push_mkdir"; |
Alex Buynytskyy | 01a65ee | 2019-01-17 13:13:56 -0800 | [diff] [blame] | 80 | const char* const kFeatureAbb = "abb"; |
Josh Gao | 7b1cb66 | 2019-02-20 13:01:40 -0800 | [diff] [blame] | 81 | const char* const kFeatureFixedPushSymlinkTimestamp = "fixed_push_symlink_timestamp"; |
Alex Buynytskyy | 05626c1 | 2019-02-21 14:22:51 -0800 | [diff] [blame] | 82 | const char* const kFeatureAbbExec = "abb_exec"; |
Josh Gao | 8c2198c | 2019-07-11 14:15:32 -0700 | [diff] [blame] | 83 | const char* const kFeatureRemountShell = "remount_shell"; |
Shukang Zhou | f4ffae1 | 2020-02-13 17:01:39 -0800 | [diff] [blame] | 84 | const char* const kFeatureTrackApp = "track_app"; |
Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 85 | const char* const kFeatureSendRecv2 = "sendrecv_v2"; |
| 86 | const char* const kFeatureSendRecv2Brotli = "sendrecv_v2_brotli"; |
Josh Gao | ec44d35 | 2020-03-26 22:02:03 -0700 | [diff] [blame] | 87 | const char* const kFeatureSendRecv2LZ4 = "sendrecv_v2_lz4"; |
Josh Gao | 5949fcc | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 88 | const char* const kFeatureSendRecv2DryRunSend = "sendrecv_v2_dry_run_send"; |
Todd Kennedy | 51c05ec | 2015-11-10 00:03:25 +0000 | [diff] [blame] | 89 | |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 90 | namespace { |
| 91 | |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 92 | #if ADB_HOST |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 93 | // Tracks and handles atransport*s that are attempting reconnection. |
| 94 | class ReconnectHandler { |
| 95 | public: |
| 96 | ReconnectHandler() = default; |
| 97 | ~ReconnectHandler() = default; |
| 98 | |
| 99 | // Starts the ReconnectHandler thread. |
| 100 | void Start(); |
| 101 | |
| 102 | // Requests the ReconnectHandler thread to stop. |
| 103 | void Stop(); |
| 104 | |
| 105 | // Adds the atransport* to the queue of reconnect attempts. |
| 106 | void TrackTransport(atransport* transport); |
| 107 | |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 108 | // Wake up the ReconnectHandler thread to have it check for kicked transports. |
| 109 | void CheckForKicked(); |
| 110 | |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 111 | private: |
| 112 | // The main thread loop. |
| 113 | void Run(); |
| 114 | |
| 115 | // Tracks a reconnection attempt. |
| 116 | struct ReconnectAttempt { |
| 117 | atransport* transport; |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 118 | std::chrono::steady_clock::time_point reconnect_time; |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 119 | size_t attempts_left; |
Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 120 | |
| 121 | bool operator<(const ReconnectAttempt& rhs) const { |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 122 | if (reconnect_time == rhs.reconnect_time) { |
| 123 | return reinterpret_cast<uintptr_t>(transport) < |
| 124 | reinterpret_cast<uintptr_t>(rhs.transport); |
| 125 | } |
| 126 | return reconnect_time < rhs.reconnect_time; |
Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 127 | } |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | // Only retry for up to one minute. |
Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 131 | static constexpr const std::chrono::seconds kDefaultTimeout = 10s; |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 132 | static constexpr const size_t kMaxAttempts = 6; |
| 133 | |
| 134 | // Protects all members. |
| 135 | std::mutex reconnect_mutex_; |
| 136 | bool running_ GUARDED_BY(reconnect_mutex_) = true; |
| 137 | std::thread handler_thread_; |
| 138 | std::condition_variable reconnect_cv_; |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 139 | std::set<ReconnectAttempt> reconnect_queue_ GUARDED_BY(reconnect_mutex_); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 140 | |
| 141 | DISALLOW_COPY_AND_ASSIGN(ReconnectHandler); |
| 142 | }; |
| 143 | |
| 144 | void ReconnectHandler::Start() { |
| 145 | check_main_thread(); |
| 146 | handler_thread_ = std::thread(&ReconnectHandler::Run, this); |
| 147 | } |
| 148 | |
| 149 | void ReconnectHandler::Stop() { |
| 150 | check_main_thread(); |
| 151 | { |
| 152 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 153 | running_ = false; |
| 154 | } |
| 155 | reconnect_cv_.notify_one(); |
| 156 | handler_thread_.join(); |
| 157 | |
| 158 | // Drain the queue to free all resources. |
| 159 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 160 | while (!reconnect_queue_.empty()) { |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 161 | ReconnectAttempt attempt = *reconnect_queue_.begin(); |
| 162 | reconnect_queue_.erase(reconnect_queue_.begin()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 163 | remove_transport(attempt.transport); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void ReconnectHandler::TrackTransport(atransport* transport) { |
| 168 | check_main_thread(); |
| 169 | { |
| 170 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 171 | if (!running_) return; |
Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 172 | // Arbitrary sleep to give adbd time to get ready, if we disconnected because it exited. |
| 173 | auto reconnect_time = std::chrono::steady_clock::now() + 250ms; |
| 174 | reconnect_queue_.emplace( |
| 175 | ReconnectAttempt{transport, reconnect_time, ReconnectHandler::kMaxAttempts}); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 176 | } |
| 177 | reconnect_cv_.notify_one(); |
| 178 | } |
| 179 | |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 180 | void ReconnectHandler::CheckForKicked() { |
| 181 | reconnect_cv_.notify_one(); |
| 182 | } |
| 183 | |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 184 | void ReconnectHandler::Run() { |
| 185 | while (true) { |
| 186 | ReconnectAttempt attempt; |
| 187 | { |
| 188 | std::unique_lock<std::mutex> lock(reconnect_mutex_); |
Josh Gao | 982f7bd | 2019-02-12 13:59:03 -0800 | [diff] [blame] | 189 | ScopedLockAssertion assume_lock(reconnect_mutex_); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 190 | |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 191 | if (!reconnect_queue_.empty()) { |
| 192 | // FIXME: libstdc++ (used on Windows) implements condition_variable with |
| 193 | // system_clock as its clock, so we're probably hosed if the clock changes, |
| 194 | // even if we use steady_clock throughout. This problem goes away once we |
| 195 | // switch to libc++. |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 196 | reconnect_cv_.wait_until(lock, reconnect_queue_.begin()->reconnect_time); |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 197 | } else { |
| 198 | reconnect_cv_.wait(lock); |
| 199 | } |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 200 | |
| 201 | if (!running_) return; |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 202 | |
| 203 | // Scan the whole list for kicked transports, so that we immediately handle an explicit |
| 204 | // disconnect request. |
| 205 | bool kicked = false; |
| 206 | for (auto it = reconnect_queue_.begin(); it != reconnect_queue_.end();) { |
| 207 | if (it->transport->kicked()) { |
| 208 | D("transport %s was kicked. giving up on it.", it->transport->serial.c_str()); |
| 209 | remove_transport(it->transport); |
| 210 | it = reconnect_queue_.erase(it); |
| 211 | } else { |
| 212 | ++it; |
| 213 | } |
| 214 | kicked = true; |
| 215 | } |
| 216 | |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 217 | if (reconnect_queue_.empty()) continue; |
| 218 | |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 219 | // Go back to sleep if we either woke up spuriously, or we were woken up to remove |
| 220 | // a kicked transport, and the first transport isn't ready for reconnection yet. |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 221 | auto now = std::chrono::steady_clock::now(); |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 222 | if (reconnect_queue_.begin()->reconnect_time > now) { |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 223 | continue; |
| 224 | } |
| 225 | |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 226 | attempt = *reconnect_queue_.begin(); |
| 227 | reconnect_queue_.erase(reconnect_queue_.begin()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 228 | } |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 229 | D("attempting to reconnect %s", attempt.transport->serial.c_str()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 230 | |
Josh Gao | fc2e56f | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 231 | switch (attempt.transport->Reconnect()) { |
| 232 | case ReconnectResult::Retry: { |
| 233 | D("attempting to reconnect %s failed.", attempt.transport->serial.c_str()); |
| 234 | if (attempt.attempts_left == 0) { |
| 235 | D("transport %s exceeded the number of retry attempts. giving up on it.", |
| 236 | attempt.transport->serial.c_str()); |
| 237 | remove_transport(attempt.transport); |
| 238 | continue; |
| 239 | } |
| 240 | |
| 241 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 242 | reconnect_queue_.emplace(ReconnectAttempt{ |
| 243 | attempt.transport, |
| 244 | std::chrono::steady_clock::now() + ReconnectHandler::kDefaultTimeout, |
| 245 | attempt.attempts_left - 1}); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 246 | continue; |
| 247 | } |
| 248 | |
Josh Gao | fc2e56f | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 249 | case ReconnectResult::Success: |
| 250 | D("reconnection to %s succeeded.", attempt.transport->serial.c_str()); |
| 251 | register_transport(attempt.transport); |
| 252 | continue; |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 253 | |
Josh Gao | fc2e56f | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 254 | case ReconnectResult::Abort: |
| 255 | D("cancelling reconnection attempt to %s.", attempt.transport->serial.c_str()); |
| 256 | remove_transport(attempt.transport); |
| 257 | continue; |
| 258 | } |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | static auto& reconnect_handler = *new ReconnectHandler(); |
| 263 | |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 264 | #endif |
| 265 | |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 266 | } // namespace |
| 267 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 268 | TransportId NextTransportId() { |
| 269 | static std::atomic<TransportId> next(1); |
| 270 | return next++; |
| 271 | } |
| 272 | |
Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 273 | void Connection::Reset() { |
| 274 | LOG(INFO) << "Connection::Reset(): stopping"; |
| 275 | Stop(); |
| 276 | } |
| 277 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 278 | BlockingConnectionAdapter::BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection) |
| 279 | : underlying_(std::move(connection)) {} |
| 280 | |
| 281 | BlockingConnectionAdapter::~BlockingConnectionAdapter() { |
| 282 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): destructing"; |
| 283 | Stop(); |
| 284 | } |
| 285 | |
| 286 | void BlockingConnectionAdapter::Start() { |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 287 | std::lock_guard<std::mutex> lock(mutex_); |
| 288 | if (started_) { |
| 289 | LOG(FATAL) << "BlockingConnectionAdapter(" << this->transport_name_ |
| 290 | << "): started multiple times"; |
| 291 | } |
| 292 | |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 293 | StartReadThread(); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 294 | |
| 295 | write_thread_ = std::thread([this]() { |
| 296 | LOG(INFO) << this->transport_name_ << ": write thread spawning"; |
| 297 | while (true) { |
| 298 | std::unique_lock<std::mutex> lock(mutex_); |
Josh Gao | 982f7bd | 2019-02-12 13:59:03 -0800 | [diff] [blame] | 299 | ScopedLockAssertion assume_locked(mutex_); |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 300 | cv_.wait(lock, [this]() REQUIRES(mutex_) { |
| 301 | return this->stopped_ || !this->write_queue_.empty(); |
| 302 | }); |
| 303 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 304 | if (this->stopped_) { |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | std::unique_ptr<apacket> packet = std::move(this->write_queue_.front()); |
| 309 | this->write_queue_.pop_front(); |
| 310 | lock.unlock(); |
| 311 | |
| 312 | if (!this->underlying_->Write(packet.get())) { |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "write failed"); }); |
| 317 | }); |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 318 | |
| 319 | started_ = true; |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 322 | void BlockingConnectionAdapter::StartReadThread() { |
| 323 | read_thread_ = std::thread([this]() { |
| 324 | LOG(INFO) << this->transport_name_ << ": read thread spawning"; |
| 325 | while (true) { |
| 326 | auto packet = std::make_unique<apacket>(); |
| 327 | if (!underlying_->Read(packet.get())) { |
| 328 | PLOG(INFO) << this->transport_name_ << ": read failed"; |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | bool got_stls_cmd = false; |
| 333 | if (packet->msg.command == A_STLS) { |
| 334 | got_stls_cmd = true; |
| 335 | } |
| 336 | |
| 337 | read_callback_(this, std::move(packet)); |
| 338 | |
| 339 | // If we received the STLS packet, we are about to perform the TLS |
| 340 | // handshake. So this read thread must stop and resume after the |
| 341 | // handshake completes otherwise this will interfere in the process. |
| 342 | if (got_stls_cmd) { |
| 343 | LOG(INFO) << this->transport_name_ |
| 344 | << ": Received STLS packet. Stopping read thread."; |
| 345 | return; |
| 346 | } |
| 347 | } |
| 348 | std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "read failed"); }); |
| 349 | }); |
| 350 | } |
| 351 | |
| 352 | bool BlockingConnectionAdapter::DoTlsHandshake(RSA* key, std::string* auth_key) { |
| 353 | std::lock_guard<std::mutex> lock(mutex_); |
| 354 | if (read_thread_.joinable()) { |
| 355 | read_thread_.join(); |
| 356 | } |
| 357 | bool success = this->underlying_->DoTlsHandshake(key, auth_key); |
| 358 | StartReadThread(); |
| 359 | return success; |
| 360 | } |
| 361 | |
Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 362 | void BlockingConnectionAdapter::Reset() { |
| 363 | { |
| 364 | std::lock_guard<std::mutex> lock(mutex_); |
| 365 | if (!started_) { |
| 366 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): not started"; |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | if (stopped_) { |
| 371 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ |
| 372 | << "): already stopped"; |
| 373 | return; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): resetting"; |
| 378 | this->underlying_->Reset(); |
| 379 | Stop(); |
| 380 | } |
| 381 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 382 | void BlockingConnectionAdapter::Stop() { |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 383 | { |
| 384 | std::lock_guard<std::mutex> lock(mutex_); |
| 385 | if (!started_) { |
| 386 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): not started"; |
| 387 | return; |
| 388 | } |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 389 | |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 390 | if (stopped_) { |
| 391 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ |
| 392 | << "): already stopped"; |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | stopped_ = true; |
| 397 | } |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 398 | |
| 399 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopping"; |
| 400 | |
| 401 | this->underlying_->Close(); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 402 | this->cv_.notify_one(); |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 403 | |
| 404 | // Move the threads out into locals with the lock taken, and then unlock to let them exit. |
| 405 | std::thread read_thread; |
| 406 | std::thread write_thread; |
| 407 | |
| 408 | { |
| 409 | std::lock_guard<std::mutex> lock(mutex_); |
| 410 | read_thread = std::move(read_thread_); |
| 411 | write_thread = std::move(write_thread_); |
| 412 | } |
| 413 | |
| 414 | read_thread.join(); |
| 415 | write_thread.join(); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 416 | |
| 417 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopped"; |
| 418 | std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "requested stop"); }); |
| 419 | } |
| 420 | |
| 421 | bool BlockingConnectionAdapter::Write(std::unique_ptr<apacket> packet) { |
| 422 | { |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 423 | std::lock_guard<std::mutex> lock(this->mutex_); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 424 | write_queue_.emplace_back(std::move(packet)); |
| 425 | } |
| 426 | |
| 427 | cv_.notify_one(); |
| 428 | return true; |
| 429 | } |
| 430 | |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 431 | FdConnection::FdConnection(unique_fd fd) : fd_(std::move(fd)) {} |
| 432 | |
| 433 | FdConnection::~FdConnection() {} |
| 434 | |
| 435 | bool FdConnection::DispatchRead(void* buf, size_t len) { |
| 436 | if (tls_ != nullptr) { |
| 437 | // The TlsConnection doesn't allow 0 byte reads |
| 438 | if (len == 0) { |
| 439 | return true; |
| 440 | } |
| 441 | return tls_->ReadFully(buf, len); |
| 442 | } |
| 443 | |
| 444 | return ReadFdExactly(fd_.get(), buf, len); |
| 445 | } |
| 446 | |
| 447 | bool FdConnection::DispatchWrite(void* buf, size_t len) { |
| 448 | if (tls_ != nullptr) { |
| 449 | // The TlsConnection doesn't allow 0 byte writes |
| 450 | if (len == 0) { |
| 451 | return true; |
| 452 | } |
| 453 | return tls_->WriteFully(std::string_view(reinterpret_cast<const char*>(buf), len)); |
| 454 | } |
| 455 | |
| 456 | return WriteFdExactly(fd_.get(), buf, len); |
| 457 | } |
| 458 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 459 | bool FdConnection::Read(apacket* packet) { |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 460 | if (!DispatchRead(&packet->msg, sizeof(amessage))) { |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 461 | D("remote local: read terminated (message)"); |
| 462 | return false; |
| 463 | } |
| 464 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 465 | if (packet->msg.data_length > MAX_PAYLOAD) { |
Josh Gao | 5caaebd | 2018-02-02 14:38:04 -0800 | [diff] [blame] | 466 | D("remote local: read overflow (data length = %" PRIu32 ")", packet->msg.data_length); |
| 467 | return false; |
| 468 | } |
| 469 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 470 | packet->payload.resize(packet->msg.data_length); |
| 471 | |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 472 | if (!DispatchRead(&packet->payload[0], packet->payload.size())) { |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 473 | D("remote local: terminated (data)"); |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | return true; |
| 478 | } |
| 479 | |
| 480 | bool FdConnection::Write(apacket* packet) { |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 481 | if (!DispatchWrite(&packet->msg, sizeof(packet->msg))) { |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 482 | D("remote local: write terminated"); |
| 483 | return false; |
| 484 | } |
| 485 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 486 | if (packet->msg.data_length) { |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 487 | if (!DispatchWrite(&packet->payload[0], packet->msg.data_length)) { |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 488 | D("remote local: write terminated"); |
| 489 | return false; |
| 490 | } |
| 491 | } |
| 492 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 493 | return true; |
| 494 | } |
| 495 | |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 496 | bool FdConnection::DoTlsHandshake(RSA* key, std::string* auth_key) { |
| 497 | bssl::UniquePtr<EVP_PKEY> evp_pkey(EVP_PKEY_new()); |
| 498 | if (!EVP_PKEY_set1_RSA(evp_pkey.get(), key)) { |
| 499 | LOG(ERROR) << "EVP_PKEY_set1_RSA failed"; |
| 500 | return false; |
| 501 | } |
| 502 | auto x509 = GenerateX509Certificate(evp_pkey.get()); |
| 503 | auto x509_str = X509ToPEMString(x509.get()); |
| 504 | auto evp_str = Key::ToPEMString(evp_pkey.get()); |
Joshua Duong | 1b273ed | 2020-03-24 09:50:59 -0700 | [diff] [blame] | 505 | |
Yurii Zubrytskyi | e3e64b8 | 2020-03-26 18:16:36 -0700 | [diff] [blame] | 506 | int osh = cast_handle_to_int(adb_get_os_handle(fd_)); |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 507 | #if ADB_HOST |
Joshua Duong | 81f2db4 | 2020-04-16 15:58:19 -0700 | [diff] [blame] | 508 | tls_ = TlsConnection::Create(TlsConnection::Role::Client, x509_str, evp_str, osh); |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 509 | #else |
Joshua Duong | 81f2db4 | 2020-04-16 15:58:19 -0700 | [diff] [blame] | 510 | tls_ = TlsConnection::Create(TlsConnection::Role::Server, x509_str, evp_str, osh); |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 511 | #endif |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 512 | CHECK(tls_); |
| 513 | #if ADB_HOST |
| 514 | // TLS 1.3 gives the client no message if the server rejected the |
| 515 | // certificate. This will enable a check in the tls connection to check |
| 516 | // whether the client certificate got rejected. Note that this assumes |
| 517 | // that, on handshake success, the server speaks first. |
| 518 | tls_->EnableClientPostHandshakeCheck(true); |
| 519 | // Add callback to set the certificate when server issues the |
| 520 | // CertificateRequest. |
| 521 | tls_->SetCertificateCallback(adb_tls_set_certificate); |
| 522 | // Allow any server certificate |
| 523 | tls_->SetCertVerifyCallback([](X509_STORE_CTX*) { return 1; }); |
| 524 | #else |
| 525 | // Add callback to check certificate against a list of known public keys |
| 526 | tls_->SetCertVerifyCallback( |
| 527 | [auth_key](X509_STORE_CTX* ctx) { return adbd_tls_verify_cert(ctx, auth_key); }); |
| 528 | // Add the list of allowed client CA issuers |
| 529 | auto ca_list = adbd_tls_client_ca_list(); |
| 530 | tls_->SetClientCAList(ca_list.get()); |
| 531 | #endif |
| 532 | |
| 533 | auto err = tls_->DoHandshake(); |
| 534 | if (err == TlsError::Success) { |
| 535 | return true; |
| 536 | } |
| 537 | |
| 538 | tls_.reset(); |
| 539 | return false; |
| 540 | } |
| 541 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 542 | void FdConnection::Close() { |
| 543 | adb_shutdown(fd_.get()); |
| 544 | fd_.reset(); |
| 545 | } |
| 546 | |
Josh Gao | 06d61d4 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 547 | void send_packet(apacket* p, atransport* t) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 548 | p->msg.magic = p->msg.command ^ 0xffffffff; |
Tim Murray | de47194 | 2017-12-07 11:40:00 -0800 | [diff] [blame] | 549 | // compute a checksum for connection/auth packets for compatibility reasons |
| 550 | if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) { |
| 551 | p->msg.data_check = 0; |
| 552 | } else { |
| 553 | p->msg.data_check = calculate_apacket_checksum(p); |
| 554 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 555 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 556 | VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "to remote", p); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 557 | |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 558 | if (t == nullptr) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 559 | LOG(FATAL) << "Transport is null"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 562 | if (t->Write(p) != 0) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 563 | D("%s: failed to enqueue packet, closing transport", t->serial.c_str()); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 564 | t->Kick(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 568 | void kick_transport(atransport* t, bool reset) { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 569 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 570 | // As kick_transport() can be called from threads without guarantee that t is valid, |
| 571 | // check if the transport is in transport_list first. |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 572 | // |
| 573 | // TODO(jmgao): WTF? Is this actually true? |
Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 574 | if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) { |
Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 575 | if (reset) { |
| 576 | t->Reset(); |
| 577 | } else { |
| 578 | t->Kick(); |
| 579 | } |
Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 580 | } |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 581 | |
| 582 | #if ADB_HOST |
| 583 | reconnect_handler.CheckForKicked(); |
| 584 | #endif |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 585 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 586 | |
| 587 | static int transport_registration_send = -1; |
| 588 | static int transport_registration_recv = -1; |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 589 | static fdevent* transport_registration_fde; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 590 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 591 | #if ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 592 | |
| 593 | /* this adds support required by the 'track-devices' service. |
| 594 | * this is used to send the content of "list_transport" to any |
| 595 | * number of client connections that want it through a single |
| 596 | * live TCP connection |
| 597 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 598 | struct device_tracker { |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 599 | asocket socket; |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 600 | bool update_needed = false; |
| 601 | bool long_output = false; |
| 602 | device_tracker* next = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 603 | }; |
| 604 | |
| 605 | /* linked list of all device trackers */ |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 606 | static device_tracker* device_tracker_list; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 607 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 608 | static void device_tracker_remove(device_tracker* tracker) { |
| 609 | device_tracker** pnode = &device_tracker_list; |
| 610 | device_tracker* node = *pnode; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 611 | |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 612 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 613 | while (node) { |
| 614 | if (node == tracker) { |
| 615 | *pnode = node->next; |
| 616 | break; |
| 617 | } |
| 618 | pnode = &node->next; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 619 | node = *pnode; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 620 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 623 | static void device_tracker_close(asocket* socket) { |
| 624 | device_tracker* tracker = (device_tracker*)socket; |
| 625 | asocket* peer = socket->peer; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 626 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 627 | D("device tracker %p removed", tracker); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 628 | if (peer) { |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 629 | peer->peer = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 630 | peer->close(peer); |
| 631 | } |
| 632 | device_tracker_remove(tracker); |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 633 | delete tracker; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 636 | static int device_tracker_enqueue(asocket* socket, apacket::payload_type) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 637 | /* you can't read from a device tracker, close immediately */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 638 | device_tracker_close(socket); |
| 639 | return -1; |
| 640 | } |
| 641 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 642 | static int device_tracker_send(device_tracker* tracker, const std::string& string) { |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 643 | asocket* peer = tracker->socket.peer; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 644 | |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 645 | apacket::payload_type data; |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 646 | data.resize(4 + string.size()); |
| 647 | char buf[5]; |
| 648 | snprintf(buf, sizeof(buf), "%04x", static_cast<int>(string.size())); |
| 649 | memcpy(&data[0], buf, 4); |
| 650 | memcpy(&data[4], string.data(), string.size()); |
| 651 | return peer->enqueue(peer, std::move(data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 654 | static void device_tracker_ready(asocket* socket) { |
| 655 | device_tracker* tracker = reinterpret_cast<device_tracker*>(socket); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 656 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 657 | // We want to send the device list when the tracker connects |
| 658 | // for the first time, even if no update occurred. |
Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 659 | if (tracker->update_needed) { |
| 660 | tracker->update_needed = false; |
Elliott Hughes | fc2f5f6 | 2019-07-31 14:13:57 -0700 | [diff] [blame] | 661 | device_tracker_send(tracker, list_transports(tracker->long_output)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 665 | asocket* create_device_tracker(bool long_output) { |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 666 | device_tracker* tracker = new device_tracker(); |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 667 | if (tracker == nullptr) LOG(FATAL) << "cannot allocate device tracker"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 668 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 669 | D("device tracker %p created", tracker); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 670 | |
| 671 | tracker->socket.enqueue = device_tracker_enqueue; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 672 | tracker->socket.ready = device_tracker_ready; |
| 673 | tracker->socket.close = device_tracker_close; |
Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 674 | tracker->update_needed = true; |
| 675 | tracker->long_output = long_output; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 676 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 677 | tracker->next = device_tracker_list; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 678 | device_tracker_list = tracker; |
| 679 | |
| 680 | return &tracker->socket; |
| 681 | } |
| 682 | |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 683 | // Check if all of the USB transports are connected. |
| 684 | bool iterate_transports(std::function<bool(const atransport*)> fn) { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 685 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 686 | for (const auto& t : transport_list) { |
| 687 | if (!fn(t)) { |
| 688 | return false; |
| 689 | } |
| 690 | } |
| 691 | for (const auto& t : pending_list) { |
| 692 | if (!fn(t)) { |
| 693 | return false; |
| 694 | } |
| 695 | } |
| 696 | return true; |
| 697 | } |
| 698 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 699 | // Call this function each time the transport list has changed. |
| 700 | void update_transports() { |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 701 | update_transport_status(); |
| 702 | |
| 703 | // Notify `adb track-devices` clients. |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 704 | device_tracker* tracker = device_tracker_list; |
| 705 | while (tracker != nullptr) { |
| 706 | device_tracker* next = tracker->next; |
| 707 | // This may destroy the tracker if the connection is closed. |
Elliott Hughes | fc2f5f6 | 2019-07-31 14:13:57 -0700 | [diff] [blame] | 708 | device_tracker_send(tracker, list_transports(tracker->long_output)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 709 | tracker = next; |
| 710 | } |
| 711 | } |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 712 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 713 | #else |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 714 | |
| 715 | void update_transports() { |
| 716 | // Nothing to do on the device side. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 717 | } |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 718 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 719 | #endif // ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 720 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 721 | struct tmsg { |
| 722 | atransport* transport; |
| 723 | int action; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 724 | }; |
| 725 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 726 | static int transport_read_action(int fd, struct tmsg* m) { |
| 727 | char* p = (char*)m; |
| 728 | int len = sizeof(*m); |
| 729 | int r; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 730 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 731 | while (len > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 732 | r = adb_read(fd, p, len); |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 733 | if (r > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 734 | len -= r; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 735 | p += r; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 736 | } else { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 737 | D("transport_read_action: on fd %d: %s", fd, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 738 | return -1; |
| 739 | } |
| 740 | } |
| 741 | return 0; |
| 742 | } |
| 743 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 744 | static int transport_write_action(int fd, struct tmsg* m) { |
| 745 | char* p = (char*)m; |
| 746 | int len = sizeof(*m); |
| 747 | int r; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 748 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 749 | while (len > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 750 | r = adb_write(fd, p, len); |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 751 | if (r > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 752 | len -= r; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 753 | p += r; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 754 | } else { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 755 | D("transport_write_action: on fd %d: %s", fd, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 756 | return -1; |
| 757 | } |
| 758 | } |
| 759 | return 0; |
| 760 | } |
| 761 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 762 | static void transport_registration_func(int _fd, unsigned ev, void*) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 763 | tmsg m; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 764 | atransport* t; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 765 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 766 | if (!(ev & FDE_READ)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 770 | if (transport_read_action(_fd, &m)) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 771 | PLOG(FATAL) << "cannot read transport registration socket"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | t = m.transport; |
| 775 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 776 | if (m.action == 0) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 777 | D("transport: %s deleting", t->serial.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 778 | |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 779 | { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 780 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 781 | transport_list.remove(t); |
| 782 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 783 | |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 784 | delete t; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 785 | |
| 786 | update_transports(); |
| 787 | return; |
| 788 | } |
| 789 | |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 790 | /* don't create transport threads for inaccessible devices */ |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 791 | if (t->GetConnectionState() != kCsNoPerm) { |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 792 | // The connection gets a reference to the atransport. It will release it |
| 793 | // upon a read/write error. |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 794 | t->connection()->SetTransportName(t->serial_name()); |
| 795 | t->connection()->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) { |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 796 | if (!check_header(p.get(), t)) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 797 | D("%s: remote read: bad header", t->serial.c_str()); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 798 | return false; |
| 799 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 800 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 801 | VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "from remote", p.get()); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 802 | apacket* packet = p.release(); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 803 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 804 | // TODO: Does this need to run on the main thread? |
| 805 | fdevent_run_on_main_thread([packet, t]() { handle_packet(packet, t); }); |
| 806 | return true; |
| 807 | }); |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 808 | t->connection()->SetErrorCallback([t](Connection*, const std::string& error) { |
Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 809 | LOG(INFO) << t->serial_name() << ": connection terminated: " << error; |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 810 | fdevent_run_on_main_thread([t]() { |
| 811 | handle_offline(t); |
Josh Gao | 9a8366b | 2019-12-09 13:45:31 -0800 | [diff] [blame] | 812 | transport_destroy(t); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 813 | }); |
| 814 | }); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 815 | |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 816 | t->connection()->Start(); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 817 | #if ADB_HOST |
| 818 | send_connect(t); |
| 819 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 822 | { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 823 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 824 | auto it = std::find(pending_list.begin(), pending_list.end(), t); |
| 825 | if (it != pending_list.end()) { |
| 826 | pending_list.remove(t); |
| 827 | transport_list.push_front(t); |
| 828 | } |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 829 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 830 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 831 | update_transports(); |
| 832 | } |
| 833 | |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 834 | #if ADB_HOST |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 835 | void init_reconnect_handler(void) { |
| 836 | reconnect_handler.Start(); |
| 837 | } |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 838 | #endif |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 839 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 840 | void init_transport_registration(void) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 841 | int s[2]; |
| 842 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 843 | if (adb_socketpair(s)) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 844 | PLOG(FATAL) << "cannot open transport registration socketpair"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 845 | } |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 846 | D("socketpair: (%d,%d)", s[0], s[1]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 847 | |
| 848 | transport_registration_send = s[0]; |
| 849 | transport_registration_recv = s[1]; |
| 850 | |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 851 | transport_registration_fde = |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 852 | fdevent_create(transport_registration_recv, transport_registration_func, nullptr); |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 853 | fdevent_set(transport_registration_fde, FDE_READ); |
Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | void kick_all_transports() { |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 857 | #if ADB_HOST |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 858 | reconnect_handler.Stop(); |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 859 | #endif |
Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 860 | // To avoid only writing part of a packet to a transport after exit, kick all transports. |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 861 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 862 | for (auto t : transport_list) { |
| 863 | t->Kick(); |
| 864 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 865 | } |
| 866 | |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 867 | void kick_all_tcp_tls_transports() { |
| 868 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
| 869 | for (auto t : transport_list) { |
| 870 | if (t->IsTcpDevice() && t->use_tls) { |
| 871 | t->Kick(); |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | #if !ADB_HOST |
| 877 | void kick_all_transports_by_auth_key(std::string_view auth_key) { |
| 878 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
| 879 | for (auto t : transport_list) { |
| 880 | if (auth_key == t->auth_key) { |
| 881 | t->Kick(); |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | #endif |
| 886 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 887 | /* the fdevent select pump is single threaded */ |
Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 888 | void register_transport(atransport* transport) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 889 | tmsg m; |
| 890 | m.transport = transport; |
| 891 | m.action = 1; |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 892 | D("transport: %s registered", transport->serial.c_str()); |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 893 | if (transport_write_action(transport_registration_send, &m)) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 894 | PLOG(FATAL) << "cannot write transport registration socket"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 898 | static void remove_transport(atransport* transport) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 899 | tmsg m; |
| 900 | m.transport = transport; |
| 901 | m.action = 0; |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 902 | D("transport: %s removed", transport->serial.c_str()); |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 903 | if (transport_write_action(transport_registration_send, &m)) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 904 | PLOG(FATAL) << "cannot write transport registration socket"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Josh Gao | 9a8366b | 2019-12-09 13:45:31 -0800 | [diff] [blame] | 908 | static void transport_destroy(atransport* t) { |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 909 | check_main_thread(); |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 910 | CHECK(t != nullptr); |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 911 | |
Josh Gao | e48ecce | 2017-09-13 13:40:57 -0700 | [diff] [blame] | 912 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 9a8366b | 2019-12-09 13:45:31 -0800 | [diff] [blame] | 913 | LOG(INFO) << "destroying transport " << t->serial_name(); |
| 914 | t->connection()->Stop(); |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 915 | #if ADB_HOST |
Josh Gao | 9a8366b | 2019-12-09 13:45:31 -0800 | [diff] [blame] | 916 | if (t->IsTcpDevice() && !t->kicked()) { |
| 917 | D("transport: %s destroy (attempting reconnection)", t->serial.c_str()); |
Josh Gao | 4414e4c | 2018-12-04 01:07:50 -0800 | [diff] [blame] | 918 | |
Josh Gao | 9a8366b | 2019-12-09 13:45:31 -0800 | [diff] [blame] | 919 | // We need to clear the transport's keys, so that on the next connection, it tries |
| 920 | // again from the beginning. |
| 921 | t->ResetKeys(); |
| 922 | reconnect_handler.TrackTransport(t); |
| 923 | return; |
| 924 | } |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 925 | #endif |
| 926 | |
Josh Gao | 9a8366b | 2019-12-09 13:45:31 -0800 | [diff] [blame] | 927 | D("transport: %s destroy (kicking and closing)", t->serial.c_str()); |
| 928 | remove_transport(t); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 929 | } |
| 930 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 931 | static int qual_match(const std::string& to_test, const char* prefix, const std::string& qual, |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 932 | bool sanitize_qual) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 933 | if (to_test.empty()) /* Return true if both the qual and to_test are empty strings. */ |
| 934 | return qual.empty(); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 935 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 936 | if (qual.empty()) return 0; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 937 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 938 | const char* ptr = to_test.c_str(); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 939 | if (prefix) { |
| 940 | while (*prefix) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 941 | if (*prefix++ != *ptr++) return 0; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 945 | for (char ch : qual) { |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 946 | if (sanitize_qual && !isalnum(ch)) ch = '_'; |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 947 | if (ch != *ptr++) return 0; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 950 | /* Everything matched so far. Return true if *ptr is a NUL. */ |
| 951 | return !*ptr; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 952 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 953 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 954 | atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id, |
| 955 | bool* is_ambiguous, std::string* error_out, |
| 956 | bool accept_any_state) { |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 957 | atransport* result = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 958 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 959 | if (transport_id != 0) { |
| 960 | *error_out = |
| 961 | android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id); |
| 962 | } else if (serial) { |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 963 | *error_out = android::base::StringPrintf("device '%s' not found", serial); |
| 964 | } else if (type == kTransportLocal) { |
| 965 | *error_out = "no emulators found"; |
| 966 | } else if (type == kTransportAny) { |
| 967 | *error_out = "no devices/emulators found"; |
| 968 | } else { |
| 969 | *error_out = "no devices found"; |
| 970 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 971 | |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 972 | std::unique_lock<std::recursive_mutex> lock(transport_lock); |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 973 | for (const auto& t : transport_list) { |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 974 | if (t->GetConnectionState() == kCsNoPerm) { |
David Pursell | d2acbd1 | 2015-12-02 15:14:31 -0800 | [diff] [blame] | 975 | *error_out = UsbNoPermissionsLongHelpText(); |
Mike Lockwood | 37d3111 | 2009-08-08 13:53:16 -0400 | [diff] [blame] | 976 | continue; |
| 977 | } |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 978 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 979 | if (transport_id) { |
| 980 | if (t->id == transport_id) { |
| 981 | result = t; |
| 982 | break; |
| 983 | } |
| 984 | } else if (serial) { |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 985 | if (t->MatchesTarget(serial)) { |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 986 | if (result) { |
Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 987 | *error_out = "more than one device"; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 988 | if (is_ambiguous) *is_ambiguous = true; |
| 989 | result = nullptr; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 990 | break; |
| 991 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 992 | result = t; |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 993 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 994 | } else { |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 995 | if (type == kTransportUsb && t->type == kTransportUsb) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 996 | if (result) { |
Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 997 | *error_out = "more than one device"; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 998 | if (is_ambiguous) *is_ambiguous = true; |
| 999 | result = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1000 | break; |
| 1001 | } |
| 1002 | result = t; |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 1003 | } else if (type == kTransportLocal && t->type == kTransportLocal) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1004 | if (result) { |
Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 1005 | *error_out = "more than one emulator"; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 1006 | if (is_ambiguous) *is_ambiguous = true; |
| 1007 | result = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1008 | break; |
| 1009 | } |
| 1010 | result = t; |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 1011 | } else if (type == kTransportAny) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1012 | if (result) { |
Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 1013 | *error_out = "more than one device/emulator"; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 1014 | if (is_ambiguous) *is_ambiguous = true; |
| 1015 | result = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1016 | break; |
| 1017 | } |
| 1018 | result = t; |
| 1019 | } |
| 1020 | } |
| 1021 | } |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1022 | lock.unlock(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1023 | |
Josh Gao | 704494b | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 1024 | if (result && !accept_any_state) { |
| 1025 | // The caller requires an active transport. |
| 1026 | // Make sure that we're actually connected. |
| 1027 | ConnectionState state = result->GetConnectionState(); |
| 1028 | switch (state) { |
| 1029 | case kCsConnecting: |
| 1030 | *error_out = "device still connecting"; |
| 1031 | result = nullptr; |
| 1032 | break; |
Benoit Goby | 77e8e58 | 2013-01-15 12:36:47 -0800 | [diff] [blame] | 1033 | |
Josh Gao | 704494b | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 1034 | case kCsAuthorizing: |
| 1035 | *error_out = "device still authorizing"; |
| 1036 | result = nullptr; |
| 1037 | break; |
| 1038 | |
| 1039 | case kCsUnauthorized: { |
| 1040 | *error_out = "device unauthorized.\n"; |
| 1041 | char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS"); |
| 1042 | *error_out += "This adb server's $ADB_VENDOR_KEYS is "; |
| 1043 | *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set"; |
| 1044 | *error_out += "\n"; |
| 1045 | *error_out += "Try 'adb kill-server' if that seems wrong.\n"; |
| 1046 | *error_out += "Otherwise check for a confirmation dialog on your device."; |
| 1047 | result = nullptr; |
| 1048 | break; |
| 1049 | } |
| 1050 | |
| 1051 | case kCsOffline: |
| 1052 | *error_out = "device offline"; |
| 1053 | result = nullptr; |
| 1054 | break; |
| 1055 | |
| 1056 | default: |
| 1057 | break; |
| 1058 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | if (result) { |
Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 1062 | *error_out = "success"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | return result; |
| 1066 | } |
| 1067 | |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 1068 | bool ConnectionWaitable::WaitForConnection(std::chrono::milliseconds timeout) { |
| 1069 | std::unique_lock<std::mutex> lock(mutex_); |
Josh Gao | 982f7bd | 2019-02-12 13:59:03 -0800 | [diff] [blame] | 1070 | ScopedLockAssertion assume_locked(mutex_); |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 1071 | return cv_.wait_for(lock, timeout, [&]() REQUIRES(mutex_) { |
| 1072 | return connection_established_ready_; |
| 1073 | }) && connection_established_; |
| 1074 | } |
| 1075 | |
| 1076 | void ConnectionWaitable::SetConnectionEstablished(bool success) { |
| 1077 | { |
| 1078 | std::lock_guard<std::mutex> lock(mutex_); |
| 1079 | if (connection_established_ready_) return; |
| 1080 | connection_established_ready_ = true; |
| 1081 | connection_established_ = success; |
| 1082 | D("connection established with %d", success); |
| 1083 | } |
| 1084 | cv_.notify_one(); |
| 1085 | } |
| 1086 | |
| 1087 | atransport::~atransport() { |
| 1088 | // If the connection callback had not been run before, run it now. |
| 1089 | SetConnectionEstablished(false); |
| 1090 | } |
| 1091 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1092 | int atransport::Write(apacket* p) { |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 1093 | return this->connection()->Write(std::unique_ptr<apacket>(p)) ? 0 : -1; |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 1096 | void atransport::Reset() { |
| 1097 | if (!kicked_.exchange(true)) { |
| 1098 | LOG(INFO) << "resetting transport " << this << " " << this->serial; |
| 1099 | this->connection()->Reset(); |
| 1100 | } |
| 1101 | } |
| 1102 | |
Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 1103 | void atransport::Kick() { |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 1104 | if (!kicked_.exchange(true)) { |
Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 1105 | LOG(INFO) << "kicking transport " << this << " " << this->serial; |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 1106 | this->connection()->Stop(); |
Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1110 | ConnectionState atransport::GetConnectionState() const { |
| 1111 | return connection_state_; |
| 1112 | } |
| 1113 | |
| 1114 | void atransport::SetConnectionState(ConnectionState state) { |
| 1115 | check_main_thread(); |
| 1116 | connection_state_ = state; |
| 1117 | } |
| 1118 | |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 1119 | void atransport::SetConnection(std::unique_ptr<Connection> connection) { |
| 1120 | std::lock_guard<std::mutex> lock(mutex_); |
| 1121 | connection_ = std::shared_ptr<Connection>(std::move(connection)); |
| 1122 | } |
| 1123 | |
Josh Gao | ffbd336 | 2018-02-28 14:44:23 -0800 | [diff] [blame] | 1124 | std::string atransport::connection_state_name() const { |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1125 | ConnectionState state = GetConnectionState(); |
| 1126 | switch (state) { |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1127 | case kCsOffline: |
| 1128 | return "offline"; |
| 1129 | case kCsBootloader: |
| 1130 | return "bootloader"; |
| 1131 | case kCsDevice: |
| 1132 | return "device"; |
| 1133 | case kCsHost: |
| 1134 | return "host"; |
| 1135 | case kCsRecovery: |
| 1136 | return "recovery"; |
Tao Bao | 55d407e | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 1137 | case kCsRescue: |
| 1138 | return "rescue"; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1139 | case kCsNoPerm: |
| 1140 | return UsbNoPermissionsShortHelpText(); |
| 1141 | case kCsSideload: |
| 1142 | return "sideload"; |
| 1143 | case kCsUnauthorized: |
| 1144 | return "unauthorized"; |
Josh Gao | 704494b | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 1145 | case kCsAuthorizing: |
| 1146 | return "authorizing"; |
| 1147 | case kCsConnecting: |
| 1148 | return "connecting"; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1149 | default: |
| 1150 | return "unknown"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1151 | } |
| 1152 | } |
| 1153 | |
Tamas Berghammer | 3d2904c | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 1154 | void atransport::update_version(int version, size_t payload) { |
| 1155 | protocol_version = std::min(version, A_VERSION); |
| 1156 | max_payload = std::min(payload, MAX_PAYLOAD); |
| 1157 | } |
| 1158 | |
| 1159 | int atransport::get_protocol_version() const { |
| 1160 | return protocol_version; |
| 1161 | } |
| 1162 | |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 1163 | int atransport::get_tls_version() const { |
| 1164 | return tls_version; |
| 1165 | } |
| 1166 | |
Tamas Berghammer | 3d2904c | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 1167 | size_t atransport::get_max_payload() const { |
| 1168 | return max_payload; |
| 1169 | } |
| 1170 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1171 | const FeatureSet& supported_features() { |
Yurii Zubrytskyi | 6fc26df | 2020-03-26 18:19:28 -0700 | [diff] [blame] | 1172 | static const android::base::NoDestructor<FeatureSet> features([] { |
| 1173 | return FeatureSet{ |
| 1174 | kFeatureShell2, |
| 1175 | kFeatureCmd, |
| 1176 | kFeatureStat2, |
| 1177 | kFeatureLs2, |
| 1178 | kFeatureFixedPushMkdir, |
| 1179 | kFeatureApex, |
| 1180 | kFeatureAbb, |
| 1181 | kFeatureFixedPushSymlinkTimestamp, |
| 1182 | kFeatureAbbExec, |
| 1183 | kFeatureRemountShell, |
| 1184 | kFeatureTrackApp, |
| 1185 | kFeatureSendRecv2, |
| 1186 | kFeatureSendRecv2Brotli, |
| 1187 | kFeatureSendRecv2LZ4, |
| 1188 | kFeatureSendRecv2DryRunSend, |
| 1189 | // Increment ADB_SERVER_VERSION when adding a feature that adbd needs |
| 1190 | // to know about. Otherwise, the client can be stuck running an old |
| 1191 | // version of the server even after upgrading their copy of adb. |
| 1192 | // (http://b/24370690) |
| 1193 | }; |
| 1194 | }()); |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1195 | |
| 1196 | return *features; |
| 1197 | } |
| 1198 | |
| 1199 | std::string FeatureSetToString(const FeatureSet& features) { |
Elliott Hughes | 86ab9ff | 2018-09-05 12:13:11 -0700 | [diff] [blame] | 1200 | return android::base::Join(features, ','); |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | FeatureSet StringToFeatureSet(const std::string& features_string) { |
David Pursell | d2b588e | 2015-09-25 13:04:21 -0700 | [diff] [blame] | 1204 | if (features_string.empty()) { |
| 1205 | return FeatureSet(); |
| 1206 | } |
| 1207 | |
Yurii Zubrytskyi | 6fc26df | 2020-03-26 18:19:28 -0700 | [diff] [blame] | 1208 | return android::base::Split(features_string, ","); |
| 1209 | } |
| 1210 | |
| 1211 | template <class Range, class Value> |
| 1212 | static bool contains(const Range& r, const Value& v) { |
| 1213 | return std::find(std::begin(r), std::end(r), v) != std::end(r); |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
David Pursell | 70ef7b4 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 1216 | bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) { |
Yurii Zubrytskyi | 6fc26df | 2020-03-26 18:19:28 -0700 | [diff] [blame] | 1217 | return contains(feature_set, feature) && contains(supported_features(), feature); |
David Pursell | 70ef7b4 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1220 | bool atransport::has_feature(const std::string& feature) const { |
Yurii Zubrytskyi | 6fc26df | 2020-03-26 18:19:28 -0700 | [diff] [blame] | 1221 | return contains(features_, feature); |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1224 | void atransport::SetFeatures(const std::string& features_string) { |
| 1225 | features_ = StringToFeatureSet(features_string); |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 1228 | void atransport::AddDisconnect(adisconnect* disconnect) { |
| 1229 | disconnects_.push_back(disconnect); |
| 1230 | } |
| 1231 | |
| 1232 | void atransport::RemoveDisconnect(adisconnect* disconnect) { |
| 1233 | disconnects_.remove(disconnect); |
| 1234 | } |
| 1235 | |
| 1236 | void atransport::RunDisconnects() { |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1237 | for (const auto& disconnect : disconnects_) { |
Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 1238 | disconnect->func(disconnect->opaque, this); |
| 1239 | } |
| 1240 | disconnects_.clear(); |
| 1241 | } |
| 1242 | |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1243 | bool atransport::MatchesTarget(const std::string& target) const { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1244 | if (!serial.empty()) { |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1245 | if (target == serial) { |
| 1246 | return true; |
| 1247 | } else if (type == kTransportLocal) { |
| 1248 | // Local transports can match [tcp:|udp:]<hostname>[:port]. |
| 1249 | const char* local_target_ptr = target.c_str(); |
| 1250 | |
| 1251 | // For fastboot compatibility, ignore protocol prefixes. |
| 1252 | if (android::base::StartsWith(target, "tcp:") || |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1253 | android::base::StartsWith(target, "udp:")) { |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1254 | local_target_ptr += 4; |
| 1255 | } |
| 1256 | |
| 1257 | // Parse our |serial| and the given |target| to check if the hostnames and ports match. |
| 1258 | std::string serial_host, error; |
| 1259 | int serial_port = -1; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1260 | if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) { |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1261 | // |target| may omit the port to default to ours. |
| 1262 | std::string target_host; |
| 1263 | int target_port = serial_port; |
| 1264 | if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port, |
| 1265 | nullptr, &error) && |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1266 | serial_host == target_host && serial_port == target_port) { |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1267 | return true; |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1273 | return (target == devpath) || qual_match(target, "product:", product, false) || |
| 1274 | qual_match(target, "model:", model, true) || |
| 1275 | qual_match(target, "device:", device, false); |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 1278 | void atransport::SetConnectionEstablished(bool success) { |
| 1279 | connection_waitable_->SetConnectionEstablished(success); |
| 1280 | } |
| 1281 | |
Josh Gao | fc2e56f | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 1282 | ReconnectResult atransport::Reconnect() { |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 1283 | return reconnect_(this); |
| 1284 | } |
| 1285 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1286 | #if ADB_HOST |
| 1287 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1288 | // We use newline as our delimiter, make sure to never output it. |
| 1289 | static std::string sanitize(std::string str, bool alphanumeric) { |
| 1290 | auto pred = alphanumeric ? [](const char c) { return !isalnum(c); } |
| 1291 | : [](const char c) { return c == '\n'; }; |
| 1292 | std::replace_if(str.begin(), str.end(), pred, '_'); |
| 1293 | return str; |
| 1294 | } |
| 1295 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1296 | static void append_transport_info(std::string* result, const char* key, const std::string& value, |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1297 | bool alphanumeric) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1298 | if (value.empty()) { |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1299 | return; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1302 | *result += ' '; |
| 1303 | *result += key; |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1304 | *result += sanitize(value, alphanumeric); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1307 | static void append_transport(const atransport* t, std::string* result, bool long_listing) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1308 | std::string serial = t->serial; |
| 1309 | if (serial.empty()) { |
Dan Albert | d99d902 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 1310 | serial = "(no serial number)"; |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1311 | } |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1312 | |
| 1313 | if (!long_listing) { |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1314 | *result += serial; |
| 1315 | *result += '\t'; |
| 1316 | *result += t->connection_state_name(); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1317 | } else { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1318 | android::base::StringAppendF(result, "%-22s %s", serial.c_str(), |
| 1319 | t->connection_state_name().c_str()); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1320 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1321 | append_transport_info(result, "", t->devpath, false); |
| 1322 | append_transport_info(result, "product:", t->product, false); |
| 1323 | append_transport_info(result, "model:", t->model, true); |
| 1324 | append_transport_info(result, "device:", t->device, false); |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1325 | |
| 1326 | // Put id at the end, so that anyone parsing the output here can always find it by scanning |
| 1327 | // backwards from newlines, even with hypothetical devices named 'transport_id:1'. |
| 1328 | *result += " transport_id:"; |
| 1329 | *result += std::to_string(t->id); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1330 | } |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1331 | *result += '\n'; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1334 | std::string list_transports(bool long_listing) { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1335 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Artem Iglikov | 04398a9 | 2017-12-17 10:56:07 +0000 | [diff] [blame] | 1336 | |
| 1337 | auto sorted_transport_list = transport_list; |
| 1338 | sorted_transport_list.sort([](atransport*& x, atransport*& y) { |
| 1339 | if (x->type != y->type) { |
| 1340 | return x->type < y->type; |
| 1341 | } |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1342 | return x->serial < y->serial; |
Artem Iglikov | 04398a9 | 2017-12-17 10:56:07 +0000 | [diff] [blame] | 1343 | }); |
| 1344 | |
| 1345 | std::string result; |
| 1346 | for (const auto& t : sorted_transport_list) { |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1347 | append_transport(t, &result, long_listing); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1348 | } |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1349 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1350 | } |
| 1351 | |
Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 1352 | void close_usb_devices(std::function<bool(const atransport*)> predicate, bool reset) { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1353 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 1354 | for (auto& t : transport_list) { |
| 1355 | if (predicate(t)) { |
Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 1356 | if (reset) { |
| 1357 | t->Reset(); |
| 1358 | } else { |
| 1359 | t->Kick(); |
| 1360 | } |
Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1365 | /* hack for osx */ |
Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 1366 | void close_usb_devices(bool reset) { |
| 1367 | close_usb_devices([](const atransport*) { return true; }, reset); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1368 | } |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1369 | #endif // ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1370 | |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1371 | bool register_socket_transport(unique_fd s, std::string serial, int port, int local, |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 1372 | atransport::ReconnectCallback reconnect, bool use_tls, int* error) { |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 1373 | atransport* t = new atransport(std::move(reconnect), kCsOffline); |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 1374 | t->use_tls = use_tls; |
David 'Digit' Turner | 730ff3b | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 1375 | |
Josh Gao | 3b4de3c | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1376 | D("transport: %s init'ing for socket %d, on port %d", serial.c_str(), s.get(), port); |
Josh Gao | 56300c9 | 2018-07-25 17:21:49 -0700 | [diff] [blame] | 1377 | if (init_socket_transport(t, std::move(s), port, local) < 0) { |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1378 | delete t; |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1379 | if (error) *error = errno; |
| 1380 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1381 | } |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1382 | |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1383 | std::unique_lock<std::recursive_mutex> lock(transport_lock); |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1384 | for (const auto& transport : pending_list) { |
Josh Gao | 3b4de3c | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1385 | if (serial == transport->serial) { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1386 | VLOG(TRANSPORT) << "socket transport " << transport->serial |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1387 | << " is already in pending_list and fails to register"; |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1388 | delete t; |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1389 | if (error) *error = EALREADY; |
| 1390 | return false; |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1391 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1392 | } |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1393 | |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1394 | for (const auto& transport : transport_list) { |
Josh Gao | 3b4de3c | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1395 | if (serial == transport->serial) { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1396 | VLOG(TRANSPORT) << "socket transport " << transport->serial |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1397 | << " is already in transport_list and fails to register"; |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1398 | delete t; |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1399 | if (error) *error = EALREADY; |
| 1400 | return false; |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | |
Josh Gao | 3b4de3c | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1404 | t->serial = std::move(serial); |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1405 | pending_list.push_front(t); |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1406 | |
| 1407 | lock.unlock(); |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1408 | |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 1409 | auto waitable = t->connection_waitable(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1410 | register_transport(t); |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 1411 | |
Luis Hector Chavez | c587f02 | 2018-05-01 17:12:16 -0700 | [diff] [blame] | 1412 | if (local == 1) { |
| 1413 | // Do not wait for emulator transports. |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1414 | return true; |
Luis Hector Chavez | c587f02 | 2018-05-01 17:12:16 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1417 | if (!waitable->WaitForConnection(std::chrono::seconds(10))) { |
| 1418 | if (error) *error = ETIMEDOUT; |
| 1419 | return false; |
| 1420 | } |
| 1421 | |
| 1422 | if (t->GetConnectionState() == kCsUnauthorized) { |
| 1423 | if (error) *error = EPERM; |
| 1424 | return false; |
| 1425 | } |
| 1426 | |
| 1427 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1428 | } |
| 1429 | |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1430 | #if ADB_HOST |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1431 | atransport* find_transport(const char* serial) { |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1432 | atransport* result = nullptr; |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1433 | |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1434 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1435 | for (auto& t : transport_list) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1436 | if (strcmp(serial, t->serial.c_str()) == 0) { |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1437 | result = t; |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1438 | break; |
| 1439 | } |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1440 | } |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1441 | |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1442 | return result; |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1443 | } |
| 1444 | |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1445 | void kick_all_tcp_devices() { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1446 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1447 | for (auto& t : transport_list) { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1448 | if (t->IsTcpDevice()) { |
Yabin Cui | d6ab3c2 | 2015-08-31 11:50:24 -0700 | [diff] [blame] | 1449 | // Kicking breaks the read_transport thread of this transport out of any read, then |
| 1450 | // the read_transport thread will notify the main thread to make this transport |
| 1451 | // offline. Then the main thread will notify the write_transport thread to exit. |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1452 | // Finally, this transport will be closed and freed in the main thread. |
Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 1453 | t->Kick(); |
Mike Lockwood | cbbe79a | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1454 | } |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1455 | } |
Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 1456 | #if ADB_HOST |
| 1457 | reconnect_handler.CheckForKicked(); |
| 1458 | #endif |
Mike Lockwood | cbbe79a | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1459 | } |
| 1460 | |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1461 | #endif |
| 1462 | |
Josh Gao | 0871824 | 2020-03-27 18:09:56 -0700 | [diff] [blame] | 1463 | #if ADB_HOST |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1464 | void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath, |
| 1465 | unsigned writeable) { |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 1466 | atransport* t = new atransport(writeable ? kCsOffline : kCsNoPerm); |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1467 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1468 | D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : ""); |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1469 | init_usb_transport(t, usb); |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1470 | if (serial) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1471 | t->serial = serial; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1472 | } |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1473 | |
| 1474 | if (devpath) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1475 | t->devpath = devpath; |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1476 | } |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1477 | |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1478 | { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1479 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1480 | pending_list.push_front(t); |
| 1481 | } |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1482 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1483 | register_transport(t); |
| 1484 | } |
Josh Gao | 0871824 | 2020-03-27 18:09:56 -0700 | [diff] [blame] | 1485 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1486 | |
Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 1487 | #if ADB_HOST |
Dan Albert | dcd78a1 | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 1488 | // This should only be used for transports with connection_state == kCsNoPerm. |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1489 | void unregister_usb_transport(usb_handle* usb) { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1490 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 1491 | transport_list.remove_if([usb](atransport* t) { |
Josh Gao | ce5ce87 | 2018-12-11 13:11:52 -0800 | [diff] [blame] | 1492 | return t->GetUsbHandle() == usb && t->GetConnectionState() == kCsNoPerm; |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 1493 | }); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 1494 | } |
Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 1495 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1496 | |
Josh Gao | 36dadca | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1497 | bool check_header(apacket* p, atransport* t) { |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1498 | if (p->msg.magic != (p->msg.command ^ 0xffffffff)) { |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1499 | VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command |
| 1500 | << ", magic = " << p->msg.magic; |
Josh Gao | 36dadca | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1501 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1502 | } |
| 1503 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1504 | if (p->msg.data_length > t->get_max_payload()) { |
| 1505 | VLOG(RWX) << "check_header(): " << p->msg.data_length |
| 1506 | << " atransport::max_payload = " << t->get_max_payload(); |
Josh Gao | 36dadca | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1507 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1508 | } |
| 1509 | |
Josh Gao | 36dadca | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1510 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1511 | } |
| 1512 | |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 1513 | #if ADB_HOST |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 1514 | std::shared_ptr<RSA> atransport::Key() { |
| 1515 | if (keys_.empty()) { |
| 1516 | return nullptr; |
| 1517 | } |
| 1518 | |
| 1519 | std::shared_ptr<RSA> result = keys_[0]; |
| 1520 | return result; |
| 1521 | } |
| 1522 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 1523 | std::shared_ptr<RSA> atransport::NextKey() { |
Josh Gao | 4414e4c | 2018-12-04 01:07:50 -0800 | [diff] [blame] | 1524 | if (keys_.empty()) { |
| 1525 | LOG(INFO) << "fetching keys for transport " << this->serial_name(); |
| 1526 | keys_ = adb_auth_get_private_keys(); |
| 1527 | |
| 1528 | // We should have gotten at least one key: the one that's automatically generated. |
| 1529 | CHECK(!keys_.empty()); |
Joshua Duong | 5cf7868 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 1530 | } else { |
| 1531 | keys_.pop_front(); |
Josh Gao | 4414e4c | 2018-12-04 01:07:50 -0800 | [diff] [blame] | 1532 | } |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 1533 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 1534 | std::shared_ptr<RSA> result = keys_[0]; |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 1535 | return result; |
| 1536 | } |
Josh Gao | 4414e4c | 2018-12-04 01:07:50 -0800 | [diff] [blame] | 1537 | |
| 1538 | void atransport::ResetKeys() { |
| 1539 | keys_.clear(); |
| 1540 | } |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 1541 | #endif |