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