blob: 45d935c037bf6c8594cb5e0ad03be3cad809d8a4 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -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#define TRACE_TAG TRACE_SOCKETS
27#include "adb.h"
28
29ADB_MUTEX_DEFINE( socket_list_lock );
30
31static void local_socket_close_locked(asocket *s);
32
33int 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
46static unsigned local_socket_next_id = 1;
47
48static 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*/
57static asocket local_socket_closing_list = {
58 .next = &local_socket_closing_list,
59 .prev = &local_socket_closing_list,
60};
61
62asocket *find_local_socket(unsigned id)
63{
64 asocket *s;
65 asocket *result = NULL;
66
67 adb_mutex_lock(&socket_list_lock);
André Goddard Rosa81828292010-06-12 11:40:20 -030068 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
69 if (s->id == id) {
70 result = s;
71 break;
72 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 }
74 adb_mutex_unlock(&socket_list_lock);
75
76 return result;
77}
78
79static void
80insert_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
89void 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
99void 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
112void 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);
120restart:
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
130static 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
168enqueue:
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
183static 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
191static 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
199static void local_socket_destroy(asocket *s)
200{
201 apacket *p, *n;
JP Abgrall408fa572011-03-16 15:57:42 -0700202 D("LS(%d): destroying fde.fd=%d\n", s->id, s->fde.fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
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
220static void local_socket_close_locked(asocket *s)
221{
JP Abgrall408fa572011-03-16 15:57:42 -0700222 D("entered. LS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 if(s->peer) {
JP Abgrall408fa572011-03-16 15:57:42 -0700224 D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n",
225 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 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
234 /* If we are already closing, or if there are no
235 ** pending packets, destroy immediately
236 */
237 if (s->closing || s->pkt_first == NULL) {
238 int id = s->id;
239 local_socket_destroy(s);
240 D("LS(%d): closed\n", id);
241 return;
242 }
243
244 /* otherwise, put on the closing list
245 */
246 D("LS(%d): closing\n", s->id);
247 s->closing = 1;
248 fdevent_del(&s->fde, FDE_READ);
249 remove_socket(s);
JP Abgrall408fa572011-03-16 15:57:42 -0700250 D("LS(%d): put on socket_closing_list fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 insert_local_socket(s, &local_socket_closing_list);
252}
253
254static void local_socket_event_func(int fd, unsigned ev, void *_s)
255{
256 asocket *s = _s;
257
JP Abgrall408fa572011-03-16 15:57:42 -0700258 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);
259
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260 /* put the FDE_WRITE processing before the FDE_READ
261 ** in order to simplify the code.
262 */
263 if(ev & FDE_WRITE){
264 apacket *p;
265
266 while((p = s->pkt_first) != 0) {
267 while(p->len > 0) {
268 int r = adb_write(fd, p->ptr, p->len);
269 if(r > 0) {
270 p->ptr += r;
271 p->len -= r;
272 continue;
273 }
274 if(r < 0) {
275 /* returning here is ok because FDE_READ will
276 ** be processed in the next iteration loop
277 */
278 if(errno == EAGAIN) return;
279 if(errno == EINTR) continue;
280 }
281 s->close(s);
282 return;
283 }
284
285 if(p->len == 0) {
286 s->pkt_first = p->next;
287 if(s->pkt_first == 0) s->pkt_last = 0;
288 put_apacket(p);
289 }
290 }
291
292 /* if we sent the last packet of a closing socket,
293 ** we can now destroy it.
294 */
295 if (s->closing) {
296 s->close(s);
297 return;
298 }
299
300 /* no more packets queued, so we can ignore
301 ** writable events again and tell our peer
302 ** to resume writing
303 */
304 fdevent_del(&s->fde, FDE_WRITE);
305 s->peer->ready(s->peer);
306 }
307
308
309 if(ev & FDE_READ){
310 apacket *p = get_apacket();
311 unsigned char *x = p->data;
312 size_t avail = MAX_PAYLOAD;
313 int r;
314 int is_eof = 0;
315
316 while(avail > 0) {
317 r = adb_read(fd, x, avail);
JP Abgrall408fa572011-03-16 15:57:42 -0700318 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);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 if(r > 0) {
320 avail -= r;
321 x += r;
322 continue;
323 }
324 if(r < 0) {
325 if(errno == EAGAIN) break;
326 if(errno == EINTR) continue;
327 }
328
329 /* r = 0 or unhandled error */
330 is_eof = 1;
331 break;
332 }
JP Abgrall408fa572011-03-16 15:57:42 -0700333 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
334 s->id, s->fd, r, is_eof, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
336 put_apacket(p);
337 } else {
338 p->len = MAX_PAYLOAD - avail;
339
340 r = s->peer->enqueue(s->peer, p);
JP Abgrall408fa572011-03-16 15:57:42 -0700341 D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, r);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342
343 if(r < 0) {
344 /* error return means they closed us as a side-effect
345 ** and we must return immediately.
346 **
347 ** note that if we still have buffered packets, the
348 ** socket will be placed on the closing socket list.
349 ** this handler function will be called again
350 ** to process FDE_WRITE events.
351 */
352 return;
353 }
354
355 if(r > 0) {
356 /* if the remote cannot accept further events,
357 ** we disable notification of READs. They'll
358 ** be enabled again when we get a call to ready()
359 */
360 fdevent_del(&s->fde, FDE_READ);
361 }
362 }
JP Abgrall112445b2011-04-12 22:01:58 -0700363 /* Don't allow a forced eof if data is still there */
364 if((s->fde.force_eof && !r) || is_eof) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 s->close(s);
366 }
367 }
368
369 if(ev & FDE_ERROR){
370 /* this should be caught be the next read or write
371 ** catching it here means we may skip the last few
372 ** bytes of readable data.
373 */
374// s->close(s);
JP Abgrall408fa572011-03-16 15:57:42 -0700375 D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
376
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 return;
378 }
379}
380
381asocket *create_local_socket(int fd)
382{
383 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300384 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 s->fd = fd;
386 s->enqueue = local_socket_enqueue;
387 s->ready = local_socket_ready;
388 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700389 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390
391 fdevent_install(&s->fde, fd, local_socket_event_func, s);
392/* fdevent_add(&s->fde, FDE_ERROR); */
393 //fprintf(stderr, "Created local socket in create_local_socket \n");
394 D("LS(%d): created (fd=%d)\n", s->id, s->fd);
395 return s;
396}
397
398asocket *create_local_service_socket(const char *name)
399{
400 asocket *s;
401 int fd;
402
403#if !ADB_HOST
404 if (!strcmp(name,"jdwp")) {
405 return create_jdwp_service_socket();
406 }
407 if (!strcmp(name,"track-jdwp")) {
408 return create_jdwp_tracker_service_socket();
409 }
410#endif
411 fd = service_to_fd(name);
412 if(fd < 0) return 0;
413
414 s = create_local_socket(fd);
JP Abgrall408fa572011-03-16 15:57:42 -0700415 D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 return s;
417}
418
419#if ADB_HOST
420static asocket *create_host_service_socket(const char *name, const char* serial)
421{
422 asocket *s;
423
424 s = host_service_to_socket(name, serial);
425
426 if (s != NULL) {
427 D("LS(%d) bound to '%s'\n", s->id, name);
428 return s;
429 }
430
431 return s;
432}
433#endif /* ADB_HOST */
434
435/* a Remote socket is used to send/receive data to/from a given transport object
436** it needs to be closed when the transport is forcibly destroyed by the user
437*/
438typedef struct aremotesocket {
439 asocket socket;
440 adisconnect disconnect;
441} aremotesocket;
442
443static int remote_socket_enqueue(asocket *s, apacket *p)
444{
JP Abgrall408fa572011-03-16 15:57:42 -0700445 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n",
446 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447 p->msg.command = A_WRTE;
448 p->msg.arg0 = s->peer->id;
449 p->msg.arg1 = s->id;
450 p->msg.data_length = p->len;
451 send_packet(p, s->transport);
452 return 1;
453}
454
455static void remote_socket_ready(asocket *s)
456{
JP Abgrall408fa572011-03-16 15:57:42 -0700457 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n",
458 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 apacket *p = get_apacket();
460 p->msg.command = A_OKAY;
461 p->msg.arg0 = s->peer->id;
462 p->msg.arg1 = s->id;
463 send_packet(p, s->transport);
464}
465
466static void remote_socket_close(asocket *s)
467{
JP Abgrall408fa572011-03-16 15:57:42 -0700468 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n",
469 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 apacket *p = get_apacket();
471 p->msg.command = A_CLSE;
472 if(s->peer) {
473 p->msg.arg0 = s->peer->id;
474 s->peer->peer = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700475 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n",
476 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 s->peer->close(s->peer);
478 }
479 p->msg.arg1 = s->id;
480 send_packet(p, s->transport);
481 D("RS(%d): closed\n", s->id);
482 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
483 free(s);
484}
485
486static void remote_socket_disconnect(void* _s, atransport* t)
487{
488 asocket* s = _s;
489 asocket* peer = s->peer;
490
491 D("remote_socket_disconnect RS(%d)\n", s->id);
492 if (peer) {
493 peer->peer = NULL;
494 peer->close(peer);
495 }
496 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
497 free(s);
498}
499
500asocket *create_remote_socket(unsigned id, atransport *t)
501{
502 asocket *s = calloc(1, sizeof(aremotesocket));
503 adisconnect* dis = &((aremotesocket*)s)->disconnect;
504
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300505 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506 s->id = id;
507 s->enqueue = remote_socket_enqueue;
508 s->ready = remote_socket_ready;
509 s->close = remote_socket_close;
510 s->transport = t;
511
512 dis->func = remote_socket_disconnect;
513 dis->opaque = s;
514 add_transport_disconnect( t, dis );
515 D("RS(%d): created\n", s->id);
516 return s;
517}
518
519void connect_to_remote(asocket *s, const char *destination)
520{
JP Abgrall408fa572011-03-16 15:57:42 -0700521 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 apacket *p = get_apacket();
523 int len = strlen(destination) + 1;
524
525 if(len > (MAX_PAYLOAD-1)) {
526 fatal("destination oversized");
527 }
528
529 D("LS(%d): connect('%s')\n", s->id, destination);
530 p->msg.command = A_OPEN;
531 p->msg.arg0 = s->id;
532 p->msg.data_length = len;
533 strcpy((char*) p->data, destination);
534 send_packet(p, s->transport);
535}
536
537
538/* this is used by magic sockets to rig local sockets to
539 send the go-ahead message when they connect */
540static void local_socket_ready_notify(asocket *s)
541{
542 s->ready = local_socket_ready;
543 s->close = local_socket_close;
544 adb_write(s->fd, "OKAY", 4);
545 s->ready(s);
546}
547
548/* this is used by magic sockets to rig local sockets to
549 send the failure message if they are closed before
550 connected (to avoid closing them without a status message) */
551static void local_socket_close_notify(asocket *s)
552{
553 s->ready = local_socket_ready;
554 s->close = local_socket_close;
555 sendfailmsg(s->fd, "closed");
556 s->close(s);
557}
558
559unsigned unhex(unsigned char *s, int len)
560{
561 unsigned n = 0, c;
562
563 while(len-- > 0) {
564 switch((c = *s++)) {
565 case '0': case '1': case '2':
566 case '3': case '4': case '5':
567 case '6': case '7': case '8':
568 case '9':
569 c -= '0';
570 break;
571 case 'a': case 'b': case 'c':
572 case 'd': case 'e': case 'f':
573 c = c - 'a' + 10;
574 break;
575 case 'A': case 'B': case 'C':
576 case 'D': case 'E': case 'F':
577 c = c - 'A' + 10;
578 break;
579 default:
580 return 0xffffffff;
581 }
582
583 n = (n << 4) | c;
584 }
585
586 return n;
587}
588
Terence Haddock28e13902011-03-16 09:43:56 +0100589/* skip_host_serial return the position in a string
590 skipping over the 'serial' parameter in the ADB protocol,
591 where parameter string may be a host:port string containing
592 the protocol delimiter (colon). */
593char *skip_host_serial(char *service) {
594 char *first_colon, *serial_end;
595
596 first_colon = strchr(service, ':');
597 if (!first_colon) {
598 /* No colon in service string. */
599 return NULL;
600 }
601 serial_end = first_colon;
602 if (isdigit(serial_end[1])) {
603 serial_end++;
604 while ((*serial_end) && isdigit(*serial_end)) {
605 serial_end++;
606 }
607 if ((*serial_end) != ':') {
608 // Something other than numbers was found, reset the end.
609 serial_end = first_colon;
610 }
611 }
612 return serial_end;
613}
614
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615static int smart_socket_enqueue(asocket *s, apacket *p)
616{
617 unsigned len;
618#if ADB_HOST
619 char *service = NULL;
620 char* serial = NULL;
621 transport_type ttype = kTransportAny;
622#endif
623
624 D("SS(%d): enqueue %d\n", s->id, p->len);
625
626 if(s->pkt_first == 0) {
627 s->pkt_first = p;
628 s->pkt_last = p;
629 } else {
630 if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
631 D("SS(%d): overflow\n", s->id);
632 put_apacket(p);
633 goto fail;
634 }
635
636 memcpy(s->pkt_first->data + s->pkt_first->len,
637 p->data, p->len);
638 s->pkt_first->len += p->len;
639 put_apacket(p);
640
641 p = s->pkt_first;
642 }
643
644 /* don't bother if we can't decode the length */
645 if(p->len < 4) return 0;
646
647 len = unhex(p->data, 4);
648 if((len < 1) || (len > 1024)) {
649 D("SS(%d): bad size (%d)\n", s->id, len);
650 goto fail;
651 }
652
653 D("SS(%d): len is %d\n", s->id, len );
654 /* can't do anything until we have the full header */
655 if((len + 4) > p->len) {
656 D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
657 return 0;
658 }
659
660 p->data[len + 4] = 0;
661
662 D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));
663
664#if ADB_HOST
665 service = (char *)p->data + 4;
666 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
667 char* serial_end;
668 service += strlen("host-serial:");
669
Terence Haddock28e13902011-03-16 09:43:56 +0100670 // serial number should follow "host:" and could be a host:port string.
671 serial_end = skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672 if (serial_end) {
673 *serial_end = 0; // terminate string
674 serial = service;
675 service = serial_end + 1;
676 }
677 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
678 ttype = kTransportUsb;
679 service += strlen("host-usb:");
680 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
681 ttype = kTransportLocal;
682 service += strlen("host-local:");
683 } else if (!strncmp(service, "host:", strlen("host:"))) {
684 ttype = kTransportAny;
685 service += strlen("host:");
686 } else {
687 service = NULL;
688 }
689
690 if (service) {
691 asocket *s2;
692
693 /* some requests are handled immediately -- in that
694 ** case the handle_host_request() routine has sent
695 ** the OKAY or FAIL message and all we have to do
696 ** is clean up.
697 */
698 if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
699 /* XXX fail message? */
700 D( "SS(%d): handled host service '%s'\n", s->id, service );
701 goto fail;
702 }
703 if (!strncmp(service, "transport", strlen("transport"))) {
704 D( "SS(%d): okay transport\n", s->id );
705 p->len = 0;
706 return 0;
707 }
708
709 /* try to find a local service with this name.
710 ** if no such service exists, we'll fail out
711 ** and tear down here.
712 */
713 s2 = create_host_service_socket(service, serial);
714 if(s2 == 0) {
715 D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
716 sendfailmsg(s->peer->fd, "unknown host service");
717 goto fail;
718 }
719
720 /* we've connected to a local host service,
721 ** so we make our peer back into a regular
722 ** local socket and bind it to the new local
723 ** service socket, acknowledge the successful
724 ** connection, and close this smart socket now
725 ** that its work is done.
726 */
727 adb_write(s->peer->fd, "OKAY", 4);
728
729 s->peer->ready = local_socket_ready;
730 s->peer->close = local_socket_close;
731 s->peer->peer = s2;
732 s2->peer = s->peer;
733 s->peer = 0;
734 D( "SS(%d): okay\n", s->id );
735 s->close(s);
736
737 /* initial state is "ready" */
738 s2->ready(s2);
739 return 0;
740 }
741#else /* !ADB_HOST */
742 if (s->transport == NULL) {
743 char* error_string = "unknown failure";
744 s->transport = acquire_one_transport (CS_ANY,
745 kTransportAny, NULL, &error_string);
746
747 if (s->transport == NULL) {
748 sendfailmsg(s->peer->fd, error_string);
749 goto fail;
750 }
751 }
752#endif
753
754 if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
755 /* if there's no remote we fail the connection
756 ** right here and terminate it
757 */
758 sendfailmsg(s->peer->fd, "device offline (x)");
759 goto fail;
760 }
761
762
763 /* instrument our peer to pass the success or fail
764 ** message back once it connects or closes, then
765 ** detach from it, request the connection, and
766 ** tear down
767 */
768 s->peer->ready = local_socket_ready_notify;
769 s->peer->close = local_socket_close_notify;
770 s->peer->peer = 0;
771 /* give him our transport and upref it */
772 s->peer->transport = s->transport;
773
774 connect_to_remote(s->peer, (char*) (p->data + 4));
775 s->peer = 0;
776 s->close(s);
777 return 1;
778
779fail:
780 /* we're going to close our peer as a side-effect, so
781 ** return -1 to signal that state to the local socket
782 ** who is enqueueing against us
783 */
784 s->close(s);
785 return -1;
786}
787
788static void smart_socket_ready(asocket *s)
789{
790 D("SS(%d): ready\n", s->id);
791}
792
793static void smart_socket_close(asocket *s)
794{
795 D("SS(%d): closed\n", s->id);
796 if(s->pkt_first){
797 put_apacket(s->pkt_first);
798 }
799 if(s->peer) {
800 s->peer->peer = 0;
801 s->peer->close(s->peer);
802 }
803 free(s);
804}
805
806asocket *create_smart_socket(void (*action_cb)(asocket *s, const char *act))
807{
808 D("Creating smart socket \n");
809 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300810 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800811 s->enqueue = smart_socket_enqueue;
812 s->ready = smart_socket_ready;
813 s->close = smart_socket_close;
814 s->extra = action_cb;
815
816 D("SS(%d): created %p\n", s->id, action_cb);
817 return s;
818}
819
820void smart_socket_action(asocket *s, const char *act)
821{
822
823}
824
825void connect_to_smartsocket(asocket *s)
826{
827 D("Connecting to smart socket \n");
828 asocket *ss = create_smart_socket(smart_socket_action);
829 s->peer = ss;
830 ss->peer = s;
831 s->ready(s);
832}