blob: 72151b4e758ef5d79a9346f313a4f89e9d4525cd [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
JP Abgralld7f1feb2012-03-30 11:19:16 -0700429 if ((!strcmp(name, "root:") && getuid() != 0)
430 || !strcmp(name, "usb:")
431 || !strcmp(name, "tcpip:")) {
Benoit Gobyf366b362012-03-16 14:50:07 -0700432 D("LS(%d): enabling exit_on_close\n", s->id);
433 s->exit_on_close = 1;
434 }
435
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 return s;
437}
438
439#if ADB_HOST
440static asocket *create_host_service_socket(const char *name, const char* serial)
441{
442 asocket *s;
443
444 s = host_service_to_socket(name, serial);
445
446 if (s != NULL) {
447 D("LS(%d) bound to '%s'\n", s->id, name);
448 return s;
449 }
450
451 return s;
452}
453#endif /* ADB_HOST */
454
455/* a Remote socket is used to send/receive data to/from a given transport object
456** it needs to be closed when the transport is forcibly destroyed by the user
457*/
458typedef struct aremotesocket {
459 asocket socket;
460 adisconnect disconnect;
461} aremotesocket;
462
463static int remote_socket_enqueue(asocket *s, apacket *p)
464{
JP Abgrall408fa572011-03-16 15:57:42 -0700465 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n",
466 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 p->msg.command = A_WRTE;
468 p->msg.arg0 = s->peer->id;
469 p->msg.arg1 = s->id;
470 p->msg.data_length = p->len;
471 send_packet(p, s->transport);
472 return 1;
473}
474
475static void remote_socket_ready(asocket *s)
476{
JP Abgrall408fa572011-03-16 15:57:42 -0700477 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n",
478 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 apacket *p = get_apacket();
480 p->msg.command = A_OKAY;
481 p->msg.arg0 = s->peer->id;
482 p->msg.arg1 = s->id;
483 send_packet(p, s->transport);
484}
485
486static void remote_socket_close(asocket *s)
487{
JP Abgrall408fa572011-03-16 15:57:42 -0700488 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n",
489 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 apacket *p = get_apacket();
491 p->msg.command = A_CLSE;
492 if(s->peer) {
493 p->msg.arg0 = s->peer->id;
494 s->peer->peer = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700495 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n",
496 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 s->peer->close(s->peer);
498 }
499 p->msg.arg1 = s->id;
500 send_packet(p, s->transport);
501 D("RS(%d): closed\n", s->id);
502 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
503 free(s);
504}
505
506static void remote_socket_disconnect(void* _s, atransport* t)
507{
508 asocket* s = _s;
509 asocket* peer = s->peer;
510
511 D("remote_socket_disconnect RS(%d)\n", s->id);
512 if (peer) {
513 peer->peer = NULL;
514 peer->close(peer);
515 }
516 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
517 free(s);
518}
519
520asocket *create_remote_socket(unsigned id, atransport *t)
521{
522 asocket *s = calloc(1, sizeof(aremotesocket));
523 adisconnect* dis = &((aremotesocket*)s)->disconnect;
524
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300525 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 s->id = id;
527 s->enqueue = remote_socket_enqueue;
528 s->ready = remote_socket_ready;
529 s->close = remote_socket_close;
530 s->transport = t;
531
532 dis->func = remote_socket_disconnect;
533 dis->opaque = s;
534 add_transport_disconnect( t, dis );
535 D("RS(%d): created\n", s->id);
536 return s;
537}
538
539void connect_to_remote(asocket *s, const char *destination)
540{
JP Abgrall408fa572011-03-16 15:57:42 -0700541 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 apacket *p = get_apacket();
543 int len = strlen(destination) + 1;
544
545 if(len > (MAX_PAYLOAD-1)) {
546 fatal("destination oversized");
547 }
548
549 D("LS(%d): connect('%s')\n", s->id, destination);
550 p->msg.command = A_OPEN;
551 p->msg.arg0 = s->id;
552 p->msg.data_length = len;
553 strcpy((char*) p->data, destination);
554 send_packet(p, s->transport);
555}
556
557
558/* this is used by magic sockets to rig local sockets to
559 send the go-ahead message when they connect */
560static void local_socket_ready_notify(asocket *s)
561{
562 s->ready = local_socket_ready;
563 s->close = local_socket_close;
564 adb_write(s->fd, "OKAY", 4);
565 s->ready(s);
566}
567
568/* this is used by magic sockets to rig local sockets to
569 send the failure message if they are closed before
570 connected (to avoid closing them without a status message) */
571static void local_socket_close_notify(asocket *s)
572{
573 s->ready = local_socket_ready;
574 s->close = local_socket_close;
575 sendfailmsg(s->fd, "closed");
576 s->close(s);
577}
578
579unsigned unhex(unsigned char *s, int len)
580{
581 unsigned n = 0, c;
582
583 while(len-- > 0) {
584 switch((c = *s++)) {
585 case '0': case '1': case '2':
586 case '3': case '4': case '5':
587 case '6': case '7': case '8':
588 case '9':
589 c -= '0';
590 break;
591 case 'a': case 'b': case 'c':
592 case 'd': case 'e': case 'f':
593 c = c - 'a' + 10;
594 break;
595 case 'A': case 'B': case 'C':
596 case 'D': case 'E': case 'F':
597 c = c - 'A' + 10;
598 break;
599 default:
600 return 0xffffffff;
601 }
602
603 n = (n << 4) | c;
604 }
605
606 return n;
607}
608
Terence Haddock28e13902011-03-16 09:43:56 +0100609/* skip_host_serial return the position in a string
610 skipping over the 'serial' parameter in the ADB protocol,
611 where parameter string may be a host:port string containing
612 the protocol delimiter (colon). */
613char *skip_host_serial(char *service) {
614 char *first_colon, *serial_end;
615
616 first_colon = strchr(service, ':');
617 if (!first_colon) {
618 /* No colon in service string. */
619 return NULL;
620 }
621 serial_end = first_colon;
622 if (isdigit(serial_end[1])) {
623 serial_end++;
624 while ((*serial_end) && isdigit(*serial_end)) {
625 serial_end++;
626 }
627 if ((*serial_end) != ':') {
628 // Something other than numbers was found, reset the end.
629 serial_end = first_colon;
630 }
631 }
632 return serial_end;
633}
634
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800635static int smart_socket_enqueue(asocket *s, apacket *p)
636{
637 unsigned len;
638#if ADB_HOST
639 char *service = NULL;
640 char* serial = NULL;
641 transport_type ttype = kTransportAny;
642#endif
643
644 D("SS(%d): enqueue %d\n", s->id, p->len);
645
646 if(s->pkt_first == 0) {
647 s->pkt_first = p;
648 s->pkt_last = p;
649 } else {
650 if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
651 D("SS(%d): overflow\n", s->id);
652 put_apacket(p);
653 goto fail;
654 }
655
656 memcpy(s->pkt_first->data + s->pkt_first->len,
657 p->data, p->len);
658 s->pkt_first->len += p->len;
659 put_apacket(p);
660
661 p = s->pkt_first;
662 }
663
664 /* don't bother if we can't decode the length */
665 if(p->len < 4) return 0;
666
667 len = unhex(p->data, 4);
668 if((len < 1) || (len > 1024)) {
669 D("SS(%d): bad size (%d)\n", s->id, len);
670 goto fail;
671 }
672
673 D("SS(%d): len is %d\n", s->id, len );
674 /* can't do anything until we have the full header */
675 if((len + 4) > p->len) {
676 D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
677 return 0;
678 }
679
680 p->data[len + 4] = 0;
681
682 D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));
683
684#if ADB_HOST
685 service = (char *)p->data + 4;
686 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
687 char* serial_end;
688 service += strlen("host-serial:");
689
Terence Haddock28e13902011-03-16 09:43:56 +0100690 // serial number should follow "host:" and could be a host:port string.
691 serial_end = skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 if (serial_end) {
693 *serial_end = 0; // terminate string
694 serial = service;
695 service = serial_end + 1;
696 }
697 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
698 ttype = kTransportUsb;
699 service += strlen("host-usb:");
700 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
701 ttype = kTransportLocal;
702 service += strlen("host-local:");
703 } else if (!strncmp(service, "host:", strlen("host:"))) {
704 ttype = kTransportAny;
705 service += strlen("host:");
706 } else {
707 service = NULL;
708 }
709
710 if (service) {
711 asocket *s2;
712
713 /* some requests are handled immediately -- in that
714 ** case the handle_host_request() routine has sent
715 ** the OKAY or FAIL message and all we have to do
716 ** is clean up.
717 */
718 if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
719 /* XXX fail message? */
720 D( "SS(%d): handled host service '%s'\n", s->id, service );
721 goto fail;
722 }
723 if (!strncmp(service, "transport", strlen("transport"))) {
724 D( "SS(%d): okay transport\n", s->id );
725 p->len = 0;
726 return 0;
727 }
728
729 /* try to find a local service with this name.
730 ** if no such service exists, we'll fail out
731 ** and tear down here.
732 */
733 s2 = create_host_service_socket(service, serial);
734 if(s2 == 0) {
735 D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
736 sendfailmsg(s->peer->fd, "unknown host service");
737 goto fail;
738 }
739
740 /* we've connected to a local host service,
741 ** so we make our peer back into a regular
742 ** local socket and bind it to the new local
743 ** service socket, acknowledge the successful
744 ** connection, and close this smart socket now
745 ** that its work is done.
746 */
747 adb_write(s->peer->fd, "OKAY", 4);
748
749 s->peer->ready = local_socket_ready;
750 s->peer->close = local_socket_close;
751 s->peer->peer = s2;
752 s2->peer = s->peer;
753 s->peer = 0;
754 D( "SS(%d): okay\n", s->id );
755 s->close(s);
756
757 /* initial state is "ready" */
758 s2->ready(s2);
759 return 0;
760 }
761#else /* !ADB_HOST */
762 if (s->transport == NULL) {
763 char* error_string = "unknown failure";
764 s->transport = acquire_one_transport (CS_ANY,
765 kTransportAny, NULL, &error_string);
766
767 if (s->transport == NULL) {
768 sendfailmsg(s->peer->fd, error_string);
769 goto fail;
770 }
771 }
772#endif
773
774 if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
775 /* if there's no remote we fail the connection
776 ** right here and terminate it
777 */
778 sendfailmsg(s->peer->fd, "device offline (x)");
779 goto fail;
780 }
781
782
783 /* instrument our peer to pass the success or fail
784 ** message back once it connects or closes, then
785 ** detach from it, request the connection, and
786 ** tear down
787 */
788 s->peer->ready = local_socket_ready_notify;
789 s->peer->close = local_socket_close_notify;
790 s->peer->peer = 0;
791 /* give him our transport and upref it */
792 s->peer->transport = s->transport;
793
794 connect_to_remote(s->peer, (char*) (p->data + 4));
795 s->peer = 0;
796 s->close(s);
797 return 1;
798
799fail:
800 /* we're going to close our peer as a side-effect, so
801 ** return -1 to signal that state to the local socket
802 ** who is enqueueing against us
803 */
804 s->close(s);
805 return -1;
806}
807
808static void smart_socket_ready(asocket *s)
809{
810 D("SS(%d): ready\n", s->id);
811}
812
813static void smart_socket_close(asocket *s)
814{
815 D("SS(%d): closed\n", s->id);
816 if(s->pkt_first){
817 put_apacket(s->pkt_first);
818 }
819 if(s->peer) {
820 s->peer->peer = 0;
821 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500822 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800823 }
824 free(s);
825}
826
827asocket *create_smart_socket(void (*action_cb)(asocket *s, const char *act))
828{
829 D("Creating smart socket \n");
830 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300831 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800832 s->enqueue = smart_socket_enqueue;
833 s->ready = smart_socket_ready;
834 s->close = smart_socket_close;
835 s->extra = action_cb;
836
837 D("SS(%d): created %p\n", s->id, action_cb);
838 return s;
839}
840
841void smart_socket_action(asocket *s, const char *act)
842{
843
844}
845
846void connect_to_smartsocket(asocket *s)
847{
848 D("Connecting to smart socket \n");
849 asocket *ss = create_smart_socket(smart_socket_action);
850 s->peer = ss;
851 ss->peer = s;
852 s->ready(s);
853}