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