JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | #ifndef __TRANSPORT_H |
| 18 | #define __TRANSPORT_H |
| 19 | |
Dan Albert | e9fca14 | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 22 | #include <atomic> |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 23 | #include <deque> |
Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 24 | #include <functional> |
Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 25 | #include <list> |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 26 | #include <memory> |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 27 | #include <mutex> |
Elliott Hughes | 7be29c8 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 28 | #include <string> |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 29 | #include <unordered_set> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 31 | #include <openssl/rsa.h> |
| 32 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 33 | #include "adb.h" |
| 34 | #include "adb_unique_fd.h" |
| 35 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 36 | typedef std::unordered_set<std::string> FeatureSet; |
| 37 | |
| 38 | const FeatureSet& supported_features(); |
| 39 | |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 40 | // Encodes and decodes FeatureSet objects into human-readable strings. |
| 41 | std::string FeatureSetToString(const FeatureSet& features); |
| 42 | FeatureSet StringToFeatureSet(const std::string& features_string); |
| 43 | |
David Pursell | 70ef7b4 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 44 | // Returns true if both local features and |feature_set| support |feature|. |
| 45 | bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature); |
| 46 | |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 47 | // Do not use any of [:;=,] in feature strings, they have special meaning |
| 48 | // in the connection banner. |
Todd Kennedy | 6fa848a | 2015-11-03 16:53:08 -0800 | [diff] [blame] | 49 | extern const char* const kFeatureShell2; |
| 50 | // The 'cmd' command is available |
| 51 | extern const char* const kFeatureCmd; |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 52 | extern const char* const kFeatureStat2; |
Josh Gao | 5d1756c | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 53 | // The server is running with libusb enabled. |
| 54 | extern const char* const kFeatureLibusb; |
Dan Albert | 5176df8 | 2017-05-23 14:30:00 -0700 | [diff] [blame] | 55 | // The server supports `push --sync`. |
| 56 | extern const char* const kFeaturePushSync; |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 57 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 58 | TransportId NextTransportId(); |
| 59 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 60 | // Abstraction for a blocking packet transport. |
| 61 | struct Connection { |
| 62 | Connection() = default; |
| 63 | Connection(const Connection& copy) = delete; |
| 64 | Connection(Connection&& move) = delete; |
| 65 | |
| 66 | // Destroy a Connection. Formerly known as 'Close' in atransport. |
| 67 | virtual ~Connection() = default; |
| 68 | |
| 69 | // Read/Write a packet. These functions are concurrently called from a transport's reader/writer |
| 70 | // threads. |
| 71 | virtual bool Read(apacket* packet) = 0; |
| 72 | virtual bool Write(apacket* packet) = 0; |
| 73 | |
| 74 | // Terminate a connection. |
| 75 | // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate. |
| 76 | // Formerly known as 'Kick' in atransport. |
| 77 | virtual void Close() = 0; |
| 78 | }; |
| 79 | |
| 80 | struct FdConnection : public Connection { |
| 81 | explicit FdConnection(unique_fd fd) : fd_(std::move(fd)) {} |
| 82 | |
| 83 | bool Read(apacket* packet) override final; |
| 84 | bool Write(apacket* packet) override final; |
| 85 | |
| 86 | void Close() override; |
| 87 | |
| 88 | private: |
| 89 | unique_fd fd_; |
| 90 | }; |
| 91 | |
| 92 | struct UsbConnection : public Connection { |
| 93 | explicit UsbConnection(usb_handle* handle) : handle_(handle) {} |
| 94 | ~UsbConnection(); |
| 95 | |
| 96 | bool Read(apacket* packet) override final; |
| 97 | bool Write(apacket* packet) override final; |
| 98 | |
| 99 | void Close() override final; |
| 100 | |
| 101 | usb_handle* handle_; |
| 102 | }; |
| 103 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 104 | class atransport { |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 105 | public: |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 106 | // TODO(danalbert): We expose waaaaaaay too much stuff because this was |
| 107 | // historically just a struct, but making the whole thing a more idiomatic |
| 108 | // class in one go is a very large change. Given how bad our testing is, |
| 109 | // it's better to do this piece by piece. |
| 110 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 111 | atransport(ConnectionState state = kCsOffline) |
Josh Gao | e48ecce | 2017-09-13 13:40:57 -0700 | [diff] [blame] | 112 | : id(NextTransportId()), connection_state_(state) { |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 113 | transport_fde = {}; |
Tim Murray | de47194 | 2017-12-07 11:40:00 -0800 | [diff] [blame] | 114 | // Initialize protocol to min version for compatibility with older versions. |
| 115 | // Version will be updated post-connect. |
| 116 | protocol_version = A_VERSION_MIN; |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 117 | max_payload = MAX_PAYLOAD; |
| 118 | } |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 119 | virtual ~atransport() {} |
| 120 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 121 | int Write(apacket* p); |
Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 122 | void Kick(); |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 123 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 124 | // ConnectionState can be read by all threads, but can only be written in the main thread. |
| 125 | ConnectionState GetConnectionState() const; |
| 126 | void SetConnectionState(ConnectionState state); |
| 127 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 128 | const TransportId id; |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 129 | int fd = -1; |
| 130 | int transport_socket = -1; |
| 131 | fdevent transport_fde; |
Josh Gao | e48ecce | 2017-09-13 13:40:57 -0700 | [diff] [blame] | 132 | size_t ref_count = 0; |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 133 | uint32_t sync_token = 0; |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 134 | bool online = false; |
| 135 | TransportType type = kTransportAny; |
| 136 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 137 | std::unique_ptr<Connection> connection; |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 138 | |
| 139 | // Used to identify transports for clients. |
| 140 | char* serial = nullptr; |
| 141 | char* product = nullptr; |
| 142 | char* model = nullptr; |
| 143 | char* device = nullptr; |
| 144 | char* devpath = nullptr; |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 145 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 146 | bool IsTcpDevice() const { return type == kTransportLocal; } |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 147 | |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 148 | #if ADB_HOST |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 149 | std::shared_ptr<RSA> NextKey(); |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 150 | #endif |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 151 | |
Josh Gao | 06d61d4 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 152 | char token[TOKEN_SIZE] = {}; |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 153 | size_t failed_auth_attempts = 0; |
| 154 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 155 | const std::string serial_name() const { return serial ? serial : "<unknown>"; } |
David Pursell | d2acbd1 | 2015-12-02 15:14:31 -0800 | [diff] [blame] | 156 | const std::string connection_state_name() const; |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 157 | |
| 158 | void update_version(int version, size_t payload); |
| 159 | int get_protocol_version() const; |
| 160 | size_t get_max_payload() const; |
| 161 | |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 162 | const FeatureSet& features() const { |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 163 | return features_; |
| 164 | } |
| 165 | |
| 166 | bool has_feature(const std::string& feature) const; |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 167 | |
| 168 | // Loads the transport's feature set from the given string. |
| 169 | void SetFeatures(const std::string& features_string); |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 170 | |
Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 171 | void AddDisconnect(adisconnect* disconnect); |
| 172 | void RemoveDisconnect(adisconnect* disconnect); |
| 173 | void RunDisconnects(); |
| 174 | |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 175 | // Returns true if |target| matches this transport. A matching |target| can be any of: |
| 176 | // * <serial> |
| 177 | // * <devpath> |
| 178 | // * product:<product> |
| 179 | // * model:<model> |
| 180 | // * device:<device> |
| 181 | // |
| 182 | // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets. |
| 183 | // For example, serial "100.100.100.100:5555" would match any of: |
| 184 | // * 100.100.100.100 |
| 185 | // * tcp:100.100.100.100 |
| 186 | // * udp:100.100.100.100:5555 |
| 187 | // This is to make it easier to use the same network target for both fastboot and adb. |
| 188 | bool MatchesTarget(const std::string& target) const; |
| 189 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 190 | private: |
Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 191 | bool kicked_ = false; |
Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 192 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 193 | // A set of features transmitted in the banner with the initial connection. |
| 194 | // This is stored in the banner as 'features=feature0,feature1,etc'. |
| 195 | FeatureSet features_; |
| 196 | int protocol_version; |
| 197 | size_t max_payload; |
| 198 | |
Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 199 | // A list of adisconnect callbacks called when the transport is kicked. |
| 200 | std::list<adisconnect*> disconnects_; |
| 201 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 202 | std::atomic<ConnectionState> connection_state_; |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 203 | #if ADB_HOST |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 204 | std::deque<std::shared_ptr<RSA>> keys_; |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 205 | #endif |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 206 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 207 | DISALLOW_COPY_AND_ASSIGN(atransport); |
| 208 | }; |
| 209 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 210 | /* |
| 211 | * Obtain a transport from the available transports. |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 212 | * If serial is non-null then only the device with that serial will be chosen. |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 213 | * If transport_id is non-zero then only the device with that transport ID will be chosen. |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 214 | * If multiple devices/emulators would match, *is_ambiguous (if non-null) |
| 215 | * is set to true and nullptr returned. |
| 216 | * If no suitable transport is found, error is set and nullptr returned. |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 217 | */ |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 218 | atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id, |
| 219 | bool* is_ambiguous, std::string* error_out, |
| 220 | bool accept_any_state = false); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 221 | void kick_transport(atransport* t); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 222 | void update_transports(void); |
| 223 | |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 224 | // Iterates across all of the current and pending transports. |
| 225 | // Stops iteration and returns false if fn returns false, otherwise returns true. |
| 226 | bool iterate_transports(std::function<bool(const atransport*)> fn); |
| 227 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 228 | void init_transport_registration(void); |
Casey Dahlin | 13a269e | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 229 | void init_mdns_transport_discovery(void); |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 230 | std::string list_transports(bool long_listing); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 231 | atransport* find_transport(const char* serial); |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 232 | void kick_all_tcp_devices(); |
Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 233 | void kick_all_transports(); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 234 | |
| 235 | void register_usb_transport(usb_handle* h, const char* serial, |
| 236 | const char* devpath, unsigned writeable); |
| 237 | |
Casey Dahlin | 13a269e | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 238 | /* Connect to a network address and register it as a device */ |
| 239 | void connect_device(const std::string& address, std::string* response); |
| 240 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 241 | /* cause new transports to be init'd and added to the list */ |
| 242 | int register_socket_transport(int s, const char* serial, int port, int local); |
| 243 | |
Dan Albert | dcd78a1 | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 244 | // This should only be used for transports with connection_state == kCsNoPerm. |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 245 | void unregister_usb_transport(usb_handle* usb); |
| 246 | |
Josh Gao | 36dadca | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 247 | bool check_header(apacket* p, atransport* t); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 248 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 249 | void close_usb_devices(); |
Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 250 | void close_usb_devices(std::function<bool(const atransport*)> predicate); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 251 | |
| 252 | void send_packet(apacket* p, atransport* t); |
| 253 | |
Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 254 | asocket* create_device_tracker(bool long_output); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 255 | |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 256 | #endif /* __TRANSPORT_H */ |