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