• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #pragma once
18 
19 #include <limits.h>
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <string>
24 
25 #include <android-base/macros.h>
26 
27 #include "adb_trace.h"
28 #include "fdevent/fdevent.h"
29 #include "socket.h"
30 #include "types.h"
31 
32 constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
33 constexpr size_t MAX_PAYLOAD = 1024 * 1024;
34 constexpr size_t MAX_FRAMEWORK_PAYLOAD = 64 * 1024;
35 
36 // When delayed acks are supported, the initial number of unacknowledged bytes we're willing to
37 // receive on a socket before the other side should block.
38 constexpr size_t INITIAL_DELAYED_ACK_BYTES = 32 * 1024 * 1024;
39 
40 constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304;
41 
42 #define A_SYNC 0x434e5953
43 #define A_CNXN 0x4e584e43
44 #define A_OPEN 0x4e45504f
45 #define A_OKAY 0x59414b4f
46 #define A_CLSE 0x45534c43
47 #define A_WRTE 0x45545257
48 #define A_AUTH 0x48545541
49 #define A_STLS 0x534C5453
50 
51 // ADB protocol version.
52 // Version revision:
53 // 0x01000000: original
54 // 0x01000001: skip checksum (Dec 2017)
55 #define A_VERSION_MIN 0x01000000
56 #define A_VERSION_SKIP_CHECKSUM 0x01000001
57 #define A_VERSION 0x01000001
58 
59 // Stream-based TLS protocol version
60 #define A_STLS_VERSION_MIN 0x01000000
61 #define A_STLS_VERSION 0x01000000
62 
63 // Used for help/version information.
64 #define ADB_VERSION_MAJOR 1
65 #define ADB_VERSION_MINOR 0
66 
67 std::string adb_version();
68 
69 // Increment this when we want to force users to start a new adb server.
70 #define ADB_SERVER_VERSION 41
71 
72 using TransportId = uint64_t;
73 class atransport;
74 
75 uint32_t calculate_apacket_checksum(const apacket* packet);
76 
77 /* the adisconnect structure is used to record a callback that
78 ** will be called whenever a transport is disconnected (e.g. by the user)
79 ** this should be used to cleanup objects that depend on the
80 ** transport (e.g. remote sockets, listeners, etc...)
81 */
82 struct adisconnect {
83     void (*func)(void* opaque, atransport* t);
84     void* opaque;
85 };
86 
87 // A transport object models the connection to a remote device or emulator there
88 // is one transport per connected device/emulator. A "local transport" connects
89 // through TCP (for the emulator), while a "usb transport" through USB (for real
90 // devices).
91 //
92 // Note that kTransportHost doesn't really correspond to a real transport
93 // object, it's a special value used to indicate that a client wants to connect
94 // to a service implemented within the ADB server itself.
95 enum TransportType {
96     kTransportUsb,
97     kTransportLocal,
98     kTransportAny,
99     kTransportHost,
100 };
101 
102 #define TOKEN_SIZE 20
103 
104 enum ConnectionState {
105     kCsAny = -1,
106 
107     kCsConnecting = 0,  // Haven't received a response from the device yet.
108     kCsAuthorizing,     // Authorizing with keys from ADB_VENDOR_KEYS.
109     kCsUnauthorized,    // ADB_VENDOR_KEYS exhausted, fell back to user prompt.
110     kCsNoPerm,          // Insufficient permissions to communicate with the device.
111     kCsDetached,        // USB device that's detached from the adb server.
112     kCsOffline,
113 
114     // After CNXN packet, the ConnectionState describes not a state but the type of service
115     // on the other end of the transport.
116     kCsBootloader,  // Device running fastboot OS (fastboot) or userspace fastboot (fastbootd).
117     kCsDevice,      // Device running Android OS (adbd).
118     kCsHost,        // What a device sees from its end of a Transport (adb host).
119     kCsRecovery,    // Device with bootloader loaded but no ROM OS loaded (adbd).
120     kCsSideload,    // Device running Android OS Sideload mode (minadbd sideload mode).
121     kCsRescue,      // Device running Android OS Rescue mode (minadbd rescue mode).
122 };
123 
124 std::string to_string(ConnectionState state);
125 
ConnectionStateIsOnline(ConnectionState state)126 inline bool ConnectionStateIsOnline(ConnectionState state) {
127     switch (state) {
128         case kCsBootloader:
129         case kCsDevice:
130         case kCsHost:
131         case kCsRecovery:
132         case kCsSideload:
133         case kCsRescue:
134             return true;
135         default:
136             return false;
137     }
138 }
139 
140 void print_packet(const char* label, apacket* p);
141 
142 void handle_packet(apacket* p, atransport* t);
143 
144 int launch_server(const std::string& socket_spec, const char* one_device);
145 int adb_server_main(int is_daemon, const std::string& socket_spec, const char* one_device,
146                     int ack_reply_fd);
147 
148 /* initialize a transport object's func pointers and state */
149 int init_socket_transport(atransport* t, unique_fd s, int port, int local);
150 
151 std::string getEmulatorSerialString(int console_port);
152 #if ADB_HOST
153 atransport* find_emulator_transport_by_adb_port(int adb_port);
154 atransport* find_emulator_transport_by_console_port(int console_port);
155 #endif
156 
157 unique_fd service_to_fd(std::string_view name, atransport* transport);
158 #if !ADB_HOST
159 unique_fd daemon_service_to_fd(std::string_view name, atransport* transport);
160 #endif
161 
162 #if ADB_HOST
163 asocket* host_service_to_socket(std::string_view name, std::string_view serial,
164                                 TransportId transport_id);
165 #endif
166 
167 #if !ADB_HOST
168 asocket* daemon_service_to_socket(std::string_view name, atransport* transport);
169 #endif
170 
171 #if !ADB_HOST
172 unique_fd execute_abb_command(std::string_view command);
173 #endif
174 
175 bool handle_forward_request(const char* service, atransport* transport, int reply_fd);
176 bool handle_forward_request(const char* service,
177                             std::function<atransport*(std::string* error)> transport_acquirer,
178                             int reply_fd);
179 
180 /* packet allocator */
181 apacket* get_apacket(void);
182 void put_apacket(apacket* p);
183 
184 // Define it if you want to dump packets.
185 #define DEBUG_PACKETS 0
186 
187 #if !DEBUG_PACKETS
188 #define print_packet(tag, p) \
189     do {                     \
190     } while (0)
191 #endif
192 
193 #define DEFAULT_ADB_PORT 5037
194 
195 #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
196 
197 #define ADB_CLASS 0xff
198 #define ADB_SUBCLASS 0x42
199 #define ADB_PROTOCOL 0x1
200 
201 void local_init(const std::string& addr);
202 bool local_connect(int port);
203 int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
204 
205 extern const char* adb_device_banner;
206 
207 #define CHUNK_SIZE (64 * 1024)
208 
209 // Argument delimeter for adb abb command.
210 #define ABB_ARG_DELIMETER ('\0')
211 
212 #if !ADB_HOST
213 #define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
214 #define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH #x
215 
216 #define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
217 #define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
218 #define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
219 #endif
220 
221 enum class HostRequestResult {
222     Handled,
223     SwitchedTransport,
224     Unhandled,
225 };
226 
227 HostRequestResult handle_host_request(std::string_view service, TransportType type,
228                                       const char* serial, TransportId transport_id, int reply_fd,
229                                       asocket* s);
230 
231 void handle_online(atransport* t);
232 void handle_offline(atransport* t);
233 
234 void send_connect(atransport* t);
235 void send_tls_request(atransport* t);
236 void send_ready(unsigned local, unsigned remote, atransport* t, uint32_t ack_bytes);
237 
238 void parse_banner(const std::string&, atransport* t);
239 
240 #if ADB_HOST
241 // On startup, the adb server needs to wait until all of the connected devices are ready.
242 // To do this, we need to know when the scan has identified all of the potential new transports, and
243 // when each transport becomes ready.
244 // TODO: Do this for mDNS as well, instead of just USB?
245 
246 // We've found all of the transports we potentially care about.
247 void adb_notify_device_scan_complete();
248 
249 // One or more transports have changed status, check to see if we're ready.
250 void update_transport_status();
251 
252 // Wait until device scan has completed and every transport is ready, or a timeout elapses.
253 void adb_wait_for_device_initialization();
254 
255 // When ssh-forwarding to a remote adb server, kill-server is almost never what you actually want,
256 // and unfortunately, many other tools issue it. This adds a knob to reject kill-servers.
257 void adb_set_reject_kill_server(bool reject);
258 #endif
259 
260 void usb_init();
261