blob: 1598c3c287dd86ac92dfe1a0bc87dceebfa44d52 [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
jzhuan51297d222013-05-24 17:40:15 -040026#if !ADB_HOST
27#include <cutils/properties.h>
28#endif
29
The Android Open Source Projectdd7bc332009-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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048static unsigned local_socket_next_id = 1;
49
50static asocket local_socket_list = {
51 .next = &local_socket_list,
52 .prev = &local_socket_list,
53};
54
55/* the the list of currently closing local sockets.
56** these have no peer anymore, but still packets to
57** write to their fd.
58*/
59static asocket local_socket_closing_list = {
60 .next = &local_socket_closing_list,
61 .prev = &local_socket_closing_list,
62};
63
David 'Digit' Turner818d6412013-12-13 14:09:44 +010064// Parse the global list of sockets to find one with id |local_id|.
65// If |peer_id| is not 0, also check that it is connected to a peer
66// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
67asocket *find_local_socket(unsigned local_id, unsigned peer_id)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068{
69 asocket *s;
70 asocket *result = NULL;
71
72 adb_mutex_lock(&socket_list_lock);
André Goddard Rosa81828292010-06-12 11:40:20 -030073 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +010074 if (s->id != local_id)
75 continue;
76 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa81828292010-06-12 11:40:20 -030077 result = s;
André Goddard Rosa81828292010-06-12 11:40:20 -030078 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010079 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080 }
81 adb_mutex_unlock(&socket_list_lock);
82
83 return result;
84}
85
86static void
87insert_local_socket(asocket* s, asocket* list)
88{
89 s->next = list;
90 s->prev = s->next->prev;
91 s->prev->next = s;
92 s->next->prev = s;
93}
94
95
96void install_local_socket(asocket *s)
97{
98 adb_mutex_lock(&socket_list_lock);
99
100 s->id = local_socket_next_id++;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100101
102 // Socket ids should never be 0.
103 if (local_socket_next_id == 0)
104 local_socket_next_id = 1;
105
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 insert_local_socket(s, &local_socket_list);
107
108 adb_mutex_unlock(&socket_list_lock);
109}
110
111void remove_socket(asocket *s)
112{
113 // socket_list_lock should already be held
114 if (s->prev && s->next)
115 {
116 s->prev->next = s->next;
117 s->next->prev = s->prev;
118 s->next = 0;
119 s->prev = 0;
120 s->id = 0;
121 }
122}
123
124void close_all_sockets(atransport *t)
125{
126 asocket *s;
127
128 /* this is a little gross, but since s->close() *will* modify
129 ** the list out from under you, your options are limited.
130 */
131 adb_mutex_lock(&socket_list_lock);
132restart:
133 for(s = local_socket_list.next; s != &local_socket_list; s = s->next){
134 if(s->transport == t || (s->peer && s->peer->transport == t)) {
135 local_socket_close_locked(s);
136 goto restart;
137 }
138 }
139 adb_mutex_unlock(&socket_list_lock);
140}
141
142static int local_socket_enqueue(asocket *s, apacket *p)
143{
144 D("LS(%d): enqueue %d\n", s->id, p->len);
145
146 p->ptr = p->data;
147
148 /* if there is already data queue'd, we will receive
149 ** events when it's time to write. just add this to
150 ** the tail
151 */
152 if(s->pkt_first) {
153 goto enqueue;
154 }
155
156 /* write as much as we can, until we
157 ** would block or there is an error/eof
158 */
159 while(p->len > 0) {
160 int r = adb_write(s->fd, p->ptr, p->len);
161 if(r > 0) {
162 p->len -= r;
163 p->ptr += r;
164 continue;
165 }
166 if((r == 0) || (errno != EAGAIN)) {
167 D( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) );
168 s->close(s);
169 return 1; /* not ready (error) */
170 } else {
171 break;
172 }
173 }
174
175 if(p->len == 0) {
176 put_apacket(p);
177 return 0; /* ready for more data */
178 }
179
180enqueue:
181 p->next = 0;
182 if(s->pkt_first) {
183 s->pkt_last->next = p;
184 } else {
185 s->pkt_first = p;
186 }
187 s->pkt_last = p;
188
189 /* make sure we are notified when we can drain the queue */
190 fdevent_add(&s->fde, FDE_WRITE);
191
192 return 1; /* not ready (backlog) */
193}
194
195static void local_socket_ready(asocket *s)
196{
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100197 /* far side is ready for data, pay attention to
198 readable events */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 fdevent_add(&s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200}
201
202static void local_socket_close(asocket *s)
203{
204 adb_mutex_lock(&socket_list_lock);
205 local_socket_close_locked(s);
206 adb_mutex_unlock(&socket_list_lock);
207}
208
209// be sure to hold the socket list lock when calling this
210static void local_socket_destroy(asocket *s)
211{
212 apacket *p, *n;
Benoit Gobyf366b362012-03-16 14:50:07 -0700213 int exit_on_close = s->exit_on_close;
214
JP Abgrall408fa572011-03-16 15:57:42 -0700215 D("LS(%d): destroying fde.fd=%d\n", s->id, s->fde.fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216
217 /* IMPORTANT: the remove closes the fd
218 ** that belongs to this socket
219 */
220 fdevent_remove(&s->fde);
221
222 /* dispose of any unwritten data */
223 for(p = s->pkt_first; p; p = n) {
224 D("LS(%d): discarding %d bytes\n", s->id, p->len);
225 n = p->next;
226 put_apacket(p);
227 }
228 remove_socket(s);
229 free(s);
Benoit Gobyf366b362012-03-16 14:50:07 -0700230
231 if (exit_on_close) {
232 D("local_socket_destroy: exiting\n");
233 exit(1);
234 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235}
236
237
238static void local_socket_close_locked(asocket *s)
239{
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100240 D("entered local_socket_close_locked. LS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 if(s->peer) {
JP Abgrall408fa572011-03-16 15:57:42 -0700242 D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n",
243 s->id, s->peer->id, s->peer->fd);
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100244 /* Note: it's important to call shutdown before disconnecting from
245 * the peer, this ensures that remote sockets can still get the id
246 * of the local socket they're connected to, to send a CLOSE()
247 * protocol event. */
248 if (s->peer->shutdown)
249 s->peer->shutdown(s->peer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 s->peer->peer = 0;
251 // tweak to avoid deadlock
Tom Marlin49f18572011-05-13 13:24:55 -0500252 if (s->peer->close == local_socket_close) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 local_socket_close_locked(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500254 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500256 }
257 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 }
259
260 /* If we are already closing, or if there are no
261 ** pending packets, destroy immediately
262 */
263 if (s->closing || s->pkt_first == NULL) {
264 int id = s->id;
265 local_socket_destroy(s);
266 D("LS(%d): closed\n", id);
267 return;
268 }
269
270 /* otherwise, put on the closing list
271 */
272 D("LS(%d): closing\n", s->id);
273 s->closing = 1;
274 fdevent_del(&s->fde, FDE_READ);
275 remove_socket(s);
JP Abgrall408fa572011-03-16 15:57:42 -0700276 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 -0800277 insert_local_socket(s, &local_socket_closing_list);
278}
279
280static void local_socket_event_func(int fd, unsigned ev, void *_s)
281{
282 asocket *s = _s;
283
JP Abgrall408fa572011-03-16 15:57:42 -0700284 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);
285
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 /* put the FDE_WRITE processing before the FDE_READ
287 ** in order to simplify the code.
288 */
289 if(ev & FDE_WRITE){
290 apacket *p;
291
292 while((p = s->pkt_first) != 0) {
293 while(p->len > 0) {
294 int r = adb_write(fd, p->ptr, p->len);
295 if(r > 0) {
296 p->ptr += r;
297 p->len -= r;
298 continue;
299 }
300 if(r < 0) {
301 /* returning here is ok because FDE_READ will
302 ** be processed in the next iteration loop
303 */
304 if(errno == EAGAIN) return;
305 if(errno == EINTR) continue;
306 }
Christopher Tate5b811fa2011-06-10 11:38:37 -0700307 D(" closing after write because r=%d and errno is %d\n", r, errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 s->close(s);
309 return;
310 }
311
312 if(p->len == 0) {
313 s->pkt_first = p->next;
314 if(s->pkt_first == 0) s->pkt_last = 0;
315 put_apacket(p);
316 }
317 }
318
319 /* if we sent the last packet of a closing socket,
320 ** we can now destroy it.
321 */
322 if (s->closing) {
Christopher Tate5b811fa2011-06-10 11:38:37 -0700323 D(" closing because 'closing' is set after write\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 s->close(s);
325 return;
326 }
327
328 /* no more packets queued, so we can ignore
329 ** writable events again and tell our peer
330 ** to resume writing
331 */
332 fdevent_del(&s->fde, FDE_WRITE);
333 s->peer->ready(s->peer);
334 }
335
336
337 if(ev & FDE_READ){
338 apacket *p = get_apacket();
339 unsigned char *x = p->data;
340 size_t avail = MAX_PAYLOAD;
341 int r;
342 int is_eof = 0;
343
344 while(avail > 0) {
345 r = adb_read(fd, x, avail);
Elliott Hughesccecf142014-01-16 10:53:11 -0800346 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 Projectdd7bc332009-03-03 19:32:55 -0800347 if(r > 0) {
348 avail -= r;
349 x += r;
350 continue;
351 }
352 if(r < 0) {
353 if(errno == EAGAIN) break;
354 if(errno == EINTR) continue;
355 }
356
357 /* r = 0 or unhandled error */
358 is_eof = 1;
359 break;
360 }
JP Abgrall408fa572011-03-16 15:57:42 -0700361 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
362 s->id, s->fd, r, is_eof, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
364 put_apacket(p);
365 } else {
366 p->len = MAX_PAYLOAD - avail;
367
368 r = s->peer->enqueue(s->peer, p);
JP Abgrall408fa572011-03-16 15:57:42 -0700369 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 -0800370
371 if(r < 0) {
372 /* error return means they closed us as a side-effect
373 ** and we must return immediately.
374 **
375 ** note that if we still have buffered packets, the
376 ** socket will be placed on the closing socket list.
377 ** this handler function will be called again
378 ** to process FDE_WRITE events.
379 */
380 return;
381 }
382
383 if(r > 0) {
384 /* if the remote cannot accept further events,
385 ** we disable notification of READs. They'll
386 ** be enabled again when we get a call to ready()
387 */
388 fdevent_del(&s->fde, FDE_READ);
389 }
390 }
JP Abgrall112445b2011-04-12 22:01:58 -0700391 /* Don't allow a forced eof if data is still there */
392 if((s->fde.force_eof && !r) || is_eof) {
Christopher Tate5b811fa2011-06-10 11:38:37 -0700393 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 -0800394 s->close(s);
395 }
396 }
397
398 if(ev & FDE_ERROR){
399 /* this should be caught be the next read or write
400 ** catching it here means we may skip the last few
401 ** bytes of readable data.
402 */
JP Abgrall408fa572011-03-16 15:57:42 -0700403 D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
404
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 return;
406 }
407}
408
409asocket *create_local_socket(int fd)
410{
411 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300412 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 s->fd = fd;
414 s->enqueue = local_socket_enqueue;
415 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100416 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700418 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419
420 fdevent_install(&s->fde, fd, local_socket_event_func, s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 D("LS(%d): created (fd=%d)\n", s->id, s->fd);
422 return s;
423}
424
425asocket *create_local_service_socket(const char *name)
426{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427#if !ADB_HOST
428 if (!strcmp(name,"jdwp")) {
429 return create_jdwp_service_socket();
430 }
431 if (!strcmp(name,"track-jdwp")) {
432 return create_jdwp_tracker_service_socket();
433 }
434#endif
Dan Pasanen98858812014-10-06 12:57:20 -0500435 int fd = service_to_fd(name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 if(fd < 0) return 0;
437
Dan Pasanen98858812014-10-06 12:57:20 -0500438 asocket* s = create_local_socket(fd);
JP Abgrall408fa572011-03-16 15:57:42 -0700439 D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
Benoit Gobyf366b362012-03-16 14:50:07 -0700440
JP Abgrallf91259a2012-03-30 13:19:11 -0700441#if !ADB_HOST
Dan Pasanen98858812014-10-06 12:57:20 -0500442 char debug[PROPERTY_VALUE_MAX];
jzhuan51297d222013-05-24 17:40:15 -0400443 if (!strncmp(name, "root:", 5))
444 property_get("ro.debuggable", debug, "");
445
Dan Pasanen98858812014-10-06 12:57:20 -0500446 if ((!strncmp(name, "root:", 5) && getuid() != 0 && strcmp(debug, "1") == 0)
447 || (!strncmp(name, "unroot:", 7) && getuid() == 0)
Benoit Gobyaeceb512012-06-12 12:12:18 -0700448 || !strncmp(name, "usb:", 4)
449 || !strncmp(name, "tcpip:", 6)) {
Benoit Gobyf366b362012-03-16 14:50:07 -0700450 D("LS(%d): enabling exit_on_close\n", s->id);
451 s->exit_on_close = 1;
452 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700453#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700454
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 return s;
456}
457
458#if ADB_HOST
459static asocket *create_host_service_socket(const char *name, const char* serial)
460{
461 asocket *s;
462
463 s = host_service_to_socket(name, serial);
464
465 if (s != NULL) {
466 D("LS(%d) bound to '%s'\n", s->id, name);
467 return s;
468 }
469
470 return s;
471}
472#endif /* ADB_HOST */
473
474/* a Remote socket is used to send/receive data to/from a given transport object
475** it needs to be closed when the transport is forcibly destroyed by the user
476*/
477typedef struct aremotesocket {
478 asocket socket;
479 adisconnect disconnect;
480} aremotesocket;
481
482static int remote_socket_enqueue(asocket *s, apacket *p)
483{
JP Abgrall408fa572011-03-16 15:57:42 -0700484 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n",
485 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486 p->msg.command = A_WRTE;
487 p->msg.arg0 = s->peer->id;
488 p->msg.arg1 = s->id;
489 p->msg.data_length = p->len;
490 send_packet(p, s->transport);
491 return 1;
492}
493
494static void remote_socket_ready(asocket *s)
495{
JP Abgrall408fa572011-03-16 15:57:42 -0700496 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n",
497 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 apacket *p = get_apacket();
499 p->msg.command = A_OKAY;
500 p->msg.arg0 = s->peer->id;
501 p->msg.arg1 = s->id;
502 send_packet(p, s->transport);
503}
504
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100505static void remote_socket_shutdown(asocket *s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506{
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100507 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d\n",
JP Abgrall408fa572011-03-16 15:57:42 -0700508 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 apacket *p = get_apacket();
510 p->msg.command = A_CLSE;
511 if(s->peer) {
512 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100513 }
514 p->msg.arg1 = s->id;
515 send_packet(p, s->transport);
516}
517
518static void remote_socket_close(asocket *s)
519{
520 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521 s->peer->peer = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700522 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n",
523 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 s->peer->close(s->peer);
525 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100526 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n",
527 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 D("RS(%d): closed\n", s->id);
529 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
530 free(s);
531}
532
533static void remote_socket_disconnect(void* _s, atransport* t)
534{
535 asocket* s = _s;
536 asocket* peer = s->peer;
537
538 D("remote_socket_disconnect RS(%d)\n", s->id);
539 if (peer) {
540 peer->peer = NULL;
541 peer->close(peer);
542 }
543 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
544 free(s);
545}
546
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100547/* Create an asocket to exchange packets with a remote service through transport
548 |t|. Where |id| is the socket id of the corresponding service on the other
549 side of the transport (it is allocated by the remote side and _cannot_ be 0).
550 Returns a new non-NULL asocket handle. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800551asocket *create_remote_socket(unsigned id, atransport *t)
552{
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100553 asocket* s;
554 adisconnect* dis;
555
556 if (id == 0) fatal("invalid remote socket id (0)");
557 s = calloc(1, sizeof(aremotesocket));
558 dis = &((aremotesocket*)s)->disconnect;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800559
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300560 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561 s->id = id;
562 s->enqueue = remote_socket_enqueue;
563 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100564 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565 s->close = remote_socket_close;
566 s->transport = t;
567
568 dis->func = remote_socket_disconnect;
569 dis->opaque = s;
570 add_transport_disconnect( t, dis );
571 D("RS(%d): created\n", s->id);
572 return s;
573}
574
575void connect_to_remote(asocket *s, const char *destination)
576{
JP Abgrall408fa572011-03-16 15:57:42 -0700577 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800578 apacket *p = get_apacket();
579 int len = strlen(destination) + 1;
580
581 if(len > (MAX_PAYLOAD-1)) {
582 fatal("destination oversized");
583 }
584
585 D("LS(%d): connect('%s')\n", s->id, destination);
586 p->msg.command = A_OPEN;
587 p->msg.arg0 = s->id;
588 p->msg.data_length = len;
589 strcpy((char*) p->data, destination);
590 send_packet(p, s->transport);
591}
592
593
594/* this is used by magic sockets to rig local sockets to
595 send the go-ahead message when they connect */
596static void local_socket_ready_notify(asocket *s)
597{
598 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100599 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600 s->close = local_socket_close;
601 adb_write(s->fd, "OKAY", 4);
602 s->ready(s);
603}
604
605/* this is used by magic sockets to rig local sockets to
606 send the failure message if they are closed before
607 connected (to avoid closing them without a status message) */
608static void local_socket_close_notify(asocket *s)
609{
610 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100611 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612 s->close = local_socket_close;
613 sendfailmsg(s->fd, "closed");
614 s->close(s);
615}
616
617unsigned unhex(unsigned char *s, int len)
618{
619 unsigned n = 0, c;
620
621 while(len-- > 0) {
622 switch((c = *s++)) {
623 case '0': case '1': case '2':
624 case '3': case '4': case '5':
625 case '6': case '7': case '8':
626 case '9':
627 c -= '0';
628 break;
629 case 'a': case 'b': case 'c':
630 case 'd': case 'e': case 'f':
631 c = c - 'a' + 10;
632 break;
633 case 'A': case 'B': case 'C':
634 case 'D': case 'E': case 'F':
635 c = c - 'A' + 10;
636 break;
637 default:
638 return 0xffffffff;
639 }
640
641 n = (n << 4) | c;
642 }
643
644 return n;
645}
646
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700647#define PREFIX(str) { str, sizeof(str) - 1 }
648static const struct prefix_struct {
649 const char *str;
650 const size_t len;
651} prefixes[] = {
652 PREFIX("usb:"),
653 PREFIX("product:"),
654 PREFIX("model:"),
655 PREFIX("device:"),
656};
657static const int num_prefixes = (sizeof(prefixes) / sizeof(prefixes[0]));
658
Terence Haddock28e13902011-03-16 09:43:56 +0100659/* skip_host_serial return the position in a string
660 skipping over the 'serial' parameter in the ADB protocol,
661 where parameter string may be a host:port string containing
662 the protocol delimiter (colon). */
663char *skip_host_serial(char *service) {
664 char *first_colon, *serial_end;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700665 int i;
Terence Haddock28e13902011-03-16 09:43:56 +0100666
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700667 for (i = 0; i < num_prefixes; i++) {
668 if (!strncmp(service, prefixes[i].str, prefixes[i].len))
669 return strchr(service + prefixes[i].len, ':');
Scott Anderson3608d832012-05-31 12:04:23 -0700670 }
671
Terence Haddock28e13902011-03-16 09:43:56 +0100672 first_colon = strchr(service, ':');
673 if (!first_colon) {
674 /* No colon in service string. */
675 return NULL;
676 }
677 serial_end = first_colon;
678 if (isdigit(serial_end[1])) {
679 serial_end++;
680 while ((*serial_end) && isdigit(*serial_end)) {
681 serial_end++;
682 }
683 if ((*serial_end) != ':') {
684 // Something other than numbers was found, reset the end.
685 serial_end = first_colon;
686 }
687 }
688 return serial_end;
689}
690
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691static int smart_socket_enqueue(asocket *s, apacket *p)
692{
693 unsigned len;
694#if ADB_HOST
695 char *service = NULL;
696 char* serial = NULL;
697 transport_type ttype = kTransportAny;
698#endif
699
700 D("SS(%d): enqueue %d\n", s->id, p->len);
701
702 if(s->pkt_first == 0) {
703 s->pkt_first = p;
704 s->pkt_last = p;
705 } else {
706 if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
707 D("SS(%d): overflow\n", s->id);
708 put_apacket(p);
709 goto fail;
710 }
711
712 memcpy(s->pkt_first->data + s->pkt_first->len,
713 p->data, p->len);
714 s->pkt_first->len += p->len;
715 put_apacket(p);
716
717 p = s->pkt_first;
718 }
719
720 /* don't bother if we can't decode the length */
721 if(p->len < 4) return 0;
722
723 len = unhex(p->data, 4);
724 if((len < 1) || (len > 1024)) {
725 D("SS(%d): bad size (%d)\n", s->id, len);
726 goto fail;
727 }
728
729 D("SS(%d): len is %d\n", s->id, len );
730 /* can't do anything until we have the full header */
731 if((len + 4) > p->len) {
732 D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
733 return 0;
734 }
735
736 p->data[len + 4] = 0;
737
738 D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));
739
740#if ADB_HOST
741 service = (char *)p->data + 4;
742 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
743 char* serial_end;
744 service += strlen("host-serial:");
745
Terence Haddock28e13902011-03-16 09:43:56 +0100746 // serial number should follow "host:" and could be a host:port string.
747 serial_end = skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748 if (serial_end) {
749 *serial_end = 0; // terminate string
750 serial = service;
751 service = serial_end + 1;
752 }
753 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
754 ttype = kTransportUsb;
755 service += strlen("host-usb:");
756 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
757 ttype = kTransportLocal;
758 service += strlen("host-local:");
759 } else if (!strncmp(service, "host:", strlen("host:"))) {
760 ttype = kTransportAny;
761 service += strlen("host:");
762 } else {
763 service = NULL;
764 }
765
766 if (service) {
767 asocket *s2;
768
769 /* some requests are handled immediately -- in that
770 ** case the handle_host_request() routine has sent
771 ** the OKAY or FAIL message and all we have to do
772 ** is clean up.
773 */
774 if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
775 /* XXX fail message? */
776 D( "SS(%d): handled host service '%s'\n", s->id, service );
777 goto fail;
778 }
779 if (!strncmp(service, "transport", strlen("transport"))) {
780 D( "SS(%d): okay transport\n", s->id );
781 p->len = 0;
782 return 0;
783 }
784
785 /* try to find a local service with this name.
786 ** if no such service exists, we'll fail out
787 ** and tear down here.
788 */
789 s2 = create_host_service_socket(service, serial);
790 if(s2 == 0) {
791 D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
792 sendfailmsg(s->peer->fd, "unknown host service");
793 goto fail;
794 }
795
796 /* we've connected to a local host service,
797 ** so we make our peer back into a regular
798 ** local socket and bind it to the new local
799 ** service socket, acknowledge the successful
800 ** connection, and close this smart socket now
801 ** that its work is done.
802 */
803 adb_write(s->peer->fd, "OKAY", 4);
804
805 s->peer->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100806 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807 s->peer->close = local_socket_close;
808 s->peer->peer = s2;
809 s2->peer = s->peer;
810 s->peer = 0;
811 D( "SS(%d): okay\n", s->id );
812 s->close(s);
813
814 /* initial state is "ready" */
815 s2->ready(s2);
816 return 0;
817 }
818#else /* !ADB_HOST */
819 if (s->transport == NULL) {
820 char* error_string = "unknown failure";
821 s->transport = acquire_one_transport (CS_ANY,
822 kTransportAny, NULL, &error_string);
823
824 if (s->transport == NULL) {
825 sendfailmsg(s->peer->fd, error_string);
826 goto fail;
827 }
828 }
829#endif
830
831 if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
832 /* if there's no remote we fail the connection
833 ** right here and terminate it
834 */
835 sendfailmsg(s->peer->fd, "device offline (x)");
836 goto fail;
837 }
838
839
840 /* instrument our peer to pass the success or fail
841 ** message back once it connects or closes, then
842 ** detach from it, request the connection, and
843 ** tear down
844 */
845 s->peer->ready = local_socket_ready_notify;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100846 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800847 s->peer->close = local_socket_close_notify;
848 s->peer->peer = 0;
849 /* give him our transport and upref it */
850 s->peer->transport = s->transport;
851
852 connect_to_remote(s->peer, (char*) (p->data + 4));
853 s->peer = 0;
854 s->close(s);
855 return 1;
856
857fail:
858 /* we're going to close our peer as a side-effect, so
859 ** return -1 to signal that state to the local socket
860 ** who is enqueueing against us
861 */
862 s->close(s);
863 return -1;
864}
865
866static void smart_socket_ready(asocket *s)
867{
868 D("SS(%d): ready\n", s->id);
869}
870
871static void smart_socket_close(asocket *s)
872{
873 D("SS(%d): closed\n", s->id);
874 if(s->pkt_first){
875 put_apacket(s->pkt_first);
876 }
877 if(s->peer) {
878 s->peer->peer = 0;
879 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500880 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800881 }
882 free(s);
883}
884
Benoit Goby9470c2f2013-02-20 15:04:53 -0800885static asocket *create_smart_socket(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800886{
887 D("Creating smart socket \n");
888 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300889 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800890 s->enqueue = smart_socket_enqueue;
891 s->ready = smart_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100892 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800893 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800894
Benoit Goby9470c2f2013-02-20 15:04:53 -0800895 D("SS(%d)\n", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800896 return s;
897}
898
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800899void connect_to_smartsocket(asocket *s)
900{
901 D("Connecting to smart socket \n");
Benoit Goby9470c2f2013-02-20 15:04:53 -0800902 asocket *ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800903 s->peer = ss;
904 ss->peer = s;
905 s->ready(s);
906}