blob: 7cf7784658991f456c99cba64c460bb4b92f5332 [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 <unistd.h>
20#include <errno.h>
21#include <string.h>
22#include <ctype.h>
23
24#include "sysdeps.h"
25
jzhuan51297d222013-05-24 17:40:15 -040026#if !ADB_HOST
27#include <cutils/properties.h>
28#endif
29
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#define TRACE_TAG TRACE_SOCKETS
31#include "adb.h"
32
Josh Gao58eda352016-11-15 12:58:44 -080033#if defined(_WIN32)
34#define pthread_mutex_lock(...) abort()
35#define pthread_mutex_unlock(...) abort()
36#else
37#include <pthread.h>
Josh Gaoa9c0ac02016-10-21 11:25:46 -070038static pthread_mutex_t socket_list_lock;
39static void __attribute__((constructor)) socket_list_lock_init(void) {
40 pthread_mutexattr_t attr;
41 pthread_mutexattr_init(&attr);
42 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
43 pthread_mutex_init(&socket_list_lock, &attr);
44}
Josh Gao58eda352016-11-15 12:58:44 -080045#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
47int sendfailmsg(int fd, const char *reason)
48{
49 char buf[9];
50 int len;
51 len = strlen(reason);
52 if(len > 0xffff) len = 0xffff;
53 snprintf(buf, sizeof buf, "FAIL%04x", len);
54 if(writex(fd, buf, 8)) return -1;
55 return writex(fd, reason, len);
56}
57
58//extern int online;
59
60static unsigned local_socket_next_id = 1;
61
62static asocket local_socket_list = {
63 .next = &local_socket_list,
64 .prev = &local_socket_list,
65};
66
67/* the the list of currently closing local sockets.
68** these have no peer anymore, but still packets to
69** write to their fd.
70*/
71static asocket local_socket_closing_list = {
72 .next = &local_socket_closing_list,
73 .prev = &local_socket_closing_list,
74};
75
David 'Digit' Turner818d6412013-12-13 14:09:44 +010076// Parse the global list of sockets to find one with id |local_id|.
77// If |peer_id| is not 0, also check that it is connected to a peer
78// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
79asocket *find_local_socket(unsigned local_id, unsigned peer_id)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080{
81 asocket *s;
82 asocket *result = NULL;
83
Josh Gao83cb3032016-06-21 16:28:29 -070084 pthread_mutex_lock(&socket_list_lock);
André Goddard Rosa81828292010-06-12 11:40:20 -030085 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +010086 if (s->id != local_id)
87 continue;
88 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa81828292010-06-12 11:40:20 -030089 result = s;
André Goddard Rosa81828292010-06-12 11:40:20 -030090 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010091 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092 }
Josh Gao83cb3032016-06-21 16:28:29 -070093 pthread_mutex_unlock(&socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094
95 return result;
96}
97
98static void
99insert_local_socket(asocket* s, asocket* list)
100{
101 s->next = list;
102 s->prev = s->next->prev;
103 s->prev->next = s;
104 s->next->prev = s;
105}
106
107
108void install_local_socket(asocket *s)
109{
Josh Gao83cb3032016-06-21 16:28:29 -0700110 pthread_mutex_lock(&socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111
112 s->id = local_socket_next_id++;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100113
114 // Socket ids should never be 0.
115 if (local_socket_next_id == 0)
116 local_socket_next_id = 1;
117
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 insert_local_socket(s, &local_socket_list);
119
Josh Gao83cb3032016-06-21 16:28:29 -0700120 pthread_mutex_unlock(&socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121}
122
123void remove_socket(asocket *s)
124{
125 // socket_list_lock should already be held
126 if (s->prev && s->next)
127 {
128 s->prev->next = s->next;
129 s->next->prev = s->prev;
130 s->next = 0;
131 s->prev = 0;
132 s->id = 0;
133 }
134}
135
136void close_all_sockets(atransport *t)
137{
138 asocket *s;
139
140 /* this is a little gross, but since s->close() *will* modify
141 ** the list out from under you, your options are limited.
142 */
Josh Gao83cb3032016-06-21 16:28:29 -0700143 pthread_mutex_lock(&socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144restart:
145 for(s = local_socket_list.next; s != &local_socket_list; s = s->next){
146 if(s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gao83cb3032016-06-21 16:28:29 -0700147 s->close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 goto restart;
149 }
150 }
Josh Gao83cb3032016-06-21 16:28:29 -0700151 pthread_mutex_unlock(&socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152}
153
154static int local_socket_enqueue(asocket *s, apacket *p)
155{
156 D("LS(%d): enqueue %d\n", s->id, p->len);
157
158 p->ptr = p->data;
159
160 /* if there is already data queue'd, we will receive
161 ** events when it's time to write. just add this to
162 ** the tail
163 */
164 if(s->pkt_first) {
165 goto enqueue;
166 }
167
168 /* write as much as we can, until we
169 ** would block or there is an error/eof
170 */
171 while(p->len > 0) {
172 int r = adb_write(s->fd, p->ptr, p->len);
173 if(r > 0) {
174 p->len -= r;
175 p->ptr += r;
176 continue;
177 }
178 if((r == 0) || (errno != EAGAIN)) {
179 D( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) );
180 s->close(s);
181 return 1; /* not ready (error) */
182 } else {
183 break;
184 }
185 }
186
187 if(p->len == 0) {
188 put_apacket(p);
189 return 0; /* ready for more data */
190 }
191
192enqueue:
193 p->next = 0;
194 if(s->pkt_first) {
195 s->pkt_last->next = p;
196 } else {
197 s->pkt_first = p;
198 }
199 s->pkt_last = p;
200
201 /* make sure we are notified when we can drain the queue */
202 fdevent_add(&s->fde, FDE_WRITE);
203
204 return 1; /* not ready (backlog) */
205}
206
207static void local_socket_ready(asocket *s)
208{
209 /* far side is ready for data, pay attention to
210 readable events */
211 fdevent_add(&s->fde, FDE_READ);
212// D("LS(%d): ready()\n", s->id);
213}
214
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215// be sure to hold the socket list lock when calling this
216static void local_socket_destroy(asocket *s)
217{
218 apacket *p, *n;
Benoit Gobyf366b362012-03-16 14:50:07 -0700219 int exit_on_close = s->exit_on_close;
220
JP Abgrall408fa572011-03-16 15:57:42 -0700221 D("LS(%d): destroying fde.fd=%d\n", s->id, s->fde.fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222
223 /* IMPORTANT: the remove closes the fd
224 ** that belongs to this socket
225 */
226 fdevent_remove(&s->fde);
227
228 /* dispose of any unwritten data */
229 for(p = s->pkt_first; p; p = n) {
230 D("LS(%d): discarding %d bytes\n", s->id, p->len);
231 n = p->next;
232 put_apacket(p);
233 }
234 remove_socket(s);
235 free(s);
Benoit Gobyf366b362012-03-16 14:50:07 -0700236
237 if (exit_on_close) {
238 D("local_socket_destroy: exiting\n");
239 exit(1);
240 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241}
242
243
Josh Gao83cb3032016-06-21 16:28:29 -0700244static void local_socket_close(asocket *s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245{
Josh Gao83cb3032016-06-21 16:28:29 -0700246 pthread_mutex_lock(&socket_list_lock);
JP Abgrall408fa572011-03-16 15:57:42 -0700247 D("entered. LS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 if(s->peer) {
JP Abgrall408fa572011-03-16 15:57:42 -0700249 D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n",
250 s->id, s->peer->id, s->peer->fd);
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100251 /* Note: it's important to call shutdown before disconnecting from
252 * the peer, this ensures that remote sockets can still get the id
253 * of the local socket they're connected to, to send a CLOSE()
254 * protocol event. */
255 if (s->peer->shutdown)
256 s->peer->shutdown(s->peer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 s->peer->peer = 0;
Josh Gao83cb3032016-06-21 16:28:29 -0700258 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500259 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260 }
261
262 /* If we are already closing, or if there are no
263 ** pending packets, destroy immediately
264 */
265 if (s->closing || s->pkt_first == NULL) {
266 int id = s->id;
267 local_socket_destroy(s);
268 D("LS(%d): closed\n", id);
Josh Gao83cb3032016-06-21 16:28:29 -0700269 pthread_mutex_unlock(&socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 return;
271 }
272
273 /* otherwise, put on the closing list
274 */
275 D("LS(%d): closing\n", s->id);
276 s->closing = 1;
277 fdevent_del(&s->fde, FDE_READ);
278 remove_socket(s);
JP Abgrall408fa572011-03-16 15:57:42 -0700279 D("LS(%d): put on socket_closing_list fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 insert_local_socket(s, &local_socket_closing_list);
Josh Gao83cb3032016-06-21 16:28:29 -0700281 pthread_mutex_unlock(&socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282}
283
284static void local_socket_event_func(int fd, unsigned ev, void *_s)
285{
286 asocket *s = _s;
287
JP Abgrall408fa572011-03-16 15:57:42 -0700288 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);
289
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290 /* put the FDE_WRITE processing before the FDE_READ
291 ** in order to simplify the code.
292 */
293 if(ev & FDE_WRITE){
294 apacket *p;
295
296 while((p = s->pkt_first) != 0) {
297 while(p->len > 0) {
298 int r = adb_write(fd, p->ptr, p->len);
299 if(r > 0) {
300 p->ptr += r;
301 p->len -= r;
302 continue;
303 }
304 if(r < 0) {
305 /* returning here is ok because FDE_READ will
306 ** be processed in the next iteration loop
307 */
308 if(errno == EAGAIN) return;
309 if(errno == EINTR) continue;
310 }
Christopher Tate5b811fa2011-06-10 11:38:37 -0700311 D(" closing after write because r=%d and errno is %d\n", r, errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 s->close(s);
313 return;
314 }
315
316 if(p->len == 0) {
317 s->pkt_first = p->next;
318 if(s->pkt_first == 0) s->pkt_last = 0;
319 put_apacket(p);
320 }
321 }
322
323 /* if we sent the last packet of a closing socket,
324 ** we can now destroy it.
325 */
326 if (s->closing) {
Christopher Tate5b811fa2011-06-10 11:38:37 -0700327 D(" closing because 'closing' is set after write\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 s->close(s);
329 return;
330 }
331
332 /* no more packets queued, so we can ignore
333 ** writable events again and tell our peer
334 ** to resume writing
335 */
336 fdevent_del(&s->fde, FDE_WRITE);
337 s->peer->ready(s->peer);
338 }
339
340
341 if(ev & FDE_READ){
342 apacket *p = get_apacket();
343 unsigned char *x = p->data;
344 size_t avail = MAX_PAYLOAD;
345 int r;
346 int is_eof = 0;
347
348 while(avail > 0) {
349 r = adb_read(fd, x, avail);
Elliott Hughesccecf142014-01-16 10:53:11 -0800350 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", s->id, s->fd, r, r<0?errno:0, avail);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 if(r > 0) {
352 avail -= r;
353 x += r;
354 continue;
355 }
356 if(r < 0) {
357 if(errno == EAGAIN) break;
358 if(errno == EINTR) continue;
359 }
360
361 /* r = 0 or unhandled error */
362 is_eof = 1;
363 break;
364 }
JP Abgrall408fa572011-03-16 15:57:42 -0700365 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
366 s->id, s->fd, r, is_eof, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
368 put_apacket(p);
369 } else {
370 p->len = MAX_PAYLOAD - avail;
371
372 r = s->peer->enqueue(s->peer, p);
JP Abgrall408fa572011-03-16 15:57:42 -0700373 D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, r);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374
375 if(r < 0) {
376 /* error return means they closed us as a side-effect
377 ** and we must return immediately.
378 **
379 ** note that if we still have buffered packets, the
380 ** socket will be placed on the closing socket list.
381 ** this handler function will be called again
382 ** to process FDE_WRITE events.
383 */
384 return;
385 }
386
387 if(r > 0) {
388 /* if the remote cannot accept further events,
389 ** we disable notification of READs. They'll
390 ** be enabled again when we get a call to ready()
391 */
392 fdevent_del(&s->fde, FDE_READ);
393 }
394 }
JP Abgrall112445b2011-04-12 22:01:58 -0700395 /* Don't allow a forced eof if data is still there */
396 if((s->fde.force_eof && !r) || is_eof) {
Christopher Tate5b811fa2011-06-10 11:38:37 -0700397 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n", is_eof, r, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 s->close(s);
399 }
400 }
401
402 if(ev & FDE_ERROR){
403 /* this should be caught be the next read or write
404 ** catching it here means we may skip the last few
405 ** bytes of readable data.
406 */
407// s->close(s);
JP Abgrall408fa572011-03-16 15:57:42 -0700408 D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
409
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 return;
411 }
412}
413
414asocket *create_local_socket(int fd)
415{
416 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300417 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 s->fd = fd;
419 s->enqueue = local_socket_enqueue;
420 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100421 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700423 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424
425 fdevent_install(&s->fde, fd, local_socket_event_func, s);
426/* fdevent_add(&s->fde, FDE_ERROR); */
427 //fprintf(stderr, "Created local socket in create_local_socket \n");
428 D("LS(%d): created (fd=%d)\n", s->id, s->fd);
429 return s;
430}
431
432asocket *create_local_service_socket(const char *name)
433{
434 asocket *s;
435 int fd;
jzhuan51297d222013-05-24 17:40:15 -0400436#if !ADB_HOST
437 char debug[PROPERTY_VALUE_MAX];
438#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439
440#if !ADB_HOST
441 if (!strcmp(name,"jdwp")) {
442 return create_jdwp_service_socket();
443 }
444 if (!strcmp(name,"track-jdwp")) {
445 return create_jdwp_tracker_service_socket();
446 }
447#endif
448 fd = service_to_fd(name);
449 if(fd < 0) return 0;
450
451 s = create_local_socket(fd);
JP Abgrall408fa572011-03-16 15:57:42 -0700452 D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
Benoit Gobyf366b362012-03-16 14:50:07 -0700453
JP Abgrallf91259a2012-03-30 13:19:11 -0700454#if !ADB_HOST
jzhuan51297d222013-05-24 17:40:15 -0400455 if (!strncmp(name, "root:", 5))
456 property_get("ro.debuggable", debug, "");
457
458 if ((!strncmp(name, "root:", 5) && getuid() != 0
459 && strcmp(debug, "1") == 0)
Benoit Gobyaeceb512012-06-12 12:12:18 -0700460 || !strncmp(name, "usb:", 4)
461 || !strncmp(name, "tcpip:", 6)) {
Benoit Gobyf366b362012-03-16 14:50:07 -0700462 D("LS(%d): enabling exit_on_close\n", s->id);
463 s->exit_on_close = 1;
464 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700465#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700466
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 return s;
468}
469
470#if ADB_HOST
471static asocket *create_host_service_socket(const char *name, const char* serial)
472{
473 asocket *s;
474
475 s = host_service_to_socket(name, serial);
476
477 if (s != NULL) {
478 D("LS(%d) bound to '%s'\n", s->id, name);
479 return s;
480 }
481
482 return s;
483}
484#endif /* ADB_HOST */
485
486/* a Remote socket is used to send/receive data to/from a given transport object
487** it needs to be closed when the transport is forcibly destroyed by the user
488*/
489typedef struct aremotesocket {
490 asocket socket;
491 adisconnect disconnect;
492} aremotesocket;
493
494static int remote_socket_enqueue(asocket *s, apacket *p)
495{
JP Abgrall408fa572011-03-16 15:57:42 -0700496 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n",
497 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 p->msg.command = A_WRTE;
499 p->msg.arg0 = s->peer->id;
500 p->msg.arg1 = s->id;
501 p->msg.data_length = p->len;
502 send_packet(p, s->transport);
503 return 1;
504}
505
506static void remote_socket_ready(asocket *s)
507{
JP Abgrall408fa572011-03-16 15:57:42 -0700508 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n",
509 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 apacket *p = get_apacket();
511 p->msg.command = A_OKAY;
512 p->msg.arg0 = s->peer->id;
513 p->msg.arg1 = s->id;
514 send_packet(p, s->transport);
515}
516
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100517static void remote_socket_shutdown(asocket *s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518{
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100519 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d\n",
JP Abgrall408fa572011-03-16 15:57:42 -0700520 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521 apacket *p = get_apacket();
522 p->msg.command = A_CLSE;
523 if(s->peer) {
524 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100525 }
526 p->msg.arg1 = s->id;
527 send_packet(p, s->transport);
528}
529
530static void remote_socket_close(asocket *s)
531{
532 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533 s->peer->peer = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700534 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n",
535 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536 s->peer->close(s->peer);
537 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100538 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n",
539 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540 D("RS(%d): closed\n", s->id);
541 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
542 free(s);
543}
544
545static void remote_socket_disconnect(void* _s, atransport* t)
546{
547 asocket* s = _s;
548 asocket* peer = s->peer;
549
550 D("remote_socket_disconnect RS(%d)\n", s->id);
551 if (peer) {
552 peer->peer = NULL;
553 peer->close(peer);
554 }
555 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
556 free(s);
557}
558
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100559/* Create an asocket to exchange packets with a remote service through transport
560 |t|. Where |id| is the socket id of the corresponding service on the other
561 side of the transport (it is allocated by the remote side and _cannot_ be 0).
562 Returns a new non-NULL asocket handle. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563asocket *create_remote_socket(unsigned id, atransport *t)
564{
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100565 asocket* s;
566 adisconnect* dis;
567
568 if (id == 0) fatal("invalid remote socket id (0)");
569 s = calloc(1, sizeof(aremotesocket));
570 dis = &((aremotesocket*)s)->disconnect;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800571
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300572 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800573 s->id = id;
574 s->enqueue = remote_socket_enqueue;
575 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100576 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577 s->close = remote_socket_close;
578 s->transport = t;
579
580 dis->func = remote_socket_disconnect;
581 dis->opaque = s;
582 add_transport_disconnect( t, dis );
583 D("RS(%d): created\n", s->id);
584 return s;
585}
586
587void connect_to_remote(asocket *s, const char *destination)
588{
JP Abgrall408fa572011-03-16 15:57:42 -0700589 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 apacket *p = get_apacket();
591 int len = strlen(destination) + 1;
592
593 if(len > (MAX_PAYLOAD-1)) {
594 fatal("destination oversized");
595 }
596
597 D("LS(%d): connect('%s')\n", s->id, destination);
598 p->msg.command = A_OPEN;
599 p->msg.arg0 = s->id;
600 p->msg.data_length = len;
601 strcpy((char*) p->data, destination);
602 send_packet(p, s->transport);
603}
604
605
606/* this is used by magic sockets to rig local sockets to
607 send the go-ahead message when they connect */
608static void local_socket_ready_notify(asocket *s)
609{
610 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100611 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612 s->close = local_socket_close;
613 adb_write(s->fd, "OKAY", 4);
614 s->ready(s);
615}
616
617/* this is used by magic sockets to rig local sockets to
618 send the failure message if they are closed before
619 connected (to avoid closing them without a status message) */
620static void local_socket_close_notify(asocket *s)
621{
622 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100623 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800624 s->close = local_socket_close;
625 sendfailmsg(s->fd, "closed");
626 s->close(s);
627}
628
629unsigned unhex(unsigned char *s, int len)
630{
631 unsigned n = 0, c;
632
633 while(len-- > 0) {
634 switch((c = *s++)) {
635 case '0': case '1': case '2':
636 case '3': case '4': case '5':
637 case '6': case '7': case '8':
638 case '9':
639 c -= '0';
640 break;
641 case 'a': case 'b': case 'c':
642 case 'd': case 'e': case 'f':
643 c = c - 'a' + 10;
644 break;
645 case 'A': case 'B': case 'C':
646 case 'D': case 'E': case 'F':
647 c = c - 'A' + 10;
648 break;
649 default:
650 return 0xffffffff;
651 }
652
653 n = (n << 4) | c;
654 }
655
656 return n;
657}
658
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700659#define PREFIX(str) { str, sizeof(str) - 1 }
660static const struct prefix_struct {
661 const char *str;
662 const size_t len;
663} prefixes[] = {
664 PREFIX("usb:"),
665 PREFIX("product:"),
666 PREFIX("model:"),
667 PREFIX("device:"),
668};
669static const int num_prefixes = (sizeof(prefixes) / sizeof(prefixes[0]));
670
Terence Haddock28e13902011-03-16 09:43:56 +0100671/* skip_host_serial return the position in a string
672 skipping over the 'serial' parameter in the ADB protocol,
673 where parameter string may be a host:port string containing
674 the protocol delimiter (colon). */
675char *skip_host_serial(char *service) {
676 char *first_colon, *serial_end;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700677 int i;
Terence Haddock28e13902011-03-16 09:43:56 +0100678
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700679 for (i = 0; i < num_prefixes; i++) {
680 if (!strncmp(service, prefixes[i].str, prefixes[i].len))
681 return strchr(service + prefixes[i].len, ':');
Scott Anderson3608d832012-05-31 12:04:23 -0700682 }
683
Terence Haddock28e13902011-03-16 09:43:56 +0100684 first_colon = strchr(service, ':');
685 if (!first_colon) {
686 /* No colon in service string. */
687 return NULL;
688 }
689 serial_end = first_colon;
690 if (isdigit(serial_end[1])) {
691 serial_end++;
692 while ((*serial_end) && isdigit(*serial_end)) {
693 serial_end++;
694 }
695 if ((*serial_end) != ':') {
696 // Something other than numbers was found, reset the end.
697 serial_end = first_colon;
698 }
699 }
700 return serial_end;
701}
702
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703static int smart_socket_enqueue(asocket *s, apacket *p)
704{
705 unsigned len;
706#if ADB_HOST
707 char *service = NULL;
708 char* serial = NULL;
709 transport_type ttype = kTransportAny;
710#endif
711
712 D("SS(%d): enqueue %d\n", s->id, p->len);
713
714 if(s->pkt_first == 0) {
715 s->pkt_first = p;
716 s->pkt_last = p;
717 } else {
718 if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
719 D("SS(%d): overflow\n", s->id);
720 put_apacket(p);
721 goto fail;
722 }
723
724 memcpy(s->pkt_first->data + s->pkt_first->len,
725 p->data, p->len);
726 s->pkt_first->len += p->len;
727 put_apacket(p);
728
729 p = s->pkt_first;
730 }
731
732 /* don't bother if we can't decode the length */
733 if(p->len < 4) return 0;
734
735 len = unhex(p->data, 4);
736 if((len < 1) || (len > 1024)) {
737 D("SS(%d): bad size (%d)\n", s->id, len);
738 goto fail;
739 }
740
741 D("SS(%d): len is %d\n", s->id, len );
742 /* can't do anything until we have the full header */
743 if((len + 4) > p->len) {
744 D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
745 return 0;
746 }
747
748 p->data[len + 4] = 0;
749
750 D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));
751
752#if ADB_HOST
753 service = (char *)p->data + 4;
754 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
755 char* serial_end;
756 service += strlen("host-serial:");
757
Terence Haddock28e13902011-03-16 09:43:56 +0100758 // serial number should follow "host:" and could be a host:port string.
759 serial_end = skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 if (serial_end) {
761 *serial_end = 0; // terminate string
762 serial = service;
763 service = serial_end + 1;
764 }
765 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
766 ttype = kTransportUsb;
767 service += strlen("host-usb:");
768 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
769 ttype = kTransportLocal;
770 service += strlen("host-local:");
771 } else if (!strncmp(service, "host:", strlen("host:"))) {
772 ttype = kTransportAny;
773 service += strlen("host:");
774 } else {
775 service = NULL;
776 }
777
778 if (service) {
779 asocket *s2;
780
781 /* some requests are handled immediately -- in that
782 ** case the handle_host_request() routine has sent
783 ** the OKAY or FAIL message and all we have to do
784 ** is clean up.
785 */
786 if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
787 /* XXX fail message? */
788 D( "SS(%d): handled host service '%s'\n", s->id, service );
789 goto fail;
790 }
791 if (!strncmp(service, "transport", strlen("transport"))) {
792 D( "SS(%d): okay transport\n", s->id );
793 p->len = 0;
794 return 0;
795 }
796
797 /* try to find a local service with this name.
798 ** if no such service exists, we'll fail out
799 ** and tear down here.
800 */
801 s2 = create_host_service_socket(service, serial);
802 if(s2 == 0) {
803 D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
804 sendfailmsg(s->peer->fd, "unknown host service");
805 goto fail;
806 }
807
808 /* we've connected to a local host service,
809 ** so we make our peer back into a regular
810 ** local socket and bind it to the new local
811 ** service socket, acknowledge the successful
812 ** connection, and close this smart socket now
813 ** that its work is done.
814 */
815 adb_write(s->peer->fd, "OKAY", 4);
816
817 s->peer->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100818 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800819 s->peer->close = local_socket_close;
820 s->peer->peer = s2;
821 s2->peer = s->peer;
822 s->peer = 0;
823 D( "SS(%d): okay\n", s->id );
824 s->close(s);
825
826 /* initial state is "ready" */
827 s2->ready(s2);
828 return 0;
829 }
830#else /* !ADB_HOST */
831 if (s->transport == NULL) {
832 char* error_string = "unknown failure";
833 s->transport = acquire_one_transport (CS_ANY,
834 kTransportAny, NULL, &error_string);
835
836 if (s->transport == NULL) {
837 sendfailmsg(s->peer->fd, error_string);
838 goto fail;
839 }
840 }
841#endif
842
843 if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
844 /* if there's no remote we fail the connection
845 ** right here and terminate it
846 */
847 sendfailmsg(s->peer->fd, "device offline (x)");
848 goto fail;
849 }
850
851
852 /* instrument our peer to pass the success or fail
853 ** message back once it connects or closes, then
854 ** detach from it, request the connection, and
855 ** tear down
856 */
857 s->peer->ready = local_socket_ready_notify;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100858 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800859 s->peer->close = local_socket_close_notify;
860 s->peer->peer = 0;
861 /* give him our transport and upref it */
862 s->peer->transport = s->transport;
863
864 connect_to_remote(s->peer, (char*) (p->data + 4));
865 s->peer = 0;
866 s->close(s);
867 return 1;
868
869fail:
870 /* we're going to close our peer as a side-effect, so
871 ** return -1 to signal that state to the local socket
872 ** who is enqueueing against us
873 */
874 s->close(s);
875 return -1;
876}
877
878static void smart_socket_ready(asocket *s)
879{
880 D("SS(%d): ready\n", s->id);
881}
882
883static void smart_socket_close(asocket *s)
884{
885 D("SS(%d): closed\n", s->id);
886 if(s->pkt_first){
887 put_apacket(s->pkt_first);
888 }
889 if(s->peer) {
890 s->peer->peer = 0;
891 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500892 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800893 }
894 free(s);
895}
896
Benoit Goby9470c2f2013-02-20 15:04:53 -0800897static asocket *create_smart_socket(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800898{
899 D("Creating smart socket \n");
900 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300901 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800902 s->enqueue = smart_socket_enqueue;
903 s->ready = smart_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100904 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800905 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800906
Benoit Goby9470c2f2013-02-20 15:04:53 -0800907 D("SS(%d)\n", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800908 return s;
909}
910
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800911void connect_to_smartsocket(asocket *s)
912{
913 D("Connecting to smart socket \n");
Benoit Goby9470c2f2013-02-20 15:04:53 -0800914 asocket *ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800915 s->peer = ss;
916 ss->peer = s;
917 s->ready(s);
918}