blob: 3c2773c7ffa8f601600bd8fa63d7a5086294a3d8 [file] [log] [blame]
rjw2e8229f2022-02-15 21:08:12 +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
26#if !ADB_HOST
27#include <cutils/properties.h>
28#endif
29
30#define TRACE_TAG TRACE_SOCKETS
31#include "adb.h"
32
33ADB_MUTEX_DEFINE( socket_list_lock );
34
35static void local_socket_close_locked(asocket *s);
36
37int 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
50static unsigned local_socket_next_id = 1;
51
52static 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*/
61static 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.
69asocket *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
88static void
89insert_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
98void 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
113void 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
126void 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);
134restart:
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
144static 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
182enqueue:
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
197static 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
205static 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
213static 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
241static 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
283static 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
413asocket *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
431asocket *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 || !strncmp(name, "reconnect:", 10)) {
462 D("LS(%d): enabling exit_on_close\n", s->id);
463 s->exit_on_close = 1;
464 }
465#endif
466
467 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{
496 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n",
497 s->id, s->fd, s->peer->fd);
498 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{
508 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n",
509 s->id, s->fd, s->peer->fd);
510 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
517static void remote_socket_shutdown(asocket *s)
518{
519 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d\n",
520 s->id, s->fd, s->peer?s->peer->fd:-1);
521 apacket *p = get_apacket();
522 p->msg.command = A_CLSE;
523 if(s->peer) {
524 p->msg.arg0 = s->peer->id;
525 }
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) {
533 s->peer->peer = 0;
534 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n",
535 s->id, s->peer->id, s->peer->fd);
536 s->peer->close(s->peer);
537 }
538 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);
540 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
559/* 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. */
563asocket *create_remote_socket(unsigned id, atransport *t)
564{
565 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;
571
572 if (s == NULL) fatal("cannot allocate socket");
573 s->id = id;
574 s->enqueue = remote_socket_enqueue;
575 s->ready = remote_socket_ready;
576 s->shutdown = remote_socket_shutdown;
577 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{
589 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
590 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;
611 s->shutdown = NULL;
612 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;
623 s->shutdown = NULL;
624 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
659#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
671/* 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;
677 int i;
678
679 for (i = 0; i < num_prefixes; i++) {
680 if (!strncmp(service, prefixes[i].str, prefixes[i].len))
681 return strchr(service + prefixes[i].len, ':');
682 }
683
684 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
703static 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
758 // serial number should follow "host:" and could be a host:port string.
759 serial_end = skip_host_serial(service);
760 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;
818 s->peer->shutdown = NULL;
819 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;
858 s->peer->shutdown = NULL;
859 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);
892 s->peer = 0;
893 }
894 free(s);
895}
896
897static asocket *create_smart_socket(void)
898{
899 D("Creating smart socket \n");
900 asocket *s = calloc(1, sizeof(asocket));
901 if (s == NULL) fatal("cannot allocate socket");
902 s->enqueue = smart_socket_enqueue;
903 s->ready = smart_socket_ready;
904 s->shutdown = NULL;
905 s->close = smart_socket_close;
906
907 D("SS(%d)\n", s->id);
908 return s;
909}
910
911void connect_to_smartsocket(asocket *s)
912{
913 D("Connecting to smart socket \n");
914 asocket *ss = create_smart_socket();
915 s->peer = ss;
916 ss->peer = s;
917 s->ready(s);
918}