blob: ce3c65ef407005cb423bdae0bc96feeab745ff9a [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;
Benoit Gobyf366b362012-03-16 14:50:07 -0700202 int exit_on_close = s->exit_on_close;
203
JP Abgrall408fa572011-03-16 15:57:42 -0700204 D("LS(%d): destroying fde.fd=%d\n", s->id, s->fde.fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205
206 /* IMPORTANT: the remove closes the fd
207 ** that belongs to this socket
208 */
209 fdevent_remove(&s->fde);
210
211 /* dispose of any unwritten data */
212 for(p = s->pkt_first; p; p = n) {
213 D("LS(%d): discarding %d bytes\n", s->id, p->len);
214 n = p->next;
215 put_apacket(p);
216 }
217 remove_socket(s);
218 free(s);
Benoit Gobyf366b362012-03-16 14:50:07 -0700219
220 if (exit_on_close) {
221 D("local_socket_destroy: exiting\n");
222 exit(1);
223 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224}
225
226
227static void local_socket_close_locked(asocket *s)
228{
JP Abgrall408fa572011-03-16 15:57:42 -0700229 D("entered. LS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 if(s->peer) {
JP Abgrall408fa572011-03-16 15:57:42 -0700231 D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n",
232 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 s->peer->peer = 0;
234 // tweak to avoid deadlock
Tom Marlin49f18572011-05-13 13:24:55 -0500235 if (s->peer->close == local_socket_close) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 local_socket_close_locked(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500237 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500239 }
240 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 }
242
243 /* If we are already closing, or if there are no
244 ** pending packets, destroy immediately
245 */
246 if (s->closing || s->pkt_first == NULL) {
247 int id = s->id;
248 local_socket_destroy(s);
249 D("LS(%d): closed\n", id);
250 return;
251 }
252
253 /* otherwise, put on the closing list
254 */
255 D("LS(%d): closing\n", s->id);
256 s->closing = 1;
257 fdevent_del(&s->fde, FDE_READ);
258 remove_socket(s);
JP Abgrall408fa572011-03-16 15:57:42 -0700259 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 -0800260 insert_local_socket(s, &local_socket_closing_list);
261}
262
263static void local_socket_event_func(int fd, unsigned ev, void *_s)
264{
265 asocket *s = _s;
266
JP Abgrall408fa572011-03-16 15:57:42 -0700267 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);
268
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 /* put the FDE_WRITE processing before the FDE_READ
270 ** in order to simplify the code.
271 */
272 if(ev & FDE_WRITE){
273 apacket *p;
274
275 while((p = s->pkt_first) != 0) {
276 while(p->len > 0) {
277 int r = adb_write(fd, p->ptr, p->len);
278 if(r > 0) {
279 p->ptr += r;
280 p->len -= r;
281 continue;
282 }
283 if(r < 0) {
284 /* returning here is ok because FDE_READ will
285 ** be processed in the next iteration loop
286 */
287 if(errno == EAGAIN) return;
288 if(errno == EINTR) continue;
289 }
Christopher Tate5b811fa2011-06-10 11:38:37 -0700290 D(" closing after write because r=%d and errno is %d\n", r, errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 s->close(s);
292 return;
293 }
294
295 if(p->len == 0) {
296 s->pkt_first = p->next;
297 if(s->pkt_first == 0) s->pkt_last = 0;
298 put_apacket(p);
299 }
300 }
301
302 /* if we sent the last packet of a closing socket,
303 ** we can now destroy it.
304 */
305 if (s->closing) {
Christopher Tate5b811fa2011-06-10 11:38:37 -0700306 D(" closing because 'closing' is set after write\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 s->close(s);
308 return;
309 }
310
311 /* no more packets queued, so we can ignore
312 ** writable events again and tell our peer
313 ** to resume writing
314 */
315 fdevent_del(&s->fde, FDE_WRITE);
316 s->peer->ready(s->peer);
317 }
318
319
320 if(ev & FDE_READ){
321 apacket *p = get_apacket();
322 unsigned char *x = p->data;
323 size_t avail = MAX_PAYLOAD;
324 int r;
325 int is_eof = 0;
326
327 while(avail > 0) {
328 r = adb_read(fd, x, avail);
JP Abgrall408fa572011-03-16 15:57:42 -0700329 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 -0800330 if(r > 0) {
331 avail -= r;
332 x += r;
333 continue;
334 }
335 if(r < 0) {
336 if(errno == EAGAIN) break;
337 if(errno == EINTR) continue;
338 }
339
340 /* r = 0 or unhandled error */
341 is_eof = 1;
342 break;
343 }
JP Abgrall408fa572011-03-16 15:57:42 -0700344 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
345 s->id, s->fd, r, is_eof, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
347 put_apacket(p);
348 } else {
349 p->len = MAX_PAYLOAD - avail;
350
351 r = s->peer->enqueue(s->peer, p);
JP Abgrall408fa572011-03-16 15:57:42 -0700352 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 -0800353
354 if(r < 0) {
355 /* error return means they closed us as a side-effect
356 ** and we must return immediately.
357 **
358 ** note that if we still have buffered packets, the
359 ** socket will be placed on the closing socket list.
360 ** this handler function will be called again
361 ** to process FDE_WRITE events.
362 */
363 return;
364 }
365
366 if(r > 0) {
367 /* if the remote cannot accept further events,
368 ** we disable notification of READs. They'll
369 ** be enabled again when we get a call to ready()
370 */
371 fdevent_del(&s->fde, FDE_READ);
372 }
373 }
JP Abgrall112445b2011-04-12 22:01:58 -0700374 /* Don't allow a forced eof if data is still there */
375 if((s->fde.force_eof && !r) || is_eof) {
Christopher Tate5b811fa2011-06-10 11:38:37 -0700376 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n", is_eof, r, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 s->close(s);
378 }
379 }
380
381 if(ev & FDE_ERROR){
382 /* this should be caught be the next read or write
383 ** catching it here means we may skip the last few
384 ** bytes of readable data.
385 */
386// s->close(s);
JP Abgrall408fa572011-03-16 15:57:42 -0700387 D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
388
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 return;
390 }
391}
392
393asocket *create_local_socket(int fd)
394{
395 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300396 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 s->fd = fd;
398 s->enqueue = local_socket_enqueue;
399 s->ready = local_socket_ready;
400 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700401 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402
403 fdevent_install(&s->fde, fd, local_socket_event_func, s);
404/* fdevent_add(&s->fde, FDE_ERROR); */
405 //fprintf(stderr, "Created local socket in create_local_socket \n");
406 D("LS(%d): created (fd=%d)\n", s->id, s->fd);
407 return s;
408}
409
410asocket *create_local_service_socket(const char *name)
411{
412 asocket *s;
413 int fd;
414
415#if !ADB_HOST
416 if (!strcmp(name,"jdwp")) {
417 return create_jdwp_service_socket();
418 }
419 if (!strcmp(name,"track-jdwp")) {
420 return create_jdwp_tracker_service_socket();
421 }
422#endif
423 fd = service_to_fd(name);
424 if(fd < 0) return 0;
425
426 s = create_local_socket(fd);
JP Abgrall408fa572011-03-16 15:57:42 -0700427 D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
Benoit Gobyf366b362012-03-16 14:50:07 -0700428
429 if (!strcmp(name, "root:") || !strcmp(name, "usb:") ||
430 !strcmp(name, "tcpip:")) {
431 D("LS(%d): enabling exit_on_close\n", s->id);
432 s->exit_on_close = 1;
433 }
434
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 return s;
436}
437
438#if ADB_HOST
439static asocket *create_host_service_socket(const char *name, const char* serial)
440{
441 asocket *s;
442
443 s = host_service_to_socket(name, serial);
444
445 if (s != NULL) {
446 D("LS(%d) bound to '%s'\n", s->id, name);
447 return s;
448 }
449
450 return s;
451}
452#endif /* ADB_HOST */
453
454/* a Remote socket is used to send/receive data to/from a given transport object
455** it needs to be closed when the transport is forcibly destroyed by the user
456*/
457typedef struct aremotesocket {
458 asocket socket;
459 adisconnect disconnect;
460} aremotesocket;
461
462static int remote_socket_enqueue(asocket *s, apacket *p)
463{
JP Abgrall408fa572011-03-16 15:57:42 -0700464 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n",
465 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 p->msg.command = A_WRTE;
467 p->msg.arg0 = s->peer->id;
468 p->msg.arg1 = s->id;
469 p->msg.data_length = p->len;
470 send_packet(p, s->transport);
471 return 1;
472}
473
474static void remote_socket_ready(asocket *s)
475{
JP Abgrall408fa572011-03-16 15:57:42 -0700476 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n",
477 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 apacket *p = get_apacket();
479 p->msg.command = A_OKAY;
480 p->msg.arg0 = s->peer->id;
481 p->msg.arg1 = s->id;
482 send_packet(p, s->transport);
483}
484
485static void remote_socket_close(asocket *s)
486{
JP Abgrall408fa572011-03-16 15:57:42 -0700487 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n",
488 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489 apacket *p = get_apacket();
490 p->msg.command = A_CLSE;
491 if(s->peer) {
492 p->msg.arg0 = s->peer->id;
493 s->peer->peer = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700494 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n",
495 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496 s->peer->close(s->peer);
497 }
498 p->msg.arg1 = s->id;
499 send_packet(p, s->transport);
500 D("RS(%d): closed\n", s->id);
501 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
502 free(s);
503}
504
505static void remote_socket_disconnect(void* _s, atransport* t)
506{
507 asocket* s = _s;
508 asocket* peer = s->peer;
509
510 D("remote_socket_disconnect RS(%d)\n", s->id);
511 if (peer) {
512 peer->peer = NULL;
513 peer->close(peer);
514 }
515 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
516 free(s);
517}
518
519asocket *create_remote_socket(unsigned id, atransport *t)
520{
521 asocket *s = calloc(1, sizeof(aremotesocket));
522 adisconnect* dis = &((aremotesocket*)s)->disconnect;
523
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300524 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 s->id = id;
526 s->enqueue = remote_socket_enqueue;
527 s->ready = remote_socket_ready;
528 s->close = remote_socket_close;
529 s->transport = t;
530
531 dis->func = remote_socket_disconnect;
532 dis->opaque = s;
533 add_transport_disconnect( t, dis );
534 D("RS(%d): created\n", s->id);
535 return s;
536}
537
538void connect_to_remote(asocket *s, const char *destination)
539{
JP Abgrall408fa572011-03-16 15:57:42 -0700540 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541 apacket *p = get_apacket();
542 int len = strlen(destination) + 1;
543
544 if(len > (MAX_PAYLOAD-1)) {
545 fatal("destination oversized");
546 }
547
548 D("LS(%d): connect('%s')\n", s->id, destination);
549 p->msg.command = A_OPEN;
550 p->msg.arg0 = s->id;
551 p->msg.data_length = len;
552 strcpy((char*) p->data, destination);
553 send_packet(p, s->transport);
554}
555
556
557/* this is used by magic sockets to rig local sockets to
558 send the go-ahead message when they connect */
559static void local_socket_ready_notify(asocket *s)
560{
561 s->ready = local_socket_ready;
562 s->close = local_socket_close;
563 adb_write(s->fd, "OKAY", 4);
564 s->ready(s);
565}
566
567/* this is used by magic sockets to rig local sockets to
568 send the failure message if they are closed before
569 connected (to avoid closing them without a status message) */
570static void local_socket_close_notify(asocket *s)
571{
572 s->ready = local_socket_ready;
573 s->close = local_socket_close;
574 sendfailmsg(s->fd, "closed");
575 s->close(s);
576}
577
578unsigned unhex(unsigned char *s, int len)
579{
580 unsigned n = 0, c;
581
582 while(len-- > 0) {
583 switch((c = *s++)) {
584 case '0': case '1': case '2':
585 case '3': case '4': case '5':
586 case '6': case '7': case '8':
587 case '9':
588 c -= '0';
589 break;
590 case 'a': case 'b': case 'c':
591 case 'd': case 'e': case 'f':
592 c = c - 'a' + 10;
593 break;
594 case 'A': case 'B': case 'C':
595 case 'D': case 'E': case 'F':
596 c = c - 'A' + 10;
597 break;
598 default:
599 return 0xffffffff;
600 }
601
602 n = (n << 4) | c;
603 }
604
605 return n;
606}
607
Terence Haddock28e13902011-03-16 09:43:56 +0100608/* skip_host_serial return the position in a string
609 skipping over the 'serial' parameter in the ADB protocol,
610 where parameter string may be a host:port string containing
611 the protocol delimiter (colon). */
612char *skip_host_serial(char *service) {
613 char *first_colon, *serial_end;
614
615 first_colon = strchr(service, ':');
616 if (!first_colon) {
617 /* No colon in service string. */
618 return NULL;
619 }
620 serial_end = first_colon;
621 if (isdigit(serial_end[1])) {
622 serial_end++;
623 while ((*serial_end) && isdigit(*serial_end)) {
624 serial_end++;
625 }
626 if ((*serial_end) != ':') {
627 // Something other than numbers was found, reset the end.
628 serial_end = first_colon;
629 }
630 }
631 return serial_end;
632}
633
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634static int smart_socket_enqueue(asocket *s, apacket *p)
635{
636 unsigned len;
637#if ADB_HOST
638 char *service = NULL;
639 char* serial = NULL;
640 transport_type ttype = kTransportAny;
641#endif
642
643 D("SS(%d): enqueue %d\n", s->id, p->len);
644
645 if(s->pkt_first == 0) {
646 s->pkt_first = p;
647 s->pkt_last = p;
648 } else {
649 if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
650 D("SS(%d): overflow\n", s->id);
651 put_apacket(p);
652 goto fail;
653 }
654
655 memcpy(s->pkt_first->data + s->pkt_first->len,
656 p->data, p->len);
657 s->pkt_first->len += p->len;
658 put_apacket(p);
659
660 p = s->pkt_first;
661 }
662
663 /* don't bother if we can't decode the length */
664 if(p->len < 4) return 0;
665
666 len = unhex(p->data, 4);
667 if((len < 1) || (len > 1024)) {
668 D("SS(%d): bad size (%d)\n", s->id, len);
669 goto fail;
670 }
671
672 D("SS(%d): len is %d\n", s->id, len );
673 /* can't do anything until we have the full header */
674 if((len + 4) > p->len) {
675 D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
676 return 0;
677 }
678
679 p->data[len + 4] = 0;
680
681 D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));
682
683#if ADB_HOST
684 service = (char *)p->data + 4;
685 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
686 char* serial_end;
687 service += strlen("host-serial:");
688
Terence Haddock28e13902011-03-16 09:43:56 +0100689 // serial number should follow "host:" and could be a host:port string.
690 serial_end = skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 if (serial_end) {
692 *serial_end = 0; // terminate string
693 serial = service;
694 service = serial_end + 1;
695 }
696 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
697 ttype = kTransportUsb;
698 service += strlen("host-usb:");
699 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
700 ttype = kTransportLocal;
701 service += strlen("host-local:");
702 } else if (!strncmp(service, "host:", strlen("host:"))) {
703 ttype = kTransportAny;
704 service += strlen("host:");
705 } else {
706 service = NULL;
707 }
708
709 if (service) {
710 asocket *s2;
711
712 /* some requests are handled immediately -- in that
713 ** case the handle_host_request() routine has sent
714 ** the OKAY or FAIL message and all we have to do
715 ** is clean up.
716 */
717 if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
718 /* XXX fail message? */
719 D( "SS(%d): handled host service '%s'\n", s->id, service );
720 goto fail;
721 }
722 if (!strncmp(service, "transport", strlen("transport"))) {
723 D( "SS(%d): okay transport\n", s->id );
724 p->len = 0;
725 return 0;
726 }
727
728 /* try to find a local service with this name.
729 ** if no such service exists, we'll fail out
730 ** and tear down here.
731 */
732 s2 = create_host_service_socket(service, serial);
733 if(s2 == 0) {
734 D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
735 sendfailmsg(s->peer->fd, "unknown host service");
736 goto fail;
737 }
738
739 /* we've connected to a local host service,
740 ** so we make our peer back into a regular
741 ** local socket and bind it to the new local
742 ** service socket, acknowledge the successful
743 ** connection, and close this smart socket now
744 ** that its work is done.
745 */
746 adb_write(s->peer->fd, "OKAY", 4);
747
748 s->peer->ready = local_socket_ready;
749 s->peer->close = local_socket_close;
750 s->peer->peer = s2;
751 s2->peer = s->peer;
752 s->peer = 0;
753 D( "SS(%d): okay\n", s->id );
754 s->close(s);
755
756 /* initial state is "ready" */
757 s2->ready(s2);
758 return 0;
759 }
760#else /* !ADB_HOST */
761 if (s->transport == NULL) {
762 char* error_string = "unknown failure";
763 s->transport = acquire_one_transport (CS_ANY,
764 kTransportAny, NULL, &error_string);
765
766 if (s->transport == NULL) {
767 sendfailmsg(s->peer->fd, error_string);
768 goto fail;
769 }
770 }
771#endif
772
773 if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
774 /* if there's no remote we fail the connection
775 ** right here and terminate it
776 */
777 sendfailmsg(s->peer->fd, "device offline (x)");
778 goto fail;
779 }
780
781
782 /* instrument our peer to pass the success or fail
783 ** message back once it connects or closes, then
784 ** detach from it, request the connection, and
785 ** tear down
786 */
787 s->peer->ready = local_socket_ready_notify;
788 s->peer->close = local_socket_close_notify;
789 s->peer->peer = 0;
790 /* give him our transport and upref it */
791 s->peer->transport = s->transport;
792
793 connect_to_remote(s->peer, (char*) (p->data + 4));
794 s->peer = 0;
795 s->close(s);
796 return 1;
797
798fail:
799 /* we're going to close our peer as a side-effect, so
800 ** return -1 to signal that state to the local socket
801 ** who is enqueueing against us
802 */
803 s->close(s);
804 return -1;
805}
806
807static void smart_socket_ready(asocket *s)
808{
809 D("SS(%d): ready\n", s->id);
810}
811
812static void smart_socket_close(asocket *s)
813{
814 D("SS(%d): closed\n", s->id);
815 if(s->pkt_first){
816 put_apacket(s->pkt_first);
817 }
818 if(s->peer) {
819 s->peer->peer = 0;
820 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500821 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822 }
823 free(s);
824}
825
826asocket *create_smart_socket(void (*action_cb)(asocket *s, const char *act))
827{
828 D("Creating smart socket \n");
829 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300830 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800831 s->enqueue = smart_socket_enqueue;
832 s->ready = smart_socket_ready;
833 s->close = smart_socket_close;
834 s->extra = action_cb;
835
836 D("SS(%d): created %p\n", s->id, action_cb);
837 return s;
838}
839
840void smart_socket_action(asocket *s, const char *act)
841{
842
843}
844
845void connect_to_smartsocket(asocket *s)
846{
847 D("Connecting to smart socket \n");
848 asocket *ss = create_smart_socket(smart_socket_action);
849 s->peer = ss;
850 ss->peer = s;
851 s->ready(s);
852}