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