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