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