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