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 #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
26 #if !ADB_HOST
27 #include <cutils/properties.h>
28 #endif
29
30 #define TRACE_TAG TRACE_SOCKETS
31 #include "adb.h"
32
33 ADB_MUTEX_DEFINE( socket_list_lock );
34
35 static void local_socket_close_locked(asocket *s);
36
sendfailmsg(int fd,const char * reason)37 int sendfailmsg(int fd, const char *reason)
38 {
39 char buf[9];
40 int len;
41 len = strlen(reason);
42 if(len > 0xffff) len = 0xffff;
43 snprintf(buf, sizeof buf, "FAIL%04x", len);
44 if(writex(fd, buf, 8)) return -1;
45 return writex(fd, reason, len);
46 }
47
48 //extern int online;
49
50 static unsigned local_socket_next_id = 1;
51
52 static asocket local_socket_list = {
53 .next = &local_socket_list,
54 .prev = &local_socket_list,
55 };
56
57 /* the the list of currently closing local sockets.
58 ** these have no peer anymore, but still packets to
59 ** write to their fd.
60 */
61 static asocket local_socket_closing_list = {
62 .next = &local_socket_closing_list,
63 .prev = &local_socket_closing_list,
64 };
65
66 // Parse the global list of sockets to find one with id |local_id|.
67 // If |peer_id| is not 0, also check that it is connected to a peer
68 // with id |peer_id|. Returns an asocket handle on success, NULL on failure.
find_local_socket(unsigned local_id,unsigned peer_id)69 asocket *find_local_socket(unsigned local_id, unsigned peer_id)
70 {
71 asocket *s;
72 asocket *result = NULL;
73
74 adb_mutex_lock(&socket_list_lock);
75 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
76 if (s->id != local_id)
77 continue;
78 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
79 result = s;
80 }
81 break;
82 }
83 adb_mutex_unlock(&socket_list_lock);
84
85 return result;
86 }
87
88 static void
insert_local_socket(asocket * s,asocket * list)89 insert_local_socket(asocket* s, asocket* list)
90 {
91 s->next = list;
92 s->prev = s->next->prev;
93 s->prev->next = s;
94 s->next->prev = s;
95 }
96
97
install_local_socket(asocket * s)98 void install_local_socket(asocket *s)
99 {
100 adb_mutex_lock(&socket_list_lock);
101
102 s->id = local_socket_next_id++;
103
104 // Socket ids should never be 0.
105 if (local_socket_next_id == 0)
106 local_socket_next_id = 1;
107
108 insert_local_socket(s, &local_socket_list);
109
110 adb_mutex_unlock(&socket_list_lock);
111 }
112
remove_socket(asocket * s)113 void remove_socket(asocket *s)
114 {
115 // socket_list_lock should already be held
116 if (s->prev && s->next)
117 {
118 s->prev->next = s->next;
119 s->next->prev = s->prev;
120 s->next = 0;
121 s->prev = 0;
122 s->id = 0;
123 }
124 }
125
close_all_sockets(atransport * t)126 void close_all_sockets(atransport *t)
127 {
128 asocket *s;
129
130 /* this is a little gross, but since s->close() *will* modify
131 ** the list out from under you, your options are limited.
132 */
133 adb_mutex_lock(&socket_list_lock);
134 restart:
135 for(s = local_socket_list.next; s != &local_socket_list; s = s->next){
136 if(s->transport == t || (s->peer && s->peer->transport == t)) {
137 local_socket_close_locked(s);
138 goto restart;
139 }
140 }
141 adb_mutex_unlock(&socket_list_lock);
142 }
143
local_socket_enqueue(asocket * s,apacket * p)144 static int local_socket_enqueue(asocket *s, apacket *p)
145 {
146 D("LS(%d): enqueue %d\n", s->id, p->len);
147
148 p->ptr = p->data;
149
150 /* if there is already data queue'd, we will receive
151 ** events when it's time to write. just add this to
152 ** the tail
153 */
154 if(s->pkt_first) {
155 goto enqueue;
156 }
157
158 /* write as much as we can, until we
159 ** would block or there is an error/eof
160 */
161 while(p->len > 0) {
162 int r = adb_write(s->fd, p->ptr, p->len);
163 if(r > 0) {
164 p->len -= r;
165 p->ptr += r;
166 continue;
167 }
168 if((r == 0) || (errno != EAGAIN)) {
169 D( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) );
170 s->close(s);
171 return 1; /* not ready (error) */
172 } else {
173 break;
174 }
175 }
176
177 if(p->len == 0) {
178 put_apacket(p);
179 return 0; /* ready for more data */
180 }
181
182 enqueue:
183 p->next = 0;
184 if(s->pkt_first) {
185 s->pkt_last->next = p;
186 } else {
187 s->pkt_first = p;
188 }
189 s->pkt_last = p;
190
191 /* make sure we are notified when we can drain the queue */
192 fdevent_add(&s->fde, FDE_WRITE);
193
194 return 1; /* not ready (backlog) */
195 }
196
local_socket_ready(asocket * s)197 static void local_socket_ready(asocket *s)
198 {
199 /* far side is ready for data, pay attention to
200 readable events */
201 fdevent_add(&s->fde, FDE_READ);
202 // D("LS(%d): ready()\n", s->id);
203 }
204
local_socket_close(asocket * s)205 static void local_socket_close(asocket *s)
206 {
207 adb_mutex_lock(&socket_list_lock);
208 local_socket_close_locked(s);
209 adb_mutex_unlock(&socket_list_lock);
210 }
211
212 // be sure to hold the socket list lock when calling this
local_socket_destroy(asocket * s)213 static void local_socket_destroy(asocket *s)
214 {
215 apacket *p, *n;
216 int exit_on_close = s->exit_on_close;
217
218 D("LS(%d): destroying fde.fd=%d\n", s->id, s->fde.fd);
219
220 /* IMPORTANT: the remove closes the fd
221 ** that belongs to this socket
222 */
223 fdevent_remove(&s->fde);
224
225 /* dispose of any unwritten data */
226 for(p = s->pkt_first; p; p = n) {
227 D("LS(%d): discarding %d bytes\n", s->id, p->len);
228 n = p->next;
229 put_apacket(p);
230 }
231 remove_socket(s);
232 free(s);
233
234 if (exit_on_close) {
235 D("local_socket_destroy: exiting\n");
236 exit(1);
237 }
238 }
239
240
local_socket_close_locked(asocket * s)241 static void local_socket_close_locked(asocket *s)
242 {
243 D("entered. LS(%d) fd=%d\n", s->id, s->fd);
244 if(s->peer) {
245 D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n",
246 s->id, s->peer->id, s->peer->fd);
247 /* Note: it's important to call shutdown before disconnecting from
248 * the peer, this ensures that remote sockets can still get the id
249 * of the local socket they're connected to, to send a CLOSE()
250 * protocol event. */
251 if (s->peer->shutdown)
252 s->peer->shutdown(s->peer);
253 s->peer->peer = 0;
254 // tweak to avoid deadlock
255 if (s->peer->close == local_socket_close) {
256 local_socket_close_locked(s->peer);
257 } else {
258 s->peer->close(s->peer);
259 }
260 s->peer = 0;
261 }
262
263 /* If we are already closing, or if there are no
264 ** pending packets, destroy immediately
265 */
266 if (s->closing || s->pkt_first == NULL) {
267 int id = s->id;
268 local_socket_destroy(s);
269 D("LS(%d): closed\n", id);
270 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);
279 D("LS(%d): put on socket_closing_list fd=%d\n", s->id, s->fd);
280 insert_local_socket(s, &local_socket_closing_list);
281 }
282
local_socket_event_func(int fd,unsigned ev,void * _s)283 static void local_socket_event_func(int fd, unsigned ev, void *_s)
284 {
285 asocket *s = _s;
286
287 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);
288
289 /* put the FDE_WRITE processing before the FDE_READ
290 ** in order to simplify the code.
291 */
292 if(ev & FDE_WRITE){
293 apacket *p;
294
295 while((p = s->pkt_first) != 0) {
296 while(p->len > 0) {
297 int r = adb_write(fd, p->ptr, p->len);
298 if(r > 0) {
299 p->ptr += r;
300 p->len -= r;
301 continue;
302 }
303 if(r < 0) {
304 /* returning here is ok because FDE_READ will
305 ** be processed in the next iteration loop
306 */
307 if(errno == EAGAIN) return;
308 if(errno == EINTR) continue;
309 }
310 D(" closing after write because r=%d and errno is %d\n", r, errno);
311 s->close(s);
312 return;
313 }
314
315 if(p->len == 0) {
316 s->pkt_first = p->next;
317 if(s->pkt_first == 0) s->pkt_last = 0;
318 put_apacket(p);
319 }
320 }
321
322 /* if we sent the last packet of a closing socket,
323 ** we can now destroy it.
324 */
325 if (s->closing) {
326 D(" closing because 'closing' is set after write\n");
327 s->close(s);
328 return;
329 }
330
331 /* no more packets queued, so we can ignore
332 ** writable events again and tell our peer
333 ** to resume writing
334 */
335 fdevent_del(&s->fde, FDE_WRITE);
336 s->peer->ready(s->peer);
337 }
338
339
340 if(ev & FDE_READ){
341 apacket *p = get_apacket();
342 unsigned char *x = p->data;
343 size_t avail = MAX_PAYLOAD;
344 int r;
345 int is_eof = 0;
346
347 while(avail > 0) {
348 r = adb_read(fd, x, avail);
349 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);
350 if(r > 0) {
351 avail -= r;
352 x += r;
353 continue;
354 }
355 if(r < 0) {
356 if(errno == EAGAIN) break;
357 if(errno == EINTR) continue;
358 }
359
360 /* r = 0 or unhandled error */
361 is_eof = 1;
362 break;
363 }
364 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
365 s->id, s->fd, r, is_eof, s->fde.force_eof);
366 if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
367 put_apacket(p);
368 } else {
369 p->len = MAX_PAYLOAD - avail;
370
371 r = s->peer->enqueue(s->peer, p);
372 D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, r);
373
374 if(r < 0) {
375 /* error return means they closed us as a side-effect
376 ** and we must return immediately.
377 **
378 ** note that if we still have buffered packets, the
379 ** socket will be placed on the closing socket list.
380 ** this handler function will be called again
381 ** to process FDE_WRITE events.
382 */
383 return;
384 }
385
386 if(r > 0) {
387 /* if the remote cannot accept further events,
388 ** we disable notification of READs. They'll
389 ** be enabled again when we get a call to ready()
390 */
391 fdevent_del(&s->fde, FDE_READ);
392 }
393 }
394 /* Don't allow a forced eof if data is still there */
395 if((s->fde.force_eof && !r) || is_eof) {
396 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n", is_eof, r, s->fde.force_eof);
397 s->close(s);
398 }
399 }
400
401 if(ev & FDE_ERROR){
402 /* this should be caught be the next read or write
403 ** catching it here means we may skip the last few
404 ** bytes of readable data.
405 */
406 // s->close(s);
407 D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
408
409 return;
410 }
411 }
412
create_local_socket(int fd)413 asocket *create_local_socket(int fd)
414 {
415 asocket *s = calloc(1, sizeof(asocket));
416 if (s == NULL) fatal("cannot allocate socket");
417 s->fd = fd;
418 s->enqueue = local_socket_enqueue;
419 s->ready = local_socket_ready;
420 s->shutdown = NULL;
421 s->close = local_socket_close;
422 install_local_socket(s);
423
424 fdevent_install(&s->fde, fd, local_socket_event_func, s);
425 /* fdevent_add(&s->fde, FDE_ERROR); */
426 //fprintf(stderr, "Created local socket in create_local_socket \n");
427 D("LS(%d): created (fd=%d)\n", s->id, s->fd);
428 return s;
429 }
430
create_local_service_socket(const char * name)431 asocket *create_local_service_socket(const char *name)
432 {
433 asocket *s;
434 int fd;
435 #if !ADB_HOST
436 char debug[PROPERTY_VALUE_MAX];
437 #endif
438
439 #if !ADB_HOST
440 if (!strcmp(name,"jdwp")) {
441 return create_jdwp_service_socket();
442 }
443 if (!strcmp(name,"track-jdwp")) {
444 return create_jdwp_tracker_service_socket();
445 }
446 #endif
447 fd = service_to_fd(name);
448 if(fd < 0) return 0;
449
450 s = create_local_socket(fd);
451 D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
452
453 #if !ADB_HOST
454 if (!strncmp(name, "root:", 5))
455 property_get("ro.debuggable", debug, "");
456
457 if ((!strncmp(name, "root:", 5) && getuid() != 0
458 && strcmp(debug, "1") == 0)
459 || !strncmp(name, "usb:", 4)
460 || !strncmp(name, "tcpip:", 6)) {
461 D("LS(%d): enabling exit_on_close\n", s->id);
462 s->exit_on_close = 1;
463 }
464 #endif
465
466 return s;
467 }
468
469 #if ADB_HOST
create_host_service_socket(const char * name,const char * serial)470 static asocket *create_host_service_socket(const char *name, const char* serial)
471 {
472 asocket *s;
473
474 s = host_service_to_socket(name, serial);
475
476 if (s != NULL) {
477 D("LS(%d) bound to '%s'\n", s->id, name);
478 return s;
479 }
480
481 return s;
482 }
483 #endif /* ADB_HOST */
484
485 /* a Remote socket is used to send/receive data to/from a given transport object
486 ** it needs to be closed when the transport is forcibly destroyed by the user
487 */
488 typedef struct aremotesocket {
489 asocket socket;
490 adisconnect disconnect;
491 } aremotesocket;
492
remote_socket_enqueue(asocket * s,apacket * p)493 static int remote_socket_enqueue(asocket *s, apacket *p)
494 {
495 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n",
496 s->id, s->fd, s->peer->fd);
497 p->msg.command = A_WRTE;
498 p->msg.arg0 = s->peer->id;
499 p->msg.arg1 = s->id;
500 p->msg.data_length = p->len;
501 send_packet(p, s->transport);
502 return 1;
503 }
504
remote_socket_ready(asocket * s)505 static void remote_socket_ready(asocket *s)
506 {
507 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n",
508 s->id, s->fd, s->peer->fd);
509 apacket *p = get_apacket();
510 p->msg.command = A_OKAY;
511 p->msg.arg0 = s->peer->id;
512 p->msg.arg1 = s->id;
513 send_packet(p, s->transport);
514 }
515
remote_socket_shutdown(asocket * s)516 static void remote_socket_shutdown(asocket *s)
517 {
518 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d\n",
519 s->id, s->fd, s->peer?s->peer->fd:-1);
520 apacket *p = get_apacket();
521 p->msg.command = A_CLSE;
522 if(s->peer) {
523 p->msg.arg0 = s->peer->id;
524 }
525 p->msg.arg1 = s->id;
526 send_packet(p, s->transport);
527 }
528
remote_socket_close(asocket * s)529 static void remote_socket_close(asocket *s)
530 {
531 if (s->peer) {
532 s->peer->peer = 0;
533 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n",
534 s->id, s->peer->id, s->peer->fd);
535 s->peer->close(s->peer);
536 }
537 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n",
538 s->id, s->fd, s->peer?s->peer->fd:-1);
539 D("RS(%d): closed\n", s->id);
540 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
541 free(s);
542 }
543
remote_socket_disconnect(void * _s,atransport * t)544 static void remote_socket_disconnect(void* _s, atransport* t)
545 {
546 asocket* s = _s;
547 asocket* peer = s->peer;
548
549 D("remote_socket_disconnect RS(%d)\n", s->id);
550 if (peer) {
551 peer->peer = NULL;
552 peer->close(peer);
553 }
554 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
555 free(s);
556 }
557
558 /* Create an asocket to exchange packets with a remote service through transport
559 |t|. Where |id| is the socket id of the corresponding service on the other
560 side of the transport (it is allocated by the remote side and _cannot_ be 0).
561 Returns a new non-NULL asocket handle. */
create_remote_socket(unsigned id,atransport * t)562 asocket *create_remote_socket(unsigned id, atransport *t)
563 {
564 asocket* s;
565 adisconnect* dis;
566
567 if (id == 0) fatal("invalid remote socket id (0)");
568 s = calloc(1, sizeof(aremotesocket));
569 dis = &((aremotesocket*)s)->disconnect;
570
571 if (s == NULL) fatal("cannot allocate socket");
572 s->id = id;
573 s->enqueue = remote_socket_enqueue;
574 s->ready = remote_socket_ready;
575 s->shutdown = remote_socket_shutdown;
576 s->close = remote_socket_close;
577 s->transport = t;
578
579 dis->func = remote_socket_disconnect;
580 dis->opaque = s;
581 add_transport_disconnect( t, dis );
582 D("RS(%d): created\n", s->id);
583 return s;
584 }
585
connect_to_remote(asocket * s,const char * destination)586 void connect_to_remote(asocket *s, const char *destination)
587 {
588 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
589 apacket *p = get_apacket();
590 int len = strlen(destination) + 1;
591
592 if(len > (MAX_PAYLOAD-1)) {
593 fatal("destination oversized");
594 }
595
596 D("LS(%d): connect('%s')\n", s->id, destination);
597 p->msg.command = A_OPEN;
598 p->msg.arg0 = s->id;
599 p->msg.data_length = len;
600 strcpy((char*) p->data, destination);
601 send_packet(p, s->transport);
602 }
603
604
605 /* this is used by magic sockets to rig local sockets to
606 send the go-ahead message when they connect */
local_socket_ready_notify(asocket * s)607 static void local_socket_ready_notify(asocket *s)
608 {
609 s->ready = local_socket_ready;
610 s->shutdown = NULL;
611 s->close = local_socket_close;
612 adb_write(s->fd, "OKAY", 4);
613 s->ready(s);
614 }
615
616 /* this is used by magic sockets to rig local sockets to
617 send the failure message if they are closed before
618 connected (to avoid closing them without a status message) */
local_socket_close_notify(asocket * s)619 static void local_socket_close_notify(asocket *s)
620 {
621 s->ready = local_socket_ready;
622 s->shutdown = NULL;
623 s->close = local_socket_close;
624 sendfailmsg(s->fd, "closed");
625 s->close(s);
626 }
627
unhex(unsigned char * s,int len)628 unsigned unhex(unsigned char *s, int len)
629 {
630 unsigned n = 0, c;
631
632 while(len-- > 0) {
633 switch((c = *s++)) {
634 case '0': case '1': case '2':
635 case '3': case '4': case '5':
636 case '6': case '7': case '8':
637 case '9':
638 c -= '0';
639 break;
640 case 'a': case 'b': case 'c':
641 case 'd': case 'e': case 'f':
642 c = c - 'a' + 10;
643 break;
644 case 'A': case 'B': case 'C':
645 case 'D': case 'E': case 'F':
646 c = c - 'A' + 10;
647 break;
648 default:
649 return 0xffffffff;
650 }
651
652 n = (n << 4) | c;
653 }
654
655 return n;
656 }
657
658 #define PREFIX(str) { str, sizeof(str) - 1 }
659 static const struct prefix_struct {
660 const char *str;
661 const size_t len;
662 } prefixes[] = {
663 PREFIX("usb:"),
664 PREFIX("product:"),
665 PREFIX("model:"),
666 PREFIX("device:"),
667 };
668 static const int num_prefixes = (sizeof(prefixes) / sizeof(prefixes[0]));
669
670 /* skip_host_serial return the position in a string
671 skipping over the 'serial' parameter in the ADB protocol,
672 where parameter string may be a host:port string containing
673 the protocol delimiter (colon). */
skip_host_serial(char * service)674 char *skip_host_serial(char *service) {
675 char *first_colon, *serial_end;
676 int i;
677
678 for (i = 0; i < num_prefixes; i++) {
679 if (!strncmp(service, prefixes[i].str, prefixes[i].len))
680 return strchr(service + prefixes[i].len, ':');
681 }
682
683 first_colon = strchr(service, ':');
684 if (!first_colon) {
685 /* No colon in service string. */
686 return NULL;
687 }
688 serial_end = first_colon;
689 if (isdigit(serial_end[1])) {
690 serial_end++;
691 while ((*serial_end) && isdigit(*serial_end)) {
692 serial_end++;
693 }
694 if ((*serial_end) != ':') {
695 // Something other than numbers was found, reset the end.
696 serial_end = first_colon;
697 }
698 }
699 return serial_end;
700 }
701
smart_socket_enqueue(asocket * s,apacket * p)702 static int smart_socket_enqueue(asocket *s, apacket *p)
703 {
704 unsigned len;
705 #if ADB_HOST
706 char *service = NULL;
707 char* serial = NULL;
708 transport_type ttype = kTransportAny;
709 #endif
710
711 D("SS(%d): enqueue %d\n", s->id, p->len);
712
713 if(s->pkt_first == 0) {
714 s->pkt_first = p;
715 s->pkt_last = p;
716 } else {
717 if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
718 D("SS(%d): overflow\n", s->id);
719 put_apacket(p);
720 goto fail;
721 }
722
723 memcpy(s->pkt_first->data + s->pkt_first->len,
724 p->data, p->len);
725 s->pkt_first->len += p->len;
726 put_apacket(p);
727
728 p = s->pkt_first;
729 }
730
731 /* don't bother if we can't decode the length */
732 if(p->len < 4) return 0;
733
734 len = unhex(p->data, 4);
735 if((len < 1) || (len > 1024)) {
736 D("SS(%d): bad size (%d)\n", s->id, len);
737 goto fail;
738 }
739
740 D("SS(%d): len is %d\n", s->id, len );
741 /* can't do anything until we have the full header */
742 if((len + 4) > p->len) {
743 D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
744 return 0;
745 }
746
747 p->data[len + 4] = 0;
748
749 D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));
750
751 #if ADB_HOST
752 service = (char *)p->data + 4;
753 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
754 char* serial_end;
755 service += strlen("host-serial:");
756
757 // serial number should follow "host:" and could be a host:port string.
758 serial_end = skip_host_serial(service);
759 if (serial_end) {
760 *serial_end = 0; // terminate string
761 serial = service;
762 service = serial_end + 1;
763 }
764 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
765 ttype = kTransportUsb;
766 service += strlen("host-usb:");
767 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
768 ttype = kTransportLocal;
769 service += strlen("host-local:");
770 } else if (!strncmp(service, "host:", strlen("host:"))) {
771 ttype = kTransportAny;
772 service += strlen("host:");
773 } else {
774 service = NULL;
775 }
776
777 if (service) {
778 asocket *s2;
779
780 /* some requests are handled immediately -- in that
781 ** case the handle_host_request() routine has sent
782 ** the OKAY or FAIL message and all we have to do
783 ** is clean up.
784 */
785 if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
786 /* XXX fail message? */
787 D( "SS(%d): handled host service '%s'\n", s->id, service );
788 goto fail;
789 }
790 if (!strncmp(service, "transport", strlen("transport"))) {
791 D( "SS(%d): okay transport\n", s->id );
792 p->len = 0;
793 return 0;
794 }
795
796 /* try to find a local service with this name.
797 ** if no such service exists, we'll fail out
798 ** and tear down here.
799 */
800 s2 = create_host_service_socket(service, serial);
801 if(s2 == 0) {
802 D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
803 sendfailmsg(s->peer->fd, "unknown host service");
804 goto fail;
805 }
806
807 /* we've connected to a local host service,
808 ** so we make our peer back into a regular
809 ** local socket and bind it to the new local
810 ** service socket, acknowledge the successful
811 ** connection, and close this smart socket now
812 ** that its work is done.
813 */
814 adb_write(s->peer->fd, "OKAY", 4);
815
816 s->peer->ready = local_socket_ready;
817 s->peer->shutdown = NULL;
818 s->peer->close = local_socket_close;
819 s->peer->peer = s2;
820 s2->peer = s->peer;
821 s->peer = 0;
822 D( "SS(%d): okay\n", s->id );
823 s->close(s);
824
825 /* initial state is "ready" */
826 s2->ready(s2);
827 return 0;
828 }
829 #else /* !ADB_HOST */
830 if (s->transport == NULL) {
831 char* error_string = "unknown failure";
832 s->transport = acquire_one_transport (CS_ANY,
833 kTransportAny, NULL, &error_string);
834
835 if (s->transport == NULL) {
836 sendfailmsg(s->peer->fd, error_string);
837 goto fail;
838 }
839 }
840 #endif
841
842 if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
843 /* if there's no remote we fail the connection
844 ** right here and terminate it
845 */
846 sendfailmsg(s->peer->fd, "device offline (x)");
847 goto fail;
848 }
849
850
851 /* instrument our peer to pass the success or fail
852 ** message back once it connects or closes, then
853 ** detach from it, request the connection, and
854 ** tear down
855 */
856 s->peer->ready = local_socket_ready_notify;
857 s->peer->shutdown = NULL;
858 s->peer->close = local_socket_close_notify;
859 s->peer->peer = 0;
860 /* give him our transport and upref it */
861 s->peer->transport = s->transport;
862
863 connect_to_remote(s->peer, (char*) (p->data + 4));
864 s->peer = 0;
865 s->close(s);
866 return 1;
867
868 fail:
869 /* we're going to close our peer as a side-effect, so
870 ** return -1 to signal that state to the local socket
871 ** who is enqueueing against us
872 */
873 s->close(s);
874 return -1;
875 }
876
smart_socket_ready(asocket * s)877 static void smart_socket_ready(asocket *s)
878 {
879 D("SS(%d): ready\n", s->id);
880 }
881
smart_socket_close(asocket * s)882 static void smart_socket_close(asocket *s)
883 {
884 D("SS(%d): closed\n", s->id);
885 if(s->pkt_first){
886 put_apacket(s->pkt_first);
887 }
888 if(s->peer) {
889 s->peer->peer = 0;
890 s->peer->close(s->peer);
891 s->peer = 0;
892 }
893 free(s);
894 }
895
create_smart_socket(void)896 static asocket *create_smart_socket(void)
897 {
898 D("Creating smart socket \n");
899 asocket *s = calloc(1, sizeof(asocket));
900 if (s == NULL) fatal("cannot allocate socket");
901 s->enqueue = smart_socket_enqueue;
902 s->ready = smart_socket_ready;
903 s->shutdown = NULL;
904 s->close = smart_socket_close;
905
906 D("SS(%d)\n", s->id);
907 return s;
908 }
909
connect_to_smartsocket(asocket * s)910 void connect_to_smartsocket(asocket *s)
911 {
912 D("Connecting to smart socket \n");
913 asocket *ss = create_smart_socket();
914 s->peer = ss;
915 ss->peer = s;
916 s->ready(s);
917 }
918