blob: 104ad6b129a2dd8090074ebfd707abc3d5cb103e [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) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 s->close(s);
161 return 1; /* not ready (error) */
162 } else {
163 break;
164 }
165 }
166
167 if(p->len == 0) {
168 put_apacket(p);
169 return 0; /* ready for more data */
170 }
171
172enqueue:
173 p->next = 0;
174 if(s->pkt_first) {
175 s->pkt_last->next = p;
176 } else {
177 s->pkt_first = p;
178 }
179 s->pkt_last = p;
180
181 /* make sure we are notified when we can drain the queue */
182 fdevent_add(&s->fde, FDE_WRITE);
183
184 return 1; /* not ready (backlog) */
185}
186
187static void local_socket_ready(asocket *s)
188{
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100189 /* far side is ready for data, pay attention to
190 readable events */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 fdevent_add(&s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192}
193
194static void local_socket_close(asocket *s)
195{
196 adb_mutex_lock(&socket_list_lock);
197 local_socket_close_locked(s);
198 adb_mutex_unlock(&socket_list_lock);
199}
200
201// be sure to hold the socket list lock when calling this
202static void local_socket_destroy(asocket *s)
203{
204 apacket *p, *n;
Benoit Gobyf366b362012-03-16 14:50:07 -0700205 int exit_on_close = s->exit_on_close;
206
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700207 D("LS(%d): destroying fde.fd=%d", s->id, s->fde.fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208
209 /* IMPORTANT: the remove closes the fd
210 ** that belongs to this socket
211 */
212 fdevent_remove(&s->fde);
213
214 /* dispose of any unwritten data */
215 for(p = s->pkt_first; p; p = n) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700216 D("LS(%d): discarding %d bytes", s->id, p->len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 n = p->next;
218 put_apacket(p);
219 }
220 remove_socket(s);
221 free(s);
Benoit Gobyf366b362012-03-16 14:50:07 -0700222
223 if (exit_on_close) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700224 D("local_socket_destroy: exiting");
Benoit Gobyf366b362012-03-16 14:50:07 -0700225 exit(1);
226 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227}
228
229
230static void local_socket_close_locked(asocket *s)
231{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700232 D("entered local_socket_close_locked. LS(%d) fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 if(s->peer) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700234 D("LS(%d): closing peer. peer->id=%d peer->fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700235 s->id, s->peer->id, s->peer->fd);
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100236 /* Note: it's important to call shutdown before disconnecting from
237 * the peer, this ensures that remote sockets can still get the id
238 * of the local socket they're connected to, to send a CLOSE()
239 * protocol event. */
240 if (s->peer->shutdown)
241 s->peer->shutdown(s->peer);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 s->peer->peer = 0;
243 // tweak to avoid deadlock
Tom Marlin49f18572011-05-13 13:24:55 -0500244 if (s->peer->close == local_socket_close) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 local_socket_close_locked(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500246 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500248 }
249 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 }
251
252 /* If we are already closing, or if there are no
253 ** pending packets, destroy immediately
254 */
255 if (s->closing || s->pkt_first == NULL) {
256 int id = s->id;
257 local_socket_destroy(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700258 D("LS(%d): closed", id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 return;
260 }
261
262 /* otherwise, put on the closing list
263 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700264 D("LS(%d): closing", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 s->closing = 1;
266 fdevent_del(&s->fde, FDE_READ);
267 remove_socket(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700268 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 insert_local_socket(s, &local_socket_closing_list);
270}
271
Dan Albertbac34742015-02-25 17:51:28 -0800272static void local_socket_event_func(int fd, unsigned ev, void* _s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273{
Dan Albertbac34742015-02-25 17:51:28 -0800274 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700275 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall408fa572011-03-16 15:57:42 -0700276
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 /* put the FDE_WRITE processing before the FDE_READ
278 ** in order to simplify the code.
279 */
Dan Albertbac34742015-02-25 17:51:28 -0800280 if (ev & FDE_WRITE) {
281 apacket* p;
282 while ((p = s->pkt_first) != nullptr) {
283 while (p->len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 int r = adb_write(fd, p->ptr, p->len);
Dan Albertbac34742015-02-25 17:51:28 -0800285 if (r == -1) {
286 /* returning here is ok because FDE_READ will
287 ** be processed in the next iteration loop
288 */
289 if (errno == EAGAIN) {
290 return;
291 }
292 } else if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 p->ptr += r;
294 p->len -= r;
295 continue;
296 }
Dan Albertbac34742015-02-25 17:51:28 -0800297
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700298 D(" closing after write because r=%d and errno is %d", r, errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 s->close(s);
300 return;
301 }
302
Dan Albertbac34742015-02-25 17:51:28 -0800303 if (p->len == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 s->pkt_first = p->next;
Dan Albertbac34742015-02-25 17:51:28 -0800305 if (s->pkt_first == 0) {
306 s->pkt_last = 0;
307 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 put_apacket(p);
309 }
310 }
311
Dan Albertbac34742015-02-25 17:51:28 -0800312 /* if we sent the last packet of a closing socket,
313 ** we can now destroy it.
314 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 if (s->closing) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700316 D(" closing because 'closing' is set after write");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 s->close(s);
318 return;
319 }
320
Dan Albertbac34742015-02-25 17:51:28 -0800321 /* no more packets queued, so we can ignore
322 ** writable events again and tell our peer
323 ** to resume writing
324 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 fdevent_del(&s->fde, FDE_WRITE);
326 s->peer->ready(s->peer);
327 }
328
329
Dan Albertbac34742015-02-25 17:51:28 -0800330 if (ev & FDE_READ) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 apacket *p = get_apacket();
332 unsigned char *x = p->data;
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100333 const size_t max_payload = s->get_max_payload();
334 size_t avail = max_payload;
335 int r = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 int is_eof = 0;
337
Dan Albertbac34742015-02-25 17:51:28 -0800338 while (avail > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339 r = adb_read(fd, x, avail);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700340 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu",
Dan Albertbac34742015-02-25 17:51:28 -0800341 s->id, s->fd, r, r < 0 ? errno : 0, avail);
342 if (r == -1) {
343 if (errno == EAGAIN) {
344 break;
345 }
346 } else if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 avail -= r;
348 x += r;
349 continue;
350 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351
Dan Albertbac34742015-02-25 17:51:28 -0800352 /* r = 0 or unhandled error */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 is_eof = 1;
354 break;
355 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700356 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700357 s->id, s->fd, r, is_eof, s->fde.force_eof);
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100358 if ((avail == max_payload) || (s->peer == 0)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 put_apacket(p);
360 } else {
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100361 p->len = max_payload - avail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362
Yabin Cui00674122015-08-26 12:27:40 -0700363 // s->peer->enqueue() may call s->close() and free s,
364 // so save variables for debug printing below.
365 unsigned saved_id = s->id;
366 int saved_fd = s->fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 r = s->peer->enqueue(s->peer, p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700368 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 -0800369
Dan Albertbac34742015-02-25 17:51:28 -0800370 if (r < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 /* error return means they closed us as a side-effect
372 ** and we must return immediately.
373 **
374 ** note that if we still have buffered packets, the
375 ** socket will be placed on the closing socket list.
376 ** this handler function will be called again
377 ** to process FDE_WRITE events.
378 */
379 return;
380 }
381
Dan Albertbac34742015-02-25 17:51:28 -0800382 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383 /* if the remote cannot accept further events,
384 ** we disable notification of READs. They'll
385 ** be enabled again when we get a call to ready()
386 */
387 fdevent_del(&s->fde, FDE_READ);
388 }
389 }
JP Abgrall112445b2011-04-12 22:01:58 -0700390 /* Don't allow a forced eof if data is still there */
Dan Albertbac34742015-02-25 17:51:28 -0800391 if ((s->fde.force_eof && !r) || is_eof) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700392 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d",
Dan Albertbac34742015-02-25 17:51:28 -0800393 is_eof, r, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 s->close(s);
395 }
396 }
397
Dan Albertbac34742015-02-25 17:51:28 -0800398 if (ev & FDE_ERROR){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 /* this should be caught be the next read or write
400 ** catching it here means we may skip the last few
401 ** bytes of readable data.
402 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700403 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
JP Abgrall408fa572011-03-16 15:57:42 -0700404
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 return;
406 }
407}
408
409asocket *create_local_socket(int fd)
410{
Dan Albertbac34742015-02-25 17:51:28 -0800411 asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300412 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 s->fd = fd;
414 s->enqueue = local_socket_enqueue;
415 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100416 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700418 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419
420 fdevent_install(&s->fde, fd, local_socket_event_func, s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700421 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 return s;
423}
424
David Pursell0955c662015-08-31 10:42:13 -0700425asocket *create_local_service_socket(const char *name,
426 const atransport* transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428#if !ADB_HOST
429 if (!strcmp(name,"jdwp")) {
430 return create_jdwp_service_socket();
431 }
432 if (!strcmp(name,"track-jdwp")) {
433 return create_jdwp_tracker_service_socket();
434 }
435#endif
David Pursell0955c662015-08-31 10:42:13 -0700436 int fd = service_to_fd(name, transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 if(fd < 0) return 0;
438
Dan Pasanen98858812014-10-06 12:57:20 -0500439 asocket* s = create_local_socket(fd);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700440 D("LS(%d): bound to '%s' via %d", s->id, name, fd);
Benoit Gobyf366b362012-03-16 14:50:07 -0700441
JP Abgrallf91259a2012-03-30 13:19:11 -0700442#if !ADB_HOST
Dan Pasanen98858812014-10-06 12:57:20 -0500443 char debug[PROPERTY_VALUE_MAX];
jzhuan51297d222013-05-24 17:40:15 -0400444 if (!strncmp(name, "root:", 5))
445 property_get("ro.debuggable", debug, "");
446
Dan Pasanen98858812014-10-06 12:57:20 -0500447 if ((!strncmp(name, "root:", 5) && getuid() != 0 && strcmp(debug, "1") == 0)
448 || (!strncmp(name, "unroot:", 7) && getuid() == 0)
Benoit Gobyaeceb512012-06-12 12:12:18 -0700449 || !strncmp(name, "usb:", 4)
450 || !strncmp(name, "tcpip:", 6)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700451 D("LS(%d): enabling exit_on_close", s->id);
Benoit Gobyf366b362012-03-16 14:50:07 -0700452 s->exit_on_close = 1;
453 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700454#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700455
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 return s;
457}
458
459#if ADB_HOST
460static asocket *create_host_service_socket(const char *name, const char* serial)
461{
462 asocket *s;
463
464 s = host_service_to_socket(name, serial);
465
466 if (s != NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700467 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 return s;
469 }
470
471 return s;
472}
473#endif /* ADB_HOST */
474
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475static int remote_socket_enqueue(asocket *s, apacket *p)
476{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700477 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700478 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 p->msg.command = A_WRTE;
480 p->msg.arg0 = s->peer->id;
481 p->msg.arg1 = s->id;
482 p->msg.data_length = p->len;
483 send_packet(p, s->transport);
484 return 1;
485}
486
487static void remote_socket_ready(asocket *s)
488{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700489 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700490 s->id, s->fd, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 apacket *p = get_apacket();
492 p->msg.command = A_OKAY;
493 p->msg.arg0 = s->peer->id;
494 p->msg.arg1 = s->id;
495 send_packet(p, s->transport);
496}
497
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100498static void remote_socket_shutdown(asocket *s)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700500 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700501 s->id, s->fd, s->peer?s->peer->fd:-1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 apacket *p = get_apacket();
503 p->msg.command = A_CLSE;
504 if(s->peer) {
505 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100506 }
507 p->msg.arg1 = s->id;
508 send_packet(p, s->transport);
509}
510
511static void remote_socket_close(asocket *s)
512{
513 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514 s->peer->peer = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700515 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d",
JP Abgrall408fa572011-03-16 15:57:42 -0700516 s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 s->peer->close(s->peer);
518 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700519 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d",
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100520 s->id, s->fd, s->peer?s->peer->fd:-1);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700521 D("RS(%d): closed", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 free(s);
523}
524
Yabin Cuifd28f322015-08-27 18:50:04 -0700525// Create a remote socket to exchange packets with a remote service through transport
526// |t|. Where |id| is the socket id of the corresponding service on the other
527// side of the transport (it is allocated by the remote side and _cannot_ be 0).
528// Returns a new non-NULL asocket handle.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529asocket *create_remote_socket(unsigned id, atransport *t)
530{
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100531 if (id == 0) fatal("invalid remote socket id (0)");
Yabin Cuifd28f322015-08-27 18:50:04 -0700532 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300534 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 s->id = id;
536 s->enqueue = remote_socket_enqueue;
537 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100538 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 s->close = remote_socket_close;
540 s->transport = t;
541
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700542 D("RS(%d): created", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 return s;
544}
545
546void connect_to_remote(asocket *s, const char *destination)
547{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700548 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549 apacket *p = get_apacket();
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100550 size_t len = strlen(destination) + 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800551
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100552 if(len > (s->get_max_payload()-1)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553 fatal("destination oversized");
554 }
555
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700556 D("LS(%d): connect('%s')", s->id, destination);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557 p->msg.command = A_OPEN;
558 p->msg.arg0 = s->id;
559 p->msg.data_length = len;
560 strcpy((char*) p->data, destination);
561 send_packet(p, s->transport);
562}
563
564
565/* this is used by magic sockets to rig local sockets to
566 send the go-ahead message when they connect */
567static void local_socket_ready_notify(asocket *s)
568{
569 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100570 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800571 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700572 SendOkay(s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800573 s->ready(s);
574}
575
576/* this is used by magic sockets to rig local sockets to
577 send the failure message if they are closed before
578 connected (to avoid closing them without a status message) */
579static void local_socket_close_notify(asocket *s)
580{
581 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100582 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800583 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700584 SendFail(s->fd, "closed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585 s->close(s);
586}
587
Elliott Hughese67f1f82015-04-30 17:32:03 -0700588static unsigned unhex(unsigned char *s, int len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589{
590 unsigned n = 0, c;
591
592 while(len-- > 0) {
593 switch((c = *s++)) {
594 case '0': case '1': case '2':
595 case '3': case '4': case '5':
596 case '6': case '7': case '8':
597 case '9':
598 c -= '0';
599 break;
600 case 'a': case 'b': case 'c':
601 case 'd': case 'e': case 'f':
602 c = c - 'a' + 10;
603 break;
604 case 'A': case 'B': case 'C':
605 case 'D': case 'E': case 'F':
606 c = c - 'A' + 10;
607 break;
608 default:
609 return 0xffffffff;
610 }
611
612 n = (n << 4) | c;
613 }
614
615 return n;
616}
617
Elliott Hughese67f1f82015-04-30 17:32:03 -0700618#if ADB_HOST
619
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700620#define PREFIX(str) { str, sizeof(str) - 1 }
621static const struct prefix_struct {
622 const char *str;
623 const size_t len;
624} prefixes[] = {
625 PREFIX("usb:"),
626 PREFIX("product:"),
627 PREFIX("model:"),
628 PREFIX("device:"),
629};
630static const int num_prefixes = (sizeof(prefixes) / sizeof(prefixes[0]));
631
Terence Haddock28e13902011-03-16 09:43:56 +0100632/* skip_host_serial return the position in a string
633 skipping over the 'serial' parameter in the ADB protocol,
634 where parameter string may be a host:port string containing
635 the protocol delimiter (colon). */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700636static char *skip_host_serial(char *service) {
Terence Haddock28e13902011-03-16 09:43:56 +0100637 char *first_colon, *serial_end;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700638 int i;
Terence Haddock28e13902011-03-16 09:43:56 +0100639
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700640 for (i = 0; i < num_prefixes; i++) {
641 if (!strncmp(service, prefixes[i].str, prefixes[i].len))
642 return strchr(service + prefixes[i].len, ':');
Scott Anderson3608d832012-05-31 12:04:23 -0700643 }
644
Terence Haddock28e13902011-03-16 09:43:56 +0100645 first_colon = strchr(service, ':');
646 if (!first_colon) {
647 /* No colon in service string. */
648 return NULL;
649 }
650 serial_end = first_colon;
651 if (isdigit(serial_end[1])) {
652 serial_end++;
653 while ((*serial_end) && isdigit(*serial_end)) {
654 serial_end++;
655 }
656 if ((*serial_end) != ':') {
657 // Something other than numbers was found, reset the end.
658 serial_end = first_colon;
659 }
660 }
661 return serial_end;
662}
663
Elliott Hughese67f1f82015-04-30 17:32:03 -0700664#endif // ADB_HOST
665
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666static int smart_socket_enqueue(asocket *s, apacket *p)
667{
668 unsigned len;
669#if ADB_HOST
670 char *service = NULL;
671 char* serial = NULL;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700672 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673#endif
674
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700675 D("SS(%d): enqueue %d", s->id, p->len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676
677 if(s->pkt_first == 0) {
678 s->pkt_first = p;
679 s->pkt_last = p;
680 } else {
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100681 if((s->pkt_first->len + p->len) > s->get_max_payload()) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700682 D("SS(%d): overflow", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 put_apacket(p);
684 goto fail;
685 }
686
687 memcpy(s->pkt_first->data + s->pkt_first->len,
688 p->data, p->len);
689 s->pkt_first->len += p->len;
690 put_apacket(p);
691
692 p = s->pkt_first;
693 }
694
695 /* don't bother if we can't decode the length */
696 if(p->len < 4) return 0;
697
698 len = unhex(p->data, 4);
699 if((len < 1) || (len > 1024)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700700 D("SS(%d): bad size (%d)", s->id, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 goto fail;
702 }
703
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700704 D("SS(%d): len is %d", s->id, len );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705 /* can't do anything until we have the full header */
706 if((len + 4) > p->len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700707 D("SS(%d): waiting for %d more bytes", s->id, len+4 - p->len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708 return 0;
709 }
710
711 p->data[len + 4] = 0;
712
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700713 D("SS(%d): '%s'", s->id, (char*) (p->data + 4));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714
715#if ADB_HOST
716 service = (char *)p->data + 4;
717 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
718 char* serial_end;
719 service += strlen("host-serial:");
720
Terence Haddock28e13902011-03-16 09:43:56 +0100721 // serial number should follow "host:" and could be a host:port string.
722 serial_end = skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723 if (serial_end) {
724 *serial_end = 0; // terminate string
725 serial = service;
726 service = serial_end + 1;
727 }
728 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700729 type = kTransportUsb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800730 service += strlen("host-usb:");
731 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700732 type = kTransportLocal;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800733 service += strlen("host-local:");
734 } else if (!strncmp(service, "host:", strlen("host:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700735 type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736 service += strlen("host:");
737 } else {
738 service = NULL;
739 }
740
741 if (service) {
742 asocket *s2;
743
744 /* some requests are handled immediately -- in that
745 ** case the handle_host_request() routine has sent
746 ** the OKAY or FAIL message and all we have to do
747 ** is clean up.
748 */
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700749 if(handle_host_request(service, type, serial, s->peer->fd, s) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800750 /* XXX fail message? */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700751 D( "SS(%d): handled host service '%s'", s->id, service );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752 goto fail;
753 }
754 if (!strncmp(service, "transport", strlen("transport"))) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700755 D( "SS(%d): okay transport", s->id );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756 p->len = 0;
757 return 0;
758 }
759
760 /* try to find a local service with this name.
761 ** if no such service exists, we'll fail out
762 ** and tear down here.
763 */
764 s2 = create_host_service_socket(service, serial);
765 if(s2 == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700766 D( "SS(%d): couldn't create host service '%s'", s->id, service );
Elliott Hughese67f1f82015-04-30 17:32:03 -0700767 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800768 goto fail;
769 }
770
771 /* we've connected to a local host service,
772 ** so we make our peer back into a regular
773 ** local socket and bind it to the new local
774 ** service socket, acknowledge the successful
775 ** connection, and close this smart socket now
776 ** that its work is done.
777 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700778 SendOkay(s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800779
780 s->peer->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100781 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800782 s->peer->close = local_socket_close;
783 s->peer->peer = s2;
784 s2->peer = s->peer;
785 s->peer = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700786 D( "SS(%d): okay", s->id );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787 s->close(s);
788
789 /* initial state is "ready" */
790 s2->ready(s2);
791 return 0;
792 }
793#else /* !ADB_HOST */
794 if (s->transport == NULL) {
Elliott Hughes7be29c82015-04-16 22:54:44 -0700795 std::string error_msg = "unknown failure";
Dan Albertdcd78a12015-05-18 16:43:57 -0700796 s->transport =
797 acquire_one_transport(kCsAny, kTransportAny, NULL, &error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798
799 if (s->transport == NULL) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700800 SendFail(s->peer->fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800801 goto fail;
802 }
803 }
804#endif
805
Dan Albertdcd78a12015-05-18 16:43:57 -0700806 if(!(s->transport) || (s->transport->connection_state == kCsOffline)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807 /* if there's no remote we fail the connection
808 ** right here and terminate it
809 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700810 SendFail(s->peer->fd, "device offline (x)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800811 goto fail;
812 }
813
814
815 /* instrument our peer to pass the success or fail
816 ** message back once it connects or closes, then
817 ** detach from it, request the connection, and
818 ** tear down
819 */
820 s->peer->ready = local_socket_ready_notify;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100821 s->peer->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822 s->peer->close = local_socket_close_notify;
823 s->peer->peer = 0;
824 /* give him our transport and upref it */
825 s->peer->transport = s->transport;
826
827 connect_to_remote(s->peer, (char*) (p->data + 4));
828 s->peer = 0;
829 s->close(s);
830 return 1;
831
832fail:
833 /* we're going to close our peer as a side-effect, so
834 ** return -1 to signal that state to the local socket
835 ** who is enqueueing against us
836 */
837 s->close(s);
838 return -1;
839}
840
841static void smart_socket_ready(asocket *s)
842{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700843 D("SS(%d): ready", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800844}
845
846static void smart_socket_close(asocket *s)
847{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700848 D("SS(%d): closed", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800849 if(s->pkt_first){
850 put_apacket(s->pkt_first);
851 }
852 if(s->peer) {
853 s->peer->peer = 0;
854 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500855 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800856 }
857 free(s);
858}
859
Benoit Goby9470c2f2013-02-20 15:04:53 -0800860static asocket *create_smart_socket(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800861{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700862 D("Creating smart socket");
Dan Albertbac34742015-02-25 17:51:28 -0800863 asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300864 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800865 s->enqueue = smart_socket_enqueue;
866 s->ready = smart_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100867 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800868 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800869
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700870 D("SS(%d)", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800871 return s;
872}
873
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800874void connect_to_smartsocket(asocket *s)
875{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700876 D("Connecting to smart socket");
Benoit Goby9470c2f2013-02-20 15:04:53 -0800877 asocket *ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800878 s->peer = ss;
879 ss->peer = s;
880 s->ready(s);
881}
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100882
883size_t asocket::get_max_payload() const {
884 size_t max_payload = MAX_PAYLOAD;
885 if (transport) {
886 max_payload = std::min(max_payload, transport->get_max_payload());
887 }
888 if (peer && peer->transport) {
889 max_payload = std::min(max_payload, peer->transport->get_max_payload());
890 }
891 return max_payload;
892}