blob: bd33d79bce0c2794059e73d6c27e088159bd1396 [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
Dan Albert76649012015-02-24 15:51:19 -080028#if !ADB_HOST
29#include "cutils/properties.h"
30#endif
Dan Albert33134262015-03-19 15:21:08 -070031
32#include "adb.h"
33#include "adb_io.h"
Dan Albert76649012015-02-24 15:51:19 -080034#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
36ADB_MUTEX_DEFINE( socket_list_lock );
37
38static void local_socket_close_locked(asocket *s);
39
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040static unsigned local_socket_next_id = 1;
41
42static asocket local_socket_list = {
43 .next = &local_socket_list,
44 .prev = &local_socket_list,
45};
46
47/* the the list of currently closing local sockets.
48** these have no peer anymore, but still packets to
49** write to their fd.
50*/
51static asocket local_socket_closing_list = {
52 .next = &local_socket_closing_list,
53 .prev = &local_socket_closing_list,
54};
55
David 'Digit' Turner818d6412013-12-13 14:09:44 +010056// Parse the global list of sockets to find one with id |local_id|.
57// If |peer_id| is not 0, also check that it is connected to a peer
58// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
59asocket *find_local_socket(unsigned local_id, unsigned peer_id)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060{
61 asocket *s;
62 asocket *result = NULL;
63
64 adb_mutex_lock(&socket_list_lock);
André Goddard Rosa81828292010-06-12 11:40:20 -030065 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +010066 if (s->id != local_id)
67 continue;
68 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa81828292010-06-12 11:40:20 -030069 result = s;
André Goddard Rosa81828292010-06-12 11:40:20 -030070 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010071 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 }
73 adb_mutex_unlock(&socket_list_lock);
74
75 return result;
76}
77
78static void
79insert_local_socket(asocket* s, asocket* list)
80{
81 s->next = list;
82 s->prev = s->next->prev;
83 s->prev->next = s;
84 s->next->prev = s;
85}
86
87
88void install_local_socket(asocket *s)
89{
90 adb_mutex_lock(&socket_list_lock);
91
92 s->id = local_socket_next_id++;
David 'Digit' Turner818d6412013-12-13 14:09:44 +010093
94 // Socket ids should never be 0.
95 if (local_socket_next_id == 0)
96 local_socket_next_id = 1;
97
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 insert_local_socket(s, &local_socket_list);
99
100 adb_mutex_unlock(&socket_list_lock);
101}
102
103void remove_socket(asocket *s)
104{
105 // socket_list_lock should already be held
106 if (s->prev && s->next)
107 {
108 s->prev->next = s->next;
109 s->next->prev = s->prev;
110 s->next = 0;
111 s->prev = 0;
112 s->id = 0;
113 }
114}
115
116void close_all_sockets(atransport *t)
117{
118 asocket *s;
119
120 /* this is a little gross, but since s->close() *will* modify
121 ** the list out from under you, your options are limited.
122 */
123 adb_mutex_lock(&socket_list_lock);
124restart:
125 for(s = local_socket_list.next; s != &local_socket_list; s = s->next){
126 if(s->transport == t || (s->peer && s->peer->transport == t)) {
127 local_socket_close_locked(s);
128 goto restart;
129 }
130 }
131 adb_mutex_unlock(&socket_list_lock);
132}
133
134static int local_socket_enqueue(asocket *s, apacket *p)
135{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700136 D("LS(%d): enqueue %d", s->id, p->len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137
138 p->ptr = p->data;
139
140 /* if there is already data queue'd, we will receive
141 ** events when it's time to write. just add this to
142 ** the tail
143 */
144 if(s->pkt_first) {
145 goto enqueue;
146 }
147
148 /* write as much as we can, until we
149 ** would block or there is an error/eof
150 */
151 while(p->len > 0) {
152 int r = adb_write(s->fd, p->ptr, p->len);
153 if(r > 0) {
154 p->len -= r;
155 p->ptr += r;
156 continue;
157 }
158 if((r == 0) || (errno != EAGAIN)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700159 D( "LS(%d): not ready, errno=%d: %s", s->id, errno, strerror(errno) );
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700160 put_apacket(p);
161 s->has_write_error = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 s->close(s);
163 return 1; /* not ready (error) */
164 } else {
165 break;
166 }
167 }
168
169 if(p->len == 0) {
170 put_apacket(p);
171 return 0; /* ready for more data */
172 }
173
174enqueue:
175 p->next = 0;
176 if(s->pkt_first) {
177 s->pkt_last->next = p;
178 } else {
179 s->pkt_first = p;
180 }
181 s->pkt_last = p;
182
183 /* make sure we are notified when we can drain the queue */
184 fdevent_add(&s->fde, FDE_WRITE);
185
186 return 1; /* not ready (backlog) */
187}
188
189static void local_socket_ready(asocket *s)
190{
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100191 /* far side is ready for data, pay attention to
192 readable events */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 fdevent_add(&s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194}
195
196static void local_socket_close(asocket *s)
197{
198 adb_mutex_lock(&socket_list_lock);
199 local_socket_close_locked(s);
200 adb_mutex_unlock(&socket_list_lock);
201}
202
203// be sure to hold the socket list lock when calling this
204static void local_socket_destroy(asocket *s)
205{
206 apacket *p, *n;
Benoit Gobyf366b362012-03-16 14:50:07 -0700207 int exit_on_close = s->exit_on_close;
208
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700209 D("LS(%d): destroying fde.fd=%d", s->id, s->fde.fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210
211 /* IMPORTANT: the remove closes the fd
212 ** that belongs to this socket
213 */
214 fdevent_remove(&s->fde);
215
216 /* dispose of any unwritten data */
217 for(p = s->pkt_first; p; p = n) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700218 D("LS(%d): discarding %d bytes", s->id, p->len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 n = p->next;
220 put_apacket(p);
221 }
222 remove_socket(s);
223 free(s);
Benoit Gobyf366b362012-03-16 14:50:07 -0700224
225 if (exit_on_close) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700226 D("local_socket_destroy: exiting");
Benoit Gobyf366b362012-03-16 14:50:07 -0700227 exit(1);
228 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229}
230
231
232static void local_socket_close_locked(asocket *s)
233{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700234 D("entered local_socket_close_locked. LS(%d) fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 if(s->peer) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700236 D("LS(%d): closing peer. peer->id=%d peer->fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700237 s->id, s->peer->id, s->peer->fd);
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100238 /* Note: it's important to call shutdown before disconnecting from
239 * the peer, this ensures that remote sockets can still get the id
240 * of the local socket they're connected to, to send a CLOSE()
241 * protocol event. */
242 if (s->peer->shutdown)
243 s->peer->shutdown(s->peer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 s->peer->peer = 0;
245 // tweak to avoid deadlock
Tom Marlin49f18572011-05-13 13:24:55 -0500246 if (s->peer->close == local_socket_close) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 local_socket_close_locked(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500248 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500250 }
251 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 }
253
254 /* If we are already closing, or if there are no
255 ** pending packets, destroy immediately
256 */
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700257 if (s->closing || s->has_write_error || s->pkt_first == NULL) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 int id = s->id;
259 local_socket_destroy(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700260 D("LS(%d): closed", id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 return;
262 }
263
264 /* otherwise, put on the closing list
265 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700266 D("LS(%d): closing", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 s->closing = 1;
268 fdevent_del(&s->fde, FDE_READ);
269 remove_socket(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700270 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 insert_local_socket(s, &local_socket_closing_list);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700272 CHECK_EQ(FDE_WRITE, s->fde.state & FDE_WRITE);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273}
274
Dan Albertbac34742015-02-25 17:51:28 -0800275static void local_socket_event_func(int fd, unsigned ev, void* _s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276{
Dan Albertbac34742015-02-25 17:51:28 -0800277 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700278 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall408fa572011-03-16 15:57:42 -0700279
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 /* put the FDE_WRITE processing before the FDE_READ
281 ** in order to simplify the code.
282 */
Dan Albertbac34742015-02-25 17:51:28 -0800283 if (ev & FDE_WRITE) {
284 apacket* p;
285 while ((p = s->pkt_first) != nullptr) {
286 while (p->len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 int r = adb_write(fd, p->ptr, p->len);
Dan Albertbac34742015-02-25 17:51:28 -0800288 if (r == -1) {
289 /* returning here is ok because FDE_READ will
290 ** be processed in the next iteration loop
291 */
292 if (errno == EAGAIN) {
293 return;
294 }
295 } else if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 p->ptr += r;
297 p->len -= r;
298 continue;
299 }
Dan Albertbac34742015-02-25 17:51:28 -0800300
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700301 D(" closing after write because r=%d and errno is %d", r, errno);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700302 s->has_write_error = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 s->close(s);
304 return;
305 }
306
Dan Albertbac34742015-02-25 17:51:28 -0800307 if (p->len == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 s->pkt_first = p->next;
Dan Albertbac34742015-02-25 17:51:28 -0800309 if (s->pkt_first == 0) {
310 s->pkt_last = 0;
311 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 put_apacket(p);
313 }
314 }
315
Dan Albertbac34742015-02-25 17:51:28 -0800316 /* if we sent the last packet of a closing socket,
317 ** we can now destroy it.
318 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 if (s->closing) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700320 D(" closing because 'closing' is set after write");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321 s->close(s);
322 return;
323 }
324
Dan Albertbac34742015-02-25 17:51:28 -0800325 /* no more packets queued, so we can ignore
326 ** writable events again and tell our peer
327 ** to resume writing
328 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 fdevent_del(&s->fde, FDE_WRITE);
330 s->peer->ready(s->peer);
331 }
332
333
Dan Albertbac34742015-02-25 17:51:28 -0800334 if (ev & FDE_READ) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 apacket *p = get_apacket();
336 unsigned char *x = p->data;
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100337 const size_t max_payload = s->get_max_payload();
338 size_t avail = max_payload;
339 int r = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 int is_eof = 0;
341
Dan Albertbac34742015-02-25 17:51:28 -0800342 while (avail > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 r = adb_read(fd, x, avail);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700344 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu",
Dan Albertbac34742015-02-25 17:51:28 -0800345 s->id, s->fd, r, r < 0 ? errno : 0, avail);
346 if (r == -1) {
347 if (errno == EAGAIN) {
348 break;
349 }
350 } else if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 avail -= r;
352 x += r;
353 continue;
354 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355
Dan Albertbac34742015-02-25 17:51:28 -0800356 /* r = 0 or unhandled error */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357 is_eof = 1;
358 break;
359 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700360 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700361 s->id, s->fd, r, is_eof, s->fde.force_eof);
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100362 if ((avail == max_payload) || (s->peer == 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 put_apacket(p);
364 } else {
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100365 p->len = max_payload - avail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366
Yabin Cui00674122015-08-26 12:27:40 -0700367 // s->peer->enqueue() may call s->close() and free s,
368 // so save variables for debug printing below.
369 unsigned saved_id = s->id;
370 int saved_fd = s->fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 r = s->peer->enqueue(s->peer, p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700372 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 -0800373
Dan Albertbac34742015-02-25 17:51:28 -0800374 if (r < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 /* error return means they closed us as a side-effect
376 ** and we must return immediately.
377 **
378 ** note that if we still have buffered packets, the
379 ** socket will be placed on the closing socket list.
380 ** this handler function will be called again
381 ** to process FDE_WRITE events.
382 */
383 return;
384 }
385
Dan Albertbac34742015-02-25 17:51:28 -0800386 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 /* if the remote cannot accept further events,
388 ** we disable notification of READs. They'll
389 ** be enabled again when we get a call to ready()
390 */
391 fdevent_del(&s->fde, FDE_READ);
392 }
393 }
JP Abgrall112445b2011-04-12 22:01:58 -0700394 /* Don't allow a forced eof if data is still there */
Dan Albertbac34742015-02-25 17:51:28 -0800395 if ((s->fde.force_eof && !r) || is_eof) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700396 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d",
Dan Albertbac34742015-02-25 17:51:28 -0800397 is_eof, r, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 s->close(s);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700399 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 }
401 }
402
Dan Albertbac34742015-02-25 17:51:28 -0800403 if (ev & FDE_ERROR){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404 /* this should be caught be the next read or write
405 ** catching it here means we may skip the last few
406 ** bytes of readable data.
407 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700408 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 return;
410 }
411}
412
413asocket *create_local_socket(int fd)
414{
Dan Albertbac34742015-02-25 17:51:28 -0800415 asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300416 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 s->fd = fd;
418 s->enqueue = local_socket_enqueue;
419 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100420 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700422 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423
424 fdevent_install(&s->fde, fd, local_socket_event_func, s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700425 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 return s;
427}
428
David Pursell0955c662015-08-31 10:42:13 -0700429asocket *create_local_service_socket(const char *name,
430 const atransport* transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432#if !ADB_HOST
433 if (!strcmp(name,"jdwp")) {
434 return create_jdwp_service_socket();
435 }
436 if (!strcmp(name,"track-jdwp")) {
437 return create_jdwp_tracker_service_socket();
438 }
439#endif
David Pursell0955c662015-08-31 10:42:13 -0700440 int fd = service_to_fd(name, transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 if(fd < 0) return 0;
442
Dan Pasanen98858812014-10-06 12:57:20 -0500443 asocket* s = create_local_socket(fd);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700444 D("LS(%d): bound to '%s' via %d", s->id, name, fd);
Benoit Gobyf366b362012-03-16 14:50:07 -0700445
JP Abgrallf91259a2012-03-30 13:19:11 -0700446#if !ADB_HOST
Dan Pasanen98858812014-10-06 12:57:20 -0500447 char debug[PROPERTY_VALUE_MAX];
jzhuan51297d222013-05-24 17:40:15 -0400448 if (!strncmp(name, "root:", 5))
449 property_get("ro.debuggable", debug, "");
450
Dan Pasanen98858812014-10-06 12:57:20 -0500451 if ((!strncmp(name, "root:", 5) && getuid() != 0 && strcmp(debug, "1") == 0)
452 || (!strncmp(name, "unroot:", 7) && getuid() == 0)
Benoit Gobyaeceb512012-06-12 12:12:18 -0700453 || !strncmp(name, "usb:", 4)
454 || !strncmp(name, "tcpip:", 6)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700455 D("LS(%d): enabling exit_on_close", s->id);
Benoit Gobyf366b362012-03-16 14:50:07 -0700456 s->exit_on_close = 1;
457 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700458#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700459
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 return s;
461}
462
463#if ADB_HOST
464static asocket *create_host_service_socket(const char *name, const char* serial)
465{
466 asocket *s;
467
468 s = host_service_to_socket(name, serial);
469
470 if (s != NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700471 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 return s;
473 }
474
475 return s;
476}
477#endif /* ADB_HOST */
478
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479static int remote_socket_enqueue(asocket *s, apacket *p)
480{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700481 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700482 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 p->msg.command = A_WRTE;
484 p->msg.arg0 = s->peer->id;
485 p->msg.arg1 = s->id;
486 p->msg.data_length = p->len;
487 send_packet(p, s->transport);
488 return 1;
489}
490
491static void remote_socket_ready(asocket *s)
492{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700493 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700494 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 apacket *p = get_apacket();
496 p->msg.command = A_OKAY;
497 p->msg.arg0 = s->peer->id;
498 p->msg.arg1 = s->id;
499 send_packet(p, s->transport);
500}
501
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100502static void remote_socket_shutdown(asocket *s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700504 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700505 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506 apacket *p = get_apacket();
507 p->msg.command = A_CLSE;
508 if(s->peer) {
509 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100510 }
511 p->msg.arg1 = s->id;
512 send_packet(p, s->transport);
513}
514
515static void remote_socket_close(asocket *s)
516{
517 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 s->peer->peer = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700519 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700520 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521 s->peer->close(s->peer);
522 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700523 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d",
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100524 s->id, s->fd, s->peer?s->peer->fd:-1);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700525 D("RS(%d): closed", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 free(s);
527}
528
Yabin Cuifd28f322015-08-27 18:50:04 -0700529// Create a remote socket to exchange packets with a remote service through transport
530// |t|. Where |id| is the socket id of the corresponding service on the other
531// side of the transport (it is allocated by the remote side and _cannot_ be 0).
532// Returns a new non-NULL asocket handle.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533asocket *create_remote_socket(unsigned id, atransport *t)
534{
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100535 if (id == 0) fatal("invalid remote socket id (0)");
Yabin Cuifd28f322015-08-27 18:50:04 -0700536 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300538 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 s->id = id;
540 s->enqueue = remote_socket_enqueue;
541 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100542 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 s->close = remote_socket_close;
544 s->transport = t;
545
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700546 D("RS(%d): created", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800547 return s;
548}
549
550void connect_to_remote(asocket *s, const char *destination)
551{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700552 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553 apacket *p = get_apacket();
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100554 size_t len = strlen(destination) + 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800555
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100556 if(len > (s->get_max_payload()-1)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557 fatal("destination oversized");
558 }
559
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700560 D("LS(%d): connect('%s')", s->id, destination);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561 p->msg.command = A_OPEN;
562 p->msg.arg0 = s->id;
563 p->msg.data_length = len;
564 strcpy((char*) p->data, destination);
565 send_packet(p, s->transport);
566}
567
568
569/* this is used by magic sockets to rig local sockets to
570 send the go-ahead message when they connect */
571static void local_socket_ready_notify(asocket *s)
572{
573 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100574 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700576 SendOkay(s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577 s->ready(s);
578}
579
580/* this is used by magic sockets to rig local sockets to
581 send the failure message if they are closed before
582 connected (to avoid closing them without a status message) */
583static void local_socket_close_notify(asocket *s)
584{
585 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100586 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700588 SendFail(s->fd, "closed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589 s->close(s);
590}
591
Elliott Hughese67f1f82015-04-30 17:32:03 -0700592static unsigned unhex(unsigned char *s, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593{
594 unsigned n = 0, c;
595
596 while(len-- > 0) {
597 switch((c = *s++)) {
598 case '0': case '1': case '2':
599 case '3': case '4': case '5':
600 case '6': case '7': case '8':
601 case '9':
602 c -= '0';
603 break;
604 case 'a': case 'b': case 'c':
605 case 'd': case 'e': case 'f':
606 c = c - 'a' + 10;
607 break;
608 case 'A': case 'B': case 'C':
609 case 'D': case 'E': case 'F':
610 c = c - 'A' + 10;
611 break;
612 default:
613 return 0xffffffff;
614 }
615
616 n = (n << 4) | c;
617 }
618
619 return n;
620}
621
Elliott Hughese67f1f82015-04-30 17:32:03 -0700622#if ADB_HOST
623
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700624#define PREFIX(str) { str, sizeof(str) - 1 }
625static const struct prefix_struct {
626 const char *str;
627 const size_t len;
628} prefixes[] = {
629 PREFIX("usb:"),
630 PREFIX("product:"),
631 PREFIX("model:"),
632 PREFIX("device:"),
633};
634static const int num_prefixes = (sizeof(prefixes) / sizeof(prefixes[0]));
635
Terence Haddock28e13902011-03-16 09:43:56 +0100636/* skip_host_serial return the position in a string
637 skipping over the 'serial' parameter in the ADB protocol,
638 where parameter string may be a host:port string containing
639 the protocol delimiter (colon). */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700640static char *skip_host_serial(char *service) {
Terence Haddock28e13902011-03-16 09:43:56 +0100641 char *first_colon, *serial_end;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700642 int i;
Terence Haddock28e13902011-03-16 09:43:56 +0100643
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700644 for (i = 0; i < num_prefixes; i++) {
645 if (!strncmp(service, prefixes[i].str, prefixes[i].len))
646 return strchr(service + prefixes[i].len, ':');
Scott Anderson3608d832012-05-31 12:04:23 -0700647 }
648
Terence Haddock28e13902011-03-16 09:43:56 +0100649 first_colon = strchr(service, ':');
650 if (!first_colon) {
651 /* No colon in service string. */
652 return NULL;
653 }
654 serial_end = first_colon;
655 if (isdigit(serial_end[1])) {
656 serial_end++;
657 while ((*serial_end) && isdigit(*serial_end)) {
658 serial_end++;
659 }
660 if ((*serial_end) != ':') {
661 // Something other than numbers was found, reset the end.
662 serial_end = first_colon;
663 }
664 }
665 return serial_end;
666}
667
Elliott Hughese67f1f82015-04-30 17:32:03 -0700668#endif // ADB_HOST
669
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800670static int smart_socket_enqueue(asocket *s, apacket *p)
671{
672 unsigned len;
673#if ADB_HOST
674 char *service = NULL;
675 char* serial = NULL;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700676 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677#endif
678
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700679 D("SS(%d): enqueue %d", s->id, p->len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680
681 if(s->pkt_first == 0) {
682 s->pkt_first = p;
683 s->pkt_last = p;
684 } else {
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100685 if((s->pkt_first->len + p->len) > s->get_max_payload()) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700686 D("SS(%d): overflow", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800687 put_apacket(p);
688 goto fail;
689 }
690
691 memcpy(s->pkt_first->data + s->pkt_first->len,
692 p->data, p->len);
693 s->pkt_first->len += p->len;
694 put_apacket(p);
695
696 p = s->pkt_first;
697 }
698
699 /* don't bother if we can't decode the length */
700 if(p->len < 4) return 0;
701
702 len = unhex(p->data, 4);
703 if((len < 1) || (len > 1024)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700704 D("SS(%d): bad size (%d)", s->id, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705 goto fail;
706 }
707
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700708 D("SS(%d): len is %d", s->id, len );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709 /* can't do anything until we have the full header */
710 if((len + 4) > p->len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700711 D("SS(%d): waiting for %d more bytes", s->id, len+4 - p->len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 return 0;
713 }
714
715 p->data[len + 4] = 0;
716
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700717 D("SS(%d): '%s'", s->id, (char*) (p->data + 4));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718
719#if ADB_HOST
720 service = (char *)p->data + 4;
721 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
722 char* serial_end;
723 service += strlen("host-serial:");
724
Terence Haddock28e13902011-03-16 09:43:56 +0100725 // serial number should follow "host:" and could be a host:port string.
726 serial_end = skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800727 if (serial_end) {
728 *serial_end = 0; // terminate string
729 serial = service;
730 service = serial_end + 1;
731 }
732 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700733 type = kTransportUsb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800734 service += strlen("host-usb:");
735 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700736 type = kTransportLocal;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800737 service += strlen("host-local:");
738 } else if (!strncmp(service, "host:", strlen("host:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700739 type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740 service += strlen("host:");
741 } else {
742 service = NULL;
743 }
744
745 if (service) {
746 asocket *s2;
747
748 /* some requests are handled immediately -- in that
749 ** case the handle_host_request() routine has sent
750 ** the OKAY or FAIL message and all we have to do
751 ** is clean up.
752 */
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700753 if(handle_host_request(service, type, serial, s->peer->fd, s) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800754 /* XXX fail message? */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700755 D( "SS(%d): handled host service '%s'", s->id, service );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756 goto fail;
757 }
758 if (!strncmp(service, "transport", strlen("transport"))) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700759 D( "SS(%d): okay transport", s->id );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 p->len = 0;
761 return 0;
762 }
763
764 /* try to find a local service with this name.
765 ** if no such service exists, we'll fail out
766 ** and tear down here.
767 */
768 s2 = create_host_service_socket(service, serial);
769 if(s2 == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700770 D( "SS(%d): couldn't create host service '%s'", s->id, service );
Elliott Hughese67f1f82015-04-30 17:32:03 -0700771 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800772 goto fail;
773 }
774
775 /* we've connected to a local host service,
776 ** so we make our peer back into a regular
777 ** local socket and bind it to the new local
778 ** service socket, acknowledge the successful
779 ** connection, and close this smart socket now
780 ** that its work is done.
781 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700782 SendOkay(s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800783
784 s->peer->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100785 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786 s->peer->close = local_socket_close;
787 s->peer->peer = s2;
788 s2->peer = s->peer;
789 s->peer = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700790 D( "SS(%d): okay", s->id );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800791 s->close(s);
792
793 /* initial state is "ready" */
794 s2->ready(s2);
795 return 0;
796 }
797#else /* !ADB_HOST */
798 if (s->transport == NULL) {
Elliott Hughes7be29c82015-04-16 22:54:44 -0700799 std::string error_msg = "unknown failure";
Dan Albertdcd78a12015-05-18 16:43:57 -0700800 s->transport =
801 acquire_one_transport(kCsAny, kTransportAny, NULL, &error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802
803 if (s->transport == NULL) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700804 SendFail(s->peer->fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805 goto fail;
806 }
807 }
808#endif
809
Dan Albertdcd78a12015-05-18 16:43:57 -0700810 if(!(s->transport) || (s->transport->connection_state == kCsOffline)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800811 /* if there's no remote we fail the connection
812 ** right here and terminate it
813 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700814 SendFail(s->peer->fd, "device offline (x)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800815 goto fail;
816 }
817
818
819 /* instrument our peer to pass the success or fail
820 ** message back once it connects or closes, then
821 ** detach from it, request the connection, and
822 ** tear down
823 */
824 s->peer->ready = local_socket_ready_notify;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100825 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800826 s->peer->close = local_socket_close_notify;
827 s->peer->peer = 0;
828 /* give him our transport and upref it */
829 s->peer->transport = s->transport;
830
831 connect_to_remote(s->peer, (char*) (p->data + 4));
832 s->peer = 0;
833 s->close(s);
834 return 1;
835
836fail:
837 /* we're going to close our peer as a side-effect, so
838 ** return -1 to signal that state to the local socket
839 ** who is enqueueing against us
840 */
841 s->close(s);
842 return -1;
843}
844
845static void smart_socket_ready(asocket *s)
846{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700847 D("SS(%d): ready", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800848}
849
850static void smart_socket_close(asocket *s)
851{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700852 D("SS(%d): closed", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853 if(s->pkt_first){
854 put_apacket(s->pkt_first);
855 }
856 if(s->peer) {
857 s->peer->peer = 0;
858 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500859 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800860 }
861 free(s);
862}
863
Benoit Goby9470c2f2013-02-20 15:04:53 -0800864static asocket *create_smart_socket(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800865{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700866 D("Creating smart socket");
Dan Albertbac34742015-02-25 17:51:28 -0800867 asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300868 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800869 s->enqueue = smart_socket_enqueue;
870 s->ready = smart_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100871 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800872 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800873
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700874 D("SS(%d)", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800875 return s;
876}
877
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800878void connect_to_smartsocket(asocket *s)
879{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700880 D("Connecting to smart socket");
Benoit Goby9470c2f2013-02-20 15:04:53 -0800881 asocket *ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800882 s->peer = ss;
883 ss->peer = s;
884 s->ready(s);
885}
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100886
887size_t asocket::get_max_payload() const {
888 size_t max_payload = MAX_PAYLOAD;
889 if (transport) {
890 max_payload = std::min(max_payload, transport->get_max_payload());
891 }
892 if (peer && peer->transport) {
893 max_payload = std::min(max_payload, peer->transport->get_max_payload());
894 }
895 return max_payload;
896}