blob: faa9564ca5ed7f75f820b2ca20ef6f79dac887fe [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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
jzhuan5ebbb9af2013-05-24 17:40:15 -040026#if !ADB_HOST
27#include <cutils/properties.h>
28#endif
29
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080030#define TRACE_TAG TRACE_SOCKETS
31#include "adb.h"
32
33ADB_MUTEX_DEFINE( socket_list_lock );
34
35static void local_socket_close_locked(asocket *s);
36
37int sendfailmsg(int fd, const char *reason)
38{
39 char buf[9];
40 int len;
41 len = strlen(reason);
42 if(len > 0xffff) len = 0xffff;
43 snprintf(buf, sizeof buf, "FAIL%04x", len);
44 if(writex(fd, buf, 8)) return -1;
45 return writex(fd, reason, len);
46}
47
48//extern int online;
49
50static unsigned local_socket_next_id = 1;
51
52static asocket local_socket_list = {
53 .next = &local_socket_list,
54 .prev = &local_socket_list,
55};
56
57/* the the list of currently closing local sockets.
58** these have no peer anymore, but still packets to
59** write to their fd.
60*/
61static asocket local_socket_closing_list = {
62 .next = &local_socket_closing_list,
63 .prev = &local_socket_closing_list,
64};
65
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010066// Parse the global list of sockets to find one with id |local_id|.
67// If |peer_id| is not 0, also check that it is connected to a peer
68// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
69asocket *find_local_socket(unsigned local_id, unsigned peer_id)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080070{
71 asocket *s;
72 asocket *result = NULL;
73
74 adb_mutex_lock(&socket_list_lock);
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030075 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010076 if (s->id != local_id)
77 continue;
78 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030079 result = s;
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030080 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010081 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080082 }
83 adb_mutex_unlock(&socket_list_lock);
84
85 return result;
86}
87
88static void
89insert_local_socket(asocket* s, asocket* list)
90{
91 s->next = list;
92 s->prev = s->next->prev;
93 s->prev->next = s;
94 s->next->prev = s;
95}
96
97
98void install_local_socket(asocket *s)
99{
100 adb_mutex_lock(&socket_list_lock);
101
102 s->id = local_socket_next_id++;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100103
104 // Socket ids should never be 0.
105 if (local_socket_next_id == 0)
106 local_socket_next_id = 1;
107
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800108 insert_local_socket(s, &local_socket_list);
109
110 adb_mutex_unlock(&socket_list_lock);
111}
112
113void remove_socket(asocket *s)
114{
115 // socket_list_lock should already be held
116 if (s->prev && s->next)
117 {
118 s->prev->next = s->next;
119 s->next->prev = s->prev;
120 s->next = 0;
121 s->prev = 0;
122 s->id = 0;
123 }
124}
125
126void close_all_sockets(atransport *t)
127{
128 asocket *s;
129
130 /* this is a little gross, but since s->close() *will* modify
131 ** the list out from under you, your options are limited.
132 */
133 adb_mutex_lock(&socket_list_lock);
134restart:
135 for(s = local_socket_list.next; s != &local_socket_list; s = s->next){
136 if(s->transport == t || (s->peer && s->peer->transport == t)) {
137 local_socket_close_locked(s);
138 goto restart;
139 }
140 }
141 adb_mutex_unlock(&socket_list_lock);
142}
143
144static int local_socket_enqueue(asocket *s, apacket *p)
145{
146 D("LS(%d): enqueue %d\n", s->id, p->len);
147
148 p->ptr = p->data;
149
150 /* if there is already data queue'd, we will receive
151 ** events when it's time to write. just add this to
152 ** the tail
153 */
154 if(s->pkt_first) {
155 goto enqueue;
156 }
157
158 /* write as much as we can, until we
159 ** would block or there is an error/eof
160 */
161 while(p->len > 0) {
162 int r = adb_write(s->fd, p->ptr, p->len);
163 if(r > 0) {
164 p->len -= r;
165 p->ptr += r;
166 continue;
167 }
168 if((r == 0) || (errno != EAGAIN)) {
169 D( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) );
170 s->close(s);
171 return 1; /* not ready (error) */
172 } else {
173 break;
174 }
175 }
176
177 if(p->len == 0) {
178 put_apacket(p);
179 return 0; /* ready for more data */
180 }
181
182enqueue:
183 p->next = 0;
184 if(s->pkt_first) {
185 s->pkt_last->next = p;
186 } else {
187 s->pkt_first = p;
188 }
189 s->pkt_last = p;
190
191 /* make sure we are notified when we can drain the queue */
192 fdevent_add(&s->fde, FDE_WRITE);
193
194 return 1; /* not ready (backlog) */
195}
196
197static void local_socket_ready(asocket *s)
198{
199 /* far side is ready for data, pay attention to
200 readable events */
201 fdevent_add(&s->fde, FDE_READ);
202// D("LS(%d): ready()\n", s->id);
203}
204
205static void local_socket_close(asocket *s)
206{
207 adb_mutex_lock(&socket_list_lock);
208 local_socket_close_locked(s);
209 adb_mutex_unlock(&socket_list_lock);
210}
211
212// be sure to hold the socket list lock when calling this
213static void local_socket_destroy(asocket *s)
214{
215 apacket *p, *n;
Benoit Goby88468f32012-03-16 14:50:07 -0700216 int exit_on_close = s->exit_on_close;
217
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700218 D("LS(%d): destroying fde.fd=%d\n", s->id, s->fde.fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800219
220 /* IMPORTANT: the remove closes the fd
221 ** that belongs to this socket
222 */
223 fdevent_remove(&s->fde);
224
225 /* dispose of any unwritten data */
226 for(p = s->pkt_first; p; p = n) {
227 D("LS(%d): discarding %d bytes\n", s->id, p->len);
228 n = p->next;
229 put_apacket(p);
230 }
231 remove_socket(s);
232 free(s);
Benoit Goby88468f32012-03-16 14:50:07 -0700233
234 if (exit_on_close) {
235 D("local_socket_destroy: exiting\n");
236 exit(1);
237 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800238}
239
240
241static void local_socket_close_locked(asocket *s)
242{
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700243 D("entered. LS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800244 if(s->peer) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700245 D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n",
246 s->id, s->peer->id, s->peer->fd);
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100247 /* Note: it's important to call shutdown before disconnecting from
248 * the peer, this ensures that remote sockets can still get the id
249 * of the local socket they're connected to, to send a CLOSE()
250 * protocol event. */
251 if (s->peer->shutdown)
252 s->peer->shutdown(s->peer);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800253 s->peer->peer = 0;
254 // tweak to avoid deadlock
Tom Marlinbd6614d2011-05-13 13:24:55 -0500255 if (s->peer->close == local_socket_close) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800256 local_socket_close_locked(s->peer);
Tom Marlinbd6614d2011-05-13 13:24:55 -0500257 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800258 s->peer->close(s->peer);
Tom Marlinbd6614d2011-05-13 13:24:55 -0500259 }
260 s->peer = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800261 }
262
263 /* If we are already closing, or if there are no
264 ** pending packets, destroy immediately
265 */
266 if (s->closing || s->pkt_first == NULL) {
267 int id = s->id;
268 local_socket_destroy(s);
269 D("LS(%d): closed\n", id);
270 return;
271 }
272
273 /* otherwise, put on the closing list
274 */
275 D("LS(%d): closing\n", s->id);
276 s->closing = 1;
277 fdevent_del(&s->fde, FDE_READ);
278 remove_socket(s);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700279 D("LS(%d): put on socket_closing_list fd=%d\n", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800280 insert_local_socket(s, &local_socket_closing_list);
281}
282
283static void local_socket_event_func(int fd, unsigned ev, void *_s)
284{
285 asocket *s = _s;
286
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700287 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);
288
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800289 /* put the FDE_WRITE processing before the FDE_READ
290 ** in order to simplify the code.
291 */
292 if(ev & FDE_WRITE){
293 apacket *p;
294
295 while((p = s->pkt_first) != 0) {
296 while(p->len > 0) {
297 int r = adb_write(fd, p->ptr, p->len);
298 if(r > 0) {
299 p->ptr += r;
300 p->len -= r;
301 continue;
302 }
303 if(r < 0) {
304 /* returning here is ok because FDE_READ will
305 ** be processed in the next iteration loop
306 */
307 if(errno == EAGAIN) return;
308 if(errno == EINTR) continue;
309 }
Christopher Tatea162e242011-06-10 11:38:37 -0700310 D(" closing after write because r=%d and errno is %d\n", r, errno);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800311 s->close(s);
312 return;
313 }
314
315 if(p->len == 0) {
316 s->pkt_first = p->next;
317 if(s->pkt_first == 0) s->pkt_last = 0;
318 put_apacket(p);
319 }
320 }
321
322 /* if we sent the last packet of a closing socket,
323 ** we can now destroy it.
324 */
325 if (s->closing) {
Christopher Tatea162e242011-06-10 11:38:37 -0700326 D(" closing because 'closing' is set after write\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800327 s->close(s);
328 return;
329 }
330
331 /* no more packets queued, so we can ignore
332 ** writable events again and tell our peer
333 ** to resume writing
334 */
335 fdevent_del(&s->fde, FDE_WRITE);
336 s->peer->ready(s->peer);
337 }
338
339
340 if(ev & FDE_READ){
341 apacket *p = get_apacket();
342 unsigned char *x = p->data;
343 size_t avail = MAX_PAYLOAD;
344 int r;
345 int is_eof = 0;
346
347 while(avail > 0) {
348 r = adb_read(fd, x, avail);
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800349 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", s->id, s->fd, r, r<0?errno:0, avail);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800350 if(r > 0) {
351 avail -= r;
352 x += r;
353 continue;
354 }
355 if(r < 0) {
356 if(errno == EAGAIN) break;
357 if(errno == EINTR) continue;
358 }
359
360 /* r = 0 or unhandled error */
361 is_eof = 1;
362 break;
363 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700364 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
365 s->id, s->fd, r, is_eof, s->fde.force_eof);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800366 if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
367 put_apacket(p);
368 } else {
369 p->len = MAX_PAYLOAD - avail;
370
371 r = s->peer->enqueue(s->peer, p);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700372 D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, r);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800373
374 if(r < 0) {
375 /* error return means they closed us as a side-effect
376 ** and we must return immediately.
377 **
378 ** note that if we still have buffered packets, the
379 ** socket will be placed on the closing socket list.
380 ** this handler function will be called again
381 ** to process FDE_WRITE events.
382 */
383 return;
384 }
385
386 if(r > 0) {
387 /* if the remote cannot accept further events,
388 ** we disable notification of READs. They'll
389 ** be enabled again when we get a call to ready()
390 */
391 fdevent_del(&s->fde, FDE_READ);
392 }
393 }
JP Abgrall18d2d652011-04-12 22:01:58 -0700394 /* Don't allow a forced eof if data is still there */
395 if((s->fde.force_eof && !r) || is_eof) {
Christopher Tatea162e242011-06-10 11:38:37 -0700396 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 Project9ca14dc2009-03-03 19:32:55 -0800397 s->close(s);
398 }
399 }
400
401 if(ev & FDE_ERROR){
402 /* this should be caught be the next read or write
403 ** catching it here means we may skip the last few
404 ** bytes of readable data.
405 */
406// s->close(s);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700407 D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
408
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800409 return;
410 }
411}
412
413asocket *create_local_socket(int fd)
414{
415 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosa1249de62010-06-10 20:48:19 -0300416 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417 s->fd = fd;
418 s->enqueue = local_socket_enqueue;
419 s->ready = local_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100420 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800421 s->close = local_socket_close;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700422 install_local_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800423
424 fdevent_install(&s->fde, fd, local_socket_event_func, s);
425/* fdevent_add(&s->fde, FDE_ERROR); */
426 //fprintf(stderr, "Created local socket in create_local_socket \n");
427 D("LS(%d): created (fd=%d)\n", s->id, s->fd);
428 return s;
429}
430
431asocket *create_local_service_socket(const char *name)
432{
433 asocket *s;
434 int fd;
jzhuan5ebbb9af2013-05-24 17:40:15 -0400435#if !ADB_HOST
436 char debug[PROPERTY_VALUE_MAX];
437#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800438
439#if !ADB_HOST
440 if (!strcmp(name,"jdwp")) {
441 return create_jdwp_service_socket();
442 }
443 if (!strcmp(name,"track-jdwp")) {
444 return create_jdwp_tracker_service_socket();
445 }
446#endif
447 fd = service_to_fd(name);
448 if(fd < 0) return 0;
449
450 s = create_local_socket(fd);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700451 D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
Benoit Goby88468f32012-03-16 14:50:07 -0700452
JP Abgralla84bd682012-03-30 13:19:11 -0700453#if !ADB_HOST
jzhuan5ebbb9af2013-05-24 17:40:15 -0400454 if (!strncmp(name, "root:", 5))
455 property_get("ro.debuggable", debug, "");
456
457 if ((!strncmp(name, "root:", 5) && getuid() != 0
458 && strcmp(debug, "1") == 0)
Benoit Gobyab5c3d82012-06-12 12:12:18 -0700459 || !strncmp(name, "usb:", 4)
460 || !strncmp(name, "tcpip:", 6)) {
Benoit Goby88468f32012-03-16 14:50:07 -0700461 D("LS(%d): enabling exit_on_close\n", s->id);
462 s->exit_on_close = 1;
463 }
JP Abgralla84bd682012-03-30 13:19:11 -0700464#endif
Benoit Goby88468f32012-03-16 14:50:07 -0700465
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800466 return s;
467}
468
469#if ADB_HOST
470static asocket *create_host_service_socket(const char *name, const char* serial)
471{
472 asocket *s;
473
474 s = host_service_to_socket(name, serial);
475
476 if (s != NULL) {
477 D("LS(%d) bound to '%s'\n", s->id, name);
478 return s;
479 }
480
481 return s;
482}
483#endif /* ADB_HOST */
484
485/* a Remote socket is used to send/receive data to/from a given transport object
486** it needs to be closed when the transport is forcibly destroyed by the user
487*/
488typedef struct aremotesocket {
489 asocket socket;
490 adisconnect disconnect;
491} aremotesocket;
492
493static int remote_socket_enqueue(asocket *s, apacket *p)
494{
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700495 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n",
496 s->id, s->fd, s->peer->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800497 p->msg.command = A_WRTE;
498 p->msg.arg0 = s->peer->id;
499 p->msg.arg1 = s->id;
500 p->msg.data_length = p->len;
501 send_packet(p, s->transport);
502 return 1;
503}
504
505static void remote_socket_ready(asocket *s)
506{
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700507 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n",
508 s->id, s->fd, s->peer->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800509 apacket *p = get_apacket();
510 p->msg.command = A_OKAY;
511 p->msg.arg0 = s->peer->id;
512 p->msg.arg1 = s->id;
513 send_packet(p, s->transport);
514}
515
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100516static void remote_socket_shutdown(asocket *s)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800517{
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100518 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d\n",
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700519 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800520 apacket *p = get_apacket();
521 p->msg.command = A_CLSE;
522 if(s->peer) {
523 p->msg.arg0 = s->peer->id;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100524 }
525 p->msg.arg1 = s->id;
526 send_packet(p, s->transport);
527}
528
529static void remote_socket_close(asocket *s)
530{
531 if (s->peer) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800532 s->peer->peer = 0;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700533 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n",
534 s->id, s->peer->id, s->peer->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800535 s->peer->close(s->peer);
536 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100537 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n",
538 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800539 D("RS(%d): closed\n", s->id);
540 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
541 free(s);
542}
543
544static void remote_socket_disconnect(void* _s, atransport* t)
545{
546 asocket* s = _s;
547 asocket* peer = s->peer;
548
549 D("remote_socket_disconnect RS(%d)\n", s->id);
550 if (peer) {
551 peer->peer = NULL;
552 peer->close(peer);
553 }
554 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
555 free(s);
556}
557
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100558/* Create an asocket to exchange packets with a remote service through transport
559 |t|. Where |id| is the socket id of the corresponding service on the other
560 side of the transport (it is allocated by the remote side and _cannot_ be 0).
561 Returns a new non-NULL asocket handle. */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800562asocket *create_remote_socket(unsigned id, atransport *t)
563{
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100564 asocket* s;
565 adisconnect* dis;
566
567 if (id == 0) fatal("invalid remote socket id (0)");
568 s = calloc(1, sizeof(aremotesocket));
569 dis = &((aremotesocket*)s)->disconnect;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800570
André Goddard Rosa1249de62010-06-10 20:48:19 -0300571 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800572 s->id = id;
573 s->enqueue = remote_socket_enqueue;
574 s->ready = remote_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100575 s->shutdown = remote_socket_shutdown;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800576 s->close = remote_socket_close;
577 s->transport = t;
578
579 dis->func = remote_socket_disconnect;
580 dis->opaque = s;
581 add_transport_disconnect( t, dis );
582 D("RS(%d): created\n", s->id);
583 return s;
584}
585
586void connect_to_remote(asocket *s, const char *destination)
587{
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700588 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800589 apacket *p = get_apacket();
590 int len = strlen(destination) + 1;
591
592 if(len > (MAX_PAYLOAD-1)) {
593 fatal("destination oversized");
594 }
595
596 D("LS(%d): connect('%s')\n", s->id, destination);
597 p->msg.command = A_OPEN;
598 p->msg.arg0 = s->id;
599 p->msg.data_length = len;
600 strcpy((char*) p->data, destination);
601 send_packet(p, s->transport);
602}
603
604
605/* this is used by magic sockets to rig local sockets to
606 send the go-ahead message when they connect */
607static void local_socket_ready_notify(asocket *s)
608{
609 s->ready = local_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100610 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800611 s->close = local_socket_close;
612 adb_write(s->fd, "OKAY", 4);
613 s->ready(s);
614}
615
616/* this is used by magic sockets to rig local sockets to
617 send the failure message if they are closed before
618 connected (to avoid closing them without a status message) */
619static void local_socket_close_notify(asocket *s)
620{
621 s->ready = local_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100622 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800623 s->close = local_socket_close;
624 sendfailmsg(s->fd, "closed");
625 s->close(s);
626}
627
628unsigned unhex(unsigned char *s, int len)
629{
630 unsigned n = 0, c;
631
632 while(len-- > 0) {
633 switch((c = *s++)) {
634 case '0': case '1': case '2':
635 case '3': case '4': case '5':
636 case '6': case '7': case '8':
637 case '9':
638 c -= '0';
639 break;
640 case 'a': case 'b': case 'c':
641 case 'd': case 'e': case 'f':
642 c = c - 'a' + 10;
643 break;
644 case 'A': case 'B': case 'C':
645 case 'D': case 'E': case 'F':
646 c = c - 'A' + 10;
647 break;
648 default:
649 return 0xffffffff;
650 }
651
652 n = (n << 4) | c;
653 }
654
655 return n;
656}
657
Scott Anderson27042382012-05-30 18:11:27 -0700658#define PREFIX(str) { str, sizeof(str) - 1 }
659static const struct prefix_struct {
660 const char *str;
661 const size_t len;
662} prefixes[] = {
663 PREFIX("usb:"),
664 PREFIX("product:"),
665 PREFIX("model:"),
666 PREFIX("device:"),
667};
668static const int num_prefixes = (sizeof(prefixes) / sizeof(prefixes[0]));
669
Terence Haddock6c670402011-03-16 09:43:56 +0100670/* skip_host_serial return the position in a string
671 skipping over the 'serial' parameter in the ADB protocol,
672 where parameter string may be a host:port string containing
673 the protocol delimiter (colon). */
674char *skip_host_serial(char *service) {
675 char *first_colon, *serial_end;
Scott Anderson27042382012-05-30 18:11:27 -0700676 int i;
Terence Haddock6c670402011-03-16 09:43:56 +0100677
Scott Anderson27042382012-05-30 18:11:27 -0700678 for (i = 0; i < num_prefixes; i++) {
679 if (!strncmp(service, prefixes[i].str, prefixes[i].len))
680 return strchr(service + prefixes[i].len, ':');
Scott Anderson090e5cb2012-05-31 12:04:23 -0700681 }
682
Terence Haddock6c670402011-03-16 09:43:56 +0100683 first_colon = strchr(service, ':');
684 if (!first_colon) {
685 /* No colon in service string. */
686 return NULL;
687 }
688 serial_end = first_colon;
689 if (isdigit(serial_end[1])) {
690 serial_end++;
691 while ((*serial_end) && isdigit(*serial_end)) {
692 serial_end++;
693 }
694 if ((*serial_end) != ':') {
695 // Something other than numbers was found, reset the end.
696 serial_end = first_colon;
697 }
698 }
699 return serial_end;
700}
701
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800702static int smart_socket_enqueue(asocket *s, apacket *p)
703{
704 unsigned len;
705#if ADB_HOST
706 char *service = NULL;
707 char* serial = NULL;
708 transport_type ttype = kTransportAny;
709#endif
710
711 D("SS(%d): enqueue %d\n", s->id, p->len);
712
713 if(s->pkt_first == 0) {
714 s->pkt_first = p;
715 s->pkt_last = p;
716 } else {
717 if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
718 D("SS(%d): overflow\n", s->id);
719 put_apacket(p);
720 goto fail;
721 }
722
723 memcpy(s->pkt_first->data + s->pkt_first->len,
724 p->data, p->len);
725 s->pkt_first->len += p->len;
726 put_apacket(p);
727
728 p = s->pkt_first;
729 }
730
731 /* don't bother if we can't decode the length */
732 if(p->len < 4) return 0;
733
734 len = unhex(p->data, 4);
735 if((len < 1) || (len > 1024)) {
736 D("SS(%d): bad size (%d)\n", s->id, len);
737 goto fail;
738 }
739
740 D("SS(%d): len is %d\n", s->id, len );
741 /* can't do anything until we have the full header */
742 if((len + 4) > p->len) {
743 D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
744 return 0;
745 }
746
747 p->data[len + 4] = 0;
748
749 D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));
750
751#if ADB_HOST
752 service = (char *)p->data + 4;
753 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
754 char* serial_end;
755 service += strlen("host-serial:");
756
Terence Haddock6c670402011-03-16 09:43:56 +0100757 // serial number should follow "host:" and could be a host:port string.
758 serial_end = skip_host_serial(service);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800759 if (serial_end) {
760 *serial_end = 0; // terminate string
761 serial = service;
762 service = serial_end + 1;
763 }
764 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
765 ttype = kTransportUsb;
766 service += strlen("host-usb:");
767 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
768 ttype = kTransportLocal;
769 service += strlen("host-local:");
770 } else if (!strncmp(service, "host:", strlen("host:"))) {
771 ttype = kTransportAny;
772 service += strlen("host:");
773 } else {
774 service = NULL;
775 }
776
777 if (service) {
778 asocket *s2;
779
780 /* some requests are handled immediately -- in that
781 ** case the handle_host_request() routine has sent
782 ** the OKAY or FAIL message and all we have to do
783 ** is clean up.
784 */
785 if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
786 /* XXX fail message? */
787 D( "SS(%d): handled host service '%s'\n", s->id, service );
788 goto fail;
789 }
790 if (!strncmp(service, "transport", strlen("transport"))) {
791 D( "SS(%d): okay transport\n", s->id );
792 p->len = 0;
793 return 0;
794 }
795
796 /* try to find a local service with this name.
797 ** if no such service exists, we'll fail out
798 ** and tear down here.
799 */
800 s2 = create_host_service_socket(service, serial);
801 if(s2 == 0) {
802 D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
803 sendfailmsg(s->peer->fd, "unknown host service");
804 goto fail;
805 }
806
807 /* we've connected to a local host service,
808 ** so we make our peer back into a regular
809 ** local socket and bind it to the new local
810 ** service socket, acknowledge the successful
811 ** connection, and close this smart socket now
812 ** that its work is done.
813 */
814 adb_write(s->peer->fd, "OKAY", 4);
815
816 s->peer->ready = local_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100817 s->peer->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800818 s->peer->close = local_socket_close;
819 s->peer->peer = s2;
820 s2->peer = s->peer;
821 s->peer = 0;
822 D( "SS(%d): okay\n", s->id );
823 s->close(s);
824
825 /* initial state is "ready" */
826 s2->ready(s2);
827 return 0;
828 }
829#else /* !ADB_HOST */
830 if (s->transport == NULL) {
831 char* error_string = "unknown failure";
832 s->transport = acquire_one_transport (CS_ANY,
833 kTransportAny, NULL, &error_string);
834
835 if (s->transport == NULL) {
836 sendfailmsg(s->peer->fd, error_string);
837 goto fail;
838 }
839 }
840#endif
841
842 if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
843 /* if there's no remote we fail the connection
844 ** right here and terminate it
845 */
846 sendfailmsg(s->peer->fd, "device offline (x)");
847 goto fail;
848 }
849
850
851 /* instrument our peer to pass the success or fail
852 ** message back once it connects or closes, then
853 ** detach from it, request the connection, and
854 ** tear down
855 */
856 s->peer->ready = local_socket_ready_notify;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100857 s->peer->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800858 s->peer->close = local_socket_close_notify;
859 s->peer->peer = 0;
860 /* give him our transport and upref it */
861 s->peer->transport = s->transport;
862
863 connect_to_remote(s->peer, (char*) (p->data + 4));
864 s->peer = 0;
865 s->close(s);
866 return 1;
867
868fail:
869 /* we're going to close our peer as a side-effect, so
870 ** return -1 to signal that state to the local socket
871 ** who is enqueueing against us
872 */
873 s->close(s);
874 return -1;
875}
876
877static void smart_socket_ready(asocket *s)
878{
879 D("SS(%d): ready\n", s->id);
880}
881
882static void smart_socket_close(asocket *s)
883{
884 D("SS(%d): closed\n", s->id);
885 if(s->pkt_first){
886 put_apacket(s->pkt_first);
887 }
888 if(s->peer) {
889 s->peer->peer = 0;
890 s->peer->close(s->peer);
Tom Marlinbd6614d2011-05-13 13:24:55 -0500891 s->peer = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800892 }
893 free(s);
894}
895
Benoit Goby12dc3692013-02-20 15:04:53 -0800896static asocket *create_smart_socket(void)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800897{
898 D("Creating smart socket \n");
899 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosa1249de62010-06-10 20:48:19 -0300900 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800901 s->enqueue = smart_socket_enqueue;
902 s->ready = smart_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100903 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800904 s->close = smart_socket_close;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800905
Benoit Goby12dc3692013-02-20 15:04:53 -0800906 D("SS(%d)\n", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800907 return s;
908}
909
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800910void connect_to_smartsocket(asocket *s)
911{
912 D("Connecting to smart socket \n");
Benoit Goby12dc3692013-02-20 15:04:53 -0800913 asocket *ss = create_smart_socket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800914 s->peer = ss;
915 ss->peer = s;
916 s->ready(s);
917}