blob: 8b07f748045a7d9238d06615f6a64d09e6fae791 [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>
Steven Morelandb087d302017-04-13 23:48:57 -070035#include <log/log_properties.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"
Josh Gaocd2a5292018-03-07 16:52:28 -080041#include "types.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042
Josh Gao0f1a20a2016-05-17 17:46:27 -070043static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080044static unsigned local_socket_next_id = 1;
45
Josh Gao2b3db9e2018-01-31 13:15:51 -080046static auto& local_socket_list = *new std::vector<asocket*>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080047
48/* the the list of currently closing local sockets.
49** these have no peer anymore, but still packets to
50** write to their fd.
51*/
Josh Gao2b3db9e2018-01-31 13:15:51 -080052static auto& local_socket_closing_list = *new std::vector<asocket*>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080053
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010054// Parse the global list of sockets to find one with id |local_id|.
55// If |peer_id| is not 0, also check that it is connected to a peer
56// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
Josh Gaoef550fe2016-05-17 16:55:06 -070057asocket* find_local_socket(unsigned local_id, unsigned peer_id) {
Josh Gao2b3db9e2018-01-31 13:15:51 -080058 asocket* result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059
Josh Gao0f1a20a2016-05-17 17:46:27 -070060 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao2b3db9e2018-01-31 13:15:51 -080061 for (asocket* s : local_socket_list) {
Josh Gaoef550fe2016-05-17 16:55:06 -070062 if (s->id != local_id) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010063 continue;
Josh Gaoef550fe2016-05-17 16:55:06 -070064 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010065 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030066 result = s;
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030067 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010068 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080069 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080070
71 return result;
72}
73
Josh Gaoef550fe2016-05-17 16:55:06 -070074void install_local_socket(asocket* s) {
Josh Gao0f1a20a2016-05-17 17:46:27 -070075 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080076
77 s->id = local_socket_next_id++;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010078
79 // Socket ids should never be 0.
Josh Gaoef550fe2016-05-17 16:55:06 -070080 if (local_socket_next_id == 0) {
Elliott Hughese64126b2018-10-19 13:59:44 -070081 LOG(FATAL) << "local socket id overflow";
Josh Gaoef550fe2016-05-17 16:55:06 -070082 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010083
Josh Gao2b3db9e2018-01-31 13:15:51 -080084 local_socket_list.push_back(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080085}
86
Josh Gaoef550fe2016-05-17 16:55:06 -070087void remove_socket(asocket* s) {
Josh Gaob69c78b2017-09-13 11:17:33 -070088 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao2b3db9e2018-01-31 13:15:51 -080089 for (auto list : { &local_socket_list, &local_socket_closing_list }) {
90 list->erase(std::remove_if(list->begin(), list->end(), [s](asocket* x) { return x == s; }),
91 list->end());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080092 }
93}
94
Josh Gaoef550fe2016-05-17 16:55:06 -070095void close_all_sockets(atransport* t) {
Josh Gaoef550fe2016-05-17 16:55:06 -070096 /* this is a little gross, but since s->close() *will* modify
97 ** the list out from under you, your options are limited.
98 */
Josh Gao0f1a20a2016-05-17 17:46:27 -070099 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800100restart:
Josh Gao2b3db9e2018-01-31 13:15:51 -0800101 for (asocket* s : local_socket_list) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700102 if (s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gao80814e12016-05-18 10:39:48 -0700103 s->close(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800104 goto restart;
105 }
106 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800107}
108
Josh Gao4b808502018-03-19 13:20:29 -0700109enum class SocketFlushResult {
110 Destroyed,
111 TryAgain,
112 Completed,
113};
114
115static SocketFlushResult local_socket_flush_incoming(asocket* s) {
Josh Gao9f155db2018-04-03 14:37:11 -0700116 if (!s->packet_queue.empty()) {
117 std::vector<adb_iovec> iov = s->packet_queue.iovecs();
118 ssize_t rc = adb_writev(s->fd, iov.data(), iov.size());
119 if (rc > 0 && static_cast<size_t>(rc) == s->packet_queue.size()) {
120 s->packet_queue.clear();
Josh Gao4b808502018-03-19 13:20:29 -0700121 } else if (rc > 0) {
Josh Gao9f155db2018-04-03 14:37:11 -0700122 // TODO: Implement a faster drop_front?
123 s->packet_queue.take_front(rc);
Josh Gao9528df22018-05-14 11:14:33 -0700124 fdevent_add(s->fde, FDE_WRITE);
Josh Gao4b808502018-03-19 13:20:29 -0700125 return SocketFlushResult::TryAgain;
126 } else if (rc == -1 && errno == EAGAIN) {
Josh Gao9528df22018-05-14 11:14:33 -0700127 fdevent_add(s->fde, FDE_WRITE);
Josh Gao4b808502018-03-19 13:20:29 -0700128 return SocketFlushResult::TryAgain;
Josh Gaob50b92f2018-03-30 13:56:24 -0700129 } else {
130 // We failed to write, but it's possible that we can still read from the socket.
131 // Give that a try before giving up.
132 s->has_write_error = true;
Josh Gao4b808502018-03-19 13:20:29 -0700133 }
Josh Gao4b808502018-03-19 13:20:29 -0700134 }
135
136 // If we sent the last packet of a closing socket, we can now destroy it.
137 if (s->closing) {
138 s->close(s);
139 return SocketFlushResult::Destroyed;
140 }
141
Josh Gao9528df22018-05-14 11:14:33 -0700142 fdevent_del(s->fde, FDE_WRITE);
Josh Gao4b808502018-03-19 13:20:29 -0700143 return SocketFlushResult::Completed;
144}
145
146// Returns false if the socket has been closed and destroyed as a side-effect of this function.
147static bool local_socket_flush_outgoing(asocket* s) {
148 const size_t max_payload = s->get_max_payload();
Josh Gaocd2a5292018-03-07 16:52:28 -0800149 apacket::payload_type data;
Josh Gao4b808502018-03-19 13:20:29 -0700150 data.resize(max_payload);
151 char* x = &data[0];
152 size_t avail = max_payload;
153 int r = 0;
154 int is_eof = 0;
155
156 while (avail > 0) {
157 r = adb_read(s->fd, x, avail);
158 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r,
159 r < 0 ? errno : 0, avail);
160 if (r == -1) {
161 if (errno == EAGAIN) {
162 break;
163 }
164 } else if (r > 0) {
165 avail -= r;
166 x += r;
167 continue;
168 }
169
170 /* r = 0 or unhandled error */
171 is_eof = 1;
172 break;
173 }
174 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d", s->id, s->fd, r, is_eof,
Josh Gao9528df22018-05-14 11:14:33 -0700175 s->fde->force_eof);
Josh Gao4b808502018-03-19 13:20:29 -0700176
177 if (avail != max_payload && s->peer) {
178 data.resize(max_payload - avail);
179
180 // s->peer->enqueue() may call s->close() and free s,
181 // so save variables for debug printing below.
182 unsigned saved_id = s->id;
183 int saved_fd = s->fd;
184 r = s->peer->enqueue(s->peer, std::move(data));
185 D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
186
187 if (r < 0) {
188 // Error return means they closed us as a side-effect and we must
189 // return immediately.
190 //
191 // Note that if we still have buffered packets, the socket will be
192 // placed on the closing socket list. This handler function will be
193 // called again to process FDE_WRITE events.
194 return false;
195 }
196
197 if (r > 0) {
198 /* if the remote cannot accept further events,
199 ** we disable notification of READs. They'll
200 ** be enabled again when we get a call to ready()
201 */
Josh Gao9528df22018-05-14 11:14:33 -0700202 fdevent_del(s->fde, FDE_READ);
Josh Gao4b808502018-03-19 13:20:29 -0700203 }
204 }
205
206 // Don't allow a forced eof if data is still there.
Josh Gao9528df22018-05-14 11:14:33 -0700207 if ((s->fde->force_eof && !r) || is_eof) {
208 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d", is_eof, r, s->fde->force_eof);
Josh Gao4b808502018-03-19 13:20:29 -0700209 s->close(s);
210 return false;
211 }
212
213 return true;
214}
215
Josh Gaocd2a5292018-03-07 16:52:28 -0800216static int local_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800217 D("LS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800218
Josh Gao9f155db2018-04-03 14:37:11 -0700219 s->packet_queue.append(std::move(data));
Josh Gao4b808502018-03-19 13:20:29 -0700220 switch (local_socket_flush_incoming(s)) {
221 case SocketFlushResult::Destroyed:
222 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800223
Josh Gao4b808502018-03-19 13:20:29 -0700224 case SocketFlushResult::TryAgain:
225 return 1;
226
227 case SocketFlushResult::Completed:
228 return 0;
229 }
230
231 return !s->packet_queue.empty();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800232}
233
Josh Gaoef550fe2016-05-17 16:55:06 -0700234static void local_socket_ready(asocket* s) {
Nanik Tolaramc624a7e2015-02-18 22:53:37 +1100235 /* far side is ready for data, pay attention to
236 readable events */
Josh Gao9528df22018-05-14 11:14:33 -0700237 fdevent_add(s->fde, FDE_READ);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800238}
239
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240// be sure to hold the socket list lock when calling this
Josh Gaoef550fe2016-05-17 16:55:06 -0700241static void local_socket_destroy(asocket* s) {
Benoit Goby88468f32012-03-16 14:50:07 -0700242 int exit_on_close = s->exit_on_close;
243
Josh Gao9528df22018-05-14 11:14:33 -0700244 D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800245
Josh Gao94250272018-10-12 05:08:45 +0000246 /* IMPORTANT: the remove closes the fd
247 ** that belongs to this socket
248 */
249 fdevent_destroy(s->fde);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800250
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800251 remove_socket(s);
Josh Gao5cb76ce2018-02-12 17:24:00 -0800252 delete s;
Benoit Goby88468f32012-03-16 14:50:07 -0700253
254 if (exit_on_close) {
Yabin Cui815ad882015-09-02 17:44:28 -0700255 D("local_socket_destroy: exiting");
Benoit Goby88468f32012-03-16 14:50:07 -0700256 exit(1);
257 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800258}
259
Josh Gao0f1a20a2016-05-17 17:46:27 -0700260static void local_socket_close(asocket* s) {
261 D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd);
262 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gaoef550fe2016-05-17 16:55:06 -0700263 if (s->peer) {
264 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 +0100265 /* Note: it's important to call shutdown before disconnecting from
266 * the peer, this ensures that remote sockets can still get the id
267 * of the local socket they're connected to, to send a CLOSE()
268 * protocol event. */
Josh Gaoef550fe2016-05-17 16:55:06 -0700269 if (s->peer->shutdown) {
270 s->peer->shutdown(s->peer);
271 }
Josh Gao0f1a20a2016-05-17 17:46:27 -0700272 s->peer->peer = nullptr;
273 s->peer->close(s->peer);
274 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800275 }
276
Josh Gaoef550fe2016-05-17 16:55:06 -0700277 /* If we are already closing, or if there are no
278 ** pending packets, destroy immediately
279 */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800280 if (s->closing || s->has_write_error || s->packet_queue.empty()) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700281 int id = s->id;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800282 local_socket_destroy(s);
Yabin Cui815ad882015-09-02 17:44:28 -0700283 D("LS(%d): closed", id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800284 return;
285 }
286
Josh Gaoef550fe2016-05-17 16:55:06 -0700287 /* otherwise, put on the closing list
288 */
Yabin Cui815ad882015-09-02 17:44:28 -0700289 D("LS(%d): closing", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800290 s->closing = 1;
Josh Gao9528df22018-05-14 11:14:33 -0700291 fdevent_del(s->fde, FDE_READ);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800292 remove_socket(s);
Yabin Cui815ad882015-09-02 17:44:28 -0700293 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
Josh Gao2b3db9e2018-01-31 13:15:51 -0800294 local_socket_closing_list.push_back(s);
Josh Gao9528df22018-05-14 11:14:33 -0700295 CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800296}
297
Josh Gaoef550fe2016-05-17 16:55:06 -0700298static void local_socket_event_func(int fd, unsigned ev, void* _s) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800299 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui815ad882015-09-02 17:44:28 -0700300 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700301
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800302 /* put the FDE_WRITE processing before the FDE_READ
303 ** in order to simplify the code.
304 */
Dan Albertf30d73c2015-02-25 17:51:28 -0800305 if (ev & FDE_WRITE) {
Josh Gao4b808502018-03-19 13:20:29 -0700306 switch (local_socket_flush_incoming(s)) {
307 case SocketFlushResult::Destroyed:
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800308 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800309
Josh Gao4b808502018-03-19 13:20:29 -0700310 case SocketFlushResult::TryAgain:
311 break;
312
313 case SocketFlushResult::Completed:
314 s->peer->ready(s->peer);
315 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800316 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800317 }
318
Dan Albertf30d73c2015-02-25 17:51:28 -0800319 if (ev & FDE_READ) {
Josh Gao4b808502018-03-19 13:20:29 -0700320 if (!local_socket_flush_outgoing(s)) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700321 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800322 }
323 }
324
Josh Gaoef550fe2016-05-17 16:55:06 -0700325 if (ev & FDE_ERROR) {
326 /* this should be caught be the next read or write
327 ** catching it here means we may skip the last few
328 ** bytes of readable data.
329 */
Yabin Cui815ad882015-09-02 17:44:28 -0700330 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800331 return;
332 }
333}
334
Josh Gaoef550fe2016-05-17 16:55:06 -0700335asocket* create_local_socket(int fd) {
Josh Gao5cb76ce2018-02-12 17:24:00 -0800336 asocket* s = new asocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800337 s->fd = fd;
338 s->enqueue = local_socket_enqueue;
339 s->ready = local_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700340 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800341 s->close = local_socket_close;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700342 install_local_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800343
Josh Gao9528df22018-05-14 11:14:33 -0700344 s->fde = fdevent_create(fd, local_socket_event_func, s);
Yabin Cui815ad882015-09-02 17:44:28 -0700345 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800346 return s;
347}
348
Josh Gaoebc1c312018-04-13 12:17:03 -0700349asocket* create_local_service_socket(const char* name, atransport* transport) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800350#if !ADB_HOST
Josh Gaoef550fe2016-05-17 16:55:06 -0700351 if (!strcmp(name, "jdwp")) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800352 return create_jdwp_service_socket();
353 }
Josh Gaoef550fe2016-05-17 16:55:06 -0700354 if (!strcmp(name, "track-jdwp")) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800355 return create_jdwp_tracker_service_socket();
356 }
357#endif
David Pursell8da19a42015-08-31 10:42:13 -0700358 int fd = service_to_fd(name, transport);
Josh Gaoef550fe2016-05-17 16:55:06 -0700359 if (fd < 0) {
Elliott Hughesc3d1c112016-06-15 14:46:56 -0700360 return nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700361 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800362
Dan Pasanenfb787d92014-10-06 12:57:20 -0500363 asocket* s = create_local_socket(fd);
Yabin Cui815ad882015-09-02 17:44:28 -0700364 D("LS(%d): bound to '%s' via %d", s->id, name, fd);
Benoit Goby88468f32012-03-16 14:50:07 -0700365
JP Abgralla84bd682012-03-30 13:19:11 -0700366#if !ADB_HOST
Mark Salyzync75f65f2016-03-28 15:52:13 -0700367 if ((!strncmp(name, "root:", 5) && getuid() != 0 && __android_log_is_debuggable()) ||
Josh Gaoef550fe2016-05-17 16:55:06 -0700368 (!strncmp(name, "unroot:", 7) && getuid() == 0) ||
369 !strncmp(name, "usb:", 4) ||
370 !strncmp(name, "tcpip:", 6)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700371 D("LS(%d): enabling exit_on_close", s->id);
Benoit Goby88468f32012-03-16 14:50:07 -0700372 s->exit_on_close = 1;
373 }
JP Abgralla84bd682012-03-30 13:19:11 -0700374#endif
Benoit Goby88468f32012-03-16 14:50:07 -0700375
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800376 return s;
377}
378
379#if ADB_HOST
Josh Gaob39e4152017-08-16 16:57:01 -0700380static asocket* create_host_service_socket(const char* name, const char* serial,
381 TransportId transport_id) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700382 asocket* s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800383
Josh Gaob39e4152017-08-16 16:57:01 -0700384 s = host_service_to_socket(name, serial, transport_id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800385
Yi Kong86e67182018-07-13 18:15:16 -0700386 if (s != nullptr) {
Yabin Cui815ad882015-09-02 17:44:28 -0700387 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800388 return s;
389 }
390
391 return s;
392}
393#endif /* ADB_HOST */
394
Josh Gaocd2a5292018-03-07 16:52:28 -0800395static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700396 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
Josh Gaoa7d9d712018-02-01 13:17:50 -0800397 apacket* p = get_apacket();
398
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800399 p->msg.command = A_WRTE;
400 p->msg.arg0 = s->peer->id;
401 p->msg.arg1 = s->id;
Josh Gaoa7d9d712018-02-01 13:17:50 -0800402
Josh Gao839b9322018-02-05 18:49:10 -0800403 if (data.size() > MAX_PAYLOAD) {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800404 put_apacket(p);
405 return -1;
406 }
407
Josh Gao839b9322018-02-05 18:49:10 -0800408 p->payload = std::move(data);
409 p->msg.data_length = p->payload.size();
410
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800411 send_packet(p, s->transport);
412 return 1;
413}
414
Josh Gaoef550fe2016-05-17 16:55:06 -0700415static void remote_socket_ready(asocket* s) {
416 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
417 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800418 p->msg.command = A_OKAY;
419 p->msg.arg0 = s->peer->id;
420 p->msg.arg1 = s->id;
421 send_packet(p, s->transport);
422}
423
Josh Gaoef550fe2016-05-17 16:55:06 -0700424static void remote_socket_shutdown(asocket* s) {
425 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
426 s->peer ? s->peer->fd : -1);
427 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800428 p->msg.command = A_CLSE;
Josh Gaoef550fe2016-05-17 16:55:06 -0700429 if (s->peer) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800430 p->msg.arg0 = s->peer->id;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100431 }
432 p->msg.arg1 = s->id;
433 send_packet(p, s->transport);
434}
435
Josh Gaoef550fe2016-05-17 16:55:06 -0700436static void remote_socket_close(asocket* s) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100437 if (s->peer) {
Yi Kong86e67182018-07-13 18:15:16 -0700438 s->peer->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700439 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 -0800440 s->peer->close(s->peer);
441 }
Josh Gaoef550fe2016-05-17 16:55:06 -0700442 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
443 s->peer ? s->peer->fd : -1);
Yabin Cui815ad882015-09-02 17:44:28 -0700444 D("RS(%d): closed", s->id);
Josh Gao5cb76ce2018-02-12 17:24:00 -0800445 delete s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800446}
447
Yabin Cui70ec57b2015-08-27 18:50:04 -0700448// Create a remote socket to exchange packets with a remote service through transport
449// |t|. Where |id| is the socket id of the corresponding service on the other
450// side of the transport (it is allocated by the remote side and _cannot_ be 0).
451// Returns a new non-NULL asocket handle.
Josh Gaoef550fe2016-05-17 16:55:06 -0700452asocket* create_remote_socket(unsigned id, atransport* t) {
453 if (id == 0) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700454 LOG(FATAL) << "invalid remote socket id (0)";
Josh Gaoef550fe2016-05-17 16:55:06 -0700455 }
Josh Gao5cb76ce2018-02-12 17:24:00 -0800456 asocket* s = new asocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800457 s->id = id;
458 s->enqueue = remote_socket_enqueue;
459 s->ready = remote_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100460 s->shutdown = remote_socket_shutdown;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800461 s->close = remote_socket_close;
462 s->transport = t;
463
Yabin Cui815ad882015-09-02 17:44:28 -0700464 D("RS(%d): created", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800465 return s;
466}
467
Josh Gaoef550fe2016-05-17 16:55:06 -0700468void connect_to_remote(asocket* s, const char* destination) {
Yabin Cui815ad882015-09-02 17:44:28 -0700469 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gaoef550fe2016-05-17 16:55:06 -0700470 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800471
Yabin Cui815ad882015-09-02 17:44:28 -0700472 D("LS(%d): connect('%s')", s->id, destination);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800473 p->msg.command = A_OPEN;
474 p->msg.arg0 = s->id;
Josh Gao839b9322018-02-05 18:49:10 -0800475
476 // adbd expects a null-terminated string.
Josh Gaocd2a5292018-03-07 16:52:28 -0800477 p->payload.assign(destination, destination + strlen(destination) + 1);
Josh Gao839b9322018-02-05 18:49:10 -0800478 p->msg.data_length = p->payload.size();
479
Elliott Hughese64126b2018-10-19 13:59:44 -0700480 CHECK_LE(p->msg.data_length, s->get_max_payload());
Josh Gao839b9322018-02-05 18:49:10 -0800481
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800482 send_packet(p, s->transport);
483}
484
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800485/* this is used by magic sockets to rig local sockets to
486 send the go-ahead message when they connect */
Josh Gaoef550fe2016-05-17 16:55:06 -0700487static void local_socket_ready_notify(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800488 s->ready = local_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700489 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800490 s->close = local_socket_close;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700491 SendOkay(s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800492 s->ready(s);
493}
494
495/* this is used by magic sockets to rig local sockets to
496 send the failure message if they are closed before
497 connected (to avoid closing them without a status message) */
Josh Gaoef550fe2016-05-17 16:55:06 -0700498static void local_socket_close_notify(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800499 s->ready = local_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700500 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800501 s->close = local_socket_close;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700502 SendFail(s->fd, "closed");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800503 s->close(s);
504}
505
Josh Gaoa7d9d712018-02-01 13:17:50 -0800506static unsigned unhex(const char* s, int len) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800507 unsigned n = 0, c;
508
Josh Gaoef550fe2016-05-17 16:55:06 -0700509 while (len-- > 0) {
510 switch ((c = *s++)) {
511 case '0':
512 case '1':
513 case '2':
514 case '3':
515 case '4':
516 case '5':
517 case '6':
518 case '7':
519 case '8':
520 case '9':
521 c -= '0';
522 break;
523 case 'a':
524 case 'b':
525 case 'c':
526 case 'd':
527 case 'e':
528 case 'f':
529 c = c - 'a' + 10;
530 break;
531 case 'A':
532 case 'B':
533 case 'C':
534 case 'D':
535 case 'E':
536 case 'F':
537 c = c - 'A' + 10;
538 break;
539 default:
540 return 0xffffffff;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800541 }
542
543 n = (n << 4) | c;
544 }
545
546 return n;
547}
548
Elliott Hughes88b4c852015-04-30 17:32:03 -0700549#if ADB_HOST
550
David Pursellc929c6f2016-03-01 08:58:26 -0800551namespace internal {
Scott Anderson27042382012-05-30 18:11:27 -0700552
David Pursellc929c6f2016-03-01 08:58:26 -0800553// Returns the position in |service| following the target serial parameter. Serial format can be
554// any of:
555// * [tcp:|udp:]<serial>[:<port>]:<command>
556// * <prefix>:<serial>:<command>
557// Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}.
558//
559// The returned pointer will point to the ':' just before <command>, or nullptr if not found.
Dan Austinca35e9e2016-03-28 15:32:37 -0700560char* skip_host_serial(char* service) {
David Pursellc929c6f2016-03-01 08:58:26 -0800561 static const std::vector<std::string>& prefixes =
562 *(new std::vector<std::string>{"usb:", "product:", "model:", "device:"});
Terence Haddock6c670402011-03-16 09:43:56 +0100563
David Pursellc929c6f2016-03-01 08:58:26 -0800564 for (const std::string& prefix : prefixes) {
565 if (!strncmp(service, prefix.c_str(), prefix.length())) {
566 return strchr(service + prefix.length(), ':');
567 }
Scott Anderson090e5cb2012-05-31 12:04:23 -0700568 }
569
David Pursellc929c6f2016-03-01 08:58:26 -0800570 // For fastboot compatibility, ignore protocol prefixes.
571 if (!strncmp(service, "tcp:", 4) || !strncmp(service, "udp:", 4)) {
572 service += 4;
573 }
574
David Pursell24b62a72016-09-21 12:08:37 -0700575 // Check for an IPv6 address. `adb connect` creates the serial number from the canonical
576 // network address so it will always have the [] delimiters.
577 if (service[0] == '[') {
578 char* ipv6_end = strchr(service, ']');
579 if (ipv6_end != nullptr) {
580 service = ipv6_end;
581 }
582 }
583
584 // The next colon we find must either begin the port field or the command field.
585 char* colon_ptr = strchr(service, ':');
586 if (!colon_ptr) {
David Pursellc929c6f2016-03-01 08:58:26 -0800587 // No colon in service string.
588 return nullptr;
Terence Haddock6c670402011-03-16 09:43:56 +0100589 }
David Pursellc929c6f2016-03-01 08:58:26 -0800590
David Pursell24b62a72016-09-21 12:08:37 -0700591 // If the next field is only decimal digits and ends with another colon, it's a port.
592 char* serial_end = colon_ptr;
Terence Haddock6c670402011-03-16 09:43:56 +0100593 if (isdigit(serial_end[1])) {
594 serial_end++;
David Pursellc929c6f2016-03-01 08:58:26 -0800595 while (*serial_end && isdigit(*serial_end)) {
Terence Haddock6c670402011-03-16 09:43:56 +0100596 serial_end++;
597 }
David Pursellc929c6f2016-03-01 08:58:26 -0800598 if (*serial_end != ':') {
David Pursell24b62a72016-09-21 12:08:37 -0700599 // Something other than "<port>:" was found, this must be the command field instead.
600 serial_end = colon_ptr;
Terence Haddock6c670402011-03-16 09:43:56 +0100601 }
602 }
603 return serial_end;
604}
605
David Pursellc929c6f2016-03-01 08:58:26 -0800606} // namespace internal
607
Josh Gaoef550fe2016-05-17 16:55:06 -0700608#endif // ADB_HOST
Elliott Hughes88b4c852015-04-30 17:32:03 -0700609
Josh Gaocd2a5292018-03-07 16:52:28 -0800610static int smart_socket_enqueue(asocket* s, apacket::payload_type data) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800611#if ADB_HOST
Josh Gaoef550fe2016-05-17 16:55:06 -0700612 char* service = nullptr;
Elliott Hughes67943d12015-10-07 14:55:10 -0700613 char* serial = nullptr;
Josh Gaob39e4152017-08-16 16:57:01 -0700614 TransportId transport_id = 0;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700615 TransportType type = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800616#endif
617
Josh Gaoa7d9d712018-02-01 13:17:50 -0800618 D("SS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800619
Josh Gaoa7d9d712018-02-01 13:17:50 -0800620 if (s->smart_socket_data.empty()) {
Josh Gao9f155db2018-04-03 14:37:11 -0700621 // TODO: Make this an IOVector?
Josh Gaocd2a5292018-03-07 16:52:28 -0800622 s->smart_socket_data.assign(data.begin(), data.end());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800623 } else {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800624 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800625 }
626
Josh Gao9055a582016-01-15 14:35:54 -0800627 /* don't bother if we can't decode the length */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800628 if (s->smart_socket_data.size() < 4) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700629 return 0;
630 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800631
Josh Gaoa7d9d712018-02-01 13:17:50 -0800632 uint32_t len = unhex(s->smart_socket_data.data(), 4);
633 if (len == 0 || len > MAX_PAYLOAD) {
634 D("SS(%d): bad size (%u)", s->id, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800635 goto fail;
636 }
637
Josh Gaoa7d9d712018-02-01 13:17:50 -0800638 D("SS(%d): len is %u", s->id, len);
Josh Gao9055a582016-01-15 14:35:54 -0800639 /* can't do anything until we have the full header */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800640 if ((len + 4) > s->smart_socket_data.size()) {
641 D("SS(%d): waiting for %zu more bytes", s->id, len + 4 - s->smart_socket_data.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800642 return 0;
643 }
644
Josh Gaoa7d9d712018-02-01 13:17:50 -0800645 s->smart_socket_data[len + 4] = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800646
Josh Gaoa7d9d712018-02-01 13:17:50 -0800647 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800648
649#if ADB_HOST
Josh Gaoa7d9d712018-02-01 13:17:50 -0800650 service = &s->smart_socket_data[4];
Josh Gaoef550fe2016-05-17 16:55:06 -0700651 if (!strncmp(service, "host-serial:", strlen("host-serial:"))) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800652 char* serial_end;
653 service += strlen("host-serial:");
654
Terence Haddock6c670402011-03-16 09:43:56 +0100655 // serial number should follow "host:" and could be a host:port string.
David Pursellc929c6f2016-03-01 08:58:26 -0800656 serial_end = internal::skip_host_serial(service);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800657 if (serial_end) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700658 *serial_end = 0; // terminate string
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800659 serial = service;
660 service = serial_end + 1;
661 }
Josh Gaob39e4152017-08-16 16:57:01 -0700662 } else if (!strncmp(service, "host-transport-id:", strlen("host-transport-id:"))) {
663 service += strlen("host-transport-id:");
664 transport_id = strtoll(service, &service, 10);
665
666 if (*service != ':') {
667 return -1;
668 }
669 service++;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800670 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700671 type = kTransportUsb;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800672 service += strlen("host-usb:");
673 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700674 type = kTransportLocal;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800675 service += strlen("host-local:");
676 } else if (!strncmp(service, "host:", strlen("host:"))) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700677 type = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800678 service += strlen("host:");
679 } else {
Elliott Hughes67943d12015-10-07 14:55:10 -0700680 service = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800681 }
682
683 if (service) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700684 asocket* s2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800685
Josh Gaod3067472018-08-07 14:14:21 -0700686 // Some requests are handled immediately -- in that case the handle_host_request() routine
687 // has sent the OKAY or FAIL message and all we have to do is clean up.
688 if (handle_host_request(service, type, serial, transport_id, s->peer->fd, s)) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700689 D("SS(%d): handled host service '%s'", s->id, service);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800690 goto fail;
691 }
692 if (!strncmp(service, "transport", strlen("transport"))) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700693 D("SS(%d): okay transport", s->id);
Josh Gaoa7d9d712018-02-01 13:17:50 -0800694 s->smart_socket_data.clear();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800695 return 0;
696 }
697
Josh Gaoef550fe2016-05-17 16:55:06 -0700698 /* try to find a local service with this name.
699 ** if no such service exists, we'll fail out
700 ** and tear down here.
701 */
Josh Gaob39e4152017-08-16 16:57:01 -0700702 s2 = create_host_service_socket(service, serial, transport_id);
Yi Kong86e67182018-07-13 18:15:16 -0700703 if (s2 == nullptr) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700704 D("SS(%d): couldn't create host service '%s'", s->id, service);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700705 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800706 goto fail;
707 }
708
Josh Gaoef550fe2016-05-17 16:55:06 -0700709 /* we've connected to a local host service,
710 ** so we make our peer back into a regular
711 ** local socket and bind it to the new local
712 ** service socket, acknowledge the successful
713 ** connection, and close this smart socket now
714 ** that its work is done.
715 */
Elliott Hughes88b4c852015-04-30 17:32:03 -0700716 SendOkay(s->peer->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800717
718 s->peer->ready = local_socket_ready;
Elliott Hughes67943d12015-10-07 14:55:10 -0700719 s->peer->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800720 s->peer->close = local_socket_close;
721 s->peer->peer = s2;
722 s2->peer = s->peer;
Yi Kong86e67182018-07-13 18:15:16 -0700723 s->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700724 D("SS(%d): okay", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800725 s->close(s);
726
Josh Gaoef550fe2016-05-17 16:55:06 -0700727 /* initial state is "ready" */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800728 s2->ready(s2);
729 return 0;
730 }
731#else /* !ADB_HOST */
Elliott Hughes67943d12015-10-07 14:55:10 -0700732 if (s->transport == nullptr) {
Elliott Hughesab882422015-04-16 22:54:44 -0700733 std::string error_msg = "unknown failure";
Josh Gaob39e4152017-08-16 16:57:01 -0700734 s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
Elliott Hughes67943d12015-10-07 14:55:10 -0700735 if (s->transport == nullptr) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700736 SendFail(s->peer->fd, error_msg);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800737 goto fail;
738 }
739 }
740#endif
741
Josh Gao4e562502016-10-27 14:01:08 -0700742 if (!s->transport) {
743 SendFail(s->peer->fd, "device offline (no transport)");
744 goto fail;
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700745 } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700746 /* if there's no remote we fail the connection
747 ** right here and terminate it
748 */
Josh Gao4e562502016-10-27 14:01:08 -0700749 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800750 goto fail;
751 }
752
Josh Gaoef550fe2016-05-17 16:55:06 -0700753 /* instrument our peer to pass the success or fail
754 ** message back once it connects or closes, then
755 ** detach from it, request the connection, and
756 ** tear down
757 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800758 s->peer->ready = local_socket_ready_notify;
Elliott Hughes67943d12015-10-07 14:55:10 -0700759 s->peer->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800760 s->peer->close = local_socket_close_notify;
Yi Kong86e67182018-07-13 18:15:16 -0700761 s->peer->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700762 /* give him our transport and upref it */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800763 s->peer->transport = s->transport;
764
Josh Gaoa7d9d712018-02-01 13:17:50 -0800765 connect_to_remote(s->peer, s->smart_socket_data.data() + 4);
Yi Kong86e67182018-07-13 18:15:16 -0700766 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800767 s->close(s);
768 return 1;
769
770fail:
Josh Gaoef550fe2016-05-17 16:55:06 -0700771 /* we're going to close our peer as a side-effect, so
772 ** return -1 to signal that state to the local socket
773 ** who is enqueueing against us
774 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800775 s->close(s);
776 return -1;
777}
778
Josh Gaoef550fe2016-05-17 16:55:06 -0700779static void smart_socket_ready(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700780 D("SS(%d): ready", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800781}
782
Josh Gaoef550fe2016-05-17 16:55:06 -0700783static void smart_socket_close(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700784 D("SS(%d): closed", s->id);
Josh Gaoef550fe2016-05-17 16:55:06 -0700785 if (s->peer) {
Yi Kong86e67182018-07-13 18:15:16 -0700786 s->peer->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800787 s->peer->close(s->peer);
Yi Kong86e67182018-07-13 18:15:16 -0700788 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800789 }
Josh Gao5cb76ce2018-02-12 17:24:00 -0800790 delete s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800791}
792
Josh Gaoef550fe2016-05-17 16:55:06 -0700793static asocket* create_smart_socket(void) {
Yabin Cui815ad882015-09-02 17:44:28 -0700794 D("Creating smart socket");
Josh Gao5cb76ce2018-02-12 17:24:00 -0800795 asocket* s = new asocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800796 s->enqueue = smart_socket_enqueue;
797 s->ready = smart_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700798 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800799 s->close = smart_socket_close;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800800
Yabin Cui815ad882015-09-02 17:44:28 -0700801 D("SS(%d)", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800802 return s;
803}
804
Josh Gaoef550fe2016-05-17 16:55:06 -0700805void connect_to_smartsocket(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700806 D("Connecting to smart socket");
Josh Gaoef550fe2016-05-17 16:55:06 -0700807 asocket* ss = create_smart_socket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800808 s->peer = ss;
809 ss->peer = s;
810 s->ready(s);
811}
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100812
813size_t asocket::get_max_payload() const {
814 size_t max_payload = MAX_PAYLOAD;
815 if (transport) {
816 max_payload = std::min(max_payload, transport->get_max_payload());
817 }
818 if (peer && peer->transport) {
819 max_payload = std::min(max_payload, peer->transport->get_max_payload());
820 }
821 return max_payload;
822}