blob: d34f8c6d49465b5ee988bc0715a410b62da01d84 [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
Dan Albert76649012015-02-24 15:51:19 -080017#include <ctype.h>
18#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <stdio.h>
20#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080022#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023
24#include "sysdeps.h"
25
26#define TRACE_TAG TRACE_SOCKETS
27#include "adb.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080028#include "adb_io.h"
Dan Albert76649012015-02-24 15:51:19 -080029#if !ADB_HOST
30#include "cutils/properties.h"
31#endif
32#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
34ADB_MUTEX_DEFINE( socket_list_lock );
35
36static void local_socket_close_locked(asocket *s);
37
38int sendfailmsg(int fd, const char *reason)
39{
40 char buf[9];
41 int len;
42 len = strlen(reason);
Dan Albertcc731cc2015-02-24 21:26:58 -080043 if (len > 0xffff) {
44 len = 0xffff;
45 }
46
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047 snprintf(buf, sizeof buf, "FAIL%04x", len);
Dan Albertcc731cc2015-02-24 21:26:58 -080048 if (!WriteFdExactly(fd, buf, 8)) {
49 return -1;
50 }
51
52 return WriteFdExactly(fd, reason, len) ? 0 : -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053}
54
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055static unsigned local_socket_next_id = 1;
56
57static asocket local_socket_list = {
58 .next = &local_socket_list,
59 .prev = &local_socket_list,
60};
61
62/* the the list of currently closing local sockets.
63** these have no peer anymore, but still packets to
64** write to their fd.
65*/
66static asocket local_socket_closing_list = {
67 .next = &local_socket_closing_list,
68 .prev = &local_socket_closing_list,
69};
70
David 'Digit' Turner818d6412013-12-13 14:09:44 +010071// Parse the global list of sockets to find one with id |local_id|.
72// If |peer_id| is not 0, also check that it is connected to a peer
73// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
74asocket *find_local_socket(unsigned local_id, unsigned peer_id)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075{
76 asocket *s;
77 asocket *result = NULL;
78
79 adb_mutex_lock(&socket_list_lock);
André Goddard Rosa81828292010-06-12 11:40:20 -030080 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +010081 if (s->id != local_id)
82 continue;
83 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa81828292010-06-12 11:40:20 -030084 result = s;
André Goddard Rosa81828292010-06-12 11:40:20 -030085 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010086 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087 }
88 adb_mutex_unlock(&socket_list_lock);
89
90 return result;
91}
92
93static void
94insert_local_socket(asocket* s, asocket* list)
95{
96 s->next = list;
97 s->prev = s->next->prev;
98 s->prev->next = s;
99 s->next->prev = s;
100}
101
102
103void install_local_socket(asocket *s)
104{
105 adb_mutex_lock(&socket_list_lock);
106
107 s->id = local_socket_next_id++;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100108
109 // Socket ids should never be 0.
110 if (local_socket_next_id == 0)
111 local_socket_next_id = 1;
112
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 insert_local_socket(s, &local_socket_list);
114
115 adb_mutex_unlock(&socket_list_lock);
116}
117
118void remove_socket(asocket *s)
119{
120 // socket_list_lock should already be held
121 if (s->prev && s->next)
122 {
123 s->prev->next = s->next;
124 s->next->prev = s->prev;
125 s->next = 0;
126 s->prev = 0;
127 s->id = 0;
128 }
129}
130
131void close_all_sockets(atransport *t)
132{
133 asocket *s;
134
135 /* this is a little gross, but since s->close() *will* modify
136 ** the list out from under you, your options are limited.
137 */
138 adb_mutex_lock(&socket_list_lock);
139restart:
140 for(s = local_socket_list.next; s != &local_socket_list; s = s->next){
141 if(s->transport == t || (s->peer && s->peer->transport == t)) {
142 local_socket_close_locked(s);
143 goto restart;
144 }
145 }
146 adb_mutex_unlock(&socket_list_lock);
147}
148
149static int local_socket_enqueue(asocket *s, apacket *p)
150{
151 D("LS(%d): enqueue %d\n", s->id, p->len);
152
153 p->ptr = p->data;
154
155 /* if there is already data queue'd, we will receive
156 ** events when it's time to write. just add this to
157 ** the tail
158 */
159 if(s->pkt_first) {
160 goto enqueue;
161 }
162
163 /* write as much as we can, until we
164 ** would block or there is an error/eof
165 */
166 while(p->len > 0) {
167 int r = adb_write(s->fd, p->ptr, p->len);
168 if(r > 0) {
169 p->len -= r;
170 p->ptr += r;
171 continue;
172 }
173 if((r == 0) || (errno != EAGAIN)) {
174 D( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) );
175 s->close(s);
176 return 1; /* not ready (error) */
177 } else {
178 break;
179 }
180 }
181
182 if(p->len == 0) {
183 put_apacket(p);
184 return 0; /* ready for more data */
185 }
186
187enqueue:
188 p->next = 0;
189 if(s->pkt_first) {
190 s->pkt_last->next = p;
191 } else {
192 s->pkt_first = p;
193 }
194 s->pkt_last = p;
195
196 /* make sure we are notified when we can drain the queue */
197 fdevent_add(&s->fde, FDE_WRITE);
198
199 return 1; /* not ready (backlog) */
200}
201
202static void local_socket_ready(asocket *s)
203{
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100204 /* far side is ready for data, pay attention to
205 readable events */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 fdevent_add(&s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207}
208
209static void local_socket_close(asocket *s)
210{
211 adb_mutex_lock(&socket_list_lock);
212 local_socket_close_locked(s);
213 adb_mutex_unlock(&socket_list_lock);
214}
215
216// be sure to hold the socket list lock when calling this
217static void local_socket_destroy(asocket *s)
218{
219 apacket *p, *n;
Benoit Gobyf366b362012-03-16 14:50:07 -0700220 int exit_on_close = s->exit_on_close;
221
JP Abgrall408fa572011-03-16 15:57:42 -0700222 D("LS(%d): destroying fde.fd=%d\n", s->id, s->fde.fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223
224 /* IMPORTANT: the remove closes the fd
225 ** that belongs to this socket
226 */
227 fdevent_remove(&s->fde);
228
229 /* dispose of any unwritten data */
230 for(p = s->pkt_first; p; p = n) {
231 D("LS(%d): discarding %d bytes\n", s->id, p->len);
232 n = p->next;
233 put_apacket(p);
234 }
235 remove_socket(s);
236 free(s);
Benoit Gobyf366b362012-03-16 14:50:07 -0700237
238 if (exit_on_close) {
239 D("local_socket_destroy: exiting\n");
240 exit(1);
241 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242}
243
244
245static void local_socket_close_locked(asocket *s)
246{
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100247 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 -0800248 if(s->peer) {
JP Abgrall408fa572011-03-16 15:57:42 -0700249 D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n",
250 s->id, s->peer->id, s->peer->fd);
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100251 /* Note: it's important to call shutdown before disconnecting from
252 * the peer, this ensures that remote sockets can still get the id
253 * of the local socket they're connected to, to send a CLOSE()
254 * protocol event. */
255 if (s->peer->shutdown)
256 s->peer->shutdown(s->peer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 s->peer->peer = 0;
258 // tweak to avoid deadlock
Tom Marlin49f18572011-05-13 13:24:55 -0500259 if (s->peer->close == local_socket_close) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260 local_socket_close_locked(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500261 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500263 }
264 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 }
266
267 /* If we are already closing, or if there are no
268 ** pending packets, destroy immediately
269 */
270 if (s->closing || s->pkt_first == NULL) {
271 int id = s->id;
272 local_socket_destroy(s);
273 D("LS(%d): closed\n", id);
274 return;
275 }
276
277 /* otherwise, put on the closing list
278 */
279 D("LS(%d): closing\n", s->id);
280 s->closing = 1;
281 fdevent_del(&s->fde, FDE_READ);
282 remove_socket(s);
JP Abgrall408fa572011-03-16 15:57:42 -0700283 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 -0800284 insert_local_socket(s, &local_socket_closing_list);
285}
286
287static void local_socket_event_func(int fd, unsigned ev, void *_s)
288{
289 asocket *s = _s;
290
JP Abgrall408fa572011-03-16 15:57:42 -0700291 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);
292
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 /* put the FDE_WRITE processing before the FDE_READ
294 ** in order to simplify the code.
295 */
296 if(ev & FDE_WRITE){
297 apacket *p;
298
299 while((p = s->pkt_first) != 0) {
300 while(p->len > 0) {
301 int r = adb_write(fd, p->ptr, p->len);
302 if(r > 0) {
303 p->ptr += r;
304 p->len -= r;
305 continue;
306 }
307 if(r < 0) {
308 /* returning here is ok because FDE_READ will
309 ** be processed in the next iteration loop
310 */
311 if(errno == EAGAIN) return;
312 if(errno == EINTR) continue;
313 }
Christopher Tate5b811fa2011-06-10 11:38:37 -0700314 D(" closing after write because r=%d and errno is %d\n", r, errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 s->close(s);
316 return;
317 }
318
319 if(p->len == 0) {
320 s->pkt_first = p->next;
321 if(s->pkt_first == 0) s->pkt_last = 0;
322 put_apacket(p);
323 }
324 }
325
326 /* if we sent the last packet of a closing socket,
327 ** we can now destroy it.
328 */
329 if (s->closing) {
Christopher Tate5b811fa2011-06-10 11:38:37 -0700330 D(" closing because 'closing' is set after write\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 s->close(s);
332 return;
333 }
334
335 /* no more packets queued, so we can ignore
336 ** writable events again and tell our peer
337 ** to resume writing
338 */
339 fdevent_del(&s->fde, FDE_WRITE);
340 s->peer->ready(s->peer);
341 }
342
343
344 if(ev & FDE_READ){
345 apacket *p = get_apacket();
346 unsigned char *x = p->data;
347 size_t avail = MAX_PAYLOAD;
348 int r;
349 int is_eof = 0;
350
351 while(avail > 0) {
352 r = adb_read(fd, x, avail);
Elliott Hughesccecf142014-01-16 10:53:11 -0800353 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 -0800354 if(r > 0) {
355 avail -= r;
356 x += r;
357 continue;
358 }
359 if(r < 0) {
360 if(errno == EAGAIN) break;
361 if(errno == EINTR) continue;
362 }
363
364 /* r = 0 or unhandled error */
365 is_eof = 1;
366 break;
367 }
JP Abgrall408fa572011-03-16 15:57:42 -0700368 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
369 s->id, s->fd, r, is_eof, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
371 put_apacket(p);
372 } else {
373 p->len = MAX_PAYLOAD - avail;
374
375 r = s->peer->enqueue(s->peer, p);
JP Abgrall408fa572011-03-16 15:57:42 -0700376 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 -0800377
378 if(r < 0) {
379 /* error return means they closed us as a side-effect
380 ** and we must return immediately.
381 **
382 ** note that if we still have buffered packets, the
383 ** socket will be placed on the closing socket list.
384 ** this handler function will be called again
385 ** to process FDE_WRITE events.
386 */
387 return;
388 }
389
390 if(r > 0) {
391 /* if the remote cannot accept further events,
392 ** we disable notification of READs. They'll
393 ** be enabled again when we get a call to ready()
394 */
395 fdevent_del(&s->fde, FDE_READ);
396 }
397 }
JP Abgrall112445b2011-04-12 22:01:58 -0700398 /* Don't allow a forced eof if data is still there */
399 if((s->fde.force_eof && !r) || is_eof) {
Christopher Tate5b811fa2011-06-10 11:38:37 -0700400 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 -0800401 s->close(s);
402 }
403 }
404
405 if(ev & FDE_ERROR){
406 /* this should be caught be the next read or write
407 ** catching it here means we may skip the last few
408 ** bytes of readable data.
409 */
JP Abgrall408fa572011-03-16 15:57:42 -0700410 D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
411
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 return;
413 }
414}
415
416asocket *create_local_socket(int fd)
417{
418 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300419 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 s->fd = fd;
421 s->enqueue = local_socket_enqueue;
422 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100423 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700425 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426
427 fdevent_install(&s->fde, fd, local_socket_event_func, s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428 D("LS(%d): created (fd=%d)\n", s->id, s->fd);
429 return s;
430}
431
432asocket *create_local_service_socket(const char *name)
433{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434#if !ADB_HOST
435 if (!strcmp(name,"jdwp")) {
436 return create_jdwp_service_socket();
437 }
438 if (!strcmp(name,"track-jdwp")) {
439 return create_jdwp_tracker_service_socket();
440 }
441#endif
Dan Pasanen98858812014-10-06 12:57:20 -0500442 int fd = service_to_fd(name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 if(fd < 0) return 0;
444
Dan Pasanen98858812014-10-06 12:57:20 -0500445 asocket* s = create_local_socket(fd);
JP Abgrall408fa572011-03-16 15:57:42 -0700446 D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
Benoit Gobyf366b362012-03-16 14:50:07 -0700447
JP Abgrallf91259a2012-03-30 13:19:11 -0700448#if !ADB_HOST
Dan Pasanen98858812014-10-06 12:57:20 -0500449 char debug[PROPERTY_VALUE_MAX];
jzhuan51297d222013-05-24 17:40:15 -0400450 if (!strncmp(name, "root:", 5))
451 property_get("ro.debuggable", debug, "");
452
Dan Pasanen98858812014-10-06 12:57:20 -0500453 if ((!strncmp(name, "root:", 5) && getuid() != 0 && strcmp(debug, "1") == 0)
454 || (!strncmp(name, "unroot:", 7) && getuid() == 0)
Benoit Gobyaeceb512012-06-12 12:12:18 -0700455 || !strncmp(name, "usb:", 4)
456 || !strncmp(name, "tcpip:", 6)) {
Benoit Gobyf366b362012-03-16 14:50:07 -0700457 D("LS(%d): enabling exit_on_close\n", s->id);
458 s->exit_on_close = 1;
459 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700460#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700461
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 return s;
463}
464
465#if ADB_HOST
466static asocket *create_host_service_socket(const char *name, const char* serial)
467{
468 asocket *s;
469
470 s = host_service_to_socket(name, serial);
471
472 if (s != NULL) {
473 D("LS(%d) bound to '%s'\n", s->id, name);
474 return s;
475 }
476
477 return s;
478}
479#endif /* ADB_HOST */
480
481/* a Remote socket is used to send/receive data to/from a given transport object
482** it needs to be closed when the transport is forcibly destroyed by the user
483*/
484typedef struct aremotesocket {
485 asocket socket;
486 adisconnect disconnect;
487} aremotesocket;
488
489static int remote_socket_enqueue(asocket *s, apacket *p)
490{
JP Abgrall408fa572011-03-16 15:57:42 -0700491 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n",
492 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 p->msg.command = A_WRTE;
494 p->msg.arg0 = s->peer->id;
495 p->msg.arg1 = s->id;
496 p->msg.data_length = p->len;
497 send_packet(p, s->transport);
498 return 1;
499}
500
501static void remote_socket_ready(asocket *s)
502{
JP Abgrall408fa572011-03-16 15:57:42 -0700503 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n",
504 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 apacket *p = get_apacket();
506 p->msg.command = A_OKAY;
507 p->msg.arg0 = s->peer->id;
508 p->msg.arg1 = s->id;
509 send_packet(p, s->transport);
510}
511
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100512static void remote_socket_shutdown(asocket *s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513{
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100514 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d\n",
JP Abgrall408fa572011-03-16 15:57:42 -0700515 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 apacket *p = get_apacket();
517 p->msg.command = A_CLSE;
518 if(s->peer) {
519 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100520 }
521 p->msg.arg1 = s->id;
522 send_packet(p, s->transport);
523}
524
525static void remote_socket_close(asocket *s)
526{
527 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 s->peer->peer = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700529 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n",
530 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 s->peer->close(s->peer);
532 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100533 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n",
534 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 D("RS(%d): closed\n", s->id);
536 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
537 free(s);
538}
539
540static void remote_socket_disconnect(void* _s, atransport* t)
541{
542 asocket* s = _s;
543 asocket* peer = s->peer;
544
545 D("remote_socket_disconnect RS(%d)\n", s->id);
546 if (peer) {
547 peer->peer = NULL;
548 peer->close(peer);
549 }
550 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
551 free(s);
552}
553
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100554/* Create an asocket to exchange packets with a remote service through transport
555 |t|. Where |id| is the socket id of the corresponding service on the other
556 side of the transport (it is allocated by the remote side and _cannot_ be 0).
557 Returns a new non-NULL asocket handle. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558asocket *create_remote_socket(unsigned id, atransport *t)
559{
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100560 asocket* s;
561 adisconnect* dis;
562
563 if (id == 0) fatal("invalid remote socket id (0)");
564 s = calloc(1, sizeof(aremotesocket));
565 dis = &((aremotesocket*)s)->disconnect;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300567 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568 s->id = id;
569 s->enqueue = remote_socket_enqueue;
570 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100571 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800572 s->close = remote_socket_close;
573 s->transport = t;
574
575 dis->func = remote_socket_disconnect;
576 dis->opaque = s;
577 add_transport_disconnect( t, dis );
578 D("RS(%d): created\n", s->id);
579 return s;
580}
581
582void connect_to_remote(asocket *s, const char *destination)
583{
JP Abgrall408fa572011-03-16 15:57:42 -0700584 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585 apacket *p = get_apacket();
586 int len = strlen(destination) + 1;
587
588 if(len > (MAX_PAYLOAD-1)) {
589 fatal("destination oversized");
590 }
591
592 D("LS(%d): connect('%s')\n", s->id, destination);
593 p->msg.command = A_OPEN;
594 p->msg.arg0 = s->id;
595 p->msg.data_length = len;
596 strcpy((char*) p->data, destination);
597 send_packet(p, s->transport);
598}
599
600
601/* this is used by magic sockets to rig local sockets to
602 send the go-ahead message when they connect */
603static void local_socket_ready_notify(asocket *s)
604{
605 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100606 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607 s->close = local_socket_close;
608 adb_write(s->fd, "OKAY", 4);
609 s->ready(s);
610}
611
612/* this is used by magic sockets to rig local sockets to
613 send the failure message if they are closed before
614 connected (to avoid closing them without a status message) */
615static void local_socket_close_notify(asocket *s)
616{
617 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100618 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800619 s->close = local_socket_close;
620 sendfailmsg(s->fd, "closed");
621 s->close(s);
622}
623
624unsigned unhex(unsigned char *s, int len)
625{
626 unsigned n = 0, c;
627
628 while(len-- > 0) {
629 switch((c = *s++)) {
630 case '0': case '1': case '2':
631 case '3': case '4': case '5':
632 case '6': case '7': case '8':
633 case '9':
634 c -= '0';
635 break;
636 case 'a': case 'b': case 'c':
637 case 'd': case 'e': case 'f':
638 c = c - 'a' + 10;
639 break;
640 case 'A': case 'B': case 'C':
641 case 'D': case 'E': case 'F':
642 c = c - 'A' + 10;
643 break;
644 default:
645 return 0xffffffff;
646 }
647
648 n = (n << 4) | c;
649 }
650
651 return n;
652}
653
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700654#define PREFIX(str) { str, sizeof(str) - 1 }
655static const struct prefix_struct {
656 const char *str;
657 const size_t len;
658} prefixes[] = {
659 PREFIX("usb:"),
660 PREFIX("product:"),
661 PREFIX("model:"),
662 PREFIX("device:"),
663};
664static const int num_prefixes = (sizeof(prefixes) / sizeof(prefixes[0]));
665
Terence Haddock28e13902011-03-16 09:43:56 +0100666/* skip_host_serial return the position in a string
667 skipping over the 'serial' parameter in the ADB protocol,
668 where parameter string may be a host:port string containing
669 the protocol delimiter (colon). */
670char *skip_host_serial(char *service) {
671 char *first_colon, *serial_end;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700672 int i;
Terence Haddock28e13902011-03-16 09:43:56 +0100673
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700674 for (i = 0; i < num_prefixes; i++) {
675 if (!strncmp(service, prefixes[i].str, prefixes[i].len))
676 return strchr(service + prefixes[i].len, ':');
Scott Anderson3608d832012-05-31 12:04:23 -0700677 }
678
Terence Haddock28e13902011-03-16 09:43:56 +0100679 first_colon = strchr(service, ':');
680 if (!first_colon) {
681 /* No colon in service string. */
682 return NULL;
683 }
684 serial_end = first_colon;
685 if (isdigit(serial_end[1])) {
686 serial_end++;
687 while ((*serial_end) && isdigit(*serial_end)) {
688 serial_end++;
689 }
690 if ((*serial_end) != ':') {
691 // Something other than numbers was found, reset the end.
692 serial_end = first_colon;
693 }
694 }
695 return serial_end;
696}
697
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698static int smart_socket_enqueue(asocket *s, apacket *p)
699{
700 unsigned len;
701#if ADB_HOST
702 char *service = NULL;
703 char* serial = NULL;
704 transport_type ttype = kTransportAny;
705#endif
706
707 D("SS(%d): enqueue %d\n", s->id, p->len);
708
709 if(s->pkt_first == 0) {
710 s->pkt_first = p;
711 s->pkt_last = p;
712 } else {
713 if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
714 D("SS(%d): overflow\n", s->id);
715 put_apacket(p);
716 goto fail;
717 }
718
719 memcpy(s->pkt_first->data + s->pkt_first->len,
720 p->data, p->len);
721 s->pkt_first->len += p->len;
722 put_apacket(p);
723
724 p = s->pkt_first;
725 }
726
727 /* don't bother if we can't decode the length */
728 if(p->len < 4) return 0;
729
730 len = unhex(p->data, 4);
731 if((len < 1) || (len > 1024)) {
732 D("SS(%d): bad size (%d)\n", s->id, len);
733 goto fail;
734 }
735
736 D("SS(%d): len is %d\n", s->id, len );
737 /* can't do anything until we have the full header */
738 if((len + 4) > p->len) {
739 D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
740 return 0;
741 }
742
743 p->data[len + 4] = 0;
744
745 D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));
746
747#if ADB_HOST
748 service = (char *)p->data + 4;
749 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
750 char* serial_end;
751 service += strlen("host-serial:");
752
Terence Haddock28e13902011-03-16 09:43:56 +0100753 // serial number should follow "host:" and could be a host:port string.
754 serial_end = skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755 if (serial_end) {
756 *serial_end = 0; // terminate string
757 serial = service;
758 service = serial_end + 1;
759 }
760 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
761 ttype = kTransportUsb;
762 service += strlen("host-usb:");
763 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
764 ttype = kTransportLocal;
765 service += strlen("host-local:");
766 } else if (!strncmp(service, "host:", strlen("host:"))) {
767 ttype = kTransportAny;
768 service += strlen("host:");
769 } else {
770 service = NULL;
771 }
772
773 if (service) {
774 asocket *s2;
775
776 /* some requests are handled immediately -- in that
777 ** case the handle_host_request() routine has sent
778 ** the OKAY or FAIL message and all we have to do
779 ** is clean up.
780 */
781 if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
782 /* XXX fail message? */
783 D( "SS(%d): handled host service '%s'\n", s->id, service );
784 goto fail;
785 }
786 if (!strncmp(service, "transport", strlen("transport"))) {
787 D( "SS(%d): okay transport\n", s->id );
788 p->len = 0;
789 return 0;
790 }
791
792 /* try to find a local service with this name.
793 ** if no such service exists, we'll fail out
794 ** and tear down here.
795 */
796 s2 = create_host_service_socket(service, serial);
797 if(s2 == 0) {
798 D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
799 sendfailmsg(s->peer->fd, "unknown host service");
800 goto fail;
801 }
802
803 /* we've connected to a local host service,
804 ** so we make our peer back into a regular
805 ** local socket and bind it to the new local
806 ** service socket, acknowledge the successful
807 ** connection, and close this smart socket now
808 ** that its work is done.
809 */
810 adb_write(s->peer->fd, "OKAY", 4);
811
812 s->peer->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100813 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800814 s->peer->close = local_socket_close;
815 s->peer->peer = s2;
816 s2->peer = s->peer;
817 s->peer = 0;
818 D( "SS(%d): okay\n", s->id );
819 s->close(s);
820
821 /* initial state is "ready" */
822 s2->ready(s2);
823 return 0;
824 }
825#else /* !ADB_HOST */
826 if (s->transport == NULL) {
827 char* error_string = "unknown failure";
828 s->transport = acquire_one_transport (CS_ANY,
829 kTransportAny, NULL, &error_string);
830
831 if (s->transport == NULL) {
832 sendfailmsg(s->peer->fd, error_string);
833 goto fail;
834 }
835 }
836#endif
837
838 if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
839 /* if there's no remote we fail the connection
840 ** right here and terminate it
841 */
842 sendfailmsg(s->peer->fd, "device offline (x)");
843 goto fail;
844 }
845
846
847 /* instrument our peer to pass the success or fail
848 ** message back once it connects or closes, then
849 ** detach from it, request the connection, and
850 ** tear down
851 */
852 s->peer->ready = local_socket_ready_notify;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100853 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800854 s->peer->close = local_socket_close_notify;
855 s->peer->peer = 0;
856 /* give him our transport and upref it */
857 s->peer->transport = s->transport;
858
859 connect_to_remote(s->peer, (char*) (p->data + 4));
860 s->peer = 0;
861 s->close(s);
862 return 1;
863
864fail:
865 /* we're going to close our peer as a side-effect, so
866 ** return -1 to signal that state to the local socket
867 ** who is enqueueing against us
868 */
869 s->close(s);
870 return -1;
871}
872
873static void smart_socket_ready(asocket *s)
874{
875 D("SS(%d): ready\n", s->id);
876}
877
878static void smart_socket_close(asocket *s)
879{
880 D("SS(%d): closed\n", s->id);
881 if(s->pkt_first){
882 put_apacket(s->pkt_first);
883 }
884 if(s->peer) {
885 s->peer->peer = 0;
886 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500887 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800888 }
889 free(s);
890}
891
Benoit Goby9470c2f2013-02-20 15:04:53 -0800892static asocket *create_smart_socket(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800893{
894 D("Creating smart socket \n");
895 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300896 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800897 s->enqueue = smart_socket_enqueue;
898 s->ready = smart_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100899 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800900 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800901
Benoit Goby9470c2f2013-02-20 15:04:53 -0800902 D("SS(%d)\n", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800903 return s;
904}
905
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800906void connect_to_smartsocket(asocket *s)
907{
908 D("Connecting to smart socket \n");
Benoit Goby9470c2f2013-02-20 15:04:53 -0800909 asocket *ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800910 s->peer = ss;
911 ss->peer = s;
912 s->ready(s);
913}