JP Abgrall | 2e5dd6e | 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 | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 22 | #include <atomic> |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 23 | #include <deque> |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 24 | #include <functional> |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 25 | #include <list> |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 26 | #include <memory> |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 27 | #include <mutex> |
Elliott Hughes | ab88242 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 28 | #include <string> |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 29 | #include <unordered_set> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | ab88242 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 31 | #include "adb.h" |
Dan Albert | bbbbea6 | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 32 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 33 | #include <openssl/rsa.h> |
| 34 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 35 | typedef std::unordered_set<std::string> FeatureSet; |
| 36 | |
| 37 | const FeatureSet& supported_features(); |
| 38 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 39 | // Encodes and decodes FeatureSet objects into human-readable strings. |
| 40 | std::string FeatureSetToString(const FeatureSet& features); |
| 41 | FeatureSet StringToFeatureSet(const std::string& features_string); |
| 42 | |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 43 | // Returns true if both local features and |feature_set| support |feature|. |
| 44 | bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature); |
| 45 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 46 | // Do not use any of [:;=,] in feature strings, they have special meaning |
| 47 | // in the connection banner. |
Todd Kennedy | 1e2f7dc | 2015-11-03 16:53:08 -0800 | [diff] [blame] | 48 | extern const char* const kFeatureShell2; |
| 49 | // The 'cmd' command is available |
| 50 | extern const char* const kFeatureCmd; |
Josh Gao | a2cf375 | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 51 | extern const char* const kFeatureStat2; |
Josh Gao | 210b63f | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 52 | // The server is running with libusb enabled. |
| 53 | extern const char* const kFeatureLibusb; |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 54 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 55 | class atransport { |
| 56 | public: |
| 57 | // TODO(danalbert): We expose waaaaaaay too much stuff because this was |
| 58 | // historically just a struct, but making the whole thing a more idiomatic |
| 59 | // class in one go is a very large change. Given how bad our testing is, |
| 60 | // it's better to do this piece by piece. |
| 61 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 62 | atransport(ConnectionState state = kCsOffline) : connection_state_(state) { |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 63 | transport_fde = {}; |
| 64 | protocol_version = A_VERSION; |
| 65 | max_payload = MAX_PAYLOAD; |
| 66 | } |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 67 | virtual ~atransport() {} |
| 68 | |
| 69 | int (*read_from_remote)(apacket* p, atransport* t) = nullptr; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 70 | void (*close)(atransport* t) = nullptr; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 71 | |
| 72 | void SetWriteFunction(int (*write_func)(apacket*, atransport*)) { write_func_ = write_func; } |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 73 | void SetKickFunction(void (*kick_func)(atransport*)) { |
| 74 | kick_func_ = kick_func; |
| 75 | } |
| 76 | bool IsKicked() { |
| 77 | return kicked_; |
| 78 | } |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 79 | int Write(apacket* p); |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 80 | void Kick(); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 81 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 82 | // ConnectionState can be read by all threads, but can only be written in the main thread. |
| 83 | ConnectionState GetConnectionState() const; |
| 84 | void SetConnectionState(ConnectionState state); |
| 85 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 86 | int fd = -1; |
| 87 | int transport_socket = -1; |
| 88 | fdevent transport_fde; |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 89 | size_t ref_count = 0; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 90 | uint32_t sync_token = 0; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 91 | bool online = false; |
| 92 | TransportType type = kTransportAny; |
| 93 | |
| 94 | // USB handle or socket fd as needed. |
| 95 | usb_handle* usb = nullptr; |
| 96 | int sfd = -1; |
| 97 | |
| 98 | // Used to identify transports for clients. |
| 99 | char* serial = nullptr; |
| 100 | char* product = nullptr; |
| 101 | char* model = nullptr; |
| 102 | char* device = nullptr; |
| 103 | char* devpath = nullptr; |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 104 | void SetLocalPortForEmulator(int port) { |
| 105 | CHECK_EQ(local_port_for_emulator_, -1); |
| 106 | local_port_for_emulator_ = port; |
| 107 | } |
| 108 | |
| 109 | bool GetLocalPortForEmulator(int* port) const { |
| 110 | if (type == kTransportLocal && local_port_for_emulator_ != -1) { |
| 111 | *port = local_port_for_emulator_; |
| 112 | return true; |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | bool IsTcpDevice() const { |
| 118 | return type == kTransportLocal && local_port_for_emulator_ == -1; |
| 119 | } |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 120 | |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 121 | #if ADB_HOST |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 122 | std::shared_ptr<RSA> NextKey(); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 123 | bool SetSendConnectOnError(); |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 124 | #endif |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 125 | |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 126 | char token[TOKEN_SIZE] = {}; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 127 | size_t failed_auth_attempts = 0; |
| 128 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 129 | const std::string serial_name() const { return serial ? serial : "<unknown>"; } |
David Pursell | 6e5c7eb | 2015-12-02 15:14:31 -0800 | [diff] [blame] | 130 | const std::string connection_state_name() const; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 131 | |
| 132 | void update_version(int version, size_t payload); |
| 133 | int get_protocol_version() const; |
| 134 | size_t get_max_payload() const; |
| 135 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 136 | const FeatureSet& features() const { |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 137 | return features_; |
| 138 | } |
| 139 | |
| 140 | bool has_feature(const std::string& feature) const; |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 141 | |
| 142 | // Loads the transport's feature set from the given string. |
| 143 | void SetFeatures(const std::string& features_string); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 144 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 145 | void AddDisconnect(adisconnect* disconnect); |
| 146 | void RemoveDisconnect(adisconnect* disconnect); |
| 147 | void RunDisconnects(); |
| 148 | |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 149 | // Returns true if |target| matches this transport. A matching |target| can be any of: |
| 150 | // * <serial> |
| 151 | // * <devpath> |
| 152 | // * product:<product> |
| 153 | // * model:<model> |
| 154 | // * device:<device> |
| 155 | // |
| 156 | // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets. |
| 157 | // For example, serial "100.100.100.100:5555" would match any of: |
| 158 | // * 100.100.100.100 |
| 159 | // * tcp:100.100.100.100 |
| 160 | // * udp:100.100.100.100:5555 |
| 161 | // This is to make it easier to use the same network target for both fastboot and adb. |
| 162 | bool MatchesTarget(const std::string& target) const; |
| 163 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 164 | private: |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 165 | int local_port_for_emulator_ = -1; |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 166 | bool kicked_ = false; |
| 167 | void (*kick_func_)(atransport*) = nullptr; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 168 | int (*write_func_)(apacket*, atransport*) = nullptr; |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 169 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 170 | // A set of features transmitted in the banner with the initial connection. |
| 171 | // This is stored in the banner as 'features=feature0,feature1,etc'. |
| 172 | FeatureSet features_; |
| 173 | int protocol_version; |
| 174 | size_t max_payload; |
| 175 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 176 | // A list of adisconnect callbacks called when the transport is kicked. |
| 177 | std::list<adisconnect*> disconnects_; |
| 178 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 179 | std::atomic<ConnectionState> connection_state_; |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 180 | #if ADB_HOST |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 181 | std::deque<std::shared_ptr<RSA>> keys_; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 182 | std::mutex write_msg_lock_; |
| 183 | bool has_send_connect_on_error_ = false; |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 184 | #endif |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 185 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 186 | DISALLOW_COPY_AND_ASSIGN(atransport); |
| 187 | }; |
| 188 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 189 | /* |
| 190 | * Obtain a transport from the available transports. |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 191 | * If serial is non-null then only the device with that serial will be chosen. |
| 192 | * If multiple devices/emulators would match, *is_ambiguous (if non-null) |
| 193 | * is set to true and nullptr returned. |
| 194 | * If no suitable transport is found, error is set and nullptr returned. |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 195 | */ |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 196 | atransport* acquire_one_transport(TransportType type, const char* serial, bool* is_ambiguous, |
| 197 | std::string* error_out, bool accept_any_state = false); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 198 | void kick_transport(atransport* t); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 199 | void update_transports(void); |
| 200 | |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 201 | // Iterates across all of the current and pending transports. |
| 202 | // Stops iteration and returns false if fn returns false, otherwise returns true. |
| 203 | bool iterate_transports(std::function<bool(const atransport*)> fn); |
| 204 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 205 | void init_transport_registration(void); |
Casey Dahlin | 3122cdf | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 206 | void init_mdns_transport_discovery(void); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 207 | std::string list_transports(bool long_listing); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 208 | atransport* find_transport(const char* serial); |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 209 | void kick_all_tcp_devices(); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 210 | |
| 211 | void register_usb_transport(usb_handle* h, const char* serial, |
| 212 | const char* devpath, unsigned writeable); |
| 213 | |
Casey Dahlin | 3122cdf | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 214 | /* Connect to a network address and register it as a device */ |
| 215 | void connect_device(const std::string& address, std::string* response); |
| 216 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 217 | /* cause new transports to be init'd and added to the list */ |
| 218 | int register_socket_transport(int s, const char* serial, int port, int local); |
| 219 | |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 220 | // This should only be used for transports with connection_state == kCsNoPerm. |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 221 | void unregister_usb_transport(usb_handle* usb); |
| 222 | |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 223 | int check_header(apacket* p, atransport* t); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 224 | int check_data(apacket* p); |
| 225 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 226 | void close_usb_devices(); |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 227 | void close_usb_devices(std::function<bool(const atransport*)> predicate); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 228 | |
| 229 | void send_packet(apacket* p, atransport* t); |
| 230 | |
| 231 | asocket* create_device_tracker(void); |
| 232 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 233 | #endif /* __TRANSPORT_H */ |