blob: 420a6d5a349fecd7e89074a5d079cc633a118e5d [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG SOCKETS
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
Dan Albert76649012015-02-24 15:51:19 -080021#include <ctype.h>
22#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <stdio.h>
24#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Spencer Low363af562015-11-07 18:51:54 -080028#include <algorithm>
Josh Gao74b7ec72019-01-11 14:42:08 -080029#include <chrono>
Josh Gao9b587de2016-05-17 17:46:27 -070030#include <mutex>
David Pursell3f902aa2016-03-01 08:58:26 -080031#include <string>
32#include <vector>
Spencer Low363af562015-11-07 18:51:54 -080033
Dan Albert76649012015-02-24 15:51:19 -080034#if !ADB_HOST
Elliott Hughesffdec182016-09-23 15:40:03 -070035#include <android-base/properties.h>
Steven Morelandd73be1b2017-04-13 23:48:57 -070036#include <log/log_properties.h>
Dan Albert76649012015-02-24 15:51:19 -080037#endif
Dan Albert33134262015-03-19 15:21:08 -070038
39#include "adb.h"
40#include "adb_io.h"
Josh Gaobd767202018-12-19 13:37:41 -080041#include "adb_utils.h"
Dan Albert76649012015-02-24 15:51:19 -080042#include "transport.h"
Josh Gao1ce99572018-03-07 16:52:28 -080043#include "types.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
Josh Gao74b7ec72019-01-11 14:42:08 -080045using namespace std::chrono_literals;
46
Josh Gao9b587de2016-05-17 17:46:27 -070047static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048static unsigned local_socket_next_id = 1;
49
Josh Gao5e507642018-01-31 13:15:51 -080050static auto& local_socket_list = *new std::vector<asocket*>();
The Android Open Source Projectdd7bc332009-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 Gao5e507642018-01-31 13:15:51 -080056static auto& local_socket_closing_list = *new std::vector<asocket*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
David 'Digit' Turner818d6412013-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 Gao52bd8522016-05-17 16:55:06 -070061asocket* find_local_socket(unsigned local_id, unsigned peer_id) {
Josh Gao5e507642018-01-31 13:15:51 -080062 asocket* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
Josh Gao9b587de2016-05-17 17:46:27 -070064 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao5e507642018-01-31 13:15:51 -080065 for (asocket* s : local_socket_list) {
Josh Gao52bd8522016-05-17 16:55:06 -070066 if (s->id != local_id) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +010067 continue;
Josh Gao52bd8522016-05-17 16:55:06 -070068 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010069 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa81828292010-06-12 11:40:20 -030070 result = s;
André Goddard Rosa81828292010-06-12 11:40:20 -030071 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010072 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
75 return result;
76}
77
Josh Gao52bd8522016-05-17 16:55:06 -070078void install_local_socket(asocket* s) {
Josh Gao9b587de2016-05-17 17:46:27 -070079 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
81 s->id = local_socket_next_id++;
David 'Digit' Turner818d6412013-12-13 14:09:44 +010082
83 // Socket ids should never be 0.
Josh Gao52bd8522016-05-17 16:55:06 -070084 if (local_socket_next_id == 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -070085 LOG(FATAL) << "local socket id overflow";
Josh Gao52bd8522016-05-17 16:55:06 -070086 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010087
Josh Gao5e507642018-01-31 13:15:51 -080088 local_socket_list.push_back(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089}
90
Josh Gao52bd8522016-05-17 16:55:06 -070091void remove_socket(asocket* s) {
Josh Gao62c92f02017-09-13 11:17:33 -070092 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao5e507642018-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 Projectdd7bc332009-03-03 19:32:55 -080096 }
97}
98
Josh Gao52bd8522016-05-17 16:55:06 -070099void close_all_sockets(atransport* t) {
Josh Gao52bd8522016-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 Gao9b587de2016-05-17 17:46:27 -0700103 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104restart:
Josh Gao5e507642018-01-31 13:15:51 -0800105 for (asocket* s : local_socket_list) {
Josh Gao52bd8522016-05-17 16:55:06 -0700106 if (s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gao53eb31d2016-05-18 10:39:48 -0700107 s->close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 goto restart;
109 }
110 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111}
112
Josh Gao184f4802018-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 Gao7c738cd2018-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 Gao184f4802018-03-19 13:20:29 -0700125 } else if (rc > 0) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700126 // TODO: Implement a faster drop_front?
127 s->packet_queue.take_front(rc);
Josh Gao71f775a2018-05-14 11:14:33 -0700128 fdevent_add(s->fde, FDE_WRITE);
Josh Gao184f4802018-03-19 13:20:29 -0700129 return SocketFlushResult::TryAgain;
130 } else if (rc == -1 && errno == EAGAIN) {
Josh Gao71f775a2018-05-14 11:14:33 -0700131 fdevent_add(s->fde, FDE_WRITE);
Josh Gao184f4802018-03-19 13:20:29 -0700132 return SocketFlushResult::TryAgain;
Josh Gao954e1282018-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 Gao184f4802018-03-19 13:20:29 -0700137 }
Josh Gao184f4802018-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 Gao71f775a2018-05-14 11:14:33 -0700146 fdevent_del(s->fde, FDE_WRITE);
Josh Gao184f4802018-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 Gao1ce99572018-03-07 16:52:28 -0800153 apacket::payload_type data;
Josh Gao184f4802018-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 Gao71f775a2018-05-14 11:14:33 -0700179 s->fde->force_eof);
Josh Gao184f4802018-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 Gao71f775a2018-05-14 11:14:33 -0700206 fdevent_del(s->fde, FDE_READ);
Josh Gao184f4802018-03-19 13:20:29 -0700207 }
208 }
209
210 // Don't allow a forced eof if data is still there.
Josh Gao71f775a2018-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 Gao184f4802018-03-19 13:20:29 -0700213 s->close(s);
214 return false;
215 }
216
217 return true;
218}
219
Josh Gao1ce99572018-03-07 16:52:28 -0800220static int local_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800221 D("LS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222
Josh Gao7c738cd2018-04-03 14:37:11 -0700223 s->packet_queue.append(std::move(data));
Josh Gao184f4802018-03-19 13:20:29 -0700224 switch (local_socket_flush_incoming(s)) {
225 case SocketFlushResult::Destroyed:
226 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227
Josh Gao184f4802018-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 Projectdd7bc332009-03-03 19:32:55 -0800236}
237
Josh Gao52bd8522016-05-17 16:55:06 -0700238static void local_socket_ready(asocket* s) {
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100239 /* far side is ready for data, pay attention to
240 readable events */
Josh Gao71f775a2018-05-14 11:14:33 -0700241 fdevent_add(s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242}
243
Josh Gao74b7ec72019-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 Projectdd7bc332009-03-03 19:32:55 -0800295// be sure to hold the socket list lock when calling this
Josh Gao52bd8522016-05-17 16:55:06 -0700296static void local_socket_destroy(asocket* s) {
Benoit Gobyf366b362012-03-16 14:50:07 -0700297 int exit_on_close = s->exit_on_close;
298
Josh Gao71f775a2018-05-14 11:14:33 -0700299 D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300
Josh Gao74b7ec72019-01-11 14:42:08 -0800301 deferred_close(fdevent_release(s->fde));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 remove_socket(s);
Josh Gaoe0361d12018-02-12 17:24:00 -0800304 delete s;
Benoit Gobyf366b362012-03-16 14:50:07 -0700305
306 if (exit_on_close) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700307 D("local_socket_destroy: exiting");
Benoit Gobyf366b362012-03-16 14:50:07 -0700308 exit(1);
309 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310}
311
Josh Gao9b587de2016-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 Gao52bd8522016-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' Turner818d6412013-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 Gao52bd8522016-05-17 16:55:06 -0700321 if (s->peer->shutdown) {
322 s->peer->shutdown(s->peer);
323 }
Josh Gao9b587de2016-05-17 17:46:27 -0700324 s->peer->peer = nullptr;
325 s->peer->close(s->peer);
326 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 }
328
Josh Gao52bd8522016-05-17 16:55:06 -0700329 /* If we are already closing, or if there are no
330 ** pending packets, destroy immediately
331 */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800332 if (s->closing || s->has_write_error || s->packet_queue.empty()) {
Josh Gao52bd8522016-05-17 16:55:06 -0700333 int id = s->id;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 local_socket_destroy(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700335 D("LS(%d): closed", id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 return;
337 }
338
Josh Gao52bd8522016-05-17 16:55:06 -0700339 /* otherwise, put on the closing list
340 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700341 D("LS(%d): closing", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 s->closing = 1;
Josh Gao71f775a2018-05-14 11:14:33 -0700343 fdevent_del(s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 remove_socket(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700345 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
Josh Gao5e507642018-01-31 13:15:51 -0800346 local_socket_closing_list.push_back(s);
Josh Gao71f775a2018-05-14 11:14:33 -0700347 CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348}
349
Josh Gao52bd8522016-05-17 16:55:06 -0700350static void local_socket_event_func(int fd, unsigned ev, void* _s) {
Dan Albertbac34742015-02-25 17:51:28 -0800351 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700352 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall408fa572011-03-16 15:57:42 -0700353
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 /* put the FDE_WRITE processing before the FDE_READ
355 ** in order to simplify the code.
356 */
Dan Albertbac34742015-02-25 17:51:28 -0800357 if (ev & FDE_WRITE) {
Josh Gao184f4802018-03-19 13:20:29 -0700358 switch (local_socket_flush_incoming(s)) {
359 case SocketFlushResult::Destroyed:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361
Josh Gao184f4802018-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 Projectdd7bc332009-03-03 19:32:55 -0800368 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369 }
370
Dan Albertbac34742015-02-25 17:51:28 -0800371 if (ev & FDE_READ) {
Josh Gao184f4802018-03-19 13:20:29 -0700372 if (!local_socket_flush_outgoing(s)) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700373 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 }
375 }
376
Josh Gao52bd8522016-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 Cui7a3f8d62015-09-02 17:44:28 -0700382 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383 return;
384 }
385}
386
Josh Gao74ccdf92019-01-23 15:36:42 -0800387asocket* create_local_socket(unique_fd ufd) {
388 int fd = ufd.release();
Josh Gaoe0361d12018-02-12 17:24:00 -0800389 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 s->fd = fd;
391 s->enqueue = local_socket_enqueue;
392 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700393 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700395 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396
Josh Gao71f775a2018-05-14 11:14:33 -0700397 s->fde = fdevent_create(fd, local_socket_event_func, s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700398 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 return s;
400}
401
Josh Gaod19b77a2018-12-13 14:21:00 -0800402asocket* create_local_service_socket(std::string_view name, atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403#if !ADB_HOST
Josh Gao6eb78822018-11-16 15:40:16 -0800404 if (asocket* s = daemon_service_to_socket(name); s) {
405 return s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406 }
407#endif
Josh Gao74ccdf92019-01-23 15:36:42 -0800408 unique_fd fd = service_to_fd(name, transport);
Josh Gao52bd8522016-05-17 16:55:06 -0700409 if (fd < 0) {
Elliott Hughesffc73a32016-06-15 14:46:56 -0700410 return nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700411 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412
Greg Kaiserfdb98002019-01-28 06:17:44 -0800413 int fd_value = fd.get();
Josh Gao74ccdf92019-01-23 15:36:42 -0800414 asocket* s = create_local_socket(std::move(fd));
Greg Kaiserfdb98002019-01-28 06:17:44 -0800415 LOG(VERBOSE) << "LS(" << s->id << "): bound to '" << name << "' via " << fd_value;
Benoit Gobyf366b362012-03-16 14:50:07 -0700416
JP Abgrallf91259a2012-03-30 13:19:11 -0700417#if !ADB_HOST
Josh Gaod19b77a2018-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 Cui7a3f8d62015-09-02 17:44:28 -0700421 D("LS(%d): enabling exit_on_close", s->id);
Benoit Gobyf366b362012-03-16 14:50:07 -0700422 s->exit_on_close = 1;
423 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700424#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700425
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 return s;
427}
428
429#if ADB_HOST
Josh Gaob122b172017-08-16 16:57:01 -0700430static asocket* create_host_service_socket(const char* name, const char* serial,
431 TransportId transport_id) {
Josh Gao52bd8522016-05-17 16:55:06 -0700432 asocket* s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433
Josh Gaob122b172017-08-16 16:57:01 -0700434 s = host_service_to_socket(name, serial, transport_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435
Yi Kongaed415c2018-07-13 18:15:16 -0700436 if (s != nullptr) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700437 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 return s;
439 }
440
441 return s;
442}
443#endif /* ADB_HOST */
444
Josh Gao1ce99572018-03-07 16:52:28 -0800445static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gao52bd8522016-05-17 16:55:06 -0700446 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
Josh Gao27cb7dc2018-02-01 13:17:50 -0800447 apacket* p = get_apacket();
448
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 p->msg.command = A_WRTE;
450 p->msg.arg0 = s->peer->id;
451 p->msg.arg1 = s->id;
Josh Gao27cb7dc2018-02-01 13:17:50 -0800452
Josh Gaof571fcb2018-02-05 18:49:10 -0800453 if (data.size() > MAX_PAYLOAD) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800454 put_apacket(p);
455 return -1;
456 }
457
Josh Gaof571fcb2018-02-05 18:49:10 -0800458 p->payload = std::move(data);
459 p->msg.data_length = p->payload.size();
460
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 send_packet(p, s->transport);
462 return 1;
463}
464
Josh Gao52bd8522016-05-17 16:55:06 -0700465static void remote_socket_ready(asocket* s) {
466 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
467 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 p->msg.command = A_OKAY;
469 p->msg.arg0 = s->peer->id;
470 p->msg.arg1 = s->id;
471 send_packet(p, s->transport);
472}
473
Josh Gao52bd8522016-05-17 16:55:06 -0700474static void remote_socket_shutdown(asocket* s) {
475 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
476 s->peer ? s->peer->fd : -1);
477 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 p->msg.command = A_CLSE;
Josh Gao52bd8522016-05-17 16:55:06 -0700479 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100481 }
482 p->msg.arg1 = s->id;
483 send_packet(p, s->transport);
484}
485
Josh Gao52bd8522016-05-17 16:55:06 -0700486static void remote_socket_close(asocket* s) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100487 if (s->peer) {
Yi Kongaed415c2018-07-13 18:15:16 -0700488 s->peer->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700489 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 s->peer->close(s->peer);
491 }
Josh Gao52bd8522016-05-17 16:55:06 -0700492 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
493 s->peer ? s->peer->fd : -1);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700494 D("RS(%d): closed", s->id);
Josh Gaoe0361d12018-02-12 17:24:00 -0800495 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496}
497
Yabin Cuifd28f322015-08-27 18:50:04 -0700498// Create a remote socket to exchange packets with a remote service through transport
499// |t|. Where |id| is the socket id of the corresponding service on the other
500// side of the transport (it is allocated by the remote side and _cannot_ be 0).
501// Returns a new non-NULL asocket handle.
Josh Gao52bd8522016-05-17 16:55:06 -0700502asocket* create_remote_socket(unsigned id, atransport* t) {
503 if (id == 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700504 LOG(FATAL) << "invalid remote socket id (0)";
Josh Gao52bd8522016-05-17 16:55:06 -0700505 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800506 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507 s->id = id;
508 s->enqueue = remote_socket_enqueue;
509 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100510 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 s->close = remote_socket_close;
512 s->transport = t;
513
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700514 D("RS(%d): created", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 return s;
516}
517
Josh Gaod0fa13e2018-12-20 17:00:13 -0800518void connect_to_remote(asocket* s, std::string_view destination) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700519 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gao52bd8522016-05-17 16:55:06 -0700520 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521
Josh Gaod0fa13e2018-12-20 17:00:13 -0800522 LOG(VERBOSE) << "LS(" << s->id << ": connect(" << destination << ")";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 p->msg.command = A_OPEN;
524 p->msg.arg0 = s->id;
Josh Gaof571fcb2018-02-05 18:49:10 -0800525
Josh Gaod0fa13e2018-12-20 17:00:13 -0800526 // adbd used to expect a null-terminated string.
527 // Keep doing so to maintain backward compatibility.
528 p->payload.resize(destination.size() + 1);
529 memcpy(p->payload.data(), destination.data(), destination.size());
530 p->payload[destination.size()] = '\0';
Josh Gaof571fcb2018-02-05 18:49:10 -0800531 p->msg.data_length = p->payload.size();
532
Elliott Hughes4679a392018-10-19 13:59:44 -0700533 CHECK_LE(p->msg.data_length, s->get_max_payload());
Josh Gaof571fcb2018-02-05 18:49:10 -0800534
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 send_packet(p, s->transport);
536}
537
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800538/* this is used by magic sockets to rig local sockets to
539 send the go-ahead message when they connect */
Josh Gao52bd8522016-05-17 16:55:06 -0700540static void local_socket_ready_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700542 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700544 SendOkay(s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545 s->ready(s);
546}
547
548/* this is used by magic sockets to rig local sockets to
549 send the failure message if they are closed before
550 connected (to avoid closing them without a status message) */
Josh Gao52bd8522016-05-17 16:55:06 -0700551static void local_socket_close_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800552 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700553 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700555 SendFail(s->fd, "closed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 s->close(s);
557}
558
Josh Gao27cb7dc2018-02-01 13:17:50 -0800559static unsigned unhex(const char* s, int len) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800560 unsigned n = 0, c;
561
Josh Gao52bd8522016-05-17 16:55:06 -0700562 while (len-- > 0) {
563 switch ((c = *s++)) {
564 case '0':
565 case '1':
566 case '2':
567 case '3':
568 case '4':
569 case '5':
570 case '6':
571 case '7':
572 case '8':
573 case '9':
574 c -= '0';
575 break;
576 case 'a':
577 case 'b':
578 case 'c':
579 case 'd':
580 case 'e':
581 case 'f':
582 c = c - 'a' + 10;
583 break;
584 case 'A':
585 case 'B':
586 case 'C':
587 case 'D':
588 case 'E':
589 case 'F':
590 c = c - 'A' + 10;
591 break;
592 default:
593 return 0xffffffff;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800594 }
595
596 n = (n << 4) | c;
597 }
598
599 return n;
600}
601
Elliott Hughese67f1f82015-04-30 17:32:03 -0700602#if ADB_HOST
603
David Pursell3f902aa2016-03-01 08:58:26 -0800604namespace internal {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700605
Josh Gaobd767202018-12-19 13:37:41 -0800606// Parses a host service string of the following format:
David Pursell3f902aa2016-03-01 08:58:26 -0800607// * [tcp:|udp:]<serial>[:<port>]:<command>
608// * <prefix>:<serial>:<command>
609// Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}.
Josh Gaobd767202018-12-19 13:37:41 -0800610bool parse_host_service(std::string_view* out_serial, std::string_view* out_command,
611 std::string_view full_service) {
612 if (full_service.empty()) {
613 return false;
614 }
Terence Haddock28e13902011-03-16 09:43:56 +0100615
Josh Gaobd767202018-12-19 13:37:41 -0800616 std::string_view serial;
617 std::string_view command = full_service;
618 // Remove |count| bytes from the beginning of command and add them to |serial|.
619 auto consume = [&full_service, &serial, &command](size_t count) {
620 CHECK_LE(count, command.size());
621 if (!serial.empty()) {
622 CHECK_EQ(serial.data() + serial.size(), command.data());
623 }
624
625 serial = full_service.substr(0, serial.size() + count);
626 command.remove_prefix(count);
627 };
628
629 // Remove the trailing : from serial, and assign the values to the output parameters.
630 auto finish = [out_serial, out_command, &serial, &command] {
631 if (serial.empty() || command.empty()) {
632 return false;
633 }
634
635 CHECK_EQ(':', serial.back());
636 serial.remove_suffix(1);
637
638 *out_serial = serial;
639 *out_command = command;
640 return true;
641 };
642
643 static constexpr std::string_view prefixes[] = {"usb:", "product:", "model:", "device:"};
644 for (std::string_view prefix : prefixes) {
645 if (command.starts_with(prefix)) {
646 consume(prefix.size());
647
648 size_t offset = command.find_first_of(':');
649 if (offset == std::string::npos) {
650 return false;
651 }
652 consume(offset + 1);
653 return finish();
David Pursell3f902aa2016-03-01 08:58:26 -0800654 }
Scott Anderson3608d832012-05-31 12:04:23 -0700655 }
656
David Pursell3f902aa2016-03-01 08:58:26 -0800657 // For fastboot compatibility, ignore protocol prefixes.
Josh Gaobd767202018-12-19 13:37:41 -0800658 if (command.starts_with("tcp:") || command.starts_with("udp:")) {
659 consume(4);
660 if (command.empty()) {
661 return false;
David Pursell73d55aa2016-09-21 12:08:37 -0700662 }
663 }
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800664 if (command.starts_with("vsock:")) {
665 // vsock serials are vsock:cid:port, which have an extra colon compared to tcp.
666 size_t next_colon = command.find(':');
667 if (next_colon == std::string::npos) {
668 return false;
669 }
670 consume(next_colon + 1);
671 }
David Pursell73d55aa2016-09-21 12:08:37 -0700672
Josh Gaobd767202018-12-19 13:37:41 -0800673 bool found_address = false;
674 if (command[0] == '[') {
675 // Read an IPv6 address. `adb connect` creates the serial number from the canonical
676 // network address so it will always have the [] delimiters.
677 size_t ipv6_end = command.find_first_of(']');
678 if (ipv6_end != std::string::npos) {
679 consume(ipv6_end + 1);
680 if (command.empty()) {
681 // Nothing after the IPv6 address.
682 return false;
683 } else if (command[0] != ':') {
684 // Garbage after the IPv6 address.
685 return false;
686 }
687 consume(1);
688 found_address = true;
689 }
Terence Haddock28e13902011-03-16 09:43:56 +0100690 }
David Pursell3f902aa2016-03-01 08:58:26 -0800691
Josh Gaobd767202018-12-19 13:37:41 -0800692 if (!found_address) {
693 // Scan ahead to the next colon.
694 size_t offset = command.find_first_of(':');
695 if (offset == std::string::npos) {
696 return false;
Terence Haddock28e13902011-03-16 09:43:56 +0100697 }
Josh Gaobd767202018-12-19 13:37:41 -0800698 consume(offset + 1);
699 }
700
701 // We're either at the beginning of a port, or the command itself.
702 // Look for a port in between colons.
703 size_t next_colon = command.find_first_of(':');
704 if (next_colon == std::string::npos) {
705 // No colon, we must be at the command.
706 return finish();
707 }
708
709 bool port_valid = true;
710 if (command.size() <= next_colon) {
711 return false;
712 }
713
714 std::string_view port = command.substr(0, next_colon);
715 for (auto digit : port) {
716 if (!isdigit(digit)) {
717 // Port isn't a number.
718 port_valid = false;
719 break;
Terence Haddock28e13902011-03-16 09:43:56 +0100720 }
721 }
Josh Gaobd767202018-12-19 13:37:41 -0800722
723 if (port_valid) {
724 consume(next_colon + 1);
725 }
726 return finish();
Terence Haddock28e13902011-03-16 09:43:56 +0100727}
728
David Pursell3f902aa2016-03-01 08:58:26 -0800729} // namespace internal
730
Josh Gao52bd8522016-05-17 16:55:06 -0700731#endif // ADB_HOST
Elliott Hughese67f1f82015-04-30 17:32:03 -0700732
Josh Gao1ce99572018-03-07 16:52:28 -0800733static int smart_socket_enqueue(asocket* s, apacket::payload_type data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800734#if ADB_HOST
Josh Gaobd767202018-12-19 13:37:41 -0800735 std::string_view service;
736 std::string_view serial;
Josh Gaob122b172017-08-16 16:57:01 -0700737 TransportId transport_id = 0;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700738 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800739#endif
740
Josh Gao27cb7dc2018-02-01 13:17:50 -0800741 D("SS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800742
Josh Gao27cb7dc2018-02-01 13:17:50 -0800743 if (s->smart_socket_data.empty()) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700744 // TODO: Make this an IOVector?
Josh Gao1ce99572018-03-07 16:52:28 -0800745 s->smart_socket_data.assign(data.begin(), data.end());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746 } else {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800747 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748 }
749
Josh Gao7e6683c2016-01-15 14:35:54 -0800750 /* don't bother if we can't decode the length */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800751 if (s->smart_socket_data.size() < 4) {
Josh Gao52bd8522016-05-17 16:55:06 -0700752 return 0;
753 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800754
Josh Gao27cb7dc2018-02-01 13:17:50 -0800755 uint32_t len = unhex(s->smart_socket_data.data(), 4);
756 if (len == 0 || len > MAX_PAYLOAD) {
757 D("SS(%d): bad size (%u)", s->id, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800758 goto fail;
759 }
760
Josh Gao27cb7dc2018-02-01 13:17:50 -0800761 D("SS(%d): len is %u", s->id, len);
Josh Gao7e6683c2016-01-15 14:35:54 -0800762 /* can't do anything until we have the full header */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800763 if ((len + 4) > s->smart_socket_data.size()) {
764 D("SS(%d): waiting for %zu more bytes", s->id, len + 4 - s->smart_socket_data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765 return 0;
766 }
767
Josh Gao27cb7dc2018-02-01 13:17:50 -0800768 s->smart_socket_data[len + 4] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800769
Josh Gao27cb7dc2018-02-01 13:17:50 -0800770 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800771
772#if ADB_HOST
Josh Gaobd767202018-12-19 13:37:41 -0800773 service = std::string_view(s->smart_socket_data).substr(4);
774 if (service.starts_with("host-serial:")) {
775 service.remove_prefix(strlen("host-serial:"));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800776
Terence Haddock28e13902011-03-16 09:43:56 +0100777 // serial number should follow "host:" and could be a host:port string.
Josh Gaobd767202018-12-19 13:37:41 -0800778 if (!internal::parse_host_service(&serial, &service, service)) {
779 LOG(ERROR) << "SS(" << s->id << "): failed to parse host service: " << service;
780 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800781 }
Josh Gaobd767202018-12-19 13:37:41 -0800782 } else if (service.starts_with("host-transport-id:")) {
783 service.remove_prefix(strlen("host-transport-id:"));
784 if (!ParseUint(&transport_id, service, &service)) {
785 LOG(ERROR) << "SS(" << s->id << "): failed to parse host transport id: " << service;
Josh Gaob122b172017-08-16 16:57:01 -0700786 return -1;
787 }
Josh Gaobd767202018-12-19 13:37:41 -0800788 if (!service.starts_with(":")) {
789 LOG(ERROR) << "SS(" << s->id << "): host-transport-id without command";
790 return -1;
791 }
792 service.remove_prefix(1);
793 } else if (service.starts_with("host-usb:")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700794 type = kTransportUsb;
Josh Gaobd767202018-12-19 13:37:41 -0800795 service.remove_prefix(strlen("host-usb:"));
796 } else if (service.starts_with("host-local:")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700797 type = kTransportLocal;
Josh Gaobd767202018-12-19 13:37:41 -0800798 service.remove_prefix(strlen("host-local:"));
799 } else if (service.starts_with("host:")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700800 type = kTransportAny;
Josh Gaobd767202018-12-19 13:37:41 -0800801 service.remove_prefix(strlen("host:"));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802 } else {
Josh Gaobd767202018-12-19 13:37:41 -0800803 service = std::string_view{};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800804 }
805
Josh Gaobd767202018-12-19 13:37:41 -0800806 if (!service.empty()) {
Josh Gao52bd8522016-05-17 16:55:06 -0700807 asocket* s2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800808
Josh Gao40390512018-08-07 14:14:21 -0700809 // Some requests are handled immediately -- in that case the handle_host_request() routine
810 // has sent the OKAY or FAIL message and all we have to do is clean up.
Josh Gaobd767202018-12-19 13:37:41 -0800811 // TODO: Convert to string_view.
812 if (handle_host_request(std::string(service).c_str(), type,
813 serial.empty() ? nullptr : std::string(serial).c_str(),
814 transport_id, s->peer->fd, s)) {
815 LOG(VERBOSE) << "SS(" << s->id << "): handled host service '" << service << "'";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800816 goto fail;
817 }
Josh Gaobd767202018-12-19 13:37:41 -0800818 if (service.starts_with("transport")) {
Josh Gao52bd8522016-05-17 16:55:06 -0700819 D("SS(%d): okay transport", s->id);
Josh Gao27cb7dc2018-02-01 13:17:50 -0800820 s->smart_socket_data.clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800821 return 0;
822 }
823
Josh Gao52bd8522016-05-17 16:55:06 -0700824 /* try to find a local service with this name.
825 ** if no such service exists, we'll fail out
826 ** and tear down here.
827 */
Josh Gaobd767202018-12-19 13:37:41 -0800828 // TODO: Convert to string_view.
829 s2 = create_host_service_socket(std::string(service).c_str(), std::string(serial).c_str(),
830 transport_id);
Yi Kongaed415c2018-07-13 18:15:16 -0700831 if (s2 == nullptr) {
Josh Gaobd767202018-12-19 13:37:41 -0800832 LOG(VERBOSE) << "SS(" << s->id << "): couldn't create host service '" << service << "'";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700833 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800834 goto fail;
835 }
836
Josh Gao52bd8522016-05-17 16:55:06 -0700837 /* we've connected to a local host service,
838 ** so we make our peer back into a regular
839 ** local socket and bind it to the new local
840 ** service socket, acknowledge the successful
841 ** connection, and close this smart socket now
842 ** that its work is done.
843 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700844 SendOkay(s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800845
846 s->peer->ready = local_socket_ready;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700847 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800848 s->peer->close = local_socket_close;
849 s->peer->peer = s2;
850 s2->peer = s->peer;
Yi Kongaed415c2018-07-13 18:15:16 -0700851 s->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700852 D("SS(%d): okay", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853 s->close(s);
854
Josh Gao52bd8522016-05-17 16:55:06 -0700855 /* initial state is "ready" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800856 s2->ready(s2);
857 return 0;
858 }
859#else /* !ADB_HOST */
Elliott Hughes8d28e192015-10-07 14:55:10 -0700860 if (s->transport == nullptr) {
Elliott Hughes7be29c82015-04-16 22:54:44 -0700861 std::string error_msg = "unknown failure";
Josh Gaob122b172017-08-16 16:57:01 -0700862 s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700863 if (s->transport == nullptr) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700864 SendFail(s->peer->fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800865 goto fail;
866 }
867 }
868#endif
869
Josh Gao22d2b3e2016-10-27 14:01:08 -0700870 if (!s->transport) {
871 SendFail(s->peer->fd, "device offline (no transport)");
872 goto fail;
Josh Gao704494b2018-05-04 16:04:49 -0700873 } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) {
Josh Gao52bd8522016-05-17 16:55:06 -0700874 /* if there's no remote we fail the connection
875 ** right here and terminate it
876 */
Josh Gao22d2b3e2016-10-27 14:01:08 -0700877 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800878 goto fail;
879 }
880
Josh Gao52bd8522016-05-17 16:55:06 -0700881 /* instrument our peer to pass the success or fail
882 ** message back once it connects or closes, then
883 ** detach from it, request the connection, and
884 ** tear down
885 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800886 s->peer->ready = local_socket_ready_notify;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700887 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800888 s->peer->close = local_socket_close_notify;
Yi Kongaed415c2018-07-13 18:15:16 -0700889 s->peer->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700890 /* give him our transport and upref it */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800891 s->peer->transport = s->transport;
892
Josh Gaod0fa13e2018-12-20 17:00:13 -0800893 connect_to_remote(s->peer, std::string_view(s->smart_socket_data).substr(4));
Yi Kongaed415c2018-07-13 18:15:16 -0700894 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800895 s->close(s);
896 return 1;
897
898fail:
Josh Gao52bd8522016-05-17 16:55:06 -0700899 /* we're going to close our peer as a side-effect, so
900 ** return -1 to signal that state to the local socket
901 ** who is enqueueing against us
902 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800903 s->close(s);
904 return -1;
905}
906
Josh Gao52bd8522016-05-17 16:55:06 -0700907static void smart_socket_ready(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700908 D("SS(%d): ready", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800909}
910
Josh Gao52bd8522016-05-17 16:55:06 -0700911static void smart_socket_close(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700912 D("SS(%d): closed", s->id);
Josh Gao52bd8522016-05-17 16:55:06 -0700913 if (s->peer) {
Yi Kongaed415c2018-07-13 18:15:16 -0700914 s->peer->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800915 s->peer->close(s->peer);
Yi Kongaed415c2018-07-13 18:15:16 -0700916 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800917 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800918 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800919}
920
Josh Gao52bd8522016-05-17 16:55:06 -0700921static asocket* create_smart_socket(void) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700922 D("Creating smart socket");
Josh Gaoe0361d12018-02-12 17:24:00 -0800923 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800924 s->enqueue = smart_socket_enqueue;
925 s->ready = smart_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700926 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800927 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800928
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700929 D("SS(%d)", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800930 return s;
931}
932
Josh Gao52bd8522016-05-17 16:55:06 -0700933void connect_to_smartsocket(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700934 D("Connecting to smart socket");
Josh Gao52bd8522016-05-17 16:55:06 -0700935 asocket* ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936 s->peer = ss;
937 ss->peer = s;
938 s->ready(s);
939}
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100940
941size_t asocket::get_max_payload() const {
942 size_t max_payload = MAX_PAYLOAD;
943 if (transport) {
944 max_payload = std::min(max_payload, transport->get_max_payload());
945 }
946 if (peer && peer->transport) {
947 max_payload = std::min(max_payload, peer->transport->get_max_payload());
948 }
949 return max_payload;
950}