blob: aecaba22416755853965400048ccd68ce81c5045 [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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG SOCKETS
Dan Albert33134262015-03-19 15:21:08 -070018
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
Spencer Low363af562015-11-07 18:51:54 -080028#include <algorithm>
David Pursell3f902aa2016-03-01 08:58:26 -080029#include <string>
30#include <vector>
Spencer Low363af562015-11-07 18:51:54 -080031
Dan Albert76649012015-02-24 15:51:19 -080032#if !ADB_HOST
33#include "cutils/properties.h"
34#endif
Dan Albert33134262015-03-19 15:21:08 -070035
36#include "adb.h"
37#include "adb_io.h"
Dan Albert76649012015-02-24 15:51:19 -080038#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
40ADB_MUTEX_DEFINE( socket_list_lock );
41
42static void local_socket_close_locked(asocket *s);
43
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044static unsigned local_socket_next_id = 1;
45
46static asocket local_socket_list = {
47 .next = &local_socket_list,
48 .prev = &local_socket_list,
49};
50
51/* the the list of currently closing local sockets.
52** these have no peer anymore, but still packets to
53** write to their fd.
54*/
55static asocket local_socket_closing_list = {
56 .next = &local_socket_closing_list,
57 .prev = &local_socket_closing_list,
58};
59
David 'Digit' Turner818d6412013-12-13 14:09:44 +010060// Parse the global list of sockets to find one with id |local_id|.
61// If |peer_id| is not 0, also check that it is connected to a peer
62// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
63asocket *find_local_socket(unsigned local_id, unsigned peer_id)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064{
65 asocket *s;
66 asocket *result = NULL;
67
68 adb_mutex_lock(&socket_list_lock);
André Goddard Rosa81828292010-06-12 11:40:20 -030069 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +010070 if (s->id != local_id)
71 continue;
72 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa81828292010-06-12 11:40:20 -030073 result = s;
André Goddard Rosa81828292010-06-12 11:40:20 -030074 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010075 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076 }
77 adb_mutex_unlock(&socket_list_lock);
78
79 return result;
80}
81
82static void
83insert_local_socket(asocket* s, asocket* list)
84{
85 s->next = list;
86 s->prev = s->next->prev;
87 s->prev->next = s;
88 s->next->prev = s;
89}
90
91
92void install_local_socket(asocket *s)
93{
94 adb_mutex_lock(&socket_list_lock);
95
96 s->id = local_socket_next_id++;
David 'Digit' Turner818d6412013-12-13 14:09:44 +010097
98 // Socket ids should never be 0.
99 if (local_socket_next_id == 0)
100 local_socket_next_id = 1;
101
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 insert_local_socket(s, &local_socket_list);
103
104 adb_mutex_unlock(&socket_list_lock);
105}
106
107void remove_socket(asocket *s)
108{
109 // socket_list_lock should already be held
110 if (s->prev && s->next)
111 {
112 s->prev->next = s->next;
113 s->next->prev = s->prev;
114 s->next = 0;
115 s->prev = 0;
116 s->id = 0;
117 }
118}
119
120void close_all_sockets(atransport *t)
121{
122 asocket *s;
123
124 /* this is a little gross, but since s->close() *will* modify
125 ** the list out from under you, your options are limited.
126 */
127 adb_mutex_lock(&socket_list_lock);
128restart:
129 for(s = local_socket_list.next; s != &local_socket_list; s = s->next){
130 if(s->transport == t || (s->peer && s->peer->transport == t)) {
131 local_socket_close_locked(s);
132 goto restart;
133 }
134 }
135 adb_mutex_unlock(&socket_list_lock);
136}
137
138static int local_socket_enqueue(asocket *s, apacket *p)
139{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700140 D("LS(%d): enqueue %d", s->id, p->len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141
142 p->ptr = p->data;
143
144 /* if there is already data queue'd, we will receive
145 ** events when it's time to write. just add this to
146 ** the tail
147 */
148 if(s->pkt_first) {
149 goto enqueue;
150 }
151
152 /* write as much as we can, until we
153 ** would block or there is an error/eof
154 */
155 while(p->len > 0) {
156 int r = adb_write(s->fd, p->ptr, p->len);
157 if(r > 0) {
158 p->len -= r;
159 p->ptr += r;
160 continue;
161 }
162 if((r == 0) || (errno != EAGAIN)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700163 D( "LS(%d): not ready, errno=%d: %s", s->id, errno, strerror(errno) );
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700164 put_apacket(p);
165 s->has_write_error = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 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{
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100195 /* far side is ready for data, pay attention to
196 readable events */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 fdevent_add(&s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198}
199
200static void local_socket_close(asocket *s)
201{
202 adb_mutex_lock(&socket_list_lock);
203 local_socket_close_locked(s);
204 adb_mutex_unlock(&socket_list_lock);
205}
206
207// be sure to hold the socket list lock when calling this
208static void local_socket_destroy(asocket *s)
209{
210 apacket *p, *n;
Benoit Gobyf366b362012-03-16 14:50:07 -0700211 int exit_on_close = s->exit_on_close;
212
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700213 D("LS(%d): destroying fde.fd=%d", s->id, s->fde.fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214
215 /* IMPORTANT: the remove closes the fd
216 ** that belongs to this socket
217 */
218 fdevent_remove(&s->fde);
219
220 /* dispose of any unwritten data */
221 for(p = s->pkt_first; p; p = n) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700222 D("LS(%d): discarding %d bytes", s->id, p->len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 n = p->next;
224 put_apacket(p);
225 }
226 remove_socket(s);
227 free(s);
Benoit Gobyf366b362012-03-16 14:50:07 -0700228
229 if (exit_on_close) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700230 D("local_socket_destroy: exiting");
Benoit Gobyf366b362012-03-16 14:50:07 -0700231 exit(1);
232 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233}
234
235
236static void local_socket_close_locked(asocket *s)
237{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700238 D("entered local_socket_close_locked. LS(%d) fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 if(s->peer) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700240 D("LS(%d): closing peer. peer->id=%d peer->fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700241 s->id, s->peer->id, s->peer->fd);
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100242 /* Note: it's important to call shutdown before disconnecting from
243 * the peer, this ensures that remote sockets can still get the id
244 * of the local socket they're connected to, to send a CLOSE()
245 * protocol event. */
246 if (s->peer->shutdown)
247 s->peer->shutdown(s->peer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 s->peer->peer = 0;
249 // tweak to avoid deadlock
Tom Marlin49f18572011-05-13 13:24:55 -0500250 if (s->peer->close == local_socket_close) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 local_socket_close_locked(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500252 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500254 }
255 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 }
257
258 /* If we are already closing, or if there are no
259 ** pending packets, destroy immediately
260 */
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700261 if (s->closing || s->has_write_error || s->pkt_first == NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 int id = s->id;
263 local_socket_destroy(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700264 D("LS(%d): closed", id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 return;
266 }
267
268 /* otherwise, put on the closing list
269 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700270 D("LS(%d): closing", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 s->closing = 1;
272 fdevent_del(&s->fde, FDE_READ);
273 remove_socket(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700274 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 insert_local_socket(s, &local_socket_closing_list);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700276 CHECK_EQ(FDE_WRITE, s->fde.state & FDE_WRITE);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277}
278
Dan Albertbac34742015-02-25 17:51:28 -0800279static void local_socket_event_func(int fd, unsigned ev, void* _s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280{
Dan Albertbac34742015-02-25 17:51:28 -0800281 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700282 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall408fa572011-03-16 15:57:42 -0700283
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 /* put the FDE_WRITE processing before the FDE_READ
285 ** in order to simplify the code.
286 */
Dan Albertbac34742015-02-25 17:51:28 -0800287 if (ev & FDE_WRITE) {
288 apacket* p;
289 while ((p = s->pkt_first) != nullptr) {
290 while (p->len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 int r = adb_write(fd, p->ptr, p->len);
Dan Albertbac34742015-02-25 17:51:28 -0800292 if (r == -1) {
293 /* returning here is ok because FDE_READ will
294 ** be processed in the next iteration loop
295 */
296 if (errno == EAGAIN) {
297 return;
298 }
299 } else if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 p->ptr += r;
301 p->len -= r;
302 continue;
303 }
Dan Albertbac34742015-02-25 17:51:28 -0800304
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700305 D(" closing after write because r=%d and errno is %d", r, errno);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700306 s->has_write_error = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 s->close(s);
308 return;
309 }
310
Dan Albertbac34742015-02-25 17:51:28 -0800311 if (p->len == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 s->pkt_first = p->next;
Dan Albertbac34742015-02-25 17:51:28 -0800313 if (s->pkt_first == 0) {
314 s->pkt_last = 0;
315 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 put_apacket(p);
317 }
318 }
319
Dan Albertbac34742015-02-25 17:51:28 -0800320 /* if we sent the last packet of a closing socket,
321 ** we can now destroy it.
322 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 if (s->closing) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700324 D(" closing because 'closing' is set after write");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 s->close(s);
326 return;
327 }
328
Dan Albertbac34742015-02-25 17:51:28 -0800329 /* no more packets queued, so we can ignore
330 ** writable events again and tell our peer
331 ** to resume writing
332 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 fdevent_del(&s->fde, FDE_WRITE);
334 s->peer->ready(s->peer);
335 }
336
337
Dan Albertbac34742015-02-25 17:51:28 -0800338 if (ev & FDE_READ) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339 apacket *p = get_apacket();
340 unsigned char *x = p->data;
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100341 const size_t max_payload = s->get_max_payload();
342 size_t avail = max_payload;
343 int r = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 int is_eof = 0;
345
Dan Albertbac34742015-02-25 17:51:28 -0800346 while (avail > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 r = adb_read(fd, x, avail);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700348 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu",
Dan Albertbac34742015-02-25 17:51:28 -0800349 s->id, s->fd, r, r < 0 ? errno : 0, avail);
350 if (r == -1) {
351 if (errno == EAGAIN) {
352 break;
353 }
354 } else if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 avail -= r;
356 x += r;
357 continue;
358 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359
Dan Albertbac34742015-02-25 17:51:28 -0800360 /* r = 0 or unhandled error */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 is_eof = 1;
362 break;
363 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700364 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700365 s->id, s->fd, r, is_eof, s->fde.force_eof);
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100366 if ((avail == max_payload) || (s->peer == 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 put_apacket(p);
368 } else {
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100369 p->len = max_payload - avail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370
Yabin Cui00674122015-08-26 12:27:40 -0700371 // s->peer->enqueue() may call s->close() and free s,
372 // so save variables for debug printing below.
373 unsigned saved_id = s->id;
374 int saved_fd = s->fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 r = s->peer->enqueue(s->peer, p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700376 D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377
Dan Albertbac34742015-02-25 17:51:28 -0800378 if (r < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 /* 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
Dan Albertbac34742015-02-25 17:51:28 -0800390 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391 /* 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 */
Dan Albertbac34742015-02-25 17:51:28 -0800399 if ((s->fde.force_eof && !r) || is_eof) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700400 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d",
Dan Albertbac34742015-02-25 17:51:28 -0800401 is_eof, r, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 s->close(s);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700403 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404 }
405 }
406
Dan Albertbac34742015-02-25 17:51:28 -0800407 if (ev & FDE_ERROR){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 /* this should be caught be the next read or write
409 ** catching it here means we may skip the last few
410 ** bytes of readable data.
411 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700412 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 return;
414 }
415}
416
417asocket *create_local_socket(int fd)
418{
Dan Albertbac34742015-02-25 17:51:28 -0800419 asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300420 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 s->fd = fd;
422 s->enqueue = local_socket_enqueue;
423 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100424 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700426 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427
428 fdevent_install(&s->fde, fd, local_socket_event_func, s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700429 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 return s;
431}
432
David Pursell0955c662015-08-31 10:42:13 -0700433asocket *create_local_service_socket(const char *name,
434 const atransport* transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436#if !ADB_HOST
437 if (!strcmp(name,"jdwp")) {
438 return create_jdwp_service_socket();
439 }
440 if (!strcmp(name,"track-jdwp")) {
441 return create_jdwp_tracker_service_socket();
442 }
443#endif
David Pursell0955c662015-08-31 10:42:13 -0700444 int fd = service_to_fd(name, transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445 if(fd < 0) return 0;
446
Dan Pasanen98858812014-10-06 12:57:20 -0500447 asocket* s = create_local_socket(fd);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700448 D("LS(%d): bound to '%s' via %d", s->id, name, fd);
Benoit Gobyf366b362012-03-16 14:50:07 -0700449
JP Abgrallf91259a2012-03-30 13:19:11 -0700450#if !ADB_HOST
Dan Pasanen98858812014-10-06 12:57:20 -0500451 char debug[PROPERTY_VALUE_MAX];
jzhuan51297d222013-05-24 17:40:15 -0400452 if (!strncmp(name, "root:", 5))
453 property_get("ro.debuggable", debug, "");
454
Dan Pasanen98858812014-10-06 12:57:20 -0500455 if ((!strncmp(name, "root:", 5) && getuid() != 0 && strcmp(debug, "1") == 0)
456 || (!strncmp(name, "unroot:", 7) && getuid() == 0)
Benoit Gobyaeceb512012-06-12 12:12:18 -0700457 || !strncmp(name, "usb:", 4)
458 || !strncmp(name, "tcpip:", 6)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700459 D("LS(%d): enabling exit_on_close", s->id);
Benoit Gobyf366b362012-03-16 14:50:07 -0700460 s->exit_on_close = 1;
461 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700462#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700463
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 return s;
465}
466
467#if ADB_HOST
468static asocket *create_host_service_socket(const char *name, const char* serial)
469{
470 asocket *s;
471
472 s = host_service_to_socket(name, serial);
473
474 if (s != NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700475 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 return s;
477 }
478
479 return s;
480}
481#endif /* ADB_HOST */
482
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483static int remote_socket_enqueue(asocket *s, apacket *p)
484{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700485 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700486 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 p->msg.command = A_WRTE;
488 p->msg.arg0 = s->peer->id;
489 p->msg.arg1 = s->id;
490 p->msg.data_length = p->len;
491 send_packet(p, s->transport);
492 return 1;
493}
494
495static void remote_socket_ready(asocket *s)
496{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700497 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700498 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 apacket *p = get_apacket();
500 p->msg.command = A_OKAY;
501 p->msg.arg0 = s->peer->id;
502 p->msg.arg1 = s->id;
503 send_packet(p, s->transport);
504}
505
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100506static void remote_socket_shutdown(asocket *s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700508 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700509 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 apacket *p = get_apacket();
511 p->msg.command = A_CLSE;
512 if(s->peer) {
513 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100514 }
515 p->msg.arg1 = s->id;
516 send_packet(p, s->transport);
517}
518
519static void remote_socket_close(asocket *s)
520{
521 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 s->peer->peer = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700523 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700524 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 s->peer->close(s->peer);
526 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700527 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d",
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100528 s->id, s->fd, s->peer?s->peer->fd:-1);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700529 D("RS(%d): closed", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 free(s);
531}
532
Yabin Cuifd28f322015-08-27 18:50:04 -0700533// Create a remote socket to exchange packets with a remote service through transport
534// |t|. Where |id| is the socket id of the corresponding service on the other
535// side of the transport (it is allocated by the remote side and _cannot_ be 0).
536// Returns a new non-NULL asocket handle.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537asocket *create_remote_socket(unsigned id, atransport *t)
538{
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100539 if (id == 0) fatal("invalid remote socket id (0)");
Yabin Cuifd28f322015-08-27 18:50:04 -0700540 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300542 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 s->id = id;
544 s->enqueue = remote_socket_enqueue;
545 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100546 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800547 s->close = remote_socket_close;
548 s->transport = t;
549
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700550 D("RS(%d): created", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800551 return s;
552}
553
554void connect_to_remote(asocket *s, const char *destination)
555{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700556 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557 apacket *p = get_apacket();
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100558 size_t len = strlen(destination) + 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800559
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100560 if(len > (s->get_max_payload()-1)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561 fatal("destination oversized");
562 }
563
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700564 D("LS(%d): connect('%s')", s->id, destination);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565 p->msg.command = A_OPEN;
566 p->msg.arg0 = s->id;
567 p->msg.data_length = len;
568 strcpy((char*) p->data, destination);
569 send_packet(p, s->transport);
570}
571
572
573/* this is used by magic sockets to rig local sockets to
574 send the go-ahead message when they connect */
575static void local_socket_ready_notify(asocket *s)
576{
577 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100578 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700580 SendOkay(s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581 s->ready(s);
582}
583
584/* this is used by magic sockets to rig local sockets to
585 send the failure message if they are closed before
586 connected (to avoid closing them without a status message) */
587static void local_socket_close_notify(asocket *s)
588{
589 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100590 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800591 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700592 SendFail(s->fd, "closed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593 s->close(s);
594}
595
Elliott Hughese67f1f82015-04-30 17:32:03 -0700596static unsigned unhex(unsigned char *s, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597{
598 unsigned n = 0, c;
599
600 while(len-- > 0) {
601 switch((c = *s++)) {
602 case '0': case '1': case '2':
603 case '3': case '4': case '5':
604 case '6': case '7': case '8':
605 case '9':
606 c -= '0';
607 break;
608 case 'a': case 'b': case 'c':
609 case 'd': case 'e': case 'f':
610 c = c - 'a' + 10;
611 break;
612 case 'A': case 'B': case 'C':
613 case 'D': case 'E': case 'F':
614 c = c - 'A' + 10;
615 break;
616 default:
617 return 0xffffffff;
618 }
619
620 n = (n << 4) | c;
621 }
622
623 return n;
624}
625
Elliott Hughese67f1f82015-04-30 17:32:03 -0700626#if ADB_HOST
627
David Pursell3f902aa2016-03-01 08:58:26 -0800628namespace internal {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700629
David Pursell3f902aa2016-03-01 08:58:26 -0800630// Returns the position in |service| following the target serial parameter. Serial format can be
631// any of:
632// * [tcp:|udp:]<serial>[:<port>]:<command>
633// * <prefix>:<serial>:<command>
634// Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}.
635//
636// The returned pointer will point to the ':' just before <command>, or nullptr if not found.
Dan Austinb4cff492016-03-28 15:32:37 -0700637char* skip_host_serial(char* service) {
David Pursell3f902aa2016-03-01 08:58:26 -0800638 static const std::vector<std::string>& prefixes =
639 *(new std::vector<std::string>{"usb:", "product:", "model:", "device:"});
Terence Haddock28e13902011-03-16 09:43:56 +0100640
David Pursell3f902aa2016-03-01 08:58:26 -0800641 for (const std::string& prefix : prefixes) {
642 if (!strncmp(service, prefix.c_str(), prefix.length())) {
643 return strchr(service + prefix.length(), ':');
644 }
Scott Anderson3608d832012-05-31 12:04:23 -0700645 }
646
David Pursell3f902aa2016-03-01 08:58:26 -0800647 // For fastboot compatibility, ignore protocol prefixes.
648 if (!strncmp(service, "tcp:", 4) || !strncmp(service, "udp:", 4)) {
649 service += 4;
650 }
651
652 char* first_colon = strchr(service, ':');
Terence Haddock28e13902011-03-16 09:43:56 +0100653 if (!first_colon) {
David Pursell3f902aa2016-03-01 08:58:26 -0800654 // No colon in service string.
655 return nullptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100656 }
David Pursell3f902aa2016-03-01 08:58:26 -0800657
658 char* serial_end = first_colon;
Terence Haddock28e13902011-03-16 09:43:56 +0100659 if (isdigit(serial_end[1])) {
660 serial_end++;
David Pursell3f902aa2016-03-01 08:58:26 -0800661 while (*serial_end && isdigit(*serial_end)) {
Terence Haddock28e13902011-03-16 09:43:56 +0100662 serial_end++;
663 }
David Pursell3f902aa2016-03-01 08:58:26 -0800664 if (*serial_end != ':') {
Terence Haddock28e13902011-03-16 09:43:56 +0100665 // Something other than numbers was found, reset the end.
666 serial_end = first_colon;
667 }
668 }
669 return serial_end;
670}
671
David Pursell3f902aa2016-03-01 08:58:26 -0800672} // namespace internal
673
Elliott Hughese67f1f82015-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
Elliott Hughes8d28e192015-10-07 14:55:10 -0700680 char *service = nullptr;
681 char* serial = nullptr;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700682 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683#endif
684
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700685 D("SS(%d): enqueue %d", s->id, p->len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686
687 if(s->pkt_first == 0) {
688 s->pkt_first = p;
689 s->pkt_last = p;
690 } else {
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100691 if((s->pkt_first->len + p->len) > s->get_max_payload()) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700692 D("SS(%d): overflow", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 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
Josh Gao7e6683c2016-01-15 14:35:54 -0800705 /* don't bother if we can't decode the length */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 if(p->len < 4) return 0;
707
708 len = unhex(p->data, 4);
Josh Gao7e6683c2016-01-15 14:35:54 -0800709 if ((len < 1) || (len > MAX_PAYLOAD_V1)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700710 D("SS(%d): bad size (%d)", s->id, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 goto fail;
712 }
713
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700714 D("SS(%d): len is %d", s->id, len );
Josh Gao7e6683c2016-01-15 14:35:54 -0800715 /* can't do anything until we have the full header */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716 if((len + 4) > p->len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700717 D("SS(%d): waiting for %d more bytes", s->id, len+4 - p->len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718 return 0;
719 }
720
721 p->data[len + 4] = 0;
722
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700723 D("SS(%d): '%s'", s->id, (char*) (p->data + 4));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800724
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.
David Pursell3f902aa2016-03-01 08:58:26 -0800732 serial_end = internal::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:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700739 type = kTransportUsb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740 service += strlen("host-usb:");
741 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700742 type = kTransportLocal;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800743 service += strlen("host-local:");
744 } else if (!strncmp(service, "host:", strlen("host:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700745 type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746 service += strlen("host:");
747 } else {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700748 service = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800749 }
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 */
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700759 if(handle_host_request(service, type, serial, s->peer->fd, s) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 /* XXX fail message? */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700761 D( "SS(%d): handled host service '%s'", s->id, service );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800762 goto fail;
763 }
764 if (!strncmp(service, "transport", strlen("transport"))) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700765 D( "SS(%d): okay transport", s->id );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800766 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) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700776 D( "SS(%d): couldn't create host service '%s'", s->id, service );
Elliott Hughese67f1f82015-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 Hughese67f1f82015-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;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700791 s->peer->shutdown = nullptr;
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;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700796 D( "SS(%d): okay", s->id );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800797 s->close(s);
798
799 /* initial state is "ready" */
800 s2->ready(s2);
801 return 0;
802 }
803#else /* !ADB_HOST */
Elliott Hughes8d28e192015-10-07 14:55:10 -0700804 if (s->transport == nullptr) {
Elliott Hughes7be29c82015-04-16 22:54:44 -0700805 std::string error_msg = "unknown failure";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700806 s->transport = acquire_one_transport(kTransportAny, nullptr, nullptr, &error_msg);
807 if (s->transport == nullptr) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700808 SendFail(s->peer->fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809 goto fail;
810 }
811 }
812#endif
813
Dan Albertdcd78a12015-05-18 16:43:57 -0700814 if(!(s->transport) || (s->transport->connection_state == kCsOffline)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800815 /* if there's no remote we fail the connection
816 ** right here and terminate it
817 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700818 SendFail(s->peer->fd, "device offline (x)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800819 goto fail;
820 }
821
822
823 /* instrument our peer to pass the success or fail
824 ** message back once it connects or closes, then
825 ** detach from it, request the connection, and
826 ** tear down
827 */
828 s->peer->ready = local_socket_ready_notify;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700829 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800830 s->peer->close = local_socket_close_notify;
831 s->peer->peer = 0;
832 /* give him our transport and upref it */
833 s->peer->transport = s->transport;
834
835 connect_to_remote(s->peer, (char*) (p->data + 4));
836 s->peer = 0;
837 s->close(s);
838 return 1;
839
840fail:
841 /* we're going to close our peer as a side-effect, so
842 ** return -1 to signal that state to the local socket
843 ** who is enqueueing against us
844 */
845 s->close(s);
846 return -1;
847}
848
849static void smart_socket_ready(asocket *s)
850{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700851 D("SS(%d): ready", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852}
853
854static void smart_socket_close(asocket *s)
855{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700856 D("SS(%d): closed", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800857 if(s->pkt_first){
858 put_apacket(s->pkt_first);
859 }
860 if(s->peer) {
861 s->peer->peer = 0;
862 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500863 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800864 }
865 free(s);
866}
867
Benoit Goby9470c2f2013-02-20 15:04:53 -0800868static asocket *create_smart_socket(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800869{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700870 D("Creating smart socket");
Dan Albertbac34742015-02-25 17:51:28 -0800871 asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300872 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800873 s->enqueue = smart_socket_enqueue;
874 s->ready = smart_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100875 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800876 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800877
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700878 D("SS(%d)", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800879 return s;
880}
881
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800882void connect_to_smartsocket(asocket *s)
883{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700884 D("Connecting to smart socket");
Benoit Goby9470c2f2013-02-20 15:04:53 -0800885 asocket *ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800886 s->peer = ss;
887 ss->peer = s;
888 s->ready(s);
889}
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100890
891size_t asocket::get_max_payload() const {
892 size_t max_payload = MAX_PAYLOAD;
893 if (transport) {
894 max_payload = std::min(max_payload, transport->get_max_payload());
895 }
896 if (peer && peer->transport) {
897 max_payload = std::min(max_payload, peer->transport->get_max_payload());
898 }
899 return max_payload;
900}