blob: c05903f9e5e3d8823b8094e692b845e8fa1a38c9 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG SOCKETS
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
Dan Albertb302d122015-02-24 15:51:19 -080021#include <ctype.h>
22#include <errno.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080023#include <stdio.h>
24#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080025#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080026#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080027
Spencer Low28bc2cb2015-11-07 18:51:54 -080028#include <algorithm>
Josh Gao0f1a20a2016-05-17 17:46:27 -070029#include <mutex>
David Pursellc929c6f2016-03-01 08:58:26 -080030#include <string>
31#include <vector>
Spencer Low28bc2cb2015-11-07 18:51:54 -080032
Dan Albertb302d122015-02-24 15:51:19 -080033#if !ADB_HOST
Elliott Hughes8b249d22016-09-23 15:40:03 -070034#include <android-base/properties.h>
Mark Salyzync75f65f2016-03-28 15:52:13 -070035#include <private/android_logger.h>
Dan Albertb302d122015-02-24 15:51:19 -080036#endif
Dan Albertdb6fe642015-03-19 15:21:08 -070037
38#include "adb.h"
39#include "adb_io.h"
Dan Albertb302d122015-02-24 15:51:19 -080040#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080041
Josh Gao0f1a20a2016-05-17 17:46:27 -070042static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080043static unsigned local_socket_next_id = 1;
44
45static asocket local_socket_list = {
Josh Gaoef550fe2016-05-17 16:55:06 -070046 .next = &local_socket_list, .prev = &local_socket_list,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080047};
48
49/* the the list of currently closing local sockets.
50** these have no peer anymore, but still packets to
51** write to their fd.
52*/
53static asocket local_socket_closing_list = {
Josh Gaoef550fe2016-05-17 16:55:06 -070054 .next = &local_socket_closing_list, .prev = &local_socket_closing_list,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080055};
56
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010057// Parse the global list of sockets to find one with id |local_id|.
58// If |peer_id| is not 0, also check that it is connected to a peer
59// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
Josh Gaoef550fe2016-05-17 16:55:06 -070060asocket* find_local_socket(unsigned local_id, unsigned peer_id) {
61 asocket* s;
62 asocket* result = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080063
Josh Gao0f1a20a2016-05-17 17:46:27 -070064 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030065 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
Josh Gaoef550fe2016-05-17 16:55:06 -070066 if (s->id != local_id) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010067 continue;
Josh Gaoef550fe2016-05-17 16:55:06 -070068 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010069 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030070 result = s;
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030071 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010072 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080073 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080074
75 return result;
76}
77
Josh Gaoef550fe2016-05-17 16:55:06 -070078static void insert_local_socket(asocket* s, asocket* list) {
79 s->next = list;
80 s->prev = s->next->prev;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080081 s->prev->next = s;
82 s->next->prev = s;
83}
84
Josh Gaoef550fe2016-05-17 16:55:06 -070085void install_local_socket(asocket* s) {
Josh Gao0f1a20a2016-05-17 17:46:27 -070086 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080087
88 s->id = local_socket_next_id++;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010089
90 // Socket ids should never be 0.
Josh Gaoef550fe2016-05-17 16:55:06 -070091 if (local_socket_next_id == 0) {
Josh Gao0f1a20a2016-05-17 17:46:27 -070092 fatal("local socket id overflow");
Josh Gaoef550fe2016-05-17 16:55:06 -070093 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010094
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080095 insert_local_socket(s, &local_socket_list);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080096}
97
Josh Gaoef550fe2016-05-17 16:55:06 -070098void remove_socket(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080099 // socket_list_lock should already be held
Josh Gaoef550fe2016-05-17 16:55:06 -0700100 if (s->prev && s->next) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800101 s->prev->next = s->next;
102 s->next->prev = s->prev;
103 s->next = 0;
104 s->prev = 0;
105 s->id = 0;
106 }
107}
108
Josh Gaoef550fe2016-05-17 16:55:06 -0700109void close_all_sockets(atransport* t) {
110 asocket* s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800111
Josh Gaoef550fe2016-05-17 16:55:06 -0700112 /* this is a little gross, but since s->close() *will* modify
113 ** the list out from under you, your options are limited.
114 */
Josh Gao0f1a20a2016-05-17 17:46:27 -0700115 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800116restart:
Josh Gaoef550fe2016-05-17 16:55:06 -0700117 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
118 if (s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gao80814e12016-05-18 10:39:48 -0700119 s->close(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800120 goto restart;
121 }
122 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800123}
124
Josh Gaoef550fe2016-05-17 16:55:06 -0700125static int local_socket_enqueue(asocket* s, apacket* p) {
Josh Gao67ac3792016-10-06 13:31:44 -0700126 D("LS(%d): enqueue %zu", s->id, p->len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800127
128 p->ptr = p->data;
129
Josh Gaoef550fe2016-05-17 16:55:06 -0700130 /* if there is already data queue'd, we will receive
131 ** events when it's time to write. just add this to
132 ** the tail
133 */
134 if (s->pkt_first) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800135 goto enqueue;
136 }
137
Josh Gaoef550fe2016-05-17 16:55:06 -0700138 /* write as much as we can, until we
139 ** would block or there is an error/eof
140 */
141 while (p->len > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800142 int r = adb_write(s->fd, p->ptr, p->len);
Josh Gaoef550fe2016-05-17 16:55:06 -0700143 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800144 p->len -= r;
145 p->ptr += r;
146 continue;
147 }
Josh Gaoef550fe2016-05-17 16:55:06 -0700148 if ((r == 0) || (errno != EAGAIN)) {
149 D("LS(%d): not ready, errno=%d: %s", s->id, errno, strerror(errno));
Yabin Cui2ce9d562015-09-15 16:27:09 -0700150 put_apacket(p);
151 s->has_write_error = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800152 s->close(s);
153 return 1; /* not ready (error) */
154 } else {
155 break;
156 }
157 }
158
Josh Gaoef550fe2016-05-17 16:55:06 -0700159 if (p->len == 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800160 put_apacket(p);
161 return 0; /* ready for more data */
162 }
163
164enqueue:
165 p->next = 0;
Josh Gaoef550fe2016-05-17 16:55:06 -0700166 if (s->pkt_first) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800167 s->pkt_last->next = p;
168 } else {
169 s->pkt_first = p;
170 }
171 s->pkt_last = p;
172
Josh Gaoef550fe2016-05-17 16:55:06 -0700173 /* make sure we are notified when we can drain the queue */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800174 fdevent_add(&s->fde, FDE_WRITE);
175
176 return 1; /* not ready (backlog) */
177}
178
Josh Gaoef550fe2016-05-17 16:55:06 -0700179static void local_socket_ready(asocket* s) {
Nanik Tolaramc624a7e2015-02-18 22:53:37 +1100180 /* far side is ready for data, pay attention to
181 readable events */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800182 fdevent_add(&s->fde, FDE_READ);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800183}
184
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800185// be sure to hold the socket list lock when calling this
Josh Gaoef550fe2016-05-17 16:55:06 -0700186static void local_socket_destroy(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800187 apacket *p, *n;
Benoit Goby88468f32012-03-16 14:50:07 -0700188 int exit_on_close = s->exit_on_close;
189
Yabin Cui815ad882015-09-02 17:44:28 -0700190 D("LS(%d): destroying fde.fd=%d", s->id, s->fde.fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800191
Josh Gaoef550fe2016-05-17 16:55:06 -0700192 /* IMPORTANT: the remove closes the fd
193 ** that belongs to this socket
194 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800195 fdevent_remove(&s->fde);
196
Josh Gaoef550fe2016-05-17 16:55:06 -0700197 /* dispose of any unwritten data */
198 for (p = s->pkt_first; p; p = n) {
Josh Gao67ac3792016-10-06 13:31:44 -0700199 D("LS(%d): discarding %zu bytes", s->id, p->len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800200 n = p->next;
201 put_apacket(p);
202 }
203 remove_socket(s);
204 free(s);
Benoit Goby88468f32012-03-16 14:50:07 -0700205
206 if (exit_on_close) {
Yabin Cui815ad882015-09-02 17:44:28 -0700207 D("local_socket_destroy: exiting");
Benoit Goby88468f32012-03-16 14:50:07 -0700208 exit(1);
209 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210}
211
Josh Gao0f1a20a2016-05-17 17:46:27 -0700212static void local_socket_close(asocket* s) {
213 D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd);
214 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gaoef550fe2016-05-17 16:55:06 -0700215 if (s->peer) {
216 D("LS(%d): closing peer. peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100217 /* Note: it's important to call shutdown before disconnecting from
218 * the peer, this ensures that remote sockets can still get the id
219 * of the local socket they're connected to, to send a CLOSE()
220 * protocol event. */
Josh Gaoef550fe2016-05-17 16:55:06 -0700221 if (s->peer->shutdown) {
222 s->peer->shutdown(s->peer);
223 }
Josh Gao0f1a20a2016-05-17 17:46:27 -0700224 s->peer->peer = nullptr;
225 s->peer->close(s->peer);
226 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800227 }
228
Josh Gaoef550fe2016-05-17 16:55:06 -0700229 /* If we are already closing, or if there are no
230 ** pending packets, destroy immediately
231 */
Yabin Cui2ce9d562015-09-15 16:27:09 -0700232 if (s->closing || s->has_write_error || s->pkt_first == NULL) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700233 int id = s->id;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800234 local_socket_destroy(s);
Yabin Cui815ad882015-09-02 17:44:28 -0700235 D("LS(%d): closed", id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800236 return;
237 }
238
Josh Gaoef550fe2016-05-17 16:55:06 -0700239 /* otherwise, put on the closing list
240 */
Yabin Cui815ad882015-09-02 17:44:28 -0700241 D("LS(%d): closing", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800242 s->closing = 1;
243 fdevent_del(&s->fde, FDE_READ);
244 remove_socket(s);
Yabin Cui815ad882015-09-02 17:44:28 -0700245 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800246 insert_local_socket(s, &local_socket_closing_list);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700247 CHECK_EQ(FDE_WRITE, s->fde.state & FDE_WRITE);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800248}
249
Josh Gaoef550fe2016-05-17 16:55:06 -0700250static void local_socket_event_func(int fd, unsigned ev, void* _s) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800251 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui815ad882015-09-02 17:44:28 -0700252 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700253
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800254 /* put the FDE_WRITE processing before the FDE_READ
255 ** in order to simplify the code.
256 */
Dan Albertf30d73c2015-02-25 17:51:28 -0800257 if (ev & FDE_WRITE) {
258 apacket* p;
259 while ((p = s->pkt_first) != nullptr) {
260 while (p->len > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800261 int r = adb_write(fd, p->ptr, p->len);
Dan Albertf30d73c2015-02-25 17:51:28 -0800262 if (r == -1) {
263 /* returning here is ok because FDE_READ will
264 ** be processed in the next iteration loop
265 */
266 if (errno == EAGAIN) {
267 return;
268 }
269 } else if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800270 p->ptr += r;
271 p->len -= r;
272 continue;
273 }
Dan Albertf30d73c2015-02-25 17:51:28 -0800274
Yabin Cui815ad882015-09-02 17:44:28 -0700275 D(" closing after write because r=%d and errno is %d", r, errno);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700276 s->has_write_error = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800277 s->close(s);
278 return;
279 }
280
Dan Albertf30d73c2015-02-25 17:51:28 -0800281 if (p->len == 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800282 s->pkt_first = p->next;
Dan Albertf30d73c2015-02-25 17:51:28 -0800283 if (s->pkt_first == 0) {
284 s->pkt_last = 0;
285 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800286 put_apacket(p);
287 }
288 }
289
Dan Albertf30d73c2015-02-25 17:51:28 -0800290 /* if we sent the last packet of a closing socket,
291 ** we can now destroy it.
292 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800293 if (s->closing) {
Yabin Cui815ad882015-09-02 17:44:28 -0700294 D(" closing because 'closing' is set after write");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800295 s->close(s);
296 return;
297 }
298
Dan Albertf30d73c2015-02-25 17:51:28 -0800299 /* no more packets queued, so we can ignore
300 ** writable events again and tell our peer
301 ** to resume writing
302 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800303 fdevent_del(&s->fde, FDE_WRITE);
304 s->peer->ready(s->peer);
305 }
306
Dan Albertf30d73c2015-02-25 17:51:28 -0800307 if (ev & FDE_READ) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700308 apacket* p = get_apacket();
Josh Gao67ac3792016-10-06 13:31:44 -0700309 char* x = p->data;
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100310 const size_t max_payload = s->get_max_payload();
311 size_t avail = max_payload;
312 int r = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800313 int is_eof = 0;
314
Dan Albertf30d73c2015-02-25 17:51:28 -0800315 while (avail > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800316 r = adb_read(fd, x, avail);
Josh Gaoef550fe2016-05-17 16:55:06 -0700317 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r,
318 r < 0 ? errno : 0, avail);
Dan Albertf30d73c2015-02-25 17:51:28 -0800319 if (r == -1) {
320 if (errno == EAGAIN) {
321 break;
322 }
323 } else if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800324 avail -= r;
325 x += r;
326 continue;
327 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800328
Dan Albertf30d73c2015-02-25 17:51:28 -0800329 /* r = 0 or unhandled error */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800330 is_eof = 1;
331 break;
332 }
Josh Gaoef550fe2016-05-17 16:55:06 -0700333 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d", s->id, s->fd, r, is_eof,
334 s->fde.force_eof);
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100335 if ((avail == max_payload) || (s->peer == 0)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800336 put_apacket(p);
337 } else {
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100338 p->len = max_payload - avail;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800339
Yabin Cui8e503aa2015-08-26 12:27:40 -0700340 // s->peer->enqueue() may call s->close() and free s,
341 // so save variables for debug printing below.
342 unsigned saved_id = s->id;
343 int saved_fd = s->fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800344 r = s->peer->enqueue(s->peer, p);
Yabin Cui815ad882015-09-02 17:44:28 -0700345 D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800346
Dan Albertf30d73c2015-02-25 17:51:28 -0800347 if (r < 0) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700348 /* error return means they closed us as a side-effect
349 ** and we must return immediately.
350 **
351 ** note that if we still have buffered packets, the
352 ** socket will be placed on the closing socket list.
353 ** this handler function will be called again
354 ** to process FDE_WRITE events.
355 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800356 return;
357 }
358
Dan Albertf30d73c2015-02-25 17:51:28 -0800359 if (r > 0) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700360 /* if the remote cannot accept further events,
361 ** we disable notification of READs. They'll
362 ** be enabled again when we get a call to ready()
363 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800364 fdevent_del(&s->fde, FDE_READ);
365 }
366 }
JP Abgrall18d2d652011-04-12 22:01:58 -0700367 /* Don't allow a forced eof if data is still there */
Dan Albertf30d73c2015-02-25 17:51:28 -0800368 if ((s->fde.force_eof && !r) || is_eof) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700369 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d", is_eof, r, s->fde.force_eof);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800370 s->close(s);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700371 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800372 }
373 }
374
Josh Gaoef550fe2016-05-17 16:55:06 -0700375 if (ev & FDE_ERROR) {
376 /* this should be caught be the next read or write
377 ** catching it here means we may skip the last few
378 ** bytes of readable data.
379 */
Yabin Cui815ad882015-09-02 17:44:28 -0700380 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800381 return;
382 }
383}
384
Josh Gaoef550fe2016-05-17 16:55:06 -0700385asocket* create_local_socket(int fd) {
386 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
387 if (s == NULL) {
388 fatal("cannot allocate socket");
389 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800390 s->fd = fd;
391 s->enqueue = local_socket_enqueue;
392 s->ready = local_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100393 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800394 s->close = local_socket_close;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700395 install_local_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800396
397 fdevent_install(&s->fde, fd, local_socket_event_func, s);
Yabin Cui815ad882015-09-02 17:44:28 -0700398 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800399 return s;
400}
401
Josh Gaoef550fe2016-05-17 16:55:06 -0700402asocket* create_local_service_socket(const char* name, const atransport* transport) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800403#if !ADB_HOST
Josh Gaoef550fe2016-05-17 16:55:06 -0700404 if (!strcmp(name, "jdwp")) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800405 return create_jdwp_service_socket();
406 }
Josh Gaoef550fe2016-05-17 16:55:06 -0700407 if (!strcmp(name, "track-jdwp")) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800408 return create_jdwp_tracker_service_socket();
409 }
410#endif
David Pursell8da19a42015-08-31 10:42:13 -0700411 int fd = service_to_fd(name, transport);
Josh Gaoef550fe2016-05-17 16:55:06 -0700412 if (fd < 0) {
Elliott Hughesc3d1c112016-06-15 14:46:56 -0700413 return nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700414 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800415
Dan Pasanenfb787d92014-10-06 12:57:20 -0500416 asocket* s = create_local_socket(fd);
Yabin Cui815ad882015-09-02 17:44:28 -0700417 D("LS(%d): bound to '%s' via %d", s->id, name, fd);
Benoit Goby88468f32012-03-16 14:50:07 -0700418
JP Abgralla84bd682012-03-30 13:19:11 -0700419#if !ADB_HOST
Mark Salyzync75f65f2016-03-28 15:52:13 -0700420 if ((!strncmp(name, "root:", 5) && getuid() != 0 && __android_log_is_debuggable()) ||
Josh Gaoef550fe2016-05-17 16:55:06 -0700421 (!strncmp(name, "unroot:", 7) && getuid() == 0) ||
422 !strncmp(name, "usb:", 4) ||
423 !strncmp(name, "tcpip:", 6)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700424 D("LS(%d): enabling exit_on_close", s->id);
Benoit Goby88468f32012-03-16 14:50:07 -0700425 s->exit_on_close = 1;
426 }
JP Abgralla84bd682012-03-30 13:19:11 -0700427#endif
Benoit Goby88468f32012-03-16 14:50:07 -0700428
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800429 return s;
430}
431
432#if ADB_HOST
Josh Gaoef550fe2016-05-17 16:55:06 -0700433static asocket* create_host_service_socket(const char* name, const char* serial) {
434 asocket* s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800435
436 s = host_service_to_socket(name, serial);
437
438 if (s != NULL) {
Yabin Cui815ad882015-09-02 17:44:28 -0700439 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800440 return s;
441 }
442
443 return s;
444}
445#endif /* ADB_HOST */
446
Josh Gaoef550fe2016-05-17 16:55:06 -0700447static int remote_socket_enqueue(asocket* s, apacket* p) {
448 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800449 p->msg.command = A_WRTE;
450 p->msg.arg0 = s->peer->id;
451 p->msg.arg1 = s->id;
452 p->msg.data_length = p->len;
453 send_packet(p, s->transport);
454 return 1;
455}
456
Josh Gaoef550fe2016-05-17 16:55:06 -0700457static void remote_socket_ready(asocket* s) {
458 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
459 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800460 p->msg.command = A_OKAY;
461 p->msg.arg0 = s->peer->id;
462 p->msg.arg1 = s->id;
463 send_packet(p, s->transport);
464}
465
Josh Gaoef550fe2016-05-17 16:55:06 -0700466static void remote_socket_shutdown(asocket* s) {
467 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
468 s->peer ? s->peer->fd : -1);
469 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800470 p->msg.command = A_CLSE;
Josh Gaoef550fe2016-05-17 16:55:06 -0700471 if (s->peer) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800472 p->msg.arg0 = s->peer->id;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100473 }
474 p->msg.arg1 = s->id;
475 send_packet(p, s->transport);
476}
477
Josh Gaoef550fe2016-05-17 16:55:06 -0700478static void remote_socket_close(asocket* s) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100479 if (s->peer) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800480 s->peer->peer = 0;
Josh Gaoef550fe2016-05-17 16:55:06 -0700481 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800482 s->peer->close(s->peer);
483 }
Josh Gaoef550fe2016-05-17 16:55:06 -0700484 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
485 s->peer ? s->peer->fd : -1);
Yabin Cui815ad882015-09-02 17:44:28 -0700486 D("RS(%d): closed", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800487 free(s);
488}
489
Yabin Cui70ec57b2015-08-27 18:50:04 -0700490// Create a remote socket to exchange packets with a remote service through transport
491// |t|. Where |id| is the socket id of the corresponding service on the other
492// side of the transport (it is allocated by the remote side and _cannot_ be 0).
493// Returns a new non-NULL asocket handle.
Josh Gaoef550fe2016-05-17 16:55:06 -0700494asocket* create_remote_socket(unsigned id, atransport* t) {
495 if (id == 0) {
496 fatal("invalid remote socket id (0)");
497 }
Yabin Cui70ec57b2015-08-27 18:50:04 -0700498 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800499
Josh Gaoef550fe2016-05-17 16:55:06 -0700500 if (s == NULL) {
501 fatal("cannot allocate socket");
502 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800503 s->id = id;
504 s->enqueue = remote_socket_enqueue;
505 s->ready = remote_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100506 s->shutdown = remote_socket_shutdown;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800507 s->close = remote_socket_close;
508 s->transport = t;
509
Yabin Cui815ad882015-09-02 17:44:28 -0700510 D("RS(%d): created", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800511 return s;
512}
513
Josh Gaoef550fe2016-05-17 16:55:06 -0700514void connect_to_remote(asocket* s, const char* destination) {
Yabin Cui815ad882015-09-02 17:44:28 -0700515 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gaoef550fe2016-05-17 16:55:06 -0700516 apacket* p = get_apacket();
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100517 size_t len = strlen(destination) + 1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800518
Josh Gaoef550fe2016-05-17 16:55:06 -0700519 if (len > (s->get_max_payload() - 1)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800520 fatal("destination oversized");
521 }
522
Yabin Cui815ad882015-09-02 17:44:28 -0700523 D("LS(%d): connect('%s')", s->id, destination);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800524 p->msg.command = A_OPEN;
525 p->msg.arg0 = s->id;
526 p->msg.data_length = len;
Josh Gaoef550fe2016-05-17 16:55:06 -0700527 strcpy((char*)p->data, destination);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800528 send_packet(p, s->transport);
529}
530
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800531/* this is used by magic sockets to rig local sockets to
532 send the go-ahead message when they connect */
Josh Gaoef550fe2016-05-17 16:55:06 -0700533static void local_socket_ready_notify(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800534 s->ready = local_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100535 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800536 s->close = local_socket_close;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700537 SendOkay(s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800538 s->ready(s);
539}
540
541/* this is used by magic sockets to rig local sockets to
542 send the failure message if they are closed before
543 connected (to avoid closing them without a status message) */
Josh Gaoef550fe2016-05-17 16:55:06 -0700544static void local_socket_close_notify(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800545 s->ready = local_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100546 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800547 s->close = local_socket_close;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700548 SendFail(s->fd, "closed");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800549 s->close(s);
550}
551
Josh Gao67ac3792016-10-06 13:31:44 -0700552static unsigned unhex(char* s, int len) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800553 unsigned n = 0, c;
554
Josh Gaoef550fe2016-05-17 16:55:06 -0700555 while (len-- > 0) {
556 switch ((c = *s++)) {
557 case '0':
558 case '1':
559 case '2':
560 case '3':
561 case '4':
562 case '5':
563 case '6':
564 case '7':
565 case '8':
566 case '9':
567 c -= '0';
568 break;
569 case 'a':
570 case 'b':
571 case 'c':
572 case 'd':
573 case 'e':
574 case 'f':
575 c = c - 'a' + 10;
576 break;
577 case 'A':
578 case 'B':
579 case 'C':
580 case 'D':
581 case 'E':
582 case 'F':
583 c = c - 'A' + 10;
584 break;
585 default:
586 return 0xffffffff;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800587 }
588
589 n = (n << 4) | c;
590 }
591
592 return n;
593}
594
Elliott Hughes88b4c852015-04-30 17:32:03 -0700595#if ADB_HOST
596
David Pursellc929c6f2016-03-01 08:58:26 -0800597namespace internal {
Scott Anderson27042382012-05-30 18:11:27 -0700598
David Pursellc929c6f2016-03-01 08:58:26 -0800599// Returns the position in |service| following the target serial parameter. Serial format can be
600// any of:
601// * [tcp:|udp:]<serial>[:<port>]:<command>
602// * <prefix>:<serial>:<command>
603// Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}.
604//
605// The returned pointer will point to the ':' just before <command>, or nullptr if not found.
Dan Austinca35e9e2016-03-28 15:32:37 -0700606char* skip_host_serial(char* service) {
David Pursellc929c6f2016-03-01 08:58:26 -0800607 static const std::vector<std::string>& prefixes =
608 *(new std::vector<std::string>{"usb:", "product:", "model:", "device:"});
Terence Haddock6c670402011-03-16 09:43:56 +0100609
David Pursellc929c6f2016-03-01 08:58:26 -0800610 for (const std::string& prefix : prefixes) {
611 if (!strncmp(service, prefix.c_str(), prefix.length())) {
612 return strchr(service + prefix.length(), ':');
613 }
Scott Anderson090e5cb2012-05-31 12:04:23 -0700614 }
615
David Pursellc929c6f2016-03-01 08:58:26 -0800616 // For fastboot compatibility, ignore protocol prefixes.
617 if (!strncmp(service, "tcp:", 4) || !strncmp(service, "udp:", 4)) {
618 service += 4;
619 }
620
David Pursell24b62a72016-09-21 12:08:37 -0700621 // Check for an IPv6 address. `adb connect` creates the serial number from the canonical
622 // network address so it will always have the [] delimiters.
623 if (service[0] == '[') {
624 char* ipv6_end = strchr(service, ']');
625 if (ipv6_end != nullptr) {
626 service = ipv6_end;
627 }
628 }
629
630 // The next colon we find must either begin the port field or the command field.
631 char* colon_ptr = strchr(service, ':');
632 if (!colon_ptr) {
David Pursellc929c6f2016-03-01 08:58:26 -0800633 // No colon in service string.
634 return nullptr;
Terence Haddock6c670402011-03-16 09:43:56 +0100635 }
David Pursellc929c6f2016-03-01 08:58:26 -0800636
David Pursell24b62a72016-09-21 12:08:37 -0700637 // If the next field is only decimal digits and ends with another colon, it's a port.
638 char* serial_end = colon_ptr;
Terence Haddock6c670402011-03-16 09:43:56 +0100639 if (isdigit(serial_end[1])) {
640 serial_end++;
David Pursellc929c6f2016-03-01 08:58:26 -0800641 while (*serial_end && isdigit(*serial_end)) {
Terence Haddock6c670402011-03-16 09:43:56 +0100642 serial_end++;
643 }
David Pursellc929c6f2016-03-01 08:58:26 -0800644 if (*serial_end != ':') {
David Pursell24b62a72016-09-21 12:08:37 -0700645 // Something other than "<port>:" was found, this must be the command field instead.
646 serial_end = colon_ptr;
Terence Haddock6c670402011-03-16 09:43:56 +0100647 }
648 }
649 return serial_end;
650}
651
David Pursellc929c6f2016-03-01 08:58:26 -0800652} // namespace internal
653
Josh Gaoef550fe2016-05-17 16:55:06 -0700654#endif // ADB_HOST
Elliott Hughes88b4c852015-04-30 17:32:03 -0700655
Josh Gaoef550fe2016-05-17 16:55:06 -0700656static int smart_socket_enqueue(asocket* s, apacket* p) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800657 unsigned len;
658#if ADB_HOST
Josh Gaoef550fe2016-05-17 16:55:06 -0700659 char* service = nullptr;
Elliott Hughes67943d12015-10-07 14:55:10 -0700660 char* serial = nullptr;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700661 TransportType type = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800662#endif
663
Josh Gao67ac3792016-10-06 13:31:44 -0700664 D("SS(%d): enqueue %zu", s->id, p->len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800665
Josh Gaoef550fe2016-05-17 16:55:06 -0700666 if (s->pkt_first == 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800667 s->pkt_first = p;
668 s->pkt_last = p;
669 } else {
Josh Gaoef550fe2016-05-17 16:55:06 -0700670 if ((s->pkt_first->len + p->len) > s->get_max_payload()) {
Yabin Cui815ad882015-09-02 17:44:28 -0700671 D("SS(%d): overflow", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800672 put_apacket(p);
673 goto fail;
674 }
675
Josh Gaoef550fe2016-05-17 16:55:06 -0700676 memcpy(s->pkt_first->data + s->pkt_first->len, p->data, p->len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800677 s->pkt_first->len += p->len;
678 put_apacket(p);
679
680 p = s->pkt_first;
681 }
682
Josh Gao9055a582016-01-15 14:35:54 -0800683 /* don't bother if we can't decode the length */
Josh Gaoef550fe2016-05-17 16:55:06 -0700684 if (p->len < 4) {
685 return 0;
686 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800687
688 len = unhex(p->data, 4);
Josh Gao9055a582016-01-15 14:35:54 -0800689 if ((len < 1) || (len > MAX_PAYLOAD_V1)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700690 D("SS(%d): bad size (%d)", s->id, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800691 goto fail;
692 }
693
Josh Gaoef550fe2016-05-17 16:55:06 -0700694 D("SS(%d): len is %d", s->id, len);
Josh Gao9055a582016-01-15 14:35:54 -0800695 /* can't do anything until we have the full header */
Josh Gaoef550fe2016-05-17 16:55:06 -0700696 if ((len + 4) > p->len) {
Josh Gao67ac3792016-10-06 13:31:44 -0700697 D("SS(%d): waiting for %zu more bytes", s->id, len + 4 - p->len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800698 return 0;
699 }
700
701 p->data[len + 4] = 0;
702
Josh Gaoef550fe2016-05-17 16:55:06 -0700703 D("SS(%d): '%s'", s->id, (char*)(p->data + 4));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800704
705#if ADB_HOST
Josh Gaoef550fe2016-05-17 16:55:06 -0700706 service = (char*)p->data + 4;
707 if (!strncmp(service, "host-serial:", strlen("host-serial:"))) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800708 char* serial_end;
709 service += strlen("host-serial:");
710
Terence Haddock6c670402011-03-16 09:43:56 +0100711 // serial number should follow "host:" and could be a host:port string.
David Pursellc929c6f2016-03-01 08:58:26 -0800712 serial_end = internal::skip_host_serial(service);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800713 if (serial_end) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700714 *serial_end = 0; // terminate string
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800715 serial = service;
716 service = serial_end + 1;
717 }
718 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700719 type = kTransportUsb;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800720 service += strlen("host-usb:");
721 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700722 type = kTransportLocal;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800723 service += strlen("host-local:");
724 } else if (!strncmp(service, "host:", strlen("host:"))) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700725 type = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800726 service += strlen("host:");
727 } else {
Elliott Hughes67943d12015-10-07 14:55:10 -0700728 service = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800729 }
730
731 if (service) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700732 asocket* s2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800733
Josh Gaoef550fe2016-05-17 16:55:06 -0700734 /* some requests are handled immediately -- in that
735 ** case the handle_host_request() routine has sent
736 ** the OKAY or FAIL message and all we have to do
737 ** is clean up.
738 */
739 if (handle_host_request(service, type, serial, s->peer->fd, s) == 0) {
740 /* XXX fail message? */
741 D("SS(%d): handled host service '%s'", s->id, service);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800742 goto fail;
743 }
744 if (!strncmp(service, "transport", strlen("transport"))) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700745 D("SS(%d): okay transport", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800746 p->len = 0;
747 return 0;
748 }
749
Josh Gaoef550fe2016-05-17 16:55:06 -0700750 /* try to find a local service with this name.
751 ** if no such service exists, we'll fail out
752 ** and tear down here.
753 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800754 s2 = create_host_service_socket(service, serial);
Josh Gaoef550fe2016-05-17 16:55:06 -0700755 if (s2 == 0) {
756 D("SS(%d): couldn't create host service '%s'", s->id, service);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700757 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800758 goto fail;
759 }
760
Josh Gaoef550fe2016-05-17 16:55:06 -0700761 /* we've connected to a local host service,
762 ** so we make our peer back into a regular
763 ** local socket and bind it to the new local
764 ** service socket, acknowledge the successful
765 ** connection, and close this smart socket now
766 ** that its work is done.
767 */
Elliott Hughes88b4c852015-04-30 17:32:03 -0700768 SendOkay(s->peer->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800769
770 s->peer->ready = local_socket_ready;
Elliott Hughes67943d12015-10-07 14:55:10 -0700771 s->peer->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800772 s->peer->close = local_socket_close;
773 s->peer->peer = s2;
774 s2->peer = s->peer;
775 s->peer = 0;
Josh Gaoef550fe2016-05-17 16:55:06 -0700776 D("SS(%d): okay", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800777 s->close(s);
778
Josh Gaoef550fe2016-05-17 16:55:06 -0700779 /* initial state is "ready" */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800780 s2->ready(s2);
781 return 0;
782 }
783#else /* !ADB_HOST */
Elliott Hughes67943d12015-10-07 14:55:10 -0700784 if (s->transport == nullptr) {
Elliott Hughesab882422015-04-16 22:54:44 -0700785 std::string error_msg = "unknown failure";
Elliott Hughes67943d12015-10-07 14:55:10 -0700786 s->transport = acquire_one_transport(kTransportAny, nullptr, nullptr, &error_msg);
787 if (s->transport == nullptr) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700788 SendFail(s->peer->fd, error_msg);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800789 goto fail;
790 }
791 }
792#endif
793
Josh Gao4e562502016-10-27 14:01:08 -0700794 if (!s->transport) {
795 SendFail(s->peer->fd, "device offline (no transport)");
796 goto fail;
797 } else if (s->transport->connection_state == kCsOffline) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700798 /* if there's no remote we fail the connection
799 ** right here and terminate it
800 */
Josh Gao4e562502016-10-27 14:01:08 -0700801 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800802 goto fail;
803 }
804
Josh Gaoef550fe2016-05-17 16:55:06 -0700805 /* instrument our peer to pass the success or fail
806 ** message back once it connects or closes, then
807 ** detach from it, request the connection, and
808 ** tear down
809 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800810 s->peer->ready = local_socket_ready_notify;
Elliott Hughes67943d12015-10-07 14:55:10 -0700811 s->peer->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800812 s->peer->close = local_socket_close_notify;
813 s->peer->peer = 0;
Josh Gaoef550fe2016-05-17 16:55:06 -0700814 /* give him our transport and upref it */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800815 s->peer->transport = s->transport;
816
Josh Gaoef550fe2016-05-17 16:55:06 -0700817 connect_to_remote(s->peer, (char*)(p->data + 4));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800818 s->peer = 0;
819 s->close(s);
820 return 1;
821
822fail:
Josh Gaoef550fe2016-05-17 16:55:06 -0700823 /* we're going to close our peer as a side-effect, so
824 ** return -1 to signal that state to the local socket
825 ** who is enqueueing against us
826 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800827 s->close(s);
828 return -1;
829}
830
Josh Gaoef550fe2016-05-17 16:55:06 -0700831static void smart_socket_ready(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700832 D("SS(%d): ready", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800833}
834
Josh Gaoef550fe2016-05-17 16:55:06 -0700835static void smart_socket_close(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700836 D("SS(%d): closed", s->id);
Josh Gaoef550fe2016-05-17 16:55:06 -0700837 if (s->pkt_first) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800838 put_apacket(s->pkt_first);
839 }
Josh Gaoef550fe2016-05-17 16:55:06 -0700840 if (s->peer) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800841 s->peer->peer = 0;
842 s->peer->close(s->peer);
Tom Marlinbd6614d2011-05-13 13:24:55 -0500843 s->peer = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800844 }
845 free(s);
846}
847
Josh Gaoef550fe2016-05-17 16:55:06 -0700848static asocket* create_smart_socket(void) {
Yabin Cui815ad882015-09-02 17:44:28 -0700849 D("Creating smart socket");
Josh Gaoef550fe2016-05-17 16:55:06 -0700850 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
André Goddard Rosa1249de62010-06-10 20:48:19 -0300851 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800852 s->enqueue = smart_socket_enqueue;
853 s->ready = smart_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100854 s->shutdown = NULL;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800855 s->close = smart_socket_close;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800856
Yabin Cui815ad882015-09-02 17:44:28 -0700857 D("SS(%d)", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800858 return s;
859}
860
Josh Gaoef550fe2016-05-17 16:55:06 -0700861void connect_to_smartsocket(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700862 D("Connecting to smart socket");
Josh Gaoef550fe2016-05-17 16:55:06 -0700863 asocket* ss = create_smart_socket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800864 s->peer = ss;
865 ss->peer = s;
866 s->ready(s);
867}
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100868
869size_t asocket::get_max_payload() const {
870 size_t max_payload = MAX_PAYLOAD;
871 if (transport) {
872 max_payload = std::min(max_payload, transport->get_max_payload());
873 }
874 if (peer && peer->transport) {
875 max_payload = std::min(max_payload, peer->transport->get_max_payload());
876 }
877 return max_payload;
878}