• 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     kCsOffline,
108 
109     kCsBootloader,
110     kCsDevice,
111     kCsHost,
112     kCsRecovery,
113     kCsSideload,
114     kCsRescue,
115 };
116 
ConnectionStateIsOnline(ConnectionState state)117 inline bool ConnectionStateIsOnline(ConnectionState state) {
118     switch (state) {
119         case kCsBootloader:
120         case kCsDevice:
121         case kCsHost:
122         case kCsRecovery:
123         case kCsSideload:
124         case kCsRescue:
125             return true;
126         default:
127             return false;
128     }
129 }
130 
131 void print_packet(const char* label, apacket* p);
132 
133 void handle_packet(apacket* p, atransport* t);
134 
135 int launch_server(const std::string& socket_spec);
136 int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd);
137 
138 /* initialize a transport object's func pointers and state */
139 int init_socket_transport(atransport* t, unique_fd s, int port, int local);
140 
141 std::string getEmulatorSerialString(int console_port);
142 #if ADB_HOST
143 atransport* find_emulator_transport_by_adb_port(int adb_port);
144 atransport* find_emulator_transport_by_console_port(int console_port);
145 #endif
146 
147 unique_fd service_to_fd(std::string_view name, atransport* transport);
148 #if !ADB_HOST
149 unique_fd daemon_service_to_fd(std::string_view name, atransport* transport);
150 #endif
151 
152 #if ADB_HOST
153 asocket* host_service_to_socket(std::string_view name, std::string_view serial,
154                                 TransportId transport_id);
155 #endif
156 
157 #if !ADB_HOST
158 asocket* daemon_service_to_socket(std::string_view name);
159 #endif
160 
161 #if !ADB_HOST
162 unique_fd execute_abb_command(std::string_view command);
163 #endif
164 
165 #if !ADB_HOST
166 int init_jdwp(void);
167 asocket* create_jdwp_service_socket();
168 asocket* create_jdwp_tracker_service_socket();
169 asocket* create_app_tracker_service_socket();
170 unique_fd create_jdwp_connection_fd(int jdwp_pid);
171 #endif
172 
173 bool handle_forward_request(const char* service, atransport* transport, int reply_fd);
174 bool handle_forward_request(const char* service,
175                             std::function<atransport*(std::string* error)> transport_acquirer,
176                             int reply_fd);
177 
178 /* packet allocator */
179 apacket* get_apacket(void);
180 void put_apacket(apacket* p);
181 
182 // Define it if you want to dump packets.
183 #define DEBUG_PACKETS 0
184 
185 #if !DEBUG_PACKETS
186 #define print_packet(tag, p) \
187     do {                     \
188     } while (0)
189 #endif
190 
191 #define DEFAULT_ADB_PORT 5037
192 
193 #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
194 
195 #define ADB_CLASS 0xff
196 #define ADB_SUBCLASS 0x42
197 #define ADB_PROTOCOL 0x1
198 
199 void local_init(const std::string& addr);
200 bool local_connect(int port);
201 int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
202 
203 ConnectionState connection_state(atransport* t);
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 
237 void parse_banner(const std::string&, atransport* t);
238 
239 #if ADB_HOST
240 // On startup, the adb server needs to wait until all of the connected devices are ready.
241 // To do this, we need to know when the scan has identified all of the potential new transports, and
242 // when each transport becomes ready.
243 // TODO: Do this for mDNS as well, instead of just USB?
244 
245 // We've found all of the transports we potentially care about.
246 void adb_notify_device_scan_complete();
247 
248 // One or more transports have changed status, check to see if we're ready.
249 void update_transport_status();
250 
251 // Wait until device scan has completed and every transport is ready, or a timeout elapses.
252 void adb_wait_for_device_initialization();
253 #endif  // ADB_HOST
254 
255 #if ADB_HOST
256 // When ssh-forwarding to a remote adb server, kill-server is almost never what you actually want,
257 // and unfortunately, many other tools issue it. This adds a knob to reject kill-servers.
258 void adb_set_reject_kill_server(bool reject);
259 #endif
260 
261 void usb_init();
262