blob: 04d92db60de6fcfad6d7d6d2f1f26bd3be3e4cb7 [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 Gao18f7a5c2019-01-11 14:42:08 -080029#include <chrono>
Josh Gao0f1a20a2016-05-17 17:46:27 -070030#include <mutex>
David Pursellc929c6f2016-03-01 08:58:26 -080031#include <string>
32#include <vector>
Spencer Low28bc2cb2015-11-07 18:51:54 -080033
Dan Albertb302d122015-02-24 15:51:19 -080034#if !ADB_HOST
Elliott Hughes8b249d22016-09-23 15:40:03 -070035#include <android-base/properties.h>
Steven Morelandb087d302017-04-13 23:48:57 -070036#include <log/log_properties.h>
Dan Albertb302d122015-02-24 15:51:19 -080037#endif
Dan Albertdb6fe642015-03-19 15:21:08 -070038
39#include "adb.h"
40#include "adb_io.h"
Josh Gao8d49e122018-12-19 13:37:41 -080041#include "adb_utils.h"
Dan Albertb302d122015-02-24 15:51:19 -080042#include "transport.h"
Josh Gaocd2a5292018-03-07 16:52:28 -080043#include "types.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080044
Josh Gao18f7a5c2019-01-11 14:42:08 -080045using namespace std::chrono_literals;
46
Josh Gao0f1a20a2016-05-17 17:46:27 -070047static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080048static unsigned local_socket_next_id = 1;
49
Josh Gao2b3db9e2018-01-31 13:15:51 -080050static auto& local_socket_list = *new std::vector<asocket*>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080051
52/* the the list of currently closing local sockets.
53** these have no peer anymore, but still packets to
54** write to their fd.
55*/
Josh Gao2b3db9e2018-01-31 13:15:51 -080056static auto& local_socket_closing_list = *new std::vector<asocket*>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080057
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010058// Parse the global list of sockets to find one with id |local_id|.
59// If |peer_id| is not 0, also check that it is connected to a peer
60// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
Josh Gaoef550fe2016-05-17 16:55:06 -070061asocket* find_local_socket(unsigned local_id, unsigned peer_id) {
Josh Gao2b3db9e2018-01-31 13:15:51 -080062 asocket* result = nullptr;
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);
Josh Gao2b3db9e2018-01-31 13:15:51 -080065 for (asocket* s : local_socket_list) {
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 -070078void install_local_socket(asocket* s) {
Josh Gao0f1a20a2016-05-17 17:46:27 -070079 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080080
81 s->id = local_socket_next_id++;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010082
83 // Socket ids should never be 0.
Josh Gaoef550fe2016-05-17 16:55:06 -070084 if (local_socket_next_id == 0) {
Elliott Hughese64126b2018-10-19 13:59:44 -070085 LOG(FATAL) << "local socket id overflow";
Josh Gaoef550fe2016-05-17 16:55:06 -070086 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010087
Josh Gao2b3db9e2018-01-31 13:15:51 -080088 local_socket_list.push_back(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080089}
90
Josh Gaoef550fe2016-05-17 16:55:06 -070091void remove_socket(asocket* s) {
Josh Gaob69c78b2017-09-13 11:17:33 -070092 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao2b3db9e2018-01-31 13:15:51 -080093 for (auto list : { &local_socket_list, &local_socket_closing_list }) {
94 list->erase(std::remove_if(list->begin(), list->end(), [s](asocket* x) { return x == s; }),
95 list->end());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080096 }
97}
98
Josh Gaoef550fe2016-05-17 16:55:06 -070099void close_all_sockets(atransport* t) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700100 /* this is a little gross, but since s->close() *will* modify
101 ** the list out from under you, your options are limited.
102 */
Josh Gao0f1a20a2016-05-17 17:46:27 -0700103 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800104restart:
Josh Gao2b3db9e2018-01-31 13:15:51 -0800105 for (asocket* s : local_socket_list) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700106 if (s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gao80814e12016-05-18 10:39:48 -0700107 s->close(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800108 goto restart;
109 }
110 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800111}
112
Josh Gao4b808502018-03-19 13:20:29 -0700113enum class SocketFlushResult {
114 Destroyed,
115 TryAgain,
116 Completed,
117};
118
119static SocketFlushResult local_socket_flush_incoming(asocket* s) {
Josh Gao9f155db2018-04-03 14:37:11 -0700120 if (!s->packet_queue.empty()) {
121 std::vector<adb_iovec> iov = s->packet_queue.iovecs();
122 ssize_t rc = adb_writev(s->fd, iov.data(), iov.size());
123 if (rc > 0 && static_cast<size_t>(rc) == s->packet_queue.size()) {
124 s->packet_queue.clear();
Josh Gao4b808502018-03-19 13:20:29 -0700125 } else if (rc > 0) {
Josh Gao9f155db2018-04-03 14:37:11 -0700126 // TODO: Implement a faster drop_front?
127 s->packet_queue.take_front(rc);
Josh Gao9528df22018-05-14 11:14:33 -0700128 fdevent_add(s->fde, FDE_WRITE);
Josh Gao4b808502018-03-19 13:20:29 -0700129 return SocketFlushResult::TryAgain;
130 } else if (rc == -1 && errno == EAGAIN) {
Josh Gao9528df22018-05-14 11:14:33 -0700131 fdevent_add(s->fde, FDE_WRITE);
Josh Gao4b808502018-03-19 13:20:29 -0700132 return SocketFlushResult::TryAgain;
Josh Gaob50b92f2018-03-30 13:56:24 -0700133 } else {
134 // We failed to write, but it's possible that we can still read from the socket.
135 // Give that a try before giving up.
136 s->has_write_error = true;
Josh Gao4b808502018-03-19 13:20:29 -0700137 }
Josh Gao4b808502018-03-19 13:20:29 -0700138 }
139
140 // If we sent the last packet of a closing socket, we can now destroy it.
141 if (s->closing) {
142 s->close(s);
143 return SocketFlushResult::Destroyed;
144 }
145
Josh Gao9528df22018-05-14 11:14:33 -0700146 fdevent_del(s->fde, FDE_WRITE);
Josh Gao4b808502018-03-19 13:20:29 -0700147 return SocketFlushResult::Completed;
148}
149
150// Returns false if the socket has been closed and destroyed as a side-effect of this function.
151static bool local_socket_flush_outgoing(asocket* s) {
152 const size_t max_payload = s->get_max_payload();
Josh Gaocd2a5292018-03-07 16:52:28 -0800153 apacket::payload_type data;
Josh Gao4b808502018-03-19 13:20:29 -0700154 data.resize(max_payload);
155 char* x = &data[0];
156 size_t avail = max_payload;
157 int r = 0;
158 int is_eof = 0;
159
160 while (avail > 0) {
161 r = adb_read(s->fd, x, avail);
162 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r,
163 r < 0 ? errno : 0, avail);
164 if (r == -1) {
165 if (errno == EAGAIN) {
166 break;
167 }
168 } else if (r > 0) {
169 avail -= r;
170 x += r;
171 continue;
172 }
173
174 /* r = 0 or unhandled error */
175 is_eof = 1;
176 break;
177 }
178 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 -0700179 s->fde->force_eof);
Josh Gao4b808502018-03-19 13:20:29 -0700180
181 if (avail != max_payload && s->peer) {
182 data.resize(max_payload - avail);
183
184 // s->peer->enqueue() may call s->close() and free s,
185 // so save variables for debug printing below.
186 unsigned saved_id = s->id;
187 int saved_fd = s->fd;
188 r = s->peer->enqueue(s->peer, std::move(data));
189 D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
190
191 if (r < 0) {
192 // Error return means they closed us as a side-effect and we must
193 // return immediately.
194 //
195 // Note that if we still have buffered packets, the socket will be
196 // placed on the closing socket list. This handler function will be
197 // called again to process FDE_WRITE events.
198 return false;
199 }
200
201 if (r > 0) {
202 /* if the remote cannot accept further events,
203 ** we disable notification of READs. They'll
204 ** be enabled again when we get a call to ready()
205 */
Josh Gao9528df22018-05-14 11:14:33 -0700206 fdevent_del(s->fde, FDE_READ);
Josh Gao4b808502018-03-19 13:20:29 -0700207 }
208 }
209
210 // Don't allow a forced eof if data is still there.
Josh Gao9528df22018-05-14 11:14:33 -0700211 if ((s->fde->force_eof && !r) || is_eof) {
212 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 -0700213 s->close(s);
214 return false;
215 }
216
217 return true;
218}
219
Josh Gaocd2a5292018-03-07 16:52:28 -0800220static int local_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800221 D("LS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800222
Josh Gao9f155db2018-04-03 14:37:11 -0700223 s->packet_queue.append(std::move(data));
Josh Gao4b808502018-03-19 13:20:29 -0700224 switch (local_socket_flush_incoming(s)) {
225 case SocketFlushResult::Destroyed:
226 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800227
Josh Gao4b808502018-03-19 13:20:29 -0700228 case SocketFlushResult::TryAgain:
229 return 1;
230
231 case SocketFlushResult::Completed:
232 return 0;
233 }
234
235 return !s->packet_queue.empty();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800236}
237
Josh Gaoef550fe2016-05-17 16:55:06 -0700238static void local_socket_ready(asocket* s) {
Nanik Tolaramc624a7e2015-02-18 22:53:37 +1100239 /* far side is ready for data, pay attention to
240 readable events */
Josh Gao9528df22018-05-14 11:14:33 -0700241 fdevent_add(s->fde, FDE_READ);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800242}
243
Josh Gao18f7a5c2019-01-11 14:42:08 -0800244struct ClosingSocket {
245 std::chrono::steady_clock::time_point begin;
246};
247
248// The standard (RFC 1122 - 4.2.2.13) says that if we call close on a
249// socket while we have pending data, a TCP RST should be sent to the
250// other end to notify it that we didn't read all of its data. However,
251// this can result in data that we've successfully written out to be dropped
252// on the other end. To avoid this, instead of immediately closing a
253// socket, call shutdown on it instead, and then read from the file
254// descriptor until we hit EOF or an error before closing.
255static void deferred_close(unique_fd fd) {
256 // Shutdown the socket in the outgoing direction only, so that
257 // we don't have the same problem on the opposite end.
258 adb_shutdown(fd.get(), SHUT_WR);
259 auto callback = [](fdevent* fde, unsigned event, void* arg) {
260 auto socket_info = static_cast<ClosingSocket*>(arg);
261 if (event & FDE_READ) {
262 ssize_t rc;
263 char buf[BUFSIZ];
264 while ((rc = adb_read(fde->fd.get(), buf, sizeof(buf))) > 0) {
265 continue;
266 }
267
268 if (rc == -1 && errno == EAGAIN) {
269 // There's potentially more data to read.
270 auto duration = std::chrono::steady_clock::now() - socket_info->begin;
271 if (duration > 1s) {
272 LOG(WARNING) << "timeout expired while flushing socket, closing";
273 } else {
274 return;
275 }
276 }
277 } else if (event & FDE_TIMEOUT) {
278 LOG(WARNING) << "timeout expired while flushing socket, closing";
279 }
280
281 // Either there was an error, we hit the end of the socket, or our timeout expired.
282 fdevent_destroy(fde);
283 delete socket_info;
284 };
285
286 ClosingSocket* socket_info = new ClosingSocket{
287 .begin = std::chrono::steady_clock::now(),
288 };
289
290 fdevent* fde = fdevent_create(fd.release(), callback, socket_info);
291 fdevent_add(fde, FDE_READ);
292 fdevent_set_timeout(fde, 1s);
293}
294
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800295// be sure to hold the socket list lock when calling this
Josh Gaoef550fe2016-05-17 16:55:06 -0700296static void local_socket_destroy(asocket* s) {
Benoit Goby88468f32012-03-16 14:50:07 -0700297 int exit_on_close = s->exit_on_close;
298
Josh Gao9528df22018-05-14 11:14:33 -0700299 D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300
Josh Gao18f7a5c2019-01-11 14:42:08 -0800301 deferred_close(fdevent_release(s->fde));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800302
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800303 remove_socket(s);
Josh Gao5cb76ce2018-02-12 17:24:00 -0800304 delete s;
Benoit Goby88468f32012-03-16 14:50:07 -0700305
306 if (exit_on_close) {
Yabin Cui815ad882015-09-02 17:44:28 -0700307 D("local_socket_destroy: exiting");
Benoit Goby88468f32012-03-16 14:50:07 -0700308 exit(1);
309 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800310}
311
Josh Gao0f1a20a2016-05-17 17:46:27 -0700312static void local_socket_close(asocket* s) {
313 D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd);
314 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gaoef550fe2016-05-17 16:55:06 -0700315 if (s->peer) {
316 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 +0100317 /* Note: it's important to call shutdown before disconnecting from
318 * the peer, this ensures that remote sockets can still get the id
319 * of the local socket they're connected to, to send a CLOSE()
320 * protocol event. */
Josh Gaoef550fe2016-05-17 16:55:06 -0700321 if (s->peer->shutdown) {
322 s->peer->shutdown(s->peer);
323 }
Josh Gao0f1a20a2016-05-17 17:46:27 -0700324 s->peer->peer = nullptr;
325 s->peer->close(s->peer);
326 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800327 }
328
Josh Gaoef550fe2016-05-17 16:55:06 -0700329 /* If we are already closing, or if there are no
330 ** pending packets, destroy immediately
331 */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800332 if (s->closing || s->has_write_error || s->packet_queue.empty()) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700333 int id = s->id;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800334 local_socket_destroy(s);
Yabin Cui815ad882015-09-02 17:44:28 -0700335 D("LS(%d): closed", id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800336 return;
337 }
338
Josh Gaoef550fe2016-05-17 16:55:06 -0700339 /* otherwise, put on the closing list
340 */
Yabin Cui815ad882015-09-02 17:44:28 -0700341 D("LS(%d): closing", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800342 s->closing = 1;
Josh Gao9528df22018-05-14 11:14:33 -0700343 fdevent_del(s->fde, FDE_READ);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800344 remove_socket(s);
Yabin Cui815ad882015-09-02 17:44:28 -0700345 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
Josh Gao2b3db9e2018-01-31 13:15:51 -0800346 local_socket_closing_list.push_back(s);
Josh Gao9528df22018-05-14 11:14:33 -0700347 CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800348}
349
Josh Gaoef550fe2016-05-17 16:55:06 -0700350static void local_socket_event_func(int fd, unsigned ev, void* _s) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800351 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui815ad882015-09-02 17:44:28 -0700352 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700353
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800354 /* put the FDE_WRITE processing before the FDE_READ
355 ** in order to simplify the code.
356 */
Dan Albertf30d73c2015-02-25 17:51:28 -0800357 if (ev & FDE_WRITE) {
Josh Gao4b808502018-03-19 13:20:29 -0700358 switch (local_socket_flush_incoming(s)) {
359 case SocketFlushResult::Destroyed:
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800360 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800361
Josh Gao4b808502018-03-19 13:20:29 -0700362 case SocketFlushResult::TryAgain:
363 break;
364
365 case SocketFlushResult::Completed:
366 s->peer->ready(s->peer);
367 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800368 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800369 }
370
Dan Albertf30d73c2015-02-25 17:51:28 -0800371 if (ev & FDE_READ) {
Josh Gao4b808502018-03-19 13:20:29 -0700372 if (!local_socket_flush_outgoing(s)) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700373 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800374 }
375 }
376
Josh Gaoef550fe2016-05-17 16:55:06 -0700377 if (ev & FDE_ERROR) {
378 /* this should be caught be the next read or write
379 ** catching it here means we may skip the last few
380 ** bytes of readable data.
381 */
Yabin Cui815ad882015-09-02 17:44:28 -0700382 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800383 return;
384 }
385}
386
Josh Gaoc2705962019-01-23 15:36:42 -0800387asocket* create_local_socket(unique_fd ufd) {
388 int fd = ufd.release();
Josh Gao5cb76ce2018-02-12 17:24:00 -0800389 asocket* s = new asocket();
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;
Yi Kong86e67182018-07-13 18:15:16 -0700393 s->shutdown = nullptr;
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
Josh Gao9528df22018-05-14 11:14:33 -0700397 s->fde = fdevent_create(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 Gao6dbf5792018-12-13 14:21:00 -0800402asocket* create_local_service_socket(std::string_view name, atransport* transport) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800403#if !ADB_HOST
Josh Gao3edf8072018-11-16 15:40:16 -0800404 if (asocket* s = daemon_service_to_socket(name); s) {
405 return s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800406 }
407#endif
Josh Gaoc2705962019-01-23 15:36:42 -0800408 unique_fd fd = service_to_fd(name, transport);
Josh Gaoef550fe2016-05-17 16:55:06 -0700409 if (fd < 0) {
Elliott Hughesc3d1c112016-06-15 14:46:56 -0700410 return nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700411 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800412
Greg Kaiser74b84982019-01-28 06:17:44 -0800413 int fd_value = fd.get();
Josh Gaoc2705962019-01-23 15:36:42 -0800414 asocket* s = create_local_socket(std::move(fd));
Greg Kaiser74b84982019-01-28 06:17:44 -0800415 LOG(VERBOSE) << "LS(" << s->id << "): bound to '" << name << "' via " << fd_value;
Benoit Goby88468f32012-03-16 14:50:07 -0700416
JP Abgralla84bd682012-03-30 13:19:11 -0700417#if !ADB_HOST
Josh Gao6dbf5792018-12-13 14:21:00 -0800418 if ((name.starts_with("root:") && getuid() != 0 && __android_log_is_debuggable()) ||
419 (name.starts_with("unroot:") && getuid() == 0) || name.starts_with("usb:") ||
420 name.starts_with("tcpip:")) {
Yabin Cui815ad882015-09-02 17:44:28 -0700421 D("LS(%d): enabling exit_on_close", s->id);
Benoit Goby88468f32012-03-16 14:50:07 -0700422 s->exit_on_close = 1;
423 }
JP Abgralla84bd682012-03-30 13:19:11 -0700424#endif
Benoit Goby88468f32012-03-16 14:50:07 -0700425
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800426 return s;
427}
428
Josh Gaocd2a5292018-03-07 16:52:28 -0800429static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700430 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 -0800431 apacket* p = get_apacket();
432
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800433 p->msg.command = A_WRTE;
434 p->msg.arg0 = s->peer->id;
435 p->msg.arg1 = s->id;
Josh Gaoa7d9d712018-02-01 13:17:50 -0800436
Josh Gao839b9322018-02-05 18:49:10 -0800437 if (data.size() > MAX_PAYLOAD) {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800438 put_apacket(p);
439 return -1;
440 }
441
Josh Gao839b9322018-02-05 18:49:10 -0800442 p->payload = std::move(data);
443 p->msg.data_length = p->payload.size();
444
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800445 send_packet(p, s->transport);
446 return 1;
447}
448
Josh Gaoef550fe2016-05-17 16:55:06 -0700449static void remote_socket_ready(asocket* s) {
450 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
451 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800452 p->msg.command = A_OKAY;
453 p->msg.arg0 = s->peer->id;
454 p->msg.arg1 = s->id;
455 send_packet(p, s->transport);
456}
457
Josh Gaoef550fe2016-05-17 16:55:06 -0700458static void remote_socket_shutdown(asocket* s) {
459 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
460 s->peer ? s->peer->fd : -1);
461 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800462 p->msg.command = A_CLSE;
Josh Gaoef550fe2016-05-17 16:55:06 -0700463 if (s->peer) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800464 p->msg.arg0 = s->peer->id;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100465 }
466 p->msg.arg1 = s->id;
467 send_packet(p, s->transport);
468}
469
Josh Gaoef550fe2016-05-17 16:55:06 -0700470static void remote_socket_close(asocket* s) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100471 if (s->peer) {
Yi Kong86e67182018-07-13 18:15:16 -0700472 s->peer->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700473 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 -0800474 s->peer->close(s->peer);
475 }
Josh Gaoef550fe2016-05-17 16:55:06 -0700476 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
477 s->peer ? s->peer->fd : -1);
Yabin Cui815ad882015-09-02 17:44:28 -0700478 D("RS(%d): closed", s->id);
Josh Gao5cb76ce2018-02-12 17:24:00 -0800479 delete s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800480}
481
Yabin Cui70ec57b2015-08-27 18:50:04 -0700482// Create a remote socket to exchange packets with a remote service through transport
483// |t|. Where |id| is the socket id of the corresponding service on the other
484// side of the transport (it is allocated by the remote side and _cannot_ be 0).
485// Returns a new non-NULL asocket handle.
Josh Gaoef550fe2016-05-17 16:55:06 -0700486asocket* create_remote_socket(unsigned id, atransport* t) {
487 if (id == 0) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700488 LOG(FATAL) << "invalid remote socket id (0)";
Josh Gaoef550fe2016-05-17 16:55:06 -0700489 }
Josh Gao5cb76ce2018-02-12 17:24:00 -0800490 asocket* s = new asocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800491 s->id = id;
492 s->enqueue = remote_socket_enqueue;
493 s->ready = remote_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100494 s->shutdown = remote_socket_shutdown;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800495 s->close = remote_socket_close;
496 s->transport = t;
497
Yabin Cui815ad882015-09-02 17:44:28 -0700498 D("RS(%d): created", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800499 return s;
500}
501
Josh Gao4a037e22018-12-20 17:00:13 -0800502void connect_to_remote(asocket* s, std::string_view destination) {
Yabin Cui815ad882015-09-02 17:44:28 -0700503 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gaoef550fe2016-05-17 16:55:06 -0700504 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800505
Josh Gao4a037e22018-12-20 17:00:13 -0800506 LOG(VERBOSE) << "LS(" << s->id << ": connect(" << destination << ")";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800507 p->msg.command = A_OPEN;
508 p->msg.arg0 = s->id;
Josh Gao839b9322018-02-05 18:49:10 -0800509
Josh Gao4a037e22018-12-20 17:00:13 -0800510 // adbd used to expect a null-terminated string.
511 // Keep doing so to maintain backward compatibility.
512 p->payload.resize(destination.size() + 1);
513 memcpy(p->payload.data(), destination.data(), destination.size());
514 p->payload[destination.size()] = '\0';
Josh Gao839b9322018-02-05 18:49:10 -0800515 p->msg.data_length = p->payload.size();
516
Elliott Hughese64126b2018-10-19 13:59:44 -0700517 CHECK_LE(p->msg.data_length, s->get_max_payload());
Josh Gao839b9322018-02-05 18:49:10 -0800518
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800519 send_packet(p, s->transport);
520}
521
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800522/* this is used by magic sockets to rig local sockets to
523 send the go-ahead message when they connect */
Josh Gaoef550fe2016-05-17 16:55:06 -0700524static void local_socket_ready_notify(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800525 s->ready = local_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700526 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800527 s->close = local_socket_close;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700528 SendOkay(s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800529 s->ready(s);
530}
531
532/* this is used by magic sockets to rig local sockets to
533 send the failure message if they are closed before
534 connected (to avoid closing them without a status message) */
Josh Gaoef550fe2016-05-17 16:55:06 -0700535static void local_socket_close_notify(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800536 s->ready = local_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700537 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800538 s->close = local_socket_close;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700539 SendFail(s->fd, "closed");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800540 s->close(s);
541}
542
Josh Gaoa7d9d712018-02-01 13:17:50 -0800543static unsigned unhex(const char* s, int len) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800544 unsigned n = 0, c;
545
Josh Gaoef550fe2016-05-17 16:55:06 -0700546 while (len-- > 0) {
547 switch ((c = *s++)) {
548 case '0':
549 case '1':
550 case '2':
551 case '3':
552 case '4':
553 case '5':
554 case '6':
555 case '7':
556 case '8':
557 case '9':
558 c -= '0';
559 break;
560 case 'a':
561 case 'b':
562 case 'c':
563 case 'd':
564 case 'e':
565 case 'f':
566 c = c - 'a' + 10;
567 break;
568 case 'A':
569 case 'B':
570 case 'C':
571 case 'D':
572 case 'E':
573 case 'F':
574 c = c - 'A' + 10;
575 break;
576 default:
577 return 0xffffffff;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800578 }
579
580 n = (n << 4) | c;
581 }
582
583 return n;
584}
585
Elliott Hughes88b4c852015-04-30 17:32:03 -0700586#if ADB_HOST
587
David Pursellc929c6f2016-03-01 08:58:26 -0800588namespace internal {
Scott Anderson27042382012-05-30 18:11:27 -0700589
Josh Gao8d49e122018-12-19 13:37:41 -0800590// Parses a host service string of the following format:
David Pursellc929c6f2016-03-01 08:58:26 -0800591// * [tcp:|udp:]<serial>[:<port>]:<command>
592// * <prefix>:<serial>:<command>
593// Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}.
Josh Gao8d49e122018-12-19 13:37:41 -0800594bool parse_host_service(std::string_view* out_serial, std::string_view* out_command,
595 std::string_view full_service) {
596 if (full_service.empty()) {
597 return false;
598 }
Terence Haddock6c670402011-03-16 09:43:56 +0100599
Josh Gao8d49e122018-12-19 13:37:41 -0800600 std::string_view serial;
601 std::string_view command = full_service;
602 // Remove |count| bytes from the beginning of command and add them to |serial|.
603 auto consume = [&full_service, &serial, &command](size_t count) {
604 CHECK_LE(count, command.size());
605 if (!serial.empty()) {
606 CHECK_EQ(serial.data() + serial.size(), command.data());
607 }
608
609 serial = full_service.substr(0, serial.size() + count);
610 command.remove_prefix(count);
611 };
612
613 // Remove the trailing : from serial, and assign the values to the output parameters.
614 auto finish = [out_serial, out_command, &serial, &command] {
615 if (serial.empty() || command.empty()) {
616 return false;
617 }
618
619 CHECK_EQ(':', serial.back());
620 serial.remove_suffix(1);
621
622 *out_serial = serial;
623 *out_command = command;
624 return true;
625 };
626
627 static constexpr std::string_view prefixes[] = {"usb:", "product:", "model:", "device:"};
628 for (std::string_view prefix : prefixes) {
629 if (command.starts_with(prefix)) {
630 consume(prefix.size());
631
632 size_t offset = command.find_first_of(':');
633 if (offset == std::string::npos) {
634 return false;
635 }
636 consume(offset + 1);
637 return finish();
David Pursellc929c6f2016-03-01 08:58:26 -0800638 }
Scott Anderson090e5cb2012-05-31 12:04:23 -0700639 }
640
David Pursellc929c6f2016-03-01 08:58:26 -0800641 // For fastboot compatibility, ignore protocol prefixes.
Josh Gao8d49e122018-12-19 13:37:41 -0800642 if (command.starts_with("tcp:") || command.starts_with("udp:")) {
643 consume(4);
644 if (command.empty()) {
645 return false;
David Pursell24b62a72016-09-21 12:08:37 -0700646 }
647 }
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800648 if (command.starts_with("vsock:")) {
649 // vsock serials are vsock:cid:port, which have an extra colon compared to tcp.
650 size_t next_colon = command.find(':');
651 if (next_colon == std::string::npos) {
652 return false;
653 }
654 consume(next_colon + 1);
655 }
David Pursell24b62a72016-09-21 12:08:37 -0700656
Josh Gao8d49e122018-12-19 13:37:41 -0800657 bool found_address = false;
658 if (command[0] == '[') {
659 // Read an IPv6 address. `adb connect` creates the serial number from the canonical
660 // network address so it will always have the [] delimiters.
661 size_t ipv6_end = command.find_first_of(']');
662 if (ipv6_end != std::string::npos) {
663 consume(ipv6_end + 1);
664 if (command.empty()) {
665 // Nothing after the IPv6 address.
666 return false;
667 } else if (command[0] != ':') {
668 // Garbage after the IPv6 address.
669 return false;
670 }
671 consume(1);
672 found_address = true;
673 }
Terence Haddock6c670402011-03-16 09:43:56 +0100674 }
David Pursellc929c6f2016-03-01 08:58:26 -0800675
Josh Gao8d49e122018-12-19 13:37:41 -0800676 if (!found_address) {
677 // Scan ahead to the next colon.
678 size_t offset = command.find_first_of(':');
679 if (offset == std::string::npos) {
680 return false;
Terence Haddock6c670402011-03-16 09:43:56 +0100681 }
Josh Gao8d49e122018-12-19 13:37:41 -0800682 consume(offset + 1);
683 }
684
685 // We're either at the beginning of a port, or the command itself.
686 // Look for a port in between colons.
687 size_t next_colon = command.find_first_of(':');
688 if (next_colon == std::string::npos) {
689 // No colon, we must be at the command.
690 return finish();
691 }
692
693 bool port_valid = true;
694 if (command.size() <= next_colon) {
695 return false;
696 }
697
698 std::string_view port = command.substr(0, next_colon);
699 for (auto digit : port) {
700 if (!isdigit(digit)) {
701 // Port isn't a number.
702 port_valid = false;
703 break;
Terence Haddock6c670402011-03-16 09:43:56 +0100704 }
705 }
Josh Gao8d49e122018-12-19 13:37:41 -0800706
707 if (port_valid) {
708 consume(next_colon + 1);
709 }
710 return finish();
Terence Haddock6c670402011-03-16 09:43:56 +0100711}
712
David Pursellc929c6f2016-03-01 08:58:26 -0800713} // namespace internal
714
Josh Gaoef550fe2016-05-17 16:55:06 -0700715#endif // ADB_HOST
Elliott Hughes88b4c852015-04-30 17:32:03 -0700716
Josh Gaocd2a5292018-03-07 16:52:28 -0800717static int smart_socket_enqueue(asocket* s, apacket::payload_type data) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800718#if ADB_HOST
Josh Gao8d49e122018-12-19 13:37:41 -0800719 std::string_view service;
720 std::string_view serial;
Josh Gaob39e4152017-08-16 16:57:01 -0700721 TransportId transport_id = 0;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700722 TransportType type = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800723#endif
724
Josh Gaoa7d9d712018-02-01 13:17:50 -0800725 D("SS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800726
Josh Gaoa7d9d712018-02-01 13:17:50 -0800727 if (s->smart_socket_data.empty()) {
Josh Gao9f155db2018-04-03 14:37:11 -0700728 // TODO: Make this an IOVector?
Josh Gaocd2a5292018-03-07 16:52:28 -0800729 s->smart_socket_data.assign(data.begin(), data.end());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800730 } else {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800731 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800732 }
733
Josh Gao9055a582016-01-15 14:35:54 -0800734 /* don't bother if we can't decode the length */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800735 if (s->smart_socket_data.size() < 4) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700736 return 0;
737 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800738
Josh Gaoa7d9d712018-02-01 13:17:50 -0800739 uint32_t len = unhex(s->smart_socket_data.data(), 4);
740 if (len == 0 || len > MAX_PAYLOAD) {
741 D("SS(%d): bad size (%u)", s->id, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800742 goto fail;
743 }
744
Josh Gaoa7d9d712018-02-01 13:17:50 -0800745 D("SS(%d): len is %u", s->id, len);
Josh Gao9055a582016-01-15 14:35:54 -0800746 /* can't do anything until we have the full header */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800747 if ((len + 4) > s->smart_socket_data.size()) {
748 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 -0800749 return 0;
750 }
751
Josh Gaoa7d9d712018-02-01 13:17:50 -0800752 s->smart_socket_data[len + 4] = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800753
Josh Gaoa7d9d712018-02-01 13:17:50 -0800754 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800755
756#if ADB_HOST
Josh Gao8d49e122018-12-19 13:37:41 -0800757 service = std::string_view(s->smart_socket_data).substr(4);
758 if (service.starts_with("host-serial:")) {
759 service.remove_prefix(strlen("host-serial:"));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800760
Terence Haddock6c670402011-03-16 09:43:56 +0100761 // serial number should follow "host:" and could be a host:port string.
Josh Gao8d49e122018-12-19 13:37:41 -0800762 if (!internal::parse_host_service(&serial, &service, service)) {
763 LOG(ERROR) << "SS(" << s->id << "): failed to parse host service: " << service;
764 goto fail;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800765 }
Josh Gao8d49e122018-12-19 13:37:41 -0800766 } else if (service.starts_with("host-transport-id:")) {
767 service.remove_prefix(strlen("host-transport-id:"));
768 if (!ParseUint(&transport_id, service, &service)) {
769 LOG(ERROR) << "SS(" << s->id << "): failed to parse host transport id: " << service;
Josh Gaob39e4152017-08-16 16:57:01 -0700770 return -1;
771 }
Josh Gao8d49e122018-12-19 13:37:41 -0800772 if (!service.starts_with(":")) {
773 LOG(ERROR) << "SS(" << s->id << "): host-transport-id without command";
774 return -1;
775 }
776 service.remove_prefix(1);
777 } else if (service.starts_with("host-usb:")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700778 type = kTransportUsb;
Josh Gao8d49e122018-12-19 13:37:41 -0800779 service.remove_prefix(strlen("host-usb:"));
780 } else if (service.starts_with("host-local:")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700781 type = kTransportLocal;
Josh Gao8d49e122018-12-19 13:37:41 -0800782 service.remove_prefix(strlen("host-local:"));
783 } else if (service.starts_with("host:")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700784 type = kTransportAny;
Josh Gao8d49e122018-12-19 13:37:41 -0800785 service.remove_prefix(strlen("host:"));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800786 } else {
Josh Gao8d49e122018-12-19 13:37:41 -0800787 service = std::string_view{};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800788 }
789
Josh Gao8d49e122018-12-19 13:37:41 -0800790 if (!service.empty()) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700791 asocket* s2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800792
Josh Gaod3067472018-08-07 14:14:21 -0700793 // Some requests are handled immediately -- in that case the handle_host_request() routine
794 // has sent the OKAY or FAIL message and all we have to do is clean up.
Josh Gaocd30bb32019-02-20 19:30:59 -0800795 if (handle_host_request(service, type,
Josh Gao8d49e122018-12-19 13:37:41 -0800796 serial.empty() ? nullptr : std::string(serial).c_str(),
797 transport_id, s->peer->fd, s)) {
798 LOG(VERBOSE) << "SS(" << s->id << "): handled host service '" << service << "'";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800799 goto fail;
800 }
Josh Gao8d49e122018-12-19 13:37:41 -0800801 if (service.starts_with("transport")) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700802 D("SS(%d): okay transport", s->id);
Josh Gaoa7d9d712018-02-01 13:17:50 -0800803 s->smart_socket_data.clear();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800804 return 0;
805 }
806
Josh Gaoef550fe2016-05-17 16:55:06 -0700807 /* try to find a local service with this name.
808 ** if no such service exists, we'll fail out
809 ** and tear down here.
810 */
Josh Gao8d49e122018-12-19 13:37:41 -0800811 // TODO: Convert to string_view.
Josh Gao0565ae02019-02-22 13:41:55 -0800812 s2 = host_service_to_socket(service, serial, transport_id);
Yi Kong86e67182018-07-13 18:15:16 -0700813 if (s2 == nullptr) {
Josh Gao8d49e122018-12-19 13:37:41 -0800814 LOG(VERBOSE) << "SS(" << s->id << "): couldn't create host service '" << service << "'";
Elliott Hughes88b4c852015-04-30 17:32:03 -0700815 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800816 goto fail;
817 }
818
Josh Gaoef550fe2016-05-17 16:55:06 -0700819 /* we've connected to a local host service,
820 ** so we make our peer back into a regular
821 ** local socket and bind it to the new local
822 ** service socket, acknowledge the successful
823 ** connection, and close this smart socket now
824 ** that its work is done.
825 */
Elliott Hughes88b4c852015-04-30 17:32:03 -0700826 SendOkay(s->peer->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800827
828 s->peer->ready = local_socket_ready;
Elliott Hughes67943d12015-10-07 14:55:10 -0700829 s->peer->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800830 s->peer->close = local_socket_close;
831 s->peer->peer = s2;
832 s2->peer = s->peer;
Yi Kong86e67182018-07-13 18:15:16 -0700833 s->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700834 D("SS(%d): okay", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800835 s->close(s);
836
Josh Gaoef550fe2016-05-17 16:55:06 -0700837 /* initial state is "ready" */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800838 s2->ready(s2);
839 return 0;
840 }
841#else /* !ADB_HOST */
Elliott Hughes67943d12015-10-07 14:55:10 -0700842 if (s->transport == nullptr) {
Elliott Hughesab882422015-04-16 22:54:44 -0700843 std::string error_msg = "unknown failure";
Josh Gaob39e4152017-08-16 16:57:01 -0700844 s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
Elliott Hughes67943d12015-10-07 14:55:10 -0700845 if (s->transport == nullptr) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700846 SendFail(s->peer->fd, error_msg);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800847 goto fail;
848 }
849 }
850#endif
851
Josh Gao4e562502016-10-27 14:01:08 -0700852 if (!s->transport) {
853 SendFail(s->peer->fd, "device offline (no transport)");
854 goto fail;
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700855 } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700856 /* if there's no remote we fail the connection
857 ** right here and terminate it
858 */
Josh Gao4e562502016-10-27 14:01:08 -0700859 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800860 goto fail;
861 }
862
Josh Gaoef550fe2016-05-17 16:55:06 -0700863 /* instrument our peer to pass the success or fail
864 ** message back once it connects or closes, then
865 ** detach from it, request the connection, and
866 ** tear down
867 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800868 s->peer->ready = local_socket_ready_notify;
Elliott Hughes67943d12015-10-07 14:55:10 -0700869 s->peer->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800870 s->peer->close = local_socket_close_notify;
Yi Kong86e67182018-07-13 18:15:16 -0700871 s->peer->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700872 /* give him our transport and upref it */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800873 s->peer->transport = s->transport;
874
Josh Gao4a037e22018-12-20 17:00:13 -0800875 connect_to_remote(s->peer, std::string_view(s->smart_socket_data).substr(4));
Yi Kong86e67182018-07-13 18:15:16 -0700876 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800877 s->close(s);
878 return 1;
879
880fail:
Josh Gaoef550fe2016-05-17 16:55:06 -0700881 /* we're going to close our peer as a side-effect, so
882 ** return -1 to signal that state to the local socket
883 ** who is enqueueing against us
884 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800885 s->close(s);
886 return -1;
887}
888
Josh Gaoef550fe2016-05-17 16:55:06 -0700889static void smart_socket_ready(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700890 D("SS(%d): ready", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800891}
892
Josh Gaoef550fe2016-05-17 16:55:06 -0700893static void smart_socket_close(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700894 D("SS(%d): closed", s->id);
Josh Gaoef550fe2016-05-17 16:55:06 -0700895 if (s->peer) {
Yi Kong86e67182018-07-13 18:15:16 -0700896 s->peer->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800897 s->peer->close(s->peer);
Yi Kong86e67182018-07-13 18:15:16 -0700898 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800899 }
Josh Gao5cb76ce2018-02-12 17:24:00 -0800900 delete s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800901}
902
Josh Gaoef550fe2016-05-17 16:55:06 -0700903static asocket* create_smart_socket(void) {
Yabin Cui815ad882015-09-02 17:44:28 -0700904 D("Creating smart socket");
Josh Gao5cb76ce2018-02-12 17:24:00 -0800905 asocket* s = new asocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800906 s->enqueue = smart_socket_enqueue;
907 s->ready = smart_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700908 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800909 s->close = smart_socket_close;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800910
Yabin Cui815ad882015-09-02 17:44:28 -0700911 D("SS(%d)", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800912 return s;
913}
914
Josh Gaoef550fe2016-05-17 16:55:06 -0700915void connect_to_smartsocket(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700916 D("Connecting to smart socket");
Josh Gaoef550fe2016-05-17 16:55:06 -0700917 asocket* ss = create_smart_socket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800918 s->peer = ss;
919 ss->peer = s;
920 s->ready(s);
921}
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100922
923size_t asocket::get_max_payload() const {
924 size_t max_payload = MAX_PAYLOAD;
925 if (transport) {
926 max_payload = std::min(max_payload, transport->get_max_payload());
927 }
928 if (peer && peer->transport) {
929 max_payload = std::min(max_payload, peer->transport->get_max_payload());
930 }
931 return max_payload;
932}