blob: 423af67f1c542016e9753669aff829dd0516ccb6 [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
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -070034#include <android-base/strings.h>
35
Dan Albertb302d122015-02-24 15:51:19 -080036#if !ADB_HOST
Elliott Hughes8b249d22016-09-23 15:40:03 -070037#include <android-base/properties.h>
Steven Morelandb087d302017-04-13 23:48:57 -070038#include <log/log_properties.h>
Dan Albertb302d122015-02-24 15:51:19 -080039#endif
Dan Albertdb6fe642015-03-19 15:21:08 -070040
41#include "adb.h"
42#include "adb_io.h"
Josh Gao8d49e122018-12-19 13:37:41 -080043#include "adb_utils.h"
Dan Albertb302d122015-02-24 15:51:19 -080044#include "transport.h"
Josh Gaocd2a5292018-03-07 16:52:28 -080045#include "types.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080046
Josh Gao18f7a5c2019-01-11 14:42:08 -080047using namespace std::chrono_literals;
48
Josh Gao0f1a20a2016-05-17 17:46:27 -070049static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080050static unsigned local_socket_next_id = 1;
51
Josh Gao2b3db9e2018-01-31 13:15:51 -080052static auto& local_socket_list = *new std::vector<asocket*>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080053
54/* the the list of currently closing local sockets.
55** these have no peer anymore, but still packets to
56** write to their fd.
57*/
Josh Gao2b3db9e2018-01-31 13:15:51 -080058static auto& local_socket_closing_list = *new std::vector<asocket*>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010060// Parse the global list of sockets to find one with id |local_id|.
61// If |peer_id| is not 0, also check that it is connected to a peer
62// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
Josh Gaoef550fe2016-05-17 16:55:06 -070063asocket* find_local_socket(unsigned local_id, unsigned peer_id) {
Josh Gao2b3db9e2018-01-31 13:15:51 -080064 asocket* result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080065
Josh Gao0f1a20a2016-05-17 17:46:27 -070066 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao2b3db9e2018-01-31 13:15:51 -080067 for (asocket* s : local_socket_list) {
Josh Gaoef550fe2016-05-17 16:55:06 -070068 if (s->id != local_id) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010069 continue;
Josh Gaoef550fe2016-05-17 16:55:06 -070070 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010071 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030072 result = s;
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030073 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010074 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080075 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080076
77 return result;
78}
79
Josh Gaoef550fe2016-05-17 16:55:06 -070080void install_local_socket(asocket* s) {
Josh Gao0f1a20a2016-05-17 17:46:27 -070081 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080082
83 s->id = local_socket_next_id++;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010084
85 // Socket ids should never be 0.
Josh Gaoef550fe2016-05-17 16:55:06 -070086 if (local_socket_next_id == 0) {
Elliott Hughese64126b2018-10-19 13:59:44 -070087 LOG(FATAL) << "local socket id overflow";
Josh Gaoef550fe2016-05-17 16:55:06 -070088 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010089
Josh Gao2b3db9e2018-01-31 13:15:51 -080090 local_socket_list.push_back(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080091}
92
Josh Gaoef550fe2016-05-17 16:55:06 -070093void remove_socket(asocket* s) {
Josh Gaob69c78b2017-09-13 11:17:33 -070094 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao2b3db9e2018-01-31 13:15:51 -080095 for (auto list : { &local_socket_list, &local_socket_closing_list }) {
96 list->erase(std::remove_if(list->begin(), list->end(), [s](asocket* x) { return x == s; }),
97 list->end());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080098 }
99}
100
Josh Gaoef550fe2016-05-17 16:55:06 -0700101void close_all_sockets(atransport* t) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700102 /* this is a little gross, but since s->close() *will* modify
103 ** the list out from under you, your options are limited.
104 */
Josh Gao0f1a20a2016-05-17 17:46:27 -0700105 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800106restart:
Josh Gao2b3db9e2018-01-31 13:15:51 -0800107 for (asocket* s : local_socket_list) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700108 if (s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gao80814e12016-05-18 10:39:48 -0700109 s->close(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800110 goto restart;
111 }
112 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800113}
114
Josh Gao4b808502018-03-19 13:20:29 -0700115enum class SocketFlushResult {
116 Destroyed,
117 TryAgain,
118 Completed,
119};
120
121static SocketFlushResult local_socket_flush_incoming(asocket* s) {
Josh Gao9f155db2018-04-03 14:37:11 -0700122 if (!s->packet_queue.empty()) {
123 std::vector<adb_iovec> iov = s->packet_queue.iovecs();
124 ssize_t rc = adb_writev(s->fd, iov.data(), iov.size());
125 if (rc > 0 && static_cast<size_t>(rc) == s->packet_queue.size()) {
126 s->packet_queue.clear();
Josh Gao4b808502018-03-19 13:20:29 -0700127 } else if (rc > 0) {
Yurii Zubrytskyie5e6b0d2019-07-12 14:11:54 -0700128 s->packet_queue.drop_front(rc);
Josh Gao9528df22018-05-14 11:14:33 -0700129 fdevent_add(s->fde, FDE_WRITE);
Josh Gao4b808502018-03-19 13:20:29 -0700130 return SocketFlushResult::TryAgain;
131 } else if (rc == -1 && errno == EAGAIN) {
Josh Gao9528df22018-05-14 11:14:33 -0700132 fdevent_add(s->fde, FDE_WRITE);
Josh Gao4b808502018-03-19 13:20:29 -0700133 return SocketFlushResult::TryAgain;
Josh Gaob50b92f2018-03-30 13:56:24 -0700134 } else {
135 // We failed to write, but it's possible that we can still read from the socket.
136 // Give that a try before giving up.
137 s->has_write_error = true;
Josh Gao4b808502018-03-19 13:20:29 -0700138 }
Josh Gao4b808502018-03-19 13:20:29 -0700139 }
140
141 // If we sent the last packet of a closing socket, we can now destroy it.
142 if (s->closing) {
143 s->close(s);
144 return SocketFlushResult::Destroyed;
145 }
146
Josh Gao9528df22018-05-14 11:14:33 -0700147 fdevent_del(s->fde, FDE_WRITE);
Josh Gao4b808502018-03-19 13:20:29 -0700148 return SocketFlushResult::Completed;
149}
150
151// Returns false if the socket has been closed and destroyed as a side-effect of this function.
152static bool local_socket_flush_outgoing(asocket* s) {
153 const size_t max_payload = s->get_max_payload();
Josh Gaocd2a5292018-03-07 16:52:28 -0800154 apacket::payload_type data;
Josh Gao4b808502018-03-19 13:20:29 -0700155 data.resize(max_payload);
156 char* x = &data[0];
157 size_t avail = max_payload;
158 int r = 0;
159 int is_eof = 0;
160
161 while (avail > 0) {
162 r = adb_read(s->fd, x, avail);
163 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r,
164 r < 0 ? errno : 0, avail);
165 if (r == -1) {
166 if (errno == EAGAIN) {
167 break;
168 }
169 } else if (r > 0) {
170 avail -= r;
171 x += r;
172 continue;
173 }
174
175 /* r = 0 or unhandled error */
176 is_eof = 1;
177 break;
178 }
179 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 -0700180 s->fde->force_eof);
Josh Gao4b808502018-03-19 13:20:29 -0700181
182 if (avail != max_payload && s->peer) {
183 data.resize(max_payload - avail);
184
185 // s->peer->enqueue() may call s->close() and free s,
186 // so save variables for debug printing below.
187 unsigned saved_id = s->id;
188 int saved_fd = s->fd;
189 r = s->peer->enqueue(s->peer, std::move(data));
190 D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
191
192 if (r < 0) {
193 // Error return means they closed us as a side-effect and we must
194 // return immediately.
195 //
196 // Note that if we still have buffered packets, the socket will be
197 // placed on the closing socket list. This handler function will be
198 // called again to process FDE_WRITE events.
199 return false;
200 }
201
202 if (r > 0) {
203 /* if the remote cannot accept further events,
204 ** we disable notification of READs. They'll
205 ** be enabled again when we get a call to ready()
206 */
Josh Gao9528df22018-05-14 11:14:33 -0700207 fdevent_del(s->fde, FDE_READ);
Josh Gao4b808502018-03-19 13:20:29 -0700208 }
209 }
210
211 // Don't allow a forced eof if data is still there.
Josh Gao9528df22018-05-14 11:14:33 -0700212 if ((s->fde->force_eof && !r) || is_eof) {
213 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 -0700214 s->close(s);
215 return false;
216 }
217
218 return true;
219}
220
Josh Gaocd2a5292018-03-07 16:52:28 -0800221static int local_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800222 D("LS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800223
Josh Gao9f155db2018-04-03 14:37:11 -0700224 s->packet_queue.append(std::move(data));
Josh Gao4b808502018-03-19 13:20:29 -0700225 switch (local_socket_flush_incoming(s)) {
226 case SocketFlushResult::Destroyed:
227 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800228
Josh Gao4b808502018-03-19 13:20:29 -0700229 case SocketFlushResult::TryAgain:
230 return 1;
231
232 case SocketFlushResult::Completed:
233 return 0;
234 }
235
236 return !s->packet_queue.empty();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800237}
238
Josh Gaoef550fe2016-05-17 16:55:06 -0700239static void local_socket_ready(asocket* s) {
Nanik Tolaramc624a7e2015-02-18 22:53:37 +1100240 /* far side is ready for data, pay attention to
241 readable events */
Josh Gao9528df22018-05-14 11:14:33 -0700242 fdevent_add(s->fde, FDE_READ);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800243}
244
Josh Gao18f7a5c2019-01-11 14:42:08 -0800245struct ClosingSocket {
246 std::chrono::steady_clock::time_point begin;
247};
248
249// The standard (RFC 1122 - 4.2.2.13) says that if we call close on a
250// socket while we have pending data, a TCP RST should be sent to the
251// other end to notify it that we didn't read all of its data. However,
252// this can result in data that we've successfully written out to be dropped
253// on the other end. To avoid this, instead of immediately closing a
254// socket, call shutdown on it instead, and then read from the file
255// descriptor until we hit EOF or an error before closing.
256static void deferred_close(unique_fd fd) {
257 // Shutdown the socket in the outgoing direction only, so that
258 // we don't have the same problem on the opposite end.
259 adb_shutdown(fd.get(), SHUT_WR);
260 auto callback = [](fdevent* fde, unsigned event, void* arg) {
261 auto socket_info = static_cast<ClosingSocket*>(arg);
262 if (event & FDE_READ) {
263 ssize_t rc;
264 char buf[BUFSIZ];
265 while ((rc = adb_read(fde->fd.get(), buf, sizeof(buf))) > 0) {
266 continue;
267 }
268
269 if (rc == -1 && errno == EAGAIN) {
270 // There's potentially more data to read.
271 auto duration = std::chrono::steady_clock::now() - socket_info->begin;
272 if (duration > 1s) {
273 LOG(WARNING) << "timeout expired while flushing socket, closing";
274 } else {
275 return;
276 }
277 }
278 } else if (event & FDE_TIMEOUT) {
279 LOG(WARNING) << "timeout expired while flushing socket, closing";
280 }
281
282 // Either there was an error, we hit the end of the socket, or our timeout expired.
283 fdevent_destroy(fde);
284 delete socket_info;
285 };
286
287 ClosingSocket* socket_info = new ClosingSocket{
288 .begin = std::chrono::steady_clock::now(),
289 };
290
291 fdevent* fde = fdevent_create(fd.release(), callback, socket_info);
292 fdevent_add(fde, FDE_READ);
293 fdevent_set_timeout(fde, 1s);
294}
295
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800296// be sure to hold the socket list lock when calling this
Josh Gaoef550fe2016-05-17 16:55:06 -0700297static void local_socket_destroy(asocket* s) {
Benoit Goby88468f32012-03-16 14:50:07 -0700298 int exit_on_close = s->exit_on_close;
299
Josh Gao9528df22018-05-14 11:14:33 -0700300 D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800301
Josh Gao18f7a5c2019-01-11 14:42:08 -0800302 deferred_close(fdevent_release(s->fde));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800303
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800304 remove_socket(s);
Josh Gao5cb76ce2018-02-12 17:24:00 -0800305 delete s;
Benoit Goby88468f32012-03-16 14:50:07 -0700306
307 if (exit_on_close) {
Yabin Cui815ad882015-09-02 17:44:28 -0700308 D("local_socket_destroy: exiting");
Benoit Goby88468f32012-03-16 14:50:07 -0700309 exit(1);
310 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800311}
312
Josh Gao0f1a20a2016-05-17 17:46:27 -0700313static void local_socket_close(asocket* s) {
314 D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd);
315 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gaoef550fe2016-05-17 16:55:06 -0700316 if (s->peer) {
317 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 +0100318 /* Note: it's important to call shutdown before disconnecting from
319 * the peer, this ensures that remote sockets can still get the id
320 * of the local socket they're connected to, to send a CLOSE()
321 * protocol event. */
Josh Gaoef550fe2016-05-17 16:55:06 -0700322 if (s->peer->shutdown) {
323 s->peer->shutdown(s->peer);
324 }
Josh Gao0f1a20a2016-05-17 17:46:27 -0700325 s->peer->peer = nullptr;
326 s->peer->close(s->peer);
327 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800328 }
329
Josh Gaoef550fe2016-05-17 16:55:06 -0700330 /* If we are already closing, or if there are no
331 ** pending packets, destroy immediately
332 */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800333 if (s->closing || s->has_write_error || s->packet_queue.empty()) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700334 int id = s->id;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800335 local_socket_destroy(s);
Yabin Cui815ad882015-09-02 17:44:28 -0700336 D("LS(%d): closed", id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800337 return;
338 }
339
Josh Gaoef550fe2016-05-17 16:55:06 -0700340 /* otherwise, put on the closing list
341 */
Yabin Cui815ad882015-09-02 17:44:28 -0700342 D("LS(%d): closing", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800343 s->closing = 1;
Josh Gao9528df22018-05-14 11:14:33 -0700344 fdevent_del(s->fde, FDE_READ);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800345 remove_socket(s);
Yabin Cui815ad882015-09-02 17:44:28 -0700346 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
Josh Gao2b3db9e2018-01-31 13:15:51 -0800347 local_socket_closing_list.push_back(s);
Josh Gao9528df22018-05-14 11:14:33 -0700348 CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800349}
350
Josh Gaoef550fe2016-05-17 16:55:06 -0700351static void local_socket_event_func(int fd, unsigned ev, void* _s) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800352 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui815ad882015-09-02 17:44:28 -0700353 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700354
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800355 /* put the FDE_WRITE processing before the FDE_READ
356 ** in order to simplify the code.
357 */
Dan Albertf30d73c2015-02-25 17:51:28 -0800358 if (ev & FDE_WRITE) {
Josh Gao4b808502018-03-19 13:20:29 -0700359 switch (local_socket_flush_incoming(s)) {
360 case SocketFlushResult::Destroyed:
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800361 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800362
Josh Gao4b808502018-03-19 13:20:29 -0700363 case SocketFlushResult::TryAgain:
364 break;
365
366 case SocketFlushResult::Completed:
367 s->peer->ready(s->peer);
368 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800369 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800370 }
371
Dan Albertf30d73c2015-02-25 17:51:28 -0800372 if (ev & FDE_READ) {
Josh Gao4b808502018-03-19 13:20:29 -0700373 if (!local_socket_flush_outgoing(s)) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700374 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800375 }
376 }
377
Josh Gaoef550fe2016-05-17 16:55:06 -0700378 if (ev & FDE_ERROR) {
379 /* this should be caught be the next read or write
380 ** catching it here means we may skip the last few
381 ** bytes of readable data.
382 */
Yabin Cui815ad882015-09-02 17:44:28 -0700383 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800384 return;
385 }
386}
387
Josh Gaoc2705962019-01-23 15:36:42 -0800388asocket* create_local_socket(unique_fd ufd) {
389 int fd = ufd.release();
Josh Gao5cb76ce2018-02-12 17:24:00 -0800390 asocket* s = new asocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800391 s->fd = fd;
392 s->enqueue = local_socket_enqueue;
393 s->ready = local_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700394 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800395 s->close = local_socket_close;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700396 install_local_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800397
Josh Gao9528df22018-05-14 11:14:33 -0700398 s->fde = fdevent_create(fd, local_socket_event_func, s);
Yabin Cui815ad882015-09-02 17:44:28 -0700399 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800400 return s;
401}
402
Josh Gao6dbf5792018-12-13 14:21:00 -0800403asocket* create_local_service_socket(std::string_view name, atransport* transport) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800404#if !ADB_HOST
Josh Gao3edf8072018-11-16 15:40:16 -0800405 if (asocket* s = daemon_service_to_socket(name); s) {
406 return s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800407 }
408#endif
Josh Gaoc2705962019-01-23 15:36:42 -0800409 unique_fd fd = service_to_fd(name, transport);
Josh Gaoef550fe2016-05-17 16:55:06 -0700410 if (fd < 0) {
Elliott Hughesc3d1c112016-06-15 14:46:56 -0700411 return nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700412 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800413
Greg Kaiser74b84982019-01-28 06:17:44 -0800414 int fd_value = fd.get();
Josh Gaoc2705962019-01-23 15:36:42 -0800415 asocket* s = create_local_socket(std::move(fd));
Greg Kaiser74b84982019-01-28 06:17:44 -0800416 LOG(VERBOSE) << "LS(" << s->id << "): bound to '" << name << "' via " << fd_value;
Benoit Goby88468f32012-03-16 14:50:07 -0700417
JP Abgralla84bd682012-03-30 13:19:11 -0700418#if !ADB_HOST
Josh Gao6dbf5792018-12-13 14:21:00 -0800419 if ((name.starts_with("root:") && getuid() != 0 && __android_log_is_debuggable()) ||
420 (name.starts_with("unroot:") && getuid() == 0) || name.starts_with("usb:") ||
421 name.starts_with("tcpip:")) {
Yabin Cui815ad882015-09-02 17:44:28 -0700422 D("LS(%d): enabling exit_on_close", s->id);
Benoit Goby88468f32012-03-16 14:50:07 -0700423 s->exit_on_close = 1;
424 }
JP Abgralla84bd682012-03-30 13:19:11 -0700425#endif
Benoit Goby88468f32012-03-16 14:50:07 -0700426
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800427 return s;
428}
429
Josh Gaocd2a5292018-03-07 16:52:28 -0800430static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700431 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 -0800432 apacket* p = get_apacket();
433
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800434 p->msg.command = A_WRTE;
435 p->msg.arg0 = s->peer->id;
436 p->msg.arg1 = s->id;
Josh Gaoa7d9d712018-02-01 13:17:50 -0800437
Josh Gao839b9322018-02-05 18:49:10 -0800438 if (data.size() > MAX_PAYLOAD) {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800439 put_apacket(p);
440 return -1;
441 }
442
Josh Gao839b9322018-02-05 18:49:10 -0800443 p->payload = std::move(data);
444 p->msg.data_length = p->payload.size();
445
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800446 send_packet(p, s->transport);
447 return 1;
448}
449
Josh Gaoef550fe2016-05-17 16:55:06 -0700450static void remote_socket_ready(asocket* s) {
451 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
452 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800453 p->msg.command = A_OKAY;
454 p->msg.arg0 = s->peer->id;
455 p->msg.arg1 = s->id;
456 send_packet(p, s->transport);
457}
458
Josh Gaoef550fe2016-05-17 16:55:06 -0700459static void remote_socket_shutdown(asocket* s) {
460 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
461 s->peer ? s->peer->fd : -1);
462 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800463 p->msg.command = A_CLSE;
Josh Gaoef550fe2016-05-17 16:55:06 -0700464 if (s->peer) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800465 p->msg.arg0 = s->peer->id;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100466 }
467 p->msg.arg1 = s->id;
468 send_packet(p, s->transport);
469}
470
Josh Gaoef550fe2016-05-17 16:55:06 -0700471static void remote_socket_close(asocket* s) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100472 if (s->peer) {
Yi Kong86e67182018-07-13 18:15:16 -0700473 s->peer->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700474 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 -0800475 s->peer->close(s->peer);
476 }
Josh Gaoef550fe2016-05-17 16:55:06 -0700477 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
478 s->peer ? s->peer->fd : -1);
Yabin Cui815ad882015-09-02 17:44:28 -0700479 D("RS(%d): closed", s->id);
Josh Gao5cb76ce2018-02-12 17:24:00 -0800480 delete s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800481}
482
Yabin Cui70ec57b2015-08-27 18:50:04 -0700483// Create a remote socket to exchange packets with a remote service through transport
484// |t|. Where |id| is the socket id of the corresponding service on the other
485// side of the transport (it is allocated by the remote side and _cannot_ be 0).
486// Returns a new non-NULL asocket handle.
Josh Gaoef550fe2016-05-17 16:55:06 -0700487asocket* create_remote_socket(unsigned id, atransport* t) {
488 if (id == 0) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700489 LOG(FATAL) << "invalid remote socket id (0)";
Josh Gaoef550fe2016-05-17 16:55:06 -0700490 }
Josh Gao5cb76ce2018-02-12 17:24:00 -0800491 asocket* s = new asocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800492 s->id = id;
493 s->enqueue = remote_socket_enqueue;
494 s->ready = remote_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100495 s->shutdown = remote_socket_shutdown;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800496 s->close = remote_socket_close;
497 s->transport = t;
498
Yabin Cui815ad882015-09-02 17:44:28 -0700499 D("RS(%d): created", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800500 return s;
501}
502
Josh Gao4a037e22018-12-20 17:00:13 -0800503void connect_to_remote(asocket* s, std::string_view destination) {
Yabin Cui815ad882015-09-02 17:44:28 -0700504 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gaoef550fe2016-05-17 16:55:06 -0700505 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800506
Josh Gao4a037e22018-12-20 17:00:13 -0800507 LOG(VERBOSE) << "LS(" << s->id << ": connect(" << destination << ")";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800508 p->msg.command = A_OPEN;
509 p->msg.arg0 = s->id;
Josh Gao839b9322018-02-05 18:49:10 -0800510
Josh Gao4a037e22018-12-20 17:00:13 -0800511 // adbd used to expect a null-terminated string.
512 // Keep doing so to maintain backward compatibility.
513 p->payload.resize(destination.size() + 1);
514 memcpy(p->payload.data(), destination.data(), destination.size());
515 p->payload[destination.size()] = '\0';
Josh Gao839b9322018-02-05 18:49:10 -0800516 p->msg.data_length = p->payload.size();
517
Elliott Hughese64126b2018-10-19 13:59:44 -0700518 CHECK_LE(p->msg.data_length, s->get_max_payload());
Josh Gao839b9322018-02-05 18:49:10 -0800519
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800520 send_packet(p, s->transport);
521}
522
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800523/* this is used by magic sockets to rig local sockets to
524 send the go-ahead message when they connect */
Josh Gaoef550fe2016-05-17 16:55:06 -0700525static void local_socket_ready_notify(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800526 s->ready = local_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700527 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800528 s->close = local_socket_close;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700529 SendOkay(s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800530 s->ready(s);
531}
532
533/* this is used by magic sockets to rig local sockets to
534 send the failure message if they are closed before
535 connected (to avoid closing them without a status message) */
Josh Gaoef550fe2016-05-17 16:55:06 -0700536static void local_socket_close_notify(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800537 s->ready = local_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700538 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800539 s->close = local_socket_close;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700540 SendFail(s->fd, "closed");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800541 s->close(s);
542}
543
Josh Gaoa7d9d712018-02-01 13:17:50 -0800544static unsigned unhex(const char* s, int len) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800545 unsigned n = 0, c;
546
Josh Gaoef550fe2016-05-17 16:55:06 -0700547 while (len-- > 0) {
548 switch ((c = *s++)) {
549 case '0':
550 case '1':
551 case '2':
552 case '3':
553 case '4':
554 case '5':
555 case '6':
556 case '7':
557 case '8':
558 case '9':
559 c -= '0';
560 break;
561 case 'a':
562 case 'b':
563 case 'c':
564 case 'd':
565 case 'e':
566 case 'f':
567 c = c - 'a' + 10;
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 default:
578 return 0xffffffff;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800579 }
580
581 n = (n << 4) | c;
582 }
583
584 return n;
585}
586
Elliott Hughes88b4c852015-04-30 17:32:03 -0700587#if ADB_HOST
588
David Pursellc929c6f2016-03-01 08:58:26 -0800589namespace internal {
Scott Anderson27042382012-05-30 18:11:27 -0700590
Josh Gao8d49e122018-12-19 13:37:41 -0800591// Parses a host service string of the following format:
David Pursellc929c6f2016-03-01 08:58:26 -0800592// * [tcp:|udp:]<serial>[:<port>]:<command>
593// * <prefix>:<serial>:<command>
594// 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 -0800595bool parse_host_service(std::string_view* out_serial, std::string_view* out_command,
596 std::string_view full_service) {
597 if (full_service.empty()) {
598 return false;
599 }
Terence Haddock6c670402011-03-16 09:43:56 +0100600
Josh Gao8d49e122018-12-19 13:37:41 -0800601 std::string_view serial;
602 std::string_view command = full_service;
603 // Remove |count| bytes from the beginning of command and add them to |serial|.
604 auto consume = [&full_service, &serial, &command](size_t count) {
605 CHECK_LE(count, command.size());
606 if (!serial.empty()) {
607 CHECK_EQ(serial.data() + serial.size(), command.data());
608 }
609
610 serial = full_service.substr(0, serial.size() + count);
611 command.remove_prefix(count);
612 };
613
614 // Remove the trailing : from serial, and assign the values to the output parameters.
615 auto finish = [out_serial, out_command, &serial, &command] {
616 if (serial.empty() || command.empty()) {
617 return false;
618 }
619
620 CHECK_EQ(':', serial.back());
621 serial.remove_suffix(1);
622
623 *out_serial = serial;
624 *out_command = command;
625 return true;
626 };
627
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900628 static constexpr std::string_view prefixes[] = {
629 "usb:", "product:", "model:", "device:", "localfilesystem:"};
Josh Gao8d49e122018-12-19 13:37:41 -0800630 for (std::string_view prefix : prefixes) {
631 if (command.starts_with(prefix)) {
632 consume(prefix.size());
633
634 size_t offset = command.find_first_of(':');
635 if (offset == std::string::npos) {
636 return false;
637 }
638 consume(offset + 1);
639 return finish();
David Pursellc929c6f2016-03-01 08:58:26 -0800640 }
Scott Anderson090e5cb2012-05-31 12:04:23 -0700641 }
642
David Pursellc929c6f2016-03-01 08:58:26 -0800643 // For fastboot compatibility, ignore protocol prefixes.
Josh Gao8d49e122018-12-19 13:37:41 -0800644 if (command.starts_with("tcp:") || command.starts_with("udp:")) {
645 consume(4);
646 if (command.empty()) {
647 return false;
David Pursell24b62a72016-09-21 12:08:37 -0700648 }
649 }
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800650 if (command.starts_with("vsock:")) {
651 // vsock serials are vsock:cid:port, which have an extra colon compared to tcp.
652 size_t next_colon = command.find(':');
653 if (next_colon == std::string::npos) {
654 return false;
655 }
656 consume(next_colon + 1);
657 }
David Pursell24b62a72016-09-21 12:08:37 -0700658
Josh Gao8d49e122018-12-19 13:37:41 -0800659 bool found_address = false;
660 if (command[0] == '[') {
661 // Read an IPv6 address. `adb connect` creates the serial number from the canonical
662 // network address so it will always have the [] delimiters.
663 size_t ipv6_end = command.find_first_of(']');
664 if (ipv6_end != std::string::npos) {
665 consume(ipv6_end + 1);
666 if (command.empty()) {
667 // Nothing after the IPv6 address.
668 return false;
669 } else if (command[0] != ':') {
670 // Garbage after the IPv6 address.
671 return false;
672 }
673 consume(1);
674 found_address = true;
675 }
Terence Haddock6c670402011-03-16 09:43:56 +0100676 }
David Pursellc929c6f2016-03-01 08:58:26 -0800677
Josh Gao8d49e122018-12-19 13:37:41 -0800678 if (!found_address) {
679 // Scan ahead to the next colon.
680 size_t offset = command.find_first_of(':');
681 if (offset == std::string::npos) {
682 return false;
Terence Haddock6c670402011-03-16 09:43:56 +0100683 }
Josh Gao8d49e122018-12-19 13:37:41 -0800684 consume(offset + 1);
685 }
686
687 // We're either at the beginning of a port, or the command itself.
688 // Look for a port in between colons.
689 size_t next_colon = command.find_first_of(':');
690 if (next_colon == std::string::npos) {
691 // No colon, we must be at the command.
692 return finish();
693 }
694
695 bool port_valid = true;
696 if (command.size() <= next_colon) {
697 return false;
698 }
699
700 std::string_view port = command.substr(0, next_colon);
701 for (auto digit : port) {
702 if (!isdigit(digit)) {
703 // Port isn't a number.
704 port_valid = false;
705 break;
Terence Haddock6c670402011-03-16 09:43:56 +0100706 }
707 }
Josh Gao8d49e122018-12-19 13:37:41 -0800708
709 if (port_valid) {
710 consume(next_colon + 1);
711 }
712 return finish();
Terence Haddock6c670402011-03-16 09:43:56 +0100713}
714
David Pursellc929c6f2016-03-01 08:58:26 -0800715} // namespace internal
716
Josh Gaoef550fe2016-05-17 16:55:06 -0700717#endif // ADB_HOST
Elliott Hughes88b4c852015-04-30 17:32:03 -0700718
Josh Gaocd2a5292018-03-07 16:52:28 -0800719static int smart_socket_enqueue(asocket* s, apacket::payload_type data) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800720#if ADB_HOST
Josh Gao8d49e122018-12-19 13:37:41 -0800721 std::string_view service;
722 std::string_view serial;
Josh Gaob39e4152017-08-16 16:57:01 -0700723 TransportId transport_id = 0;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700724 TransportType type = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800725#endif
726
Josh Gaoa7d9d712018-02-01 13:17:50 -0800727 D("SS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800728
Josh Gaoa7d9d712018-02-01 13:17:50 -0800729 if (s->smart_socket_data.empty()) {
Josh Gao9f155db2018-04-03 14:37:11 -0700730 // TODO: Make this an IOVector?
Josh Gaocd2a5292018-03-07 16:52:28 -0800731 s->smart_socket_data.assign(data.begin(), data.end());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800732 } else {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800733 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800734 }
735
Josh Gao9055a582016-01-15 14:35:54 -0800736 /* don't bother if we can't decode the length */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800737 if (s->smart_socket_data.size() < 4) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700738 return 0;
739 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800740
Josh Gaoa7d9d712018-02-01 13:17:50 -0800741 uint32_t len = unhex(s->smart_socket_data.data(), 4);
742 if (len == 0 || len > MAX_PAYLOAD) {
743 D("SS(%d): bad size (%u)", s->id, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800744 goto fail;
745 }
746
Josh Gaoa7d9d712018-02-01 13:17:50 -0800747 D("SS(%d): len is %u", s->id, len);
Josh Gao9055a582016-01-15 14:35:54 -0800748 /* can't do anything until we have the full header */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800749 if ((len + 4) > s->smart_socket_data.size()) {
750 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 -0800751 return 0;
752 }
753
Josh Gaoa7d9d712018-02-01 13:17:50 -0800754 s->smart_socket_data[len + 4] = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800755
Josh Gaoa7d9d712018-02-01 13:17:50 -0800756 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800757
758#if ADB_HOST
Josh Gao8d49e122018-12-19 13:37:41 -0800759 service = std::string_view(s->smart_socket_data).substr(4);
Josh Gao07790752019-09-13 00:12:26 +0800760
761 // TODO: These should be handled in handle_host_request.
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700762 if (android::base::ConsumePrefix(&service, "host-serial:")) {
Terence Haddock6c670402011-03-16 09:43:56 +0100763 // serial number should follow "host:" and could be a host:port string.
Josh Gao8d49e122018-12-19 13:37:41 -0800764 if (!internal::parse_host_service(&serial, &service, service)) {
765 LOG(ERROR) << "SS(" << s->id << "): failed to parse host service: " << service;
766 goto fail;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800767 }
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700768 } else if (android::base::ConsumePrefix(&service, "host-transport-id:")) {
Josh Gao8d49e122018-12-19 13:37:41 -0800769 if (!ParseUint(&transport_id, service, &service)) {
770 LOG(ERROR) << "SS(" << s->id << "): failed to parse host transport id: " << service;
Josh Gaob39e4152017-08-16 16:57:01 -0700771 return -1;
772 }
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700773 if (!android::base::ConsumePrefix(&service, ":")) {
Josh Gao8d49e122018-12-19 13:37:41 -0800774 LOG(ERROR) << "SS(" << s->id << "): host-transport-id without command";
775 return -1;
776 }
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700777 } else if (android::base::ConsumePrefix(&service, "host-usb:")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700778 type = kTransportUsb;
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700779 } else if (android::base::ConsumePrefix(&service, "host-local:")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700780 type = kTransportLocal;
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700781 } else if (android::base::ConsumePrefix(&service, "host:")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700782 type = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800783 } else {
Josh Gao8d49e122018-12-19 13:37:41 -0800784 service = std::string_view{};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800785 }
786
Josh Gao8d49e122018-12-19 13:37:41 -0800787 if (!service.empty()) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700788 asocket* s2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800789
Josh Gaod3067472018-08-07 14:14:21 -0700790 // Some requests are handled immediately -- in that case the handle_host_request() routine
791 // has sent the OKAY or FAIL message and all we have to do is clean up.
Josh Gaob13f3cd2019-02-20 20:37:26 -0800792 auto host_request_result = handle_host_request(
793 service, type, serial.empty() ? nullptr : std::string(serial).c_str(), transport_id,
794 s->peer->fd, s);
795
796 switch (host_request_result) {
797 case HostRequestResult::Handled:
798 LOG(VERBOSE) << "SS(" << s->id << "): handled host service '" << service << "'";
799 goto fail;
800
801 case HostRequestResult::SwitchedTransport:
802 D("SS(%d): okay transport", s->id);
803 s->smart_socket_data.clear();
804 return 0;
805
806 case HostRequestResult::Unhandled:
807 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800808 }
809
Josh Gaoef550fe2016-05-17 16:55:06 -0700810 /* try to find a local service with this name.
811 ** if no such service exists, we'll fail out
812 ** and tear down here.
813 */
Josh Gao8d49e122018-12-19 13:37:41 -0800814 // TODO: Convert to string_view.
Josh Gao0565ae02019-02-22 13:41:55 -0800815 s2 = host_service_to_socket(service, serial, transport_id);
Yi Kong86e67182018-07-13 18:15:16 -0700816 if (s2 == nullptr) {
Josh Gao8d49e122018-12-19 13:37:41 -0800817 LOG(VERBOSE) << "SS(" << s->id << "): couldn't create host service '" << service << "'";
Elliott Hughes88b4c852015-04-30 17:32:03 -0700818 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800819 goto fail;
820 }
821
Josh Gaoef550fe2016-05-17 16:55:06 -0700822 /* we've connected to a local host service,
823 ** so we make our peer back into a regular
824 ** local socket and bind it to the new local
825 ** service socket, acknowledge the successful
826 ** connection, and close this smart socket now
827 ** that its work is done.
828 */
Elliott Hughes88b4c852015-04-30 17:32:03 -0700829 SendOkay(s->peer->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800830
831 s->peer->ready = local_socket_ready;
Elliott Hughes67943d12015-10-07 14:55:10 -0700832 s->peer->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800833 s->peer->close = local_socket_close;
834 s->peer->peer = s2;
835 s2->peer = s->peer;
Yi Kong86e67182018-07-13 18:15:16 -0700836 s->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700837 D("SS(%d): okay", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800838 s->close(s);
839
Josh Gaoef550fe2016-05-17 16:55:06 -0700840 /* initial state is "ready" */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800841 s2->ready(s2);
842 return 0;
843 }
844#else /* !ADB_HOST */
Elliott Hughes67943d12015-10-07 14:55:10 -0700845 if (s->transport == nullptr) {
Elliott Hughesab882422015-04-16 22:54:44 -0700846 std::string error_msg = "unknown failure";
Josh Gaob39e4152017-08-16 16:57:01 -0700847 s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
Elliott Hughes67943d12015-10-07 14:55:10 -0700848 if (s->transport == nullptr) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700849 SendFail(s->peer->fd, error_msg);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800850 goto fail;
851 }
852 }
853#endif
854
Josh Gao4e562502016-10-27 14:01:08 -0700855 if (!s->transport) {
856 SendFail(s->peer->fd, "device offline (no transport)");
857 goto fail;
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700858 } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700859 /* if there's no remote we fail the connection
860 ** right here and terminate it
861 */
Josh Gao4e562502016-10-27 14:01:08 -0700862 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800863 goto fail;
864 }
865
Josh Gaoef550fe2016-05-17 16:55:06 -0700866 /* instrument our peer to pass the success or fail
867 ** message back once it connects or closes, then
868 ** detach from it, request the connection, and
869 ** tear down
870 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800871 s->peer->ready = local_socket_ready_notify;
Elliott Hughes67943d12015-10-07 14:55:10 -0700872 s->peer->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800873 s->peer->close = local_socket_close_notify;
Yi Kong86e67182018-07-13 18:15:16 -0700874 s->peer->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700875 /* give him our transport and upref it */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800876 s->peer->transport = s->transport;
877
Josh Gao4a037e22018-12-20 17:00:13 -0800878 connect_to_remote(s->peer, std::string_view(s->smart_socket_data).substr(4));
Yi Kong86e67182018-07-13 18:15:16 -0700879 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800880 s->close(s);
881 return 1;
882
883fail:
Josh Gaoef550fe2016-05-17 16:55:06 -0700884 /* we're going to close our peer as a side-effect, so
885 ** return -1 to signal that state to the local socket
886 ** who is enqueueing against us
887 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800888 s->close(s);
889 return -1;
890}
891
Josh Gaoef550fe2016-05-17 16:55:06 -0700892static void smart_socket_ready(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700893 D("SS(%d): ready", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800894}
895
Josh Gaoef550fe2016-05-17 16:55:06 -0700896static void smart_socket_close(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700897 D("SS(%d): closed", s->id);
Josh Gaoef550fe2016-05-17 16:55:06 -0700898 if (s->peer) {
Yi Kong86e67182018-07-13 18:15:16 -0700899 s->peer->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800900 s->peer->close(s->peer);
Yi Kong86e67182018-07-13 18:15:16 -0700901 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800902 }
Josh Gao5cb76ce2018-02-12 17:24:00 -0800903 delete s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800904}
905
Josh Gaoef550fe2016-05-17 16:55:06 -0700906static asocket* create_smart_socket(void) {
Yabin Cui815ad882015-09-02 17:44:28 -0700907 D("Creating smart socket");
Josh Gao5cb76ce2018-02-12 17:24:00 -0800908 asocket* s = new asocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800909 s->enqueue = smart_socket_enqueue;
910 s->ready = smart_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700911 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800912 s->close = smart_socket_close;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800913
Yabin Cui815ad882015-09-02 17:44:28 -0700914 D("SS(%d)", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800915 return s;
916}
917
Josh Gaoef550fe2016-05-17 16:55:06 -0700918void connect_to_smartsocket(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700919 D("Connecting to smart socket");
Josh Gaoef550fe2016-05-17 16:55:06 -0700920 asocket* ss = create_smart_socket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800921 s->peer = ss;
922 ss->peer = s;
923 s->ready(s);
924}
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100925
926size_t asocket::get_max_payload() const {
927 size_t max_payload = MAX_PAYLOAD;
928 if (transport) {
929 max_payload = std::min(max_payload, transport->get_max_payload());
930 }
931 if (peer && peer->transport) {
932 max_payload = std::min(max_payload, peer->transport->get_max_payload());
933 }
934 return max_payload;
935}