blob: 3919147a34d9d11834c44aabd1eb3b1a75dab1f0 [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 Albert33134262015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_SOCKETS
18
19#include "sysdeps.h"
20
Dan Albert76649012015-02-24 15:51:19 -080021#include <ctype.h>
22#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <stdio.h>
24#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Josh Gao6f641ad2016-05-17 19:23:39 -070028#include <algorithm>
29#include <mutex>
30#include <string>
31#include <vector>
32
Dan Albert76649012015-02-24 15:51:19 -080033#if !ADB_HOST
34#include "cutils/properties.h"
35#endif
Dan Albert33134262015-03-19 15:21:08 -070036
37#include "adb.h"
38#include "adb_io.h"
Josh Gao6f641ad2016-05-17 19:23:39 -070039#include "sysdeps/mutex.h"
Dan Albert76649012015-02-24 15:51:19 -080040#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
Josh Gao6f641ad2016-05-17 19:23:39 -070042#if !defined(__BIONIC__)
43using std::recursive_mutex;
44#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Josh Gao6f641ad2016-05-17 19:23:39 -070046static recursive_mutex& local_socket_list_lock = *new recursive_mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047static unsigned local_socket_next_id = 1;
48
49static asocket local_socket_list = {
50 .next = &local_socket_list,
51 .prev = &local_socket_list,
52};
53
54/* the the list of currently closing local sockets.
55** these have no peer anymore, but still packets to
56** write to their fd.
57*/
58static asocket local_socket_closing_list = {
59 .next = &local_socket_closing_list,
60 .prev = &local_socket_closing_list,
61};
62
David 'Digit' Turner818d6412013-12-13 14:09:44 +010063// Parse the global list of sockets to find one with id |local_id|.
64// If |peer_id| is not 0, also check that it is connected to a peer
65// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
66asocket *find_local_socket(unsigned local_id, unsigned peer_id)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067{
68 asocket *s;
69 asocket *result = NULL;
70
Josh Gao6f641ad2016-05-17 19:23:39 -070071 std::lock_guard<recursive_mutex> lock(local_socket_list_lock);
André Goddard Rosa81828292010-06-12 11:40:20 -030072 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +010073 if (s->id != local_id)
74 continue;
75 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa81828292010-06-12 11:40:20 -030076 result = s;
André Goddard Rosa81828292010-06-12 11:40:20 -030077 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010078 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
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
Josh Gao6f641ad2016-05-17 19:23:39 -070093void install_local_socket(asocket* s) {
94 std::lock_guard<recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095
96 s->id = local_socket_next_id++;
David 'Digit' Turner818d6412013-12-13 14:09:44 +010097
98 // Socket ids should never be 0.
Josh Gao6f641ad2016-05-17 19:23:39 -070099 if (local_socket_next_id == 0) {
100 fatal("local socket id overflow");
101 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100102
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 insert_local_socket(s, &local_socket_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104}
105
106void remove_socket(asocket *s)
107{
108 // socket_list_lock should already be held
109 if (s->prev && s->next)
110 {
111 s->prev->next = s->next;
112 s->next->prev = s->prev;
113 s->next = 0;
114 s->prev = 0;
115 s->id = 0;
116 }
117}
118
119void close_all_sockets(atransport *t)
120{
121 asocket *s;
Josh Gao6f641ad2016-05-17 19:23:39 -0700122 /* this is a little gross, but since s->close() *will* modify
123 ** the list out from under you, your options are limited.
124 */
125 std::lock_guard<recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126restart:
Josh Gao6f641ad2016-05-17 19:23:39 -0700127 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
128 if (s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gaof71c0142016-05-18 10:39:48 -0700129 s->close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 goto restart;
131 }
132 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133}
134
135static int local_socket_enqueue(asocket *s, apacket *p)
136{
137 D("LS(%d): enqueue %d\n", s->id, p->len);
138
139 p->ptr = p->data;
140
141 /* if there is already data queue'd, we will receive
142 ** events when it's time to write. just add this to
143 ** the tail
144 */
145 if(s->pkt_first) {
146 goto enqueue;
147 }
148
149 /* write as much as we can, until we
150 ** would block or there is an error/eof
151 */
152 while(p->len > 0) {
153 int r = adb_write(s->fd, p->ptr, p->len);
154 if(r > 0) {
155 p->len -= r;
156 p->ptr += r;
157 continue;
158 }
159 if((r == 0) || (errno != EAGAIN)) {
160 D( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) );
161 s->close(s);
162 return 1; /* not ready (error) */
163 } else {
164 break;
165 }
166 }
167
168 if(p->len == 0) {
169 put_apacket(p);
170 return 0; /* ready for more data */
171 }
172
173enqueue:
174 p->next = 0;
175 if(s->pkt_first) {
176 s->pkt_last->next = p;
177 } else {
178 s->pkt_first = p;
179 }
180 s->pkt_last = p;
181
182 /* make sure we are notified when we can drain the queue */
183 fdevent_add(&s->fde, FDE_WRITE);
184
185 return 1; /* not ready (backlog) */
186}
187
188static void local_socket_ready(asocket *s)
189{
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100190 /* far side is ready for data, pay attention to
191 readable events */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 fdevent_add(&s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193}
194
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195// be sure to hold the socket list lock when calling this
196static void local_socket_destroy(asocket *s)
197{
198 apacket *p, *n;
Benoit Gobyf366b362012-03-16 14:50:07 -0700199 int exit_on_close = s->exit_on_close;
200
JP Abgrall408fa572011-03-16 15:57:42 -0700201 D("LS(%d): destroying fde.fd=%d\n", s->id, s->fde.fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202
203 /* IMPORTANT: the remove closes the fd
204 ** that belongs to this socket
205 */
206 fdevent_remove(&s->fde);
207
208 /* dispose of any unwritten data */
209 for(p = s->pkt_first; p; p = n) {
210 D("LS(%d): discarding %d bytes\n", s->id, p->len);
211 n = p->next;
212 put_apacket(p);
213 }
214 remove_socket(s);
215 free(s);
Benoit Gobyf366b362012-03-16 14:50:07 -0700216
217 if (exit_on_close) {
218 D("local_socket_destroy: exiting\n");
219 exit(1);
220 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221}
222
Josh Gao6f641ad2016-05-17 19:23:39 -0700223static void local_socket_close(asocket* s) {
224 D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd);
225 std::lock_guard<recursive_mutex> lock(local_socket_list_lock);
226 if (s->peer) {
227 D("LS(%d): closing peer. peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100228 /* Note: it's important to call shutdown before disconnecting from
229 * the peer, this ensures that remote sockets can still get the id
230 * of the local socket they're connected to, to send a CLOSE()
231 * protocol event. */
Josh Gao6f641ad2016-05-17 19:23:39 -0700232 if (s->peer->shutdown) {
233 s->peer->shutdown(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500234 }
Josh Gao6f641ad2016-05-17 19:23:39 -0700235 s->peer->peer = nullptr;
236 s->peer->close(s->peer);
237 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 }
239
240 /* If we are already closing, or if there are no
241 ** pending packets, destroy immediately
242 */
243 if (s->closing || s->pkt_first == NULL) {
244 int id = s->id;
245 local_socket_destroy(s);
246 D("LS(%d): closed\n", id);
247 return;
248 }
249
250 /* otherwise, put on the closing list
251 */
252 D("LS(%d): closing\n", s->id);
253 s->closing = 1;
254 fdevent_del(&s->fde, FDE_READ);
255 remove_socket(s);
JP Abgrall408fa572011-03-16 15:57:42 -0700256 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 -0800257 insert_local_socket(s, &local_socket_closing_list);
258}
259
Dan Albertbac34742015-02-25 17:51:28 -0800260static void local_socket_event_func(int fd, unsigned ev, void* _s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261{
Dan Albertbac34742015-02-25 17:51:28 -0800262 asocket* s = reinterpret_cast<asocket*>(_s);
JP Abgrall408fa572011-03-16 15:57:42 -0700263 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);
264
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 /* put the FDE_WRITE processing before the FDE_READ
266 ** in order to simplify the code.
267 */
Dan Albertbac34742015-02-25 17:51:28 -0800268 if (ev & FDE_WRITE) {
269 apacket* p;
270 while ((p = s->pkt_first) != nullptr) {
271 while (p->len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 int r = adb_write(fd, p->ptr, p->len);
Dan Albertbac34742015-02-25 17:51:28 -0800273 if (r == -1) {
274 /* returning here is ok because FDE_READ will
275 ** be processed in the next iteration loop
276 */
277 if (errno == EAGAIN) {
278 return;
279 }
280 } else if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 p->ptr += r;
282 p->len -= r;
283 continue;
284 }
Dan Albertbac34742015-02-25 17:51:28 -0800285
Christopher Tate5b811fa2011-06-10 11:38:37 -0700286 D(" closing after write because r=%d and errno is %d\n", r, errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 s->close(s);
288 return;
289 }
290
Dan Albertbac34742015-02-25 17:51:28 -0800291 if (p->len == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 s->pkt_first = p->next;
Dan Albertbac34742015-02-25 17:51:28 -0800293 if (s->pkt_first == 0) {
294 s->pkt_last = 0;
295 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 put_apacket(p);
297 }
298 }
299
Dan Albertbac34742015-02-25 17:51:28 -0800300 /* if we sent the last packet of a closing socket,
301 ** we can now destroy it.
302 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 if (s->closing) {
Christopher Tate5b811fa2011-06-10 11:38:37 -0700304 D(" closing because 'closing' is set after write\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 s->close(s);
306 return;
307 }
308
Dan Albertbac34742015-02-25 17:51:28 -0800309 /* no more packets queued, so we can ignore
310 ** writable events again and tell our peer
311 ** to resume writing
312 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 fdevent_del(&s->fde, FDE_WRITE);
314 s->peer->ready(s->peer);
315 }
316
317
Dan Albertbac34742015-02-25 17:51:28 -0800318 if (ev & FDE_READ) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 apacket *p = get_apacket();
320 unsigned char *x = p->data;
321 size_t avail = MAX_PAYLOAD;
322 int r;
323 int is_eof = 0;
324
Dan Albertbac34742015-02-25 17:51:28 -0800325 while (avail > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 r = adb_read(fd, x, avail);
Dan Albertbac34742015-02-25 17:51:28 -0800327 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n",
328 s->id, s->fd, r, r < 0 ? errno : 0, avail);
329 if (r == -1) {
330 if (errno == EAGAIN) {
331 break;
332 }
333 } else if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 avail -= r;
335 x += r;
336 continue;
337 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338
Dan Albertbac34742015-02-25 17:51:28 -0800339 /* r = 0 or unhandled error */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 is_eof = 1;
341 break;
342 }
JP Abgrall408fa572011-03-16 15:57:42 -0700343 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
344 s->id, s->fd, r, is_eof, s->fde.force_eof);
Dan Albertbac34742015-02-25 17:51:28 -0800345 if ((avail == MAX_PAYLOAD) || (s->peer == 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 put_apacket(p);
347 } else {
348 p->len = MAX_PAYLOAD - avail;
349
350 r = s->peer->enqueue(s->peer, p);
Dan Albertbac34742015-02-25 17:51:28 -0800351 D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd,
352 r);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353
Dan Albertbac34742015-02-25 17:51:28 -0800354 if (r < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 /* error return means they closed us as a side-effect
356 ** and we must return immediately.
357 **
358 ** note that if we still have buffered packets, the
359 ** socket will be placed on the closing socket list.
360 ** this handler function will be called again
361 ** to process FDE_WRITE events.
362 */
363 return;
364 }
365
Dan Albertbac34742015-02-25 17:51:28 -0800366 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 /* if the remote cannot accept further events,
368 ** we disable notification of READs. They'll
369 ** be enabled again when we get a call to ready()
370 */
371 fdevent_del(&s->fde, FDE_READ);
372 }
373 }
JP Abgrall112445b2011-04-12 22:01:58 -0700374 /* Don't allow a forced eof if data is still there */
Dan Albertbac34742015-02-25 17:51:28 -0800375 if ((s->fde.force_eof && !r) || is_eof) {
376 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n",
377 is_eof, r, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 s->close(s);
379 }
380 }
381
Dan Albertbac34742015-02-25 17:51:28 -0800382 if (ev & FDE_ERROR){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383 /* this should be caught be the next read or write
384 ** catching it here means we may skip the last few
385 ** bytes of readable data.
386 */
JP Abgrall408fa572011-03-16 15:57:42 -0700387 D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
388
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 return;
390 }
391}
392
393asocket *create_local_socket(int fd)
394{
Dan Albertbac34742015-02-25 17:51:28 -0800395 asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300396 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 s->fd = fd;
398 s->enqueue = local_socket_enqueue;
399 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100400 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700402 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403
404 fdevent_install(&s->fde, fd, local_socket_event_func, s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 D("LS(%d): created (fd=%d)\n", s->id, s->fd);
406 return s;
407}
408
409asocket *create_local_service_socket(const char *name)
410{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411#if !ADB_HOST
412 if (!strcmp(name,"jdwp")) {
413 return create_jdwp_service_socket();
414 }
415 if (!strcmp(name,"track-jdwp")) {
416 return create_jdwp_tracker_service_socket();
417 }
418#endif
Dan Pasanen98858812014-10-06 12:57:20 -0500419 int fd = service_to_fd(name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 if(fd < 0) return 0;
421
Dan Pasanen98858812014-10-06 12:57:20 -0500422 asocket* s = create_local_socket(fd);
JP Abgrall408fa572011-03-16 15:57:42 -0700423 D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
Benoit Gobyf366b362012-03-16 14:50:07 -0700424
JP Abgrallf91259a2012-03-30 13:19:11 -0700425#if !ADB_HOST
Dan Pasanen98858812014-10-06 12:57:20 -0500426 char debug[PROPERTY_VALUE_MAX];
jzhuan51297d222013-05-24 17:40:15 -0400427 if (!strncmp(name, "root:", 5))
428 property_get("ro.debuggable", debug, "");
429
Dan Pasanen98858812014-10-06 12:57:20 -0500430 if ((!strncmp(name, "root:", 5) && getuid() != 0 && strcmp(debug, "1") == 0)
431 || (!strncmp(name, "unroot:", 7) && getuid() == 0)
Benoit Gobyaeceb512012-06-12 12:12:18 -0700432 || !strncmp(name, "usb:", 4)
433 || !strncmp(name, "tcpip:", 6)) {
Benoit Gobyf366b362012-03-16 14:50:07 -0700434 D("LS(%d): enabling exit_on_close\n", s->id);
435 s->exit_on_close = 1;
436 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700437#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700438
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 return s;
440}
441
442#if ADB_HOST
443static asocket *create_host_service_socket(const char *name, const char* serial)
444{
445 asocket *s;
446
447 s = host_service_to_socket(name, serial);
448
449 if (s != NULL) {
450 D("LS(%d) bound to '%s'\n", s->id, name);
451 return s;
452 }
453
454 return s;
455}
456#endif /* ADB_HOST */
457
458/* a Remote socket is used to send/receive data to/from a given transport object
459** it needs to be closed when the transport is forcibly destroyed by the user
460*/
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700461struct aremotesocket {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 asocket socket;
463 adisconnect disconnect;
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700464};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465
466static int remote_socket_enqueue(asocket *s, apacket *p)
467{
JP Abgrall408fa572011-03-16 15:57:42 -0700468 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n",
469 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 p->msg.command = A_WRTE;
471 p->msg.arg0 = s->peer->id;
472 p->msg.arg1 = s->id;
473 p->msg.data_length = p->len;
474 send_packet(p, s->transport);
475 return 1;
476}
477
478static void remote_socket_ready(asocket *s)
479{
JP Abgrall408fa572011-03-16 15:57:42 -0700480 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n",
481 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 apacket *p = get_apacket();
483 p->msg.command = A_OKAY;
484 p->msg.arg0 = s->peer->id;
485 p->msg.arg1 = s->id;
486 send_packet(p, s->transport);
487}
488
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100489static void remote_socket_shutdown(asocket *s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490{
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100491 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d\n",
JP Abgrall408fa572011-03-16 15:57:42 -0700492 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 apacket *p = get_apacket();
494 p->msg.command = A_CLSE;
495 if(s->peer) {
496 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100497 }
498 p->msg.arg1 = s->id;
499 send_packet(p, s->transport);
500}
501
502static void remote_socket_close(asocket *s)
503{
504 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 s->peer->peer = 0;
JP Abgrall408fa572011-03-16 15:57:42 -0700506 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n",
507 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 s->peer->close(s->peer);
509 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100510 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n",
511 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 D("RS(%d): closed\n", s->id);
513 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
514 free(s);
515}
516
517static void remote_socket_disconnect(void* _s, atransport* t)
518{
Dan Albertbac34742015-02-25 17:51:28 -0800519 asocket* s = reinterpret_cast<asocket*>(_s);
520 asocket* peer = s->peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521
522 D("remote_socket_disconnect RS(%d)\n", s->id);
523 if (peer) {
524 peer->peer = NULL;
525 peer->close(peer);
526 }
527 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
528 free(s);
529}
530
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100531/* Create an asocket to exchange packets with a remote service through transport
532 |t|. Where |id| is the socket id of the corresponding service on the other
533 side of the transport (it is allocated by the remote side and _cannot_ be 0).
534 Returns a new non-NULL asocket handle. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535asocket *create_remote_socket(unsigned id, atransport *t)
536{
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100537 if (id == 0) fatal("invalid remote socket id (0)");
Dan Albertbac34742015-02-25 17:51:28 -0800538 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(aremotesocket)));
539 adisconnect* dis = &reinterpret_cast<aremotesocket*>(s)->disconnect;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300541 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 s->id = id;
543 s->enqueue = remote_socket_enqueue;
544 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100545 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546 s->close = remote_socket_close;
547 s->transport = t;
548
549 dis->func = remote_socket_disconnect;
550 dis->opaque = s;
551 add_transport_disconnect( t, dis );
552 D("RS(%d): created\n", s->id);
553 return s;
554}
555
556void connect_to_remote(asocket *s, const char *destination)
557{
JP Abgrall408fa572011-03-16 15:57:42 -0700558 D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800559 apacket *p = get_apacket();
560 int len = strlen(destination) + 1;
561
562 if(len > (MAX_PAYLOAD-1)) {
563 fatal("destination oversized");
564 }
565
566 D("LS(%d): connect('%s')\n", s->id, destination);
567 p->msg.command = A_OPEN;
568 p->msg.arg0 = s->id;
569 p->msg.data_length = len;
570 strcpy((char*) p->data, destination);
571 send_packet(p, s->transport);
572}
573
574
575/* this is used by magic sockets to rig local sockets to
576 send the go-ahead message when they connect */
577static void local_socket_ready_notify(asocket *s)
578{
579 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100580 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581 s->close = local_socket_close;
Elliott Hughes92af7332015-04-30 17:32:03 -0700582 SendOkay(s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800583 s->ready(s);
584}
585
586/* this is used by magic sockets to rig local sockets to
587 send the failure message if they are closed before
588 connected (to avoid closing them without a status message) */
589static void local_socket_close_notify(asocket *s)
590{
591 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100592 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593 s->close = local_socket_close;
Elliott Hughes92af7332015-04-30 17:32:03 -0700594 SendFail(s->fd, "closed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595 s->close(s);
596}
597
Elliott Hughes92af7332015-04-30 17:32:03 -0700598static unsigned unhex(unsigned char *s, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599{
600 unsigned n = 0, c;
601
602 while(len-- > 0) {
603 switch((c = *s++)) {
604 case '0': case '1': case '2':
605 case '3': case '4': case '5':
606 case '6': case '7': case '8':
607 case '9':
608 c -= '0';
609 break;
610 case 'a': case 'b': case 'c':
611 case 'd': case 'e': case 'f':
612 c = c - 'a' + 10;
613 break;
614 case 'A': case 'B': case 'C':
615 case 'D': case 'E': case 'F':
616 c = c - 'A' + 10;
617 break;
618 default:
619 return 0xffffffff;
620 }
621
622 n = (n << 4) | c;
623 }
624
625 return n;
626}
627
Elliott Hughes92af7332015-04-30 17:32:03 -0700628#if ADB_HOST
629
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700630#define PREFIX(str) { str, sizeof(str) - 1 }
631static const struct prefix_struct {
632 const char *str;
633 const size_t len;
634} prefixes[] = {
635 PREFIX("usb:"),
636 PREFIX("product:"),
637 PREFIX("model:"),
638 PREFIX("device:"),
639};
640static const int num_prefixes = (sizeof(prefixes) / sizeof(prefixes[0]));
641
Terence Haddock28e13902011-03-16 09:43:56 +0100642/* skip_host_serial return the position in a string
643 skipping over the 'serial' parameter in the ADB protocol,
644 where parameter string may be a host:port string containing
645 the protocol delimiter (colon). */
Elliott Hughes92af7332015-04-30 17:32:03 -0700646static char *skip_host_serial(char *service) {
Terence Haddock28e13902011-03-16 09:43:56 +0100647 char *first_colon, *serial_end;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700648 int i;
Terence Haddock28e13902011-03-16 09:43:56 +0100649
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700650 for (i = 0; i < num_prefixes; i++) {
651 if (!strncmp(service, prefixes[i].str, prefixes[i].len))
652 return strchr(service + prefixes[i].len, ':');
Scott Anderson3608d832012-05-31 12:04:23 -0700653 }
654
Terence Haddock28e13902011-03-16 09:43:56 +0100655 first_colon = strchr(service, ':');
656 if (!first_colon) {
657 /* No colon in service string. */
658 return NULL;
659 }
660 serial_end = first_colon;
661 if (isdigit(serial_end[1])) {
662 serial_end++;
663 while ((*serial_end) && isdigit(*serial_end)) {
664 serial_end++;
665 }
666 if ((*serial_end) != ':') {
667 // Something other than numbers was found, reset the end.
668 serial_end = first_colon;
669 }
670 }
671 return serial_end;
672}
673
Elliott Hughes92af7332015-04-30 17:32:03 -0700674#endif // ADB_HOST
675
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676static int smart_socket_enqueue(asocket *s, apacket *p)
677{
678 unsigned len;
679#if ADB_HOST
680 char *service = NULL;
681 char* serial = NULL;
682 transport_type ttype = kTransportAny;
683#endif
684
685 D("SS(%d): enqueue %d\n", s->id, p->len);
686
687 if(s->pkt_first == 0) {
688 s->pkt_first = p;
689 s->pkt_last = p;
690 } else {
691 if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
692 D("SS(%d): overflow\n", s->id);
693 put_apacket(p);
694 goto fail;
695 }
696
697 memcpy(s->pkt_first->data + s->pkt_first->len,
698 p->data, p->len);
699 s->pkt_first->len += p->len;
700 put_apacket(p);
701
702 p = s->pkt_first;
703 }
704
705 /* don't bother if we can't decode the length */
706 if(p->len < 4) return 0;
707
708 len = unhex(p->data, 4);
709 if((len < 1) || (len > 1024)) {
710 D("SS(%d): bad size (%d)\n", s->id, len);
711 goto fail;
712 }
713
714 D("SS(%d): len is %d\n", s->id, len );
715 /* can't do anything until we have the full header */
716 if((len + 4) > p->len) {
717 D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
718 return 0;
719 }
720
721 p->data[len + 4] = 0;
722
723 D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));
724
725#if ADB_HOST
726 service = (char *)p->data + 4;
727 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
728 char* serial_end;
729 service += strlen("host-serial:");
730
Terence Haddock28e13902011-03-16 09:43:56 +0100731 // serial number should follow "host:" and could be a host:port string.
732 serial_end = skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800733 if (serial_end) {
734 *serial_end = 0; // terminate string
735 serial = service;
736 service = serial_end + 1;
737 }
738 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
739 ttype = kTransportUsb;
740 service += strlen("host-usb:");
741 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
742 ttype = kTransportLocal;
743 service += strlen("host-local:");
744 } else if (!strncmp(service, "host:", strlen("host:"))) {
745 ttype = kTransportAny;
746 service += strlen("host:");
747 } else {
748 service = NULL;
749 }
750
751 if (service) {
752 asocket *s2;
753
754 /* some requests are handled immediately -- in that
755 ** case the handle_host_request() routine has sent
756 ** the OKAY or FAIL message and all we have to do
757 ** is clean up.
758 */
759 if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
760 /* XXX fail message? */
761 D( "SS(%d): handled host service '%s'\n", s->id, service );
762 goto fail;
763 }
764 if (!strncmp(service, "transport", strlen("transport"))) {
765 D( "SS(%d): okay transport\n", s->id );
766 p->len = 0;
767 return 0;
768 }
769
770 /* try to find a local service with this name.
771 ** if no such service exists, we'll fail out
772 ** and tear down here.
773 */
774 s2 = create_host_service_socket(service, serial);
775 if(s2 == 0) {
776 D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
Elliott Hughes92af7332015-04-30 17:32:03 -0700777 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778 goto fail;
779 }
780
781 /* we've connected to a local host service,
782 ** so we make our peer back into a regular
783 ** local socket and bind it to the new local
784 ** service socket, acknowledge the successful
785 ** connection, and close this smart socket now
786 ** that its work is done.
787 */
Elliott Hughes92af7332015-04-30 17:32:03 -0700788 SendOkay(s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800789
790 s->peer->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100791 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792 s->peer->close = local_socket_close;
793 s->peer->peer = s2;
794 s2->peer = s->peer;
795 s->peer = 0;
796 D( "SS(%d): okay\n", s->id );
797 s->close(s);
798
799 /* initial state is "ready" */
800 s2->ready(s2);
801 return 0;
802 }
803#else /* !ADB_HOST */
804 if (s->transport == NULL) {
Elliott Hughes7be29c82015-04-16 22:54:44 -0700805 std::string error_msg = "unknown failure";
806 s->transport = acquire_one_transport(CS_ANY, kTransportAny, NULL, &error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807
808 if (s->transport == NULL) {
Elliott Hughes92af7332015-04-30 17:32:03 -0700809 SendFail(s->peer->fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800810 goto fail;
811 }
812 }
813#endif
814
815 if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
816 /* if there's no remote we fail the connection
817 ** right here and terminate it
818 */
Elliott Hughes92af7332015-04-30 17:32:03 -0700819 SendFail(s->peer->fd, "device offline (x)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800820 goto fail;
821 }
822
823
824 /* instrument our peer to pass the success or fail
825 ** message back once it connects or closes, then
826 ** detach from it, request the connection, and
827 ** tear down
828 */
829 s->peer->ready = local_socket_ready_notify;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100830 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800831 s->peer->close = local_socket_close_notify;
832 s->peer->peer = 0;
833 /* give him our transport and upref it */
834 s->peer->transport = s->transport;
835
836 connect_to_remote(s->peer, (char*) (p->data + 4));
837 s->peer = 0;
838 s->close(s);
839 return 1;
840
841fail:
842 /* we're going to close our peer as a side-effect, so
843 ** return -1 to signal that state to the local socket
844 ** who is enqueueing against us
845 */
846 s->close(s);
847 return -1;
848}
849
850static void smart_socket_ready(asocket *s)
851{
852 D("SS(%d): ready\n", s->id);
853}
854
855static void smart_socket_close(asocket *s)
856{
857 D("SS(%d): closed\n", s->id);
858 if(s->pkt_first){
859 put_apacket(s->pkt_first);
860 }
861 if(s->peer) {
862 s->peer->peer = 0;
863 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500864 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800865 }
866 free(s);
867}
868
Benoit Goby9470c2f2013-02-20 15:04:53 -0800869static asocket *create_smart_socket(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800870{
871 D("Creating smart socket \n");
Dan Albertbac34742015-02-25 17:51:28 -0800872 asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300873 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800874 s->enqueue = smart_socket_enqueue;
875 s->ready = smart_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100876 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800877 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800878
Benoit Goby9470c2f2013-02-20 15:04:53 -0800879 D("SS(%d)\n", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800880 return s;
881}
882
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800883void connect_to_smartsocket(asocket *s)
884{
885 D("Connecting to smart socket \n");
Benoit Goby9470c2f2013-02-20 15:04:53 -0800886 asocket *ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800887 s->peer = ss;
888 ss->peer = s;
889 s->ready(s);
890}