blob: aeca280c227c71fdfaa56e70f261d0369f2d1150 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <errno.h>
21
22#include "sysdeps.h"
23#include <sys/types.h>
Wei Zhongdca76e62012-03-13 10:02:50 -070024#include <arpa/inet.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025
26#define TRACE_TAG TRACE_TRANSPORT
27#include "adb.h"
28
Marcus Comstedtd340d2f2010-09-22 22:16:54 +020029#ifdef HAVE_BIG_ENDIAN
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
31static inline void fix_endians(apacket *p)
32{
33 p->msg.command = H4(p->msg.command);
34 p->msg.arg0 = H4(p->msg.arg0);
35 p->msg.arg1 = H4(p->msg.arg1);
36 p->msg.data_length = H4(p->msg.data_length);
37 p->msg.data_check = H4(p->msg.data_check);
38 p->msg.magic = H4(p->msg.magic);
39}
40#else
41#define fix_endians(p) do {} while (0)
42#endif
43
44#if ADB_HOST
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +010045/* we keep a list of opened transports. The atransport struct knows to which
46 * local transport it is connected. The list is used to detect when we're
47 * trying to connect twice to a given local transport.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048 */
49#define ADB_LOCAL_TRANSPORT_MAX 16
50
51ADB_MUTEX_DEFINE( local_transports_lock );
52
53static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
54#endif /* ADB_HOST */
55
56static int remote_read(apacket *p, atransport *t)
57{
58 if(readx(t->sfd, &p->msg, sizeof(amessage))){
59 D("remote local: read terminated (message)\n");
60 return -1;
61 }
62
63 fix_endians(p);
64
Marcus Comstedtd340d2f2010-09-22 22:16:54 +020065#if 0 && defined HAVE_BIG_ENDIAN
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066 D("read remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
67 p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
68#endif
69 if(check_header(p)) {
70 D("bad header: terminated (data)\n");
71 return -1;
72 }
73
74 if(readx(t->sfd, p->data, p->msg.data_length)){
75 D("remote local: terminated (data)\n");
76 return -1;
77 }
78
79 if(check_data(p)) {
80 D("bad data: terminated (data)\n");
81 return -1;
82 }
83
84 return 0;
85}
86
87static int remote_write(apacket *p, atransport *t)
88{
89 int length = p->msg.data_length;
90
91 fix_endians(p);
92
Marcus Comstedtd340d2f2010-09-22 22:16:54 +020093#if 0 && defined HAVE_BIG_ENDIAN
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094 D("write remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
95 p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
96#endif
97 if(writex(t->sfd, &p->msg, sizeof(amessage) + length)) {
98 D("remote local: write terminated\n");
99 return -1;
100 }
101
102 return 0;
103}
104
105
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100106int local_connect(int port) {
107 return local_connect_arbitrary_ports(port-1, port);
108}
109
110int local_connect_arbitrary_ports(int console_port, int adb_port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111{
112 char buf[64];
113 int fd = -1;
114
115#if ADB_HOST
116 const char *host = getenv("ADBHOST");
117 if (host) {
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100118 fd = socket_network_client(host, adb_port, SOCK_STREAM);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 }
120#endif
121 if (fd < 0) {
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100122 fd = socket_loopback_client(adb_port, SOCK_STREAM);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 }
124
125 if (fd >= 0) {
126 D("client: connected on remote on fd %d\n", fd);
127 close_on_exec(fd);
128 disable_tcp_nagle(fd);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100129 snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, console_port);
130 register_socket_transport(fd, buf, adb_port, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 return 0;
132 }
133 return -1;
134}
135
136
137static void *client_socket_thread(void *x)
138{
139#if ADB_HOST
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100140 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 int count = ADB_LOCAL_TRANSPORT_MAX;
142
143 D("transport: client_socket_thread() starting\n");
144
145 /* try to connect to any number of running emulator instances */
146 /* this is only done when ADB starts up. later, each new emulator */
147 /* will send a message to ADB to indicate that is is starting up */
148 for ( ; count > 0; count--, port += 2 ) {
149 (void) local_connect(port);
150 }
151#endif
152 return 0;
153}
154
Wei Zhongdca76e62012-03-13 10:02:50 -0700155#if !ADB_HOST
156static int is_whitelisted(struct sockaddr_in *addr)
157{
158 char value[PROPERTY_VALUE_MAX];
159
160 /* whitelist emulator */
161 property_get("ro.kernel.qemu", value, "");
162 if(!strcmp(value, "1")) {
163 return 1;
164 }
165
166 /* whitelist "eng" and "tests" builds */
167 property_get("ro.build.type", value, "");
168 if(!strcmp(value, "eng") || !strcmp(value, "tests")) {
169 return 1;
170 }
171
172 /* whitelist persist.service.adb.client_ip */
173 property_get("persist.service.adb.client_ip", value, "");
174 if(!strncmp(value, inet_ntoa(addr->sin_addr), sizeof(value))) {
175 return 1;
176 }
177 return 0;
178}
179#endif
180
Mike Lockwoodff196702009-08-24 15:58:40 -0700181static void *server_socket_thread(void * arg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182{
183 int serverfd, fd;
Wei Zhongdca76e62012-03-13 10:02:50 -0700184 struct sockaddr_in addr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 socklen_t alen;
Mike Lockwoodff196702009-08-24 15:58:40 -0700186 int port = (int)arg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187
188 D("transport: server_socket_thread() starting\n");
189 serverfd = -1;
190 for(;;) {
191 if(serverfd == -1) {
Mike Lockwoodff196702009-08-24 15:58:40 -0700192 serverfd = socket_inaddr_any_server(port, SOCK_STREAM);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 if(serverfd < 0) {
194 D("server: cannot bind socket yet\n");
195 adb_sleep_ms(1000);
196 continue;
197 }
198 close_on_exec(serverfd);
199 }
200
201 alen = sizeof(addr);
Mike Lockwoodff196702009-08-24 15:58:40 -0700202 D("server: trying to get new connection from %d\n", port);
Wei Zhongdca76e62012-03-13 10:02:50 -0700203 fd = adb_socket_accept(serverfd, (struct sockaddr *)&addr, &alen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 if(fd >= 0) {
205 D("server: new connection on fd %d\n", fd);
Wei Zhongdca76e62012-03-13 10:02:50 -0700206 #if !ADB_HOST
207 if(!is_whitelisted(&addr)) {
208 D("server: connection %d blacklisted and closed\n", port);
209 adb_close(fd);
210 continue;
211 }
212#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 close_on_exec(fd);
214 disable_tcp_nagle(fd);
Mike Lockwoodff196702009-08-24 15:58:40 -0700215 register_socket_transport(fd, "host", port, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 }
217 }
218 D("transport: server_socket_thread() exiting\n");
219 return 0;
220}
221
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800222/* This is relevant only for ADB daemon running inside the emulator. */
223#if !ADB_HOST
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800224/*
225 * Redefine open and write for qemu_pipe.h that contains inlined references
226 * to those routines. We will redifine them back after qemu_pipe.h inclusion.
227 */
228#undef open
229#undef write
230#define open adb_open
231#define write adb_write
232#include <hardware/qemu_pipe.h>
233#undef open
234#undef write
235#define open ___xxx_open
236#define write ___xxx_write
237
238/* A worker thread that monitors host connections, and registers a transport for
239 * every new host connection. This thread replaces server_socket_thread on
240 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
241 * pipe to communicate with adbd daemon inside the guest. This is done in order
242 * to provide more robust communication channel between ADB host and guest. The
243 * main issue with server_socket_thread approach is that it runs on top of TCP,
244 * and thus is sensitive to network disruptions. For instance, the
245 * ConnectionManager may decide to reset all network connections, in which case
246 * the connection between ADB host and guest will be lost. To make ADB traffic
247 * independent from the network, we use here 'adb' QEMUD service to transfer data
248 * between the host, and the guest. See external/qemu/android/adb-*.* that
249 * implements the emulator's side of the protocol. Another advantage of using
250 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
251 * anymore on network being set up.
252 * The guest side of the protocol contains the following phases:
253 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
254 * is opened, and it becomes clear whether or not emulator supports that
255 * protocol.
256 * - Wait for the ADB host to create connection with the guest. This is done by
257 * sending an 'accept' request to the adb QEMUD service, and waiting on
258 * response.
259 * - When new ADB host connection is accepted, the connection with adb QEMUD
260 * service is registered as the transport, and a 'start' request is sent to the
261 * adb QEMUD service, indicating that the guest is ready to receive messages.
262 * Note that the guest will ignore messages sent down from the emulator before
263 * the transport registration is completed. That's why we need to send the
264 * 'start' request after the transport is registered.
265 */
266static void *qemu_socket_thread(void * arg)
267{
268/* 'accept' request to the adb QEMUD service. */
269static const char _accept_req[] = "accept";
270/* 'start' request to the adb QEMUD service. */
271static const char _start_req[] = "start";
272/* 'ok' reply from the adb QEMUD service. */
273static const char _ok_resp[] = "ok";
274
275 const int port = (int)arg;
276 int res, fd;
277 char tmp[256];
278 char con_name[32];
279
280 D("transport: qemu_socket_thread() starting\n");
281
282 /* adb QEMUD service connection request. */
283 snprintf(con_name, sizeof(con_name), "qemud:adb:%d", port);
284
285 /* Connect to the adb QEMUD service. */
286 fd = qemu_pipe_open(con_name);
287 if (fd < 0) {
288 /* This could be an older version of the emulator, that doesn't
289 * implement adb QEMUD service. Fall back to the old TCP way. */
290 adb_thread_t thr;
291 D("adb service is not available. Falling back to TCP socket.\n");
292 adb_thread_create(&thr, server_socket_thread, arg);
293 return 0;
294 }
295
296 for(;;) {
297 /*
298 * Wait till the host creates a new connection.
299 */
300
301 /* Send the 'accept' request. */
302 res = adb_write(fd, _accept_req, strlen(_accept_req));
303 if (res == strlen(_accept_req)) {
304 /* Wait for the response. In the response we expect 'ok' on success,
305 * or 'ko' on failure. */
306 res = adb_read(fd, tmp, sizeof(tmp));
307 if (res != 2 || memcmp(tmp, _ok_resp, 2)) {
308 D("Accepting ADB host connection has failed.\n");
309 adb_close(fd);
310 } else {
311 /* Host is connected. Register the transport, and start the
312 * exchange. */
313 register_socket_transport(fd, "host", port, 1);
314 adb_write(fd, _start_req, strlen(_start_req));
315 }
316
317 /* Prepare for accepting of the next ADB host connection. */
318 fd = qemu_pipe_open(con_name);
319 if (fd < 0) {
320 D("adb service become unavailable.\n");
321 return 0;
322 }
323 } else {
324 D("Unable to send the '%s' request to ADB service.\n", _accept_req);
325 return 0;
326 }
327 }
328 D("transport: qemu_socket_thread() exiting\n");
329 return 0;
330}
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800331#endif // !ADB_HOST
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800332
Mike Lockwoodff196702009-08-24 15:58:40 -0700333void local_init(int port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334{
335 adb_thread_t thr;
336 void* (*func)(void *);
337
338 if(HOST) {
339 func = client_socket_thread;
340 } else {
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800341#if ADB_HOST
342 func = server_socket_thread;
343#else
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800344 /* For the adbd daemon in the system image we need to distinguish
345 * between the device, and the emulator. */
346 char is_qemu[PROPERTY_VALUE_MAX];
347 property_get("ro.kernel.qemu", is_qemu, "");
348 if (!strcmp(is_qemu, "1")) {
349 /* Running inside the emulator: use QEMUD pipe as the transport. */
350 func = qemu_socket_thread;
351 } else {
352 /* Running inside the device: use TCP socket as the transport. */
353 func = server_socket_thread;
354 }
Anatol Pomazaufc656102012-02-13 17:37:14 -0800355#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356 }
357
358 D("transport: local %s init\n", HOST ? "client" : "server");
359
Mike Lockwoodff196702009-08-24 15:58:40 -0700360 if(adb_thread_create(&thr, func, (void *)port)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 fatal_errno("cannot create local socket %s thread",
362 HOST ? "client" : "server");
363 }
364}
365
366static void remote_kick(atransport *t)
367{
368 int fd = t->sfd;
369 t->sfd = -1;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400370 adb_shutdown(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 adb_close(fd);
372
373#if ADB_HOST
374 if(HOST) {
375 int nn;
376 adb_mutex_lock( &local_transports_lock );
377 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
378 if (local_transports[nn] == t) {
379 local_transports[nn] = NULL;
380 break;
381 }
382 }
383 adb_mutex_unlock( &local_transports_lock );
384 }
385#endif
386}
387
388static void remote_close(atransport *t)
389{
390 adb_close(t->fd);
391}
392
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100393
394#if ADB_HOST
395/* Only call this function if you already hold local_transports_lock. */
396atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
397{
398 int i;
399 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
400 if (local_transports[i] && local_transports[i]->adb_port == adb_port) {
401 return local_transports[i];
402 }
403 }
404 return NULL;
405}
406
407atransport* find_emulator_transport_by_adb_port(int adb_port)
408{
409 adb_mutex_lock( &local_transports_lock );
410 atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
411 adb_mutex_unlock( &local_transports_lock );
412 return result;
413}
414
415/* Only call this function if you already hold local_transports_lock. */
416int get_available_local_transport_index_locked()
417{
418 int i;
419 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
420 if (local_transports[i] == NULL) {
421 return i;
422 }
423 }
424 return -1;
425}
426
427int get_available_local_transport_index()
428{
429 adb_mutex_lock( &local_transports_lock );
430 int result = get_available_local_transport_index_locked();
431 adb_mutex_unlock( &local_transports_lock );
432 return result;
433}
434#endif
435
436int init_socket_transport(atransport *t, int s, int adb_port, int local)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437{
438 int fail = 0;
439
440 t->kick = remote_kick;
441 t->close = remote_close;
442 t->read_from_remote = remote_read;
443 t->write_to_remote = remote_write;
444 t->sfd = s;
445 t->sync_token = 1;
446 t->connection_state = CS_OFFLINE;
447 t->type = kTransportLocal;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100448 t->adb_port = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449
450#if ADB_HOST
Mike Lockwoodff196702009-08-24 15:58:40 -0700451 if (HOST && local) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 adb_mutex_lock( &local_transports_lock );
453 {
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100454 t->adb_port = adb_port;
455 atransport* existing_transport =
456 find_emulator_transport_by_adb_port_locked(adb_port);
457 int index = get_available_local_transport_index_locked();
458 if (existing_transport != NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 D("local transport for port %d already registered (%p)?\n",
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100460 adb_port, existing_transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 fail = -1;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100462 } else if (index < 0) {
463 // Too many emulators.
464 D("cannot register more emulators. Maximum is %d\n",
465 ADB_LOCAL_TRANSPORT_MAX);
466 fail = -1;
467 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 local_transports[index] = t;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100469 }
470 }
471 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 }
473#endif
474 return fail;
475}