blob: de14a22548ad8fe2c8620c94425295b6d21415b1 [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
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
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010062// Parse the global list of sockets to find one with id |local_id|.
63// If |peer_id| is not 0, also check that it is connected to a peer
64// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
65asocket *find_local_socket(unsigned local_id, unsigned peer_id)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080066{
67 asocket *s;
68 asocket *result = NULL;
69
70 adb_mutex_lock(&socket_list_lock);
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030071 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010072 if (s->id != local_id)
73 continue;
74 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030075 result = s;
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030076 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010077 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080078 }
79 adb_mutex_unlock(&socket_list_lock);
80
81 return result;
82}
83
84static void
85insert_local_socket(asocket* s, asocket* list)
86{
87 s->next = list;
88 s->prev = s->next->prev;
89 s->prev->next = s;
90 s->next->prev = s;
91}
92
93
94void install_local_socket(asocket *s)
95{
96 adb_mutex_lock(&socket_list_lock);
97
98 s->id = local_socket_next_id++;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010099
100 // Socket ids should never be 0.
101 if (local_socket_next_id == 0)
102 local_socket_next_id = 1;
103
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800104 insert_local_socket(s, &local_socket_list);
105
106 adb_mutex_unlock(&socket_list_lock);
107}
108
109void remove_socket(asocket *s)
110{
111 // socket_list_lock should already be held
112 if (s->prev && s->next)
113 {
114 s->prev->next = s->next;
115 s->next->prev = s->prev;
116 s->next = 0;
117 s->prev = 0;
118 s->id = 0;
119 }
120}
121
122void close_all_sockets(atransport *t)
123{
124 asocket *s;
125
126 /* this is a little gross, but since s->close() *will* modify
127 ** the list out from under you, your options are limited.
128 */
129 adb_mutex_lock(&socket_list_lock);
130restart:
131 for(s = local_socket_list.next; s != &local_socket_list; s = s->next){
132 if(s->transport == t || (s->peer && s->peer->transport == t)) {
133 local_socket_close_locked(s);
134 goto restart;
135 }
136 }
137 adb_mutex_unlock(&socket_list_lock);
138}
139
140static int local_socket_enqueue(asocket *s, apacket *p)
141{
142 D("LS(%d): enqueue %d\n", s->id, p->len);
143
144 p->ptr = p->data;
145
146 /* if there is already data queue'd, we will receive
147 ** events when it's time to write. just add this to
148 ** the tail
149 */
150 if(s->pkt_first) {
151 goto enqueue;
152 }
153
154 /* write as much as we can, until we
155 ** would block or there is an error/eof
156 */
157 while(p->len > 0) {
158 int r = adb_write(s->fd, p->ptr, p->len);
159 if(r > 0) {
160 p->len -= r;
161 p->ptr += r;
162 continue;
163 }
164 if((r == 0) || (errno != EAGAIN)) {
165 D( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) );
166 s->close(s);
167 return 1; /* not ready (error) */
168 } else {
169 break;
170 }
171 }
172
173 if(p->len == 0) {
174 put_apacket(p);
175 return 0; /* ready for more data */
176 }
177
178enqueue:
179 p->next = 0;
180 if(s->pkt_first) {
181 s->pkt_last->next = p;
182 } else {
183 s->pkt_first = p;
184 }
185 s->pkt_last = p;
186
187 /* make sure we are notified when we can drain the queue */
188 fdevent_add(&s->fde, FDE_WRITE);
189
190 return 1; /* not ready (backlog) */
191}
192
193static void local_socket_ready(asocket *s)
194{
195 /* far side is ready for data, pay attention to
196 readable events */
197 fdevent_add(&s->fde, FDE_READ);
198// D("LS(%d): ready()\n", s->id);
199}
200
201static void local_socket_close(asocket *s)
202{
203 adb_mutex_lock(&socket_list_lock);
204 local_socket_close_locked(s);
205 adb_mutex_unlock(&socket_list_lock);
206}
207
208// be sure to hold the socket list lock when calling this
209static void local_socket_destroy(asocket *s)
210{
211 apacket *p, *n;
Benoit Goby88468f32012-03-16 14:50:07 -0700212 int exit_on_close = s->exit_on_close;
213
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700214 D("LS(%d): destroying fde.fd=%d\n", s->id, s->fde.fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800215
216 /* IMPORTANT: the remove closes the fd
217 ** that belongs to this socket
218 */
219 fdevent_remove(&s->fde);
220
221 /* dispose of any unwritten data */
222 for(p = s->pkt_first; p; p = n) {
223 D("LS(%d): discarding %d bytes\n", s->id, p->len);
224 n = p->next;
225 put_apacket(p);
226 }
227 remove_socket(s);
228 free(s);
Benoit Goby88468f32012-03-16 14:50:07 -0700229
230 if (exit_on_close) {
231 D("local_socket_destroy: exiting\n");
232 exit(1);
233 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800234}
235
236
237static void local_socket_close_locked(asocket *s)
238{
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700239 D("entered. LS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240 if(s->peer) {
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700241 D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n",
242 s->id, s->peer->id, s->peer->fd);
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100243 /* Note: it's important to call shutdown before disconnecting from
244 * the peer, this ensures that remote sockets can still get the id
245 * of the local socket they're connected to, to send a CLOSE()
246 * protocol event. */
247 if (s->peer->shutdown)
248 s->peer->shutdown(s->peer);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800249 s->peer->peer = 0;
250 // tweak to avoid deadlock
Tom Marlinbd6614d2011-05-13 13:24:55 -0500251 if (s->peer->close == local_socket_close) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800252 local_socket_close_locked(s->peer);
Tom Marlinbd6614d2011-05-13 13:24:55 -0500253 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800254 s->peer->close(s->peer);
Tom Marlinbd6614d2011-05-13 13:24:55 -0500255 }
256 s->peer = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800257 }
258
259 /* If we are already closing, or if there are no
260 ** pending packets, destroy immediately
261 */
262 if (s->closing || s->pkt_first == NULL) {
263 int id = s->id;
264 local_socket_destroy(s);
265 D("LS(%d): closed\n", id);
266 return;
267 }
268
269 /* otherwise, put on the closing list
270 */
271 D("LS(%d): closing\n", s->id);
272 s->closing = 1;
273 fdevent_del(&s->fde, FDE_READ);
274 remove_socket(s);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700275 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 -0800276 insert_local_socket(s, &local_socket_closing_list);
277}
278
279static void local_socket_event_func(int fd, unsigned ev, void *_s)
280{
281 asocket *s = _s;
282
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700283 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);
284
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800285 /* put the FDE_WRITE processing before the FDE_READ
286 ** in order to simplify the code.
287 */
288 if(ev & FDE_WRITE){
289 apacket *p;
290
291 while((p = s->pkt_first) != 0) {
292 while(p->len > 0) {
293 int r = adb_write(fd, p->ptr, p->len);
294 if(r > 0) {
295 p->ptr += r;
296 p->len -= r;
297 continue;
298 }
299 if(r < 0) {
300 /* returning here is ok because FDE_READ will
301 ** be processed in the next iteration loop
302 */
303 if(errno == EAGAIN) return;
304 if(errno == EINTR) continue;
305 }
Christopher Tatea162e242011-06-10 11:38:37 -0700306 D(" closing after write because r=%d and errno is %d\n", r, errno);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800307 s->close(s);
308 return;
309 }
310
311 if(p->len == 0) {
312 s->pkt_first = p->next;
313 if(s->pkt_first == 0) s->pkt_last = 0;
314 put_apacket(p);
315 }
316 }
317
318 /* if we sent the last packet of a closing socket,
319 ** we can now destroy it.
320 */
321 if (s->closing) {
Christopher Tatea162e242011-06-10 11:38:37 -0700322 D(" closing because 'closing' is set after write\n");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800323 s->close(s);
324 return;
325 }
326
327 /* no more packets queued, so we can ignore
328 ** writable events again and tell our peer
329 ** to resume writing
330 */
331 fdevent_del(&s->fde, FDE_WRITE);
332 s->peer->ready(s->peer);
333 }
334
335
336 if(ev & FDE_READ){
337 apacket *p = get_apacket();
338 unsigned char *x = p->data;
339 size_t avail = MAX_PAYLOAD;
340 int r;
341 int is_eof = 0;
342
343 while(avail > 0) {
344 r = adb_read(fd, x, avail);
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800345 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 -0800346 if(r > 0) {
347 avail -= r;
348 x += r;
349 continue;
350 }
351 if(r < 0) {
352 if(errno == EAGAIN) break;
353 if(errno == EINTR) continue;
354 }
355
356 /* r = 0 or unhandled error */
357 is_eof = 1;
358 break;
359 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700360 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
361 s->id, s->fd, r, is_eof, s->fde.force_eof);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800362 if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
363 put_apacket(p);
364 } else {
365 p->len = MAX_PAYLOAD - avail;
366
367 r = s->peer->enqueue(s->peer, p);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700368 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 -0800369
370 if(r < 0) {
371 /* error return means they closed us as a side-effect
372 ** and we must return immediately.
373 **
374 ** note that if we still have buffered packets, the
375 ** socket will be placed on the closing socket list.
376 ** this handler function will be called again
377 ** to process FDE_WRITE events.
378 */
379 return;
380 }
381
382 if(r > 0) {
383 /* if the remote cannot accept further events,
384 ** we disable notification of READs. They'll
385 ** be enabled again when we get a call to ready()
386 */
387 fdevent_del(&s->fde, FDE_READ);
388 }
389 }
JP Abgrall18d2d652011-04-12 22:01:58 -0700390 /* Don't allow a forced eof if data is still there */
391 if((s->fde.force_eof && !r) || is_eof) {
Christopher Tatea162e242011-06-10 11:38:37 -0700392 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 -0800393 s->close(s);
394 }
395 }
396
397 if(ev & FDE_ERROR){
398 /* this should be caught be the next read or write
399 ** catching it here means we may skip the last few
400 ** bytes of readable data.
401 */
402// s->close(s);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700403 D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
404
The Android Open Source Project9ca14dc2009-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 Rosa1249de62010-06-10 20:48:19 -0300412 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800413 s->fd = fd;
414 s->enqueue = local_socket_enqueue;
415 s->ready = local_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100416 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417 s->close = local_socket_close;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700418 install_local_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800419
420 fdevent_install(&s->fde, fd, local_socket_event_func, s);
421/* fdevent_add(&s->fde, FDE_ERROR); */
422 //fprintf(stderr, "Created local socket in create_local_socket \n");
423 D("LS(%d): created (fd=%d)\n", s->id, s->fd);
424 return s;
425}
426
427asocket *create_local_service_socket(const char *name)
428{
429 asocket *s;
430 int fd;
431
432#if !ADB_HOST
433 if (!strcmp(name,"jdwp")) {
434 return create_jdwp_service_socket();
435 }
436 if (!strcmp(name,"track-jdwp")) {
437 return create_jdwp_tracker_service_socket();
438 }
439#endif
440 fd = service_to_fd(name);
441 if(fd < 0) return 0;
442
443 s = create_local_socket(fd);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700444 D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
Benoit Goby88468f32012-03-16 14:50:07 -0700445
JP Abgralla84bd682012-03-30 13:19:11 -0700446#if !ADB_HOST
Benoit Gobyab5c3d82012-06-12 12:12:18 -0700447 if ((!strncmp(name, "root:", 5) && getuid() != 0)
448 || !strncmp(name, "usb:", 4)
449 || !strncmp(name, "tcpip:", 6)) {
Benoit Goby88468f32012-03-16 14:50:07 -0700450 D("LS(%d): enabling exit_on_close\n", s->id);
451 s->exit_on_close = 1;
452 }
JP Abgralla84bd682012-03-30 13:19:11 -0700453#endif
Benoit Goby88468f32012-03-16 14:50:07 -0700454
The Android Open Source Project9ca14dc2009-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 Abgrall2e5dd6e2011-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 Project9ca14dc2009-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 Abgrall2e5dd6e2011-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 Project9ca14dc2009-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' Turnere92344d2013-12-13 14:09:44 +0100505static void remote_socket_shutdown(asocket *s)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800506{
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100507 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d\n",
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700508 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Project9ca14dc2009-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' Turnere92344d2013-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 Project9ca14dc2009-03-03 19:32:55 -0800521 s->peer->peer = 0;
JP Abgrall2e5dd6e2011-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 Project9ca14dc2009-03-03 19:32:55 -0800524 s->peer->close(s->peer);
525 }
David 'Digit' Turnere92344d2013-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 Project9ca14dc2009-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' Turnere92344d2013-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 Project9ca14dc2009-03-03 19:32:55 -0800551asocket *create_remote_socket(unsigned id, atransport *t)
552{
David 'Digit' Turnere92344d2013-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 Project9ca14dc2009-03-03 19:32:55 -0800559
André Goddard Rosa1249de62010-06-10 20:48:19 -0300560 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800561 s->id = id;
562 s->enqueue = remote_socket_enqueue;
563 s->ready = remote_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100564 s->shutdown = remote_socket_shutdown;
The Android Open Source Project9ca14dc2009-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 Abgrall2e5dd6e2011-03-16 15:57:42 -0700577 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Project9ca14dc2009-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' Turnere92344d2013-12-13 14:09:44 +0100599 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-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' Turnere92344d2013-12-13 14:09:44 +0100611 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-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 Anderson27042382012-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 Haddock6c670402011-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 Anderson27042382012-05-30 18:11:27 -0700665 int i;
Terence Haddock6c670402011-03-16 09:43:56 +0100666
Scott Anderson27042382012-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 Anderson090e5cb2012-05-31 12:04:23 -0700670 }
671
Terence Haddock6c670402011-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 Project9ca14dc2009-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 Haddock6c670402011-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 Project9ca14dc2009-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' Turnere92344d2013-12-13 14:09:44 +0100806 s->peer->shutdown = NULL;
The Android Open Source Project9ca14dc2009-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' Turnere92344d2013-12-13 14:09:44 +0100846 s->peer->shutdown = NULL;
The Android Open Source Project9ca14dc2009-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 Marlinbd6614d2011-05-13 13:24:55 -0500880 s->peer = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800881 }
882 free(s);
883}
884
Benoit Goby12dc3692013-02-20 15:04:53 -0800885static asocket *create_smart_socket(void)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800886{
887 D("Creating smart socket \n");
888 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosa1249de62010-06-10 20:48:19 -0300889 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800890 s->enqueue = smart_socket_enqueue;
891 s->ready = smart_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100892 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800893 s->close = smart_socket_close;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800894
Benoit Goby12dc3692013-02-20 15:04:53 -0800895 D("SS(%d)\n", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800896 return s;
897}
898
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800899void connect_to_smartsocket(asocket *s)
900{
901 D("Connecting to smart socket \n");
Benoit Goby12dc3692013-02-20 15:04:53 -0800902 asocket *ss = create_smart_socket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800903 s->peer = ss;
904 ss->peer = s;
905 s->ready(s);
906}