blob: 3d62f1e541cd754d6bc184b7e6997f77329f653a [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
Tom Marlin49f18572011-05-13 13:24:55 -0500228 if (s->peer->close == local_socket_close) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 local_socket_close_locked(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500230 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500232 }
233 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 }
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);
JP Abgrall408fa572011-03-16 15:57:42 -0700252 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 -0800253 insert_local_socket(s, &local_socket_closing_list);
254}
255
256static void local_socket_event_func(int fd, unsigned ev, void *_s)
257{
258 asocket *s = _s;
259
JP Abgrall408fa572011-03-16 15:57:42 -0700260 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);
261
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 /* 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 s->close(s);
284 return;
285 }
286
287 if(p->len == 0) {
288 s->pkt_first = p->next;
289 if(s->pkt_first == 0) s->pkt_last = 0;
290 put_apacket(p);
291 }
292 }
293
294 /* if we sent the last packet of a closing socket,
295 ** we can now destroy it.
296 */
297 if (s->closing) {
298 s->close(s);
299 return;
300 }
301
302 /* no more packets queued, so we can ignore
303 ** writable events again and tell our peer
304 ** to resume writing
305 */
306 fdevent_del(&s->fde, FDE_WRITE);
307 s->peer->ready(s->peer);
308 }
309
310
311 if(ev & FDE_READ){
312 apacket *p = get_apacket();
313 unsigned char *x = p->data;
314 size_t avail = MAX_PAYLOAD;
315 int r;
316 int is_eof = 0;
317
318 while(avail > 0) {
319 r = adb_read(fd, x, avail);
JP Abgrall408fa572011-03-16 15:57:42 -0700320 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 -0800321 if(r > 0) {
322 avail -= r;
323 x += r;
324 continue;
325 }
326 if(r < 0) {
327 if(errno == EAGAIN) break;
328 if(errno == EINTR) continue;
329 }
330
331 /* r = 0 or unhandled error */
332 is_eof = 1;
333 break;
334 }
JP Abgrall408fa572011-03-16 15:57:42 -0700335 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
336 s->id, s->fd, r, is_eof, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
338 put_apacket(p);
339 } else {
340 p->len = MAX_PAYLOAD - avail;
341
342 r = s->peer->enqueue(s->peer, p);
JP Abgrall408fa572011-03-16 15:57:42 -0700343 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 -0800344
345 if(r < 0) {
346 /* error return means they closed us as a side-effect
347 ** and we must return immediately.
348 **
349 ** note that if we still have buffered packets, the
350 ** socket will be placed on the closing socket list.
351 ** this handler function will be called again
352 ** to process FDE_WRITE events.
353 */
354 return;
355 }
356
357 if(r > 0) {
358 /* if the remote cannot accept further events,
359 ** we disable notification of READs. They'll
360 ** be enabled again when we get a call to ready()
361 */
362 fdevent_del(&s->fde, FDE_READ);
363 }
364 }
JP Abgrall112445b2011-04-12 22:01:58 -0700365 /* Don't allow a forced eof if data is still there */
366 if((s->fde.force_eof && !r) || is_eof) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 s->close(s);
368 }
369 }
370
371 if(ev & FDE_ERROR){
372 /* this should be caught be the next read or write
373 ** catching it here means we may skip the last few
374 ** bytes of readable data.
375 */
376// s->close(s);
JP Abgrall408fa572011-03-16 15:57:42 -0700377 D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
378
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 return;
380 }
381}
382
383asocket *create_local_socket(int fd)
384{
385 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300386 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 s->fd = fd;
388 s->enqueue = local_socket_enqueue;
389 s->ready = local_socket_ready;
390 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700391 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392
393 fdevent_install(&s->fde, fd, local_socket_event_func, s);
394/* fdevent_add(&s->fde, FDE_ERROR); */
395 //fprintf(stderr, "Created local socket in create_local_socket \n");
396 D("LS(%d): created (fd=%d)\n", s->id, s->fd);
397 return s;
398}
399
400asocket *create_local_service_socket(const char *name)
401{
402 asocket *s;
403 int fd;
404
405#if !ADB_HOST
406 if (!strcmp(name,"jdwp")) {
407 return create_jdwp_service_socket();
408 }
409 if (!strcmp(name,"track-jdwp")) {
410 return create_jdwp_tracker_service_socket();
411 }
412#endif
413 fd = service_to_fd(name);
414 if(fd < 0) return 0;
415
416 s = create_local_socket(fd);
JP Abgrall408fa572011-03-16 15:57:42 -0700417 D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 return s;
419}
420
421#if ADB_HOST
422static asocket *create_host_service_socket(const char *name, const char* serial)
423{
424 asocket *s;
425
426 s = host_service_to_socket(name, serial);
427
428 if (s != NULL) {
429 D("LS(%d) bound to '%s'\n", s->id, name);
430 return s;
431 }
432
433 return s;
434}
435#endif /* ADB_HOST */
436
437/* a Remote socket is used to send/receive data to/from a given transport object
438** it needs to be closed when the transport is forcibly destroyed by the user
439*/
440typedef struct aremotesocket {
441 asocket socket;
442 adisconnect disconnect;
443} aremotesocket;
444
445static int remote_socket_enqueue(asocket *s, apacket *p)
446{
JP Abgrall408fa572011-03-16 15:57:42 -0700447 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n",
448 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 p->msg.command = A_WRTE;
450 p->msg.arg0 = s->peer->id;
451 p->msg.arg1 = s->id;
452 p->msg.data_length = p->len;
453 send_packet(p, s->transport);
454 return 1;
455}
456
457static void remote_socket_ready(asocket *s)
458{
JP Abgrall408fa572011-03-16 15:57:42 -0700459 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n",
460 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 apacket *p = get_apacket();
462 p->msg.command = A_OKAY;
463 p->msg.arg0 = s->peer->id;
464 p->msg.arg1 = s->id;
465 send_packet(p, s->transport);
466}
467
468static void remote_socket_close(asocket *s)
469{
JP Abgrall408fa572011-03-16 15:57:42 -0700470 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n",
471 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 apacket *p = get_apacket();
473 p->msg.command = A_CLSE;
474 if(s->peer) {
475 p->msg.arg0 = s->peer->id;
476 s->peer->peer = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700477 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n",
478 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 s->peer->close(s->peer);
480 }
481 p->msg.arg1 = s->id;
482 send_packet(p, s->transport);
483 D("RS(%d): closed\n", s->id);
484 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
485 free(s);
486}
487
488static void remote_socket_disconnect(void* _s, atransport* t)
489{
490 asocket* s = _s;
491 asocket* peer = s->peer;
492
493 D("remote_socket_disconnect RS(%d)\n", s->id);
494 if (peer) {
495 peer->peer = NULL;
496 peer->close(peer);
497 }
498 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
499 free(s);
500}
501
502asocket *create_remote_socket(unsigned id, atransport *t)
503{
504 asocket *s = calloc(1, sizeof(aremotesocket));
505 adisconnect* dis = &((aremotesocket*)s)->disconnect;
506
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300507 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 s->id = id;
509 s->enqueue = remote_socket_enqueue;
510 s->ready = remote_socket_ready;
511 s->close = remote_socket_close;
512 s->transport = t;
513
514 dis->func = remote_socket_disconnect;
515 dis->opaque = s;
516 add_transport_disconnect( t, dis );
517 D("RS(%d): created\n", s->id);
518 return s;
519}
520
521void connect_to_remote(asocket *s, const char *destination)
522{
JP Abgrall408fa572011-03-16 15:57:42 -0700523 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 apacket *p = get_apacket();
525 int len = strlen(destination) + 1;
526
527 if(len > (MAX_PAYLOAD-1)) {
528 fatal("destination oversized");
529 }
530
531 D("LS(%d): connect('%s')\n", s->id, destination);
532 p->msg.command = A_OPEN;
533 p->msg.arg0 = s->id;
534 p->msg.data_length = len;
535 strcpy((char*) p->data, destination);
536 send_packet(p, s->transport);
537}
538
539
540/* this is used by magic sockets to rig local sockets to
541 send the go-ahead message when they connect */
542static void local_socket_ready_notify(asocket *s)
543{
544 s->ready = local_socket_ready;
545 s->close = local_socket_close;
546 adb_write(s->fd, "OKAY", 4);
547 s->ready(s);
548}
549
550/* this is used by magic sockets to rig local sockets to
551 send the failure message if they are closed before
552 connected (to avoid closing them without a status message) */
553static void local_socket_close_notify(asocket *s)
554{
555 s->ready = local_socket_ready;
556 s->close = local_socket_close;
557 sendfailmsg(s->fd, "closed");
558 s->close(s);
559}
560
561unsigned unhex(unsigned char *s, int len)
562{
563 unsigned n = 0, c;
564
565 while(len-- > 0) {
566 switch((c = *s++)) {
567 case '0': case '1': case '2':
568 case '3': case '4': case '5':
569 case '6': case '7': case '8':
570 case '9':
571 c -= '0';
572 break;
573 case 'a': case 'b': case 'c':
574 case 'd': case 'e': case 'f':
575 c = c - 'a' + 10;
576 break;
577 case 'A': case 'B': case 'C':
578 case 'D': case 'E': case 'F':
579 c = c - 'A' + 10;
580 break;
581 default:
582 return 0xffffffff;
583 }
584
585 n = (n << 4) | c;
586 }
587
588 return n;
589}
590
Terence Haddock28e13902011-03-16 09:43:56 +0100591/* skip_host_serial return the position in a string
592 skipping over the 'serial' parameter in the ADB protocol,
593 where parameter string may be a host:port string containing
594 the protocol delimiter (colon). */
595char *skip_host_serial(char *service) {
596 char *first_colon, *serial_end;
597
598 first_colon = strchr(service, ':');
599 if (!first_colon) {
600 /* No colon in service string. */
601 return NULL;
602 }
603 serial_end = first_colon;
604 if (isdigit(serial_end[1])) {
605 serial_end++;
606 while ((*serial_end) && isdigit(*serial_end)) {
607 serial_end++;
608 }
609 if ((*serial_end) != ':') {
610 // Something other than numbers was found, reset the end.
611 serial_end = first_colon;
612 }
613 }
614 return serial_end;
615}
616
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617static int smart_socket_enqueue(asocket *s, apacket *p)
618{
619 unsigned len;
620#if ADB_HOST
621 char *service = NULL;
622 char* serial = NULL;
623 transport_type ttype = kTransportAny;
624#endif
625
626 D("SS(%d): enqueue %d\n", s->id, p->len);
627
628 if(s->pkt_first == 0) {
629 s->pkt_first = p;
630 s->pkt_last = p;
631 } else {
632 if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
633 D("SS(%d): overflow\n", s->id);
634 put_apacket(p);
635 goto fail;
636 }
637
638 memcpy(s->pkt_first->data + s->pkt_first->len,
639 p->data, p->len);
640 s->pkt_first->len += p->len;
641 put_apacket(p);
642
643 p = s->pkt_first;
644 }
645
646 /* don't bother if we can't decode the length */
647 if(p->len < 4) return 0;
648
649 len = unhex(p->data, 4);
650 if((len < 1) || (len > 1024)) {
651 D("SS(%d): bad size (%d)\n", s->id, len);
652 goto fail;
653 }
654
655 D("SS(%d): len is %d\n", s->id, len );
656 /* can't do anything until we have the full header */
657 if((len + 4) > p->len) {
658 D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
659 return 0;
660 }
661
662 p->data[len + 4] = 0;
663
664 D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));
665
666#if ADB_HOST
667 service = (char *)p->data + 4;
668 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
669 char* serial_end;
670 service += strlen("host-serial:");
671
Terence Haddock28e13902011-03-16 09:43:56 +0100672 // serial number should follow "host:" and could be a host:port string.
673 serial_end = skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800674 if (serial_end) {
675 *serial_end = 0; // terminate string
676 serial = service;
677 service = serial_end + 1;
678 }
679 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
680 ttype = kTransportUsb;
681 service += strlen("host-usb:");
682 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
683 ttype = kTransportLocal;
684 service += strlen("host-local:");
685 } else if (!strncmp(service, "host:", strlen("host:"))) {
686 ttype = kTransportAny;
687 service += strlen("host:");
688 } else {
689 service = NULL;
690 }
691
692 if (service) {
693 asocket *s2;
694
695 /* some requests are handled immediately -- in that
696 ** case the handle_host_request() routine has sent
697 ** the OKAY or FAIL message and all we have to do
698 ** is clean up.
699 */
700 if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
701 /* XXX fail message? */
702 D( "SS(%d): handled host service '%s'\n", s->id, service );
703 goto fail;
704 }
705 if (!strncmp(service, "transport", strlen("transport"))) {
706 D( "SS(%d): okay transport\n", s->id );
707 p->len = 0;
708 return 0;
709 }
710
711 /* try to find a local service with this name.
712 ** if no such service exists, we'll fail out
713 ** and tear down here.
714 */
715 s2 = create_host_service_socket(service, serial);
716 if(s2 == 0) {
717 D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
718 sendfailmsg(s->peer->fd, "unknown host service");
719 goto fail;
720 }
721
722 /* we've connected to a local host service,
723 ** so we make our peer back into a regular
724 ** local socket and bind it to the new local
725 ** service socket, acknowledge the successful
726 ** connection, and close this smart socket now
727 ** that its work is done.
728 */
729 adb_write(s->peer->fd, "OKAY", 4);
730
731 s->peer->ready = local_socket_ready;
732 s->peer->close = local_socket_close;
733 s->peer->peer = s2;
734 s2->peer = s->peer;
735 s->peer = 0;
736 D( "SS(%d): okay\n", s->id );
737 s->close(s);
738
739 /* initial state is "ready" */
740 s2->ready(s2);
741 return 0;
742 }
743#else /* !ADB_HOST */
744 if (s->transport == NULL) {
745 char* error_string = "unknown failure";
746 s->transport = acquire_one_transport (CS_ANY,
747 kTransportAny, NULL, &error_string);
748
749 if (s->transport == NULL) {
750 sendfailmsg(s->peer->fd, error_string);
751 goto fail;
752 }
753 }
754#endif
755
756 if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
757 /* if there's no remote we fail the connection
758 ** right here and terminate it
759 */
760 sendfailmsg(s->peer->fd, "device offline (x)");
761 goto fail;
762 }
763
764
765 /* instrument our peer to pass the success or fail
766 ** message back once it connects or closes, then
767 ** detach from it, request the connection, and
768 ** tear down
769 */
770 s->peer->ready = local_socket_ready_notify;
771 s->peer->close = local_socket_close_notify;
772 s->peer->peer = 0;
773 /* give him our transport and upref it */
774 s->peer->transport = s->transport;
775
776 connect_to_remote(s->peer, (char*) (p->data + 4));
777 s->peer = 0;
778 s->close(s);
779 return 1;
780
781fail:
782 /* we're going to close our peer as a side-effect, so
783 ** return -1 to signal that state to the local socket
784 ** who is enqueueing against us
785 */
786 s->close(s);
787 return -1;
788}
789
790static void smart_socket_ready(asocket *s)
791{
792 D("SS(%d): ready\n", s->id);
793}
794
795static void smart_socket_close(asocket *s)
796{
797 D("SS(%d): closed\n", s->id);
798 if(s->pkt_first){
799 put_apacket(s->pkt_first);
800 }
801 if(s->peer) {
802 s->peer->peer = 0;
803 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500804 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805 }
806 free(s);
807}
808
809asocket *create_smart_socket(void (*action_cb)(asocket *s, const char *act))
810{
811 D("Creating smart socket \n");
812 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300813 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800814 s->enqueue = smart_socket_enqueue;
815 s->ready = smart_socket_ready;
816 s->close = smart_socket_close;
817 s->extra = action_cb;
818
819 D("SS(%d): created %p\n", s->id, action_cb);
820 return s;
821}
822
823void smart_socket_action(asocket *s, const char *act)
824{
825
826}
827
828void connect_to_smartsocket(asocket *s)
829{
830 D("Connecting to smart socket \n");
831 asocket *ss = create_smart_socket(smart_socket_action);
832 s->peer = ss;
833 ss->peer = s;
834 s->ready(s);
835}