blob: f7c39f0a1f961cad53b8b6cb38b1ed76125ff515 [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 Gao9b587de2016-05-17 17:46:27 -070029#include <mutex>
David Pursell3f902aa2016-03-01 08:58:26 -080030#include <string>
31#include <vector>
Spencer Low363af562015-11-07 18:51:54 -080032
Dan Albert76649012015-02-24 15:51:19 -080033#if !ADB_HOST
Elliott Hughesffdec182016-09-23 15:40:03 -070034#include <android-base/properties.h>
Steven Morelandd73be1b2017-04-13 23:48:57 -070035#include <log/log_properties.h>
Dan Albert76649012015-02-24 15:51:19 -080036#endif
Dan Albert33134262015-03-19 15:21:08 -070037
38#include "adb.h"
39#include "adb_io.h"
Josh Gaobd767202018-12-19 13:37:41 -080040#include "adb_utils.h"
Dan Albert76649012015-02-24 15:51:19 -080041#include "transport.h"
Josh Gao1ce99572018-03-07 16:52:28 -080042#include "types.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
Josh Gao9b587de2016-05-17 17:46:27 -070044static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045static unsigned local_socket_next_id = 1;
46
Josh Gao5e507642018-01-31 13:15:51 -080047static auto& local_socket_list = *new std::vector<asocket*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
49/* the the list of currently closing local sockets.
50** these have no peer anymore, but still packets to
51** write to their fd.
52*/
Josh Gao5e507642018-01-31 13:15:51 -080053static auto& local_socket_closing_list = *new std::vector<asocket*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
David 'Digit' Turner818d6412013-12-13 14:09:44 +010055// Parse the global list of sockets to find one with id |local_id|.
56// If |peer_id| is not 0, also check that it is connected to a peer
57// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
Josh Gao52bd8522016-05-17 16:55:06 -070058asocket* find_local_socket(unsigned local_id, unsigned peer_id) {
Josh Gao5e507642018-01-31 13:15:51 -080059 asocket* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Josh Gao9b587de2016-05-17 17:46:27 -070061 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao5e507642018-01-31 13:15:51 -080062 for (asocket* s : local_socket_list) {
Josh Gao52bd8522016-05-17 16:55:06 -070063 if (s->id != local_id) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +010064 continue;
Josh Gao52bd8522016-05-17 16:55:06 -070065 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010066 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa81828292010-06-12 11:40:20 -030067 result = s;
André Goddard Rosa81828292010-06-12 11:40:20 -030068 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010069 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
72 return result;
73}
74
Josh Gao52bd8522016-05-17 16:55:06 -070075void install_local_socket(asocket* s) {
Josh Gao9b587de2016-05-17 17:46:27 -070076 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077
78 s->id = local_socket_next_id++;
David 'Digit' Turner818d6412013-12-13 14:09:44 +010079
80 // Socket ids should never be 0.
Josh Gao52bd8522016-05-17 16:55:06 -070081 if (local_socket_next_id == 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -070082 LOG(FATAL) << "local socket id overflow";
Josh Gao52bd8522016-05-17 16:55:06 -070083 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010084
Josh Gao5e507642018-01-31 13:15:51 -080085 local_socket_list.push_back(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086}
87
Josh Gao52bd8522016-05-17 16:55:06 -070088void remove_socket(asocket* s) {
Josh Gao62c92f02017-09-13 11:17:33 -070089 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao5e507642018-01-31 13:15:51 -080090 for (auto list : { &local_socket_list, &local_socket_closing_list }) {
91 list->erase(std::remove_if(list->begin(), list->end(), [s](asocket* x) { return x == s; }),
92 list->end());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093 }
94}
95
Josh Gao52bd8522016-05-17 16:55:06 -070096void close_all_sockets(atransport* t) {
Josh Gao52bd8522016-05-17 16:55:06 -070097 /* this is a little gross, but since s->close() *will* modify
98 ** the list out from under you, your options are limited.
99 */
Josh Gao9b587de2016-05-17 17:46:27 -0700100 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101restart:
Josh Gao5e507642018-01-31 13:15:51 -0800102 for (asocket* s : local_socket_list) {
Josh Gao52bd8522016-05-17 16:55:06 -0700103 if (s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gao53eb31d2016-05-18 10:39:48 -0700104 s->close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105 goto restart;
106 }
107 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108}
109
Josh Gao184f4802018-03-19 13:20:29 -0700110enum class SocketFlushResult {
111 Destroyed,
112 TryAgain,
113 Completed,
114};
115
116static SocketFlushResult local_socket_flush_incoming(asocket* s) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700117 if (!s->packet_queue.empty()) {
118 std::vector<adb_iovec> iov = s->packet_queue.iovecs();
119 ssize_t rc = adb_writev(s->fd, iov.data(), iov.size());
120 if (rc > 0 && static_cast<size_t>(rc) == s->packet_queue.size()) {
121 s->packet_queue.clear();
Josh Gao184f4802018-03-19 13:20:29 -0700122 } else if (rc > 0) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700123 // TODO: Implement a faster drop_front?
124 s->packet_queue.take_front(rc);
Josh Gao71f775a2018-05-14 11:14:33 -0700125 fdevent_add(s->fde, FDE_WRITE);
Josh Gao184f4802018-03-19 13:20:29 -0700126 return SocketFlushResult::TryAgain;
127 } else if (rc == -1 && errno == EAGAIN) {
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;
Josh Gao954e1282018-03-30 13:56:24 -0700130 } else {
131 // We failed to write, but it's possible that we can still read from the socket.
132 // Give that a try before giving up.
133 s->has_write_error = true;
Josh Gao184f4802018-03-19 13:20:29 -0700134 }
Josh Gao184f4802018-03-19 13:20:29 -0700135 }
136
137 // If we sent the last packet of a closing socket, we can now destroy it.
138 if (s->closing) {
139 s->close(s);
140 return SocketFlushResult::Destroyed;
141 }
142
Josh Gao71f775a2018-05-14 11:14:33 -0700143 fdevent_del(s->fde, FDE_WRITE);
Josh Gao184f4802018-03-19 13:20:29 -0700144 return SocketFlushResult::Completed;
145}
146
147// Returns false if the socket has been closed and destroyed as a side-effect of this function.
148static bool local_socket_flush_outgoing(asocket* s) {
149 const size_t max_payload = s->get_max_payload();
Josh Gao1ce99572018-03-07 16:52:28 -0800150 apacket::payload_type data;
Josh Gao184f4802018-03-19 13:20:29 -0700151 data.resize(max_payload);
152 char* x = &data[0];
153 size_t avail = max_payload;
154 int r = 0;
155 int is_eof = 0;
156
157 while (avail > 0) {
158 r = adb_read(s->fd, x, avail);
159 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r,
160 r < 0 ? errno : 0, avail);
161 if (r == -1) {
162 if (errno == EAGAIN) {
163 break;
164 }
165 } else if (r > 0) {
166 avail -= r;
167 x += r;
168 continue;
169 }
170
171 /* r = 0 or unhandled error */
172 is_eof = 1;
173 break;
174 }
175 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 -0700176 s->fde->force_eof);
Josh Gao184f4802018-03-19 13:20:29 -0700177
178 if (avail != max_payload && s->peer) {
179 data.resize(max_payload - avail);
180
181 // s->peer->enqueue() may call s->close() and free s,
182 // so save variables for debug printing below.
183 unsigned saved_id = s->id;
184 int saved_fd = s->fd;
185 r = s->peer->enqueue(s->peer, std::move(data));
186 D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
187
188 if (r < 0) {
189 // Error return means they closed us as a side-effect and we must
190 // return immediately.
191 //
192 // Note that if we still have buffered packets, the socket will be
193 // placed on the closing socket list. This handler function will be
194 // called again to process FDE_WRITE events.
195 return false;
196 }
197
198 if (r > 0) {
199 /* if the remote cannot accept further events,
200 ** we disable notification of READs. They'll
201 ** be enabled again when we get a call to ready()
202 */
Josh Gao71f775a2018-05-14 11:14:33 -0700203 fdevent_del(s->fde, FDE_READ);
Josh Gao184f4802018-03-19 13:20:29 -0700204 }
205 }
206
207 // Don't allow a forced eof if data is still there.
Josh Gao71f775a2018-05-14 11:14:33 -0700208 if ((s->fde->force_eof && !r) || is_eof) {
209 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 -0700210 s->close(s);
211 return false;
212 }
213
214 return true;
215}
216
Josh Gao1ce99572018-03-07 16:52:28 -0800217static int local_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800218 D("LS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219
Josh Gao7c738cd2018-04-03 14:37:11 -0700220 s->packet_queue.append(std::move(data));
Josh Gao184f4802018-03-19 13:20:29 -0700221 switch (local_socket_flush_incoming(s)) {
222 case SocketFlushResult::Destroyed:
223 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224
Josh Gao184f4802018-03-19 13:20:29 -0700225 case SocketFlushResult::TryAgain:
226 return 1;
227
228 case SocketFlushResult::Completed:
229 return 0;
230 }
231
232 return !s->packet_queue.empty();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233}
234
Josh Gao52bd8522016-05-17 16:55:06 -0700235static void local_socket_ready(asocket* s) {
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100236 /* far side is ready for data, pay attention to
237 readable events */
Josh Gao71f775a2018-05-14 11:14:33 -0700238 fdevent_add(s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239}
240
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241// be sure to hold the socket list lock when calling this
Josh Gao52bd8522016-05-17 16:55:06 -0700242static void local_socket_destroy(asocket* s) {
Benoit Gobyf366b362012-03-16 14:50:07 -0700243 int exit_on_close = s->exit_on_close;
244
Josh Gao71f775a2018-05-14 11:14:33 -0700245 D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246
Josh Gaofaf13282018-10-12 05:08:45 +0000247 /* IMPORTANT: the remove closes the fd
248 ** that belongs to this socket
249 */
250 fdevent_destroy(s->fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 remove_socket(s);
Josh Gaoe0361d12018-02-12 17:24:00 -0800253 delete s;
Benoit Gobyf366b362012-03-16 14:50:07 -0700254
255 if (exit_on_close) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700256 D("local_socket_destroy: exiting");
Benoit Gobyf366b362012-03-16 14:50:07 -0700257 exit(1);
258 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259}
260
Josh Gao9b587de2016-05-17 17:46:27 -0700261static void local_socket_close(asocket* s) {
262 D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd);
263 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao52bd8522016-05-17 16:55:06 -0700264 if (s->peer) {
265 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 +0100266 /* Note: it's important to call shutdown before disconnecting from
267 * the peer, this ensures that remote sockets can still get the id
268 * of the local socket they're connected to, to send a CLOSE()
269 * protocol event. */
Josh Gao52bd8522016-05-17 16:55:06 -0700270 if (s->peer->shutdown) {
271 s->peer->shutdown(s->peer);
272 }
Josh Gao9b587de2016-05-17 17:46:27 -0700273 s->peer->peer = nullptr;
274 s->peer->close(s->peer);
275 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276 }
277
Josh Gao52bd8522016-05-17 16:55:06 -0700278 /* If we are already closing, or if there are no
279 ** pending packets, destroy immediately
280 */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800281 if (s->closing || s->has_write_error || s->packet_queue.empty()) {
Josh Gao52bd8522016-05-17 16:55:06 -0700282 int id = s->id;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283 local_socket_destroy(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700284 D("LS(%d): closed", id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 return;
286 }
287
Josh Gao52bd8522016-05-17 16:55:06 -0700288 /* otherwise, put on the closing list
289 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700290 D("LS(%d): closing", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 s->closing = 1;
Josh Gao71f775a2018-05-14 11:14:33 -0700292 fdevent_del(s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 remove_socket(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700294 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
Josh Gao5e507642018-01-31 13:15:51 -0800295 local_socket_closing_list.push_back(s);
Josh Gao71f775a2018-05-14 11:14:33 -0700296 CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297}
298
Josh Gao52bd8522016-05-17 16:55:06 -0700299static void local_socket_event_func(int fd, unsigned ev, void* _s) {
Dan Albertbac34742015-02-25 17:51:28 -0800300 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700301 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall408fa572011-03-16 15:57:42 -0700302
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 /* put the FDE_WRITE processing before the FDE_READ
304 ** in order to simplify the code.
305 */
Dan Albertbac34742015-02-25 17:51:28 -0800306 if (ev & FDE_WRITE) {
Josh Gao184f4802018-03-19 13:20:29 -0700307 switch (local_socket_flush_incoming(s)) {
308 case SocketFlushResult::Destroyed:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310
Josh Gao184f4802018-03-19 13:20:29 -0700311 case SocketFlushResult::TryAgain:
312 break;
313
314 case SocketFlushResult::Completed:
315 s->peer->ready(s->peer);
316 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318 }
319
Dan Albertbac34742015-02-25 17:51:28 -0800320 if (ev & FDE_READ) {
Josh Gao184f4802018-03-19 13:20:29 -0700321 if (!local_socket_flush_outgoing(s)) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700322 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 }
324 }
325
Josh Gao52bd8522016-05-17 16:55:06 -0700326 if (ev & FDE_ERROR) {
327 /* this should be caught be the next read or write
328 ** catching it here means we may skip the last few
329 ** bytes of readable data.
330 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700331 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 return;
333 }
334}
335
Josh Gao74ccdf92019-01-23 15:36:42 -0800336asocket* create_local_socket(unique_fd ufd) {
337 int fd = ufd.release();
Josh Gaoe0361d12018-02-12 17:24:00 -0800338 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339 s->fd = fd;
340 s->enqueue = local_socket_enqueue;
341 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700342 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700344 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345
Josh Gao71f775a2018-05-14 11:14:33 -0700346 s->fde = fdevent_create(fd, local_socket_event_func, s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700347 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 return s;
349}
350
Josh Gaod19b77a2018-12-13 14:21:00 -0800351asocket* create_local_service_socket(std::string_view name, atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352#if !ADB_HOST
Josh Gao6eb78822018-11-16 15:40:16 -0800353 if (asocket* s = daemon_service_to_socket(name); s) {
354 return s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 }
356#endif
Josh Gao74ccdf92019-01-23 15:36:42 -0800357 unique_fd fd = service_to_fd(name, transport);
Josh Gao52bd8522016-05-17 16:55:06 -0700358 if (fd < 0) {
Elliott Hughesffc73a32016-06-15 14:46:56 -0700359 return nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700360 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361
Greg Kaiserfdb98002019-01-28 06:17:44 -0800362 int fd_value = fd.get();
Josh Gao74ccdf92019-01-23 15:36:42 -0800363 asocket* s = create_local_socket(std::move(fd));
Greg Kaiserfdb98002019-01-28 06:17:44 -0800364 LOG(VERBOSE) << "LS(" << s->id << "): bound to '" << name << "' via " << fd_value;
Benoit Gobyf366b362012-03-16 14:50:07 -0700365
JP Abgrallf91259a2012-03-30 13:19:11 -0700366#if !ADB_HOST
Josh Gaod19b77a2018-12-13 14:21:00 -0800367 if ((name.starts_with("root:") && getuid() != 0 && __android_log_is_debuggable()) ||
368 (name.starts_with("unroot:") && getuid() == 0) || name.starts_with("usb:") ||
369 name.starts_with("tcpip:")) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700370 D("LS(%d): enabling exit_on_close", s->id);
Benoit Gobyf366b362012-03-16 14:50:07 -0700371 s->exit_on_close = 1;
372 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700373#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700374
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 return s;
376}
377
378#if ADB_HOST
Josh Gaob122b172017-08-16 16:57:01 -0700379static asocket* create_host_service_socket(const char* name, const char* serial,
380 TransportId transport_id) {
Josh Gao52bd8522016-05-17 16:55:06 -0700381 asocket* s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382
Josh Gaob122b172017-08-16 16:57:01 -0700383 s = host_service_to_socket(name, serial, transport_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384
Yi Kongaed415c2018-07-13 18:15:16 -0700385 if (s != nullptr) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700386 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 return s;
388 }
389
390 return s;
391}
392#endif /* ADB_HOST */
393
Josh Gao1ce99572018-03-07 16:52:28 -0800394static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gao52bd8522016-05-17 16:55:06 -0700395 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 -0800396 apacket* p = get_apacket();
397
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 p->msg.command = A_WRTE;
399 p->msg.arg0 = s->peer->id;
400 p->msg.arg1 = s->id;
Josh Gao27cb7dc2018-02-01 13:17:50 -0800401
Josh Gaof571fcb2018-02-05 18:49:10 -0800402 if (data.size() > MAX_PAYLOAD) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800403 put_apacket(p);
404 return -1;
405 }
406
Josh Gaof571fcb2018-02-05 18:49:10 -0800407 p->payload = std::move(data);
408 p->msg.data_length = p->payload.size();
409
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 send_packet(p, s->transport);
411 return 1;
412}
413
Josh Gao52bd8522016-05-17 16:55:06 -0700414static void remote_socket_ready(asocket* s) {
415 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
416 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 p->msg.command = A_OKAY;
418 p->msg.arg0 = s->peer->id;
419 p->msg.arg1 = s->id;
420 send_packet(p, s->transport);
421}
422
Josh Gao52bd8522016-05-17 16:55:06 -0700423static void remote_socket_shutdown(asocket* s) {
424 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
425 s->peer ? s->peer->fd : -1);
426 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427 p->msg.command = A_CLSE;
Josh Gao52bd8522016-05-17 16:55:06 -0700428 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100430 }
431 p->msg.arg1 = s->id;
432 send_packet(p, s->transport);
433}
434
Josh Gao52bd8522016-05-17 16:55:06 -0700435static void remote_socket_close(asocket* s) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100436 if (s->peer) {
Yi Kongaed415c2018-07-13 18:15:16 -0700437 s->peer->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700438 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 -0800439 s->peer->close(s->peer);
440 }
Josh Gao52bd8522016-05-17 16:55:06 -0700441 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
442 s->peer ? s->peer->fd : -1);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700443 D("RS(%d): closed", s->id);
Josh Gaoe0361d12018-02-12 17:24:00 -0800444 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445}
446
Yabin Cuifd28f322015-08-27 18:50:04 -0700447// Create a remote socket to exchange packets with a remote service through transport
448// |t|. Where |id| is the socket id of the corresponding service on the other
449// side of the transport (it is allocated by the remote side and _cannot_ be 0).
450// Returns a new non-NULL asocket handle.
Josh Gao52bd8522016-05-17 16:55:06 -0700451asocket* create_remote_socket(unsigned id, atransport* t) {
452 if (id == 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700453 LOG(FATAL) << "invalid remote socket id (0)";
Josh Gao52bd8522016-05-17 16:55:06 -0700454 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800455 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 s->id = id;
457 s->enqueue = remote_socket_enqueue;
458 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100459 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 s->close = remote_socket_close;
461 s->transport = t;
462
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700463 D("RS(%d): created", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 return s;
465}
466
Josh Gaod0fa13e2018-12-20 17:00:13 -0800467void connect_to_remote(asocket* s, std::string_view destination) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700468 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gao52bd8522016-05-17 16:55:06 -0700469 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470
Josh Gaod0fa13e2018-12-20 17:00:13 -0800471 LOG(VERBOSE) << "LS(" << s->id << ": connect(" << destination << ")";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 p->msg.command = A_OPEN;
473 p->msg.arg0 = s->id;
Josh Gaof571fcb2018-02-05 18:49:10 -0800474
Josh Gaod0fa13e2018-12-20 17:00:13 -0800475 // adbd used to expect a null-terminated string.
476 // Keep doing so to maintain backward compatibility.
477 p->payload.resize(destination.size() + 1);
478 memcpy(p->payload.data(), destination.data(), destination.size());
479 p->payload[destination.size()] = '\0';
Josh Gaof571fcb2018-02-05 18:49:10 -0800480 p->msg.data_length = p->payload.size();
481
Elliott Hughes4679a392018-10-19 13:59:44 -0700482 CHECK_LE(p->msg.data_length, s->get_max_payload());
Josh Gaof571fcb2018-02-05 18:49:10 -0800483
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484 send_packet(p, s->transport);
485}
486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487/* this is used by magic sockets to rig local sockets to
488 send the go-ahead message when they connect */
Josh Gao52bd8522016-05-17 16:55:06 -0700489static void local_socket_ready_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700491 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700493 SendOkay(s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 s->ready(s);
495}
496
497/* this is used by magic sockets to rig local sockets to
498 send the failure message if they are closed before
499 connected (to avoid closing them without a status message) */
Josh Gao52bd8522016-05-17 16:55:06 -0700500static void local_socket_close_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700502 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700504 SendFail(s->fd, "closed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 s->close(s);
506}
507
Josh Gao27cb7dc2018-02-01 13:17:50 -0800508static unsigned unhex(const char* s, int len) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 unsigned n = 0, c;
510
Josh Gao52bd8522016-05-17 16:55:06 -0700511 while (len-- > 0) {
512 switch ((c = *s++)) {
513 case '0':
514 case '1':
515 case '2':
516 case '3':
517 case '4':
518 case '5':
519 case '6':
520 case '7':
521 case '8':
522 case '9':
523 c -= '0';
524 break;
525 case 'a':
526 case 'b':
527 case 'c':
528 case 'd':
529 case 'e':
530 case 'f':
531 c = c - 'a' + 10;
532 break;
533 case 'A':
534 case 'B':
535 case 'C':
536 case 'D':
537 case 'E':
538 case 'F':
539 c = c - 'A' + 10;
540 break;
541 default:
542 return 0xffffffff;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 }
544
545 n = (n << 4) | c;
546 }
547
548 return n;
549}
550
Elliott Hughese67f1f82015-04-30 17:32:03 -0700551#if ADB_HOST
552
David Pursell3f902aa2016-03-01 08:58:26 -0800553namespace internal {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700554
Josh Gaobd767202018-12-19 13:37:41 -0800555// Parses a host service string of the following format:
David Pursell3f902aa2016-03-01 08:58:26 -0800556// * [tcp:|udp:]<serial>[:<port>]:<command>
557// * <prefix>:<serial>:<command>
558// 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 -0800559bool parse_host_service(std::string_view* out_serial, std::string_view* out_command,
560 std::string_view full_service) {
561 if (full_service.empty()) {
562 return false;
563 }
Terence Haddock28e13902011-03-16 09:43:56 +0100564
Josh Gaobd767202018-12-19 13:37:41 -0800565 std::string_view serial;
566 std::string_view command = full_service;
567 // Remove |count| bytes from the beginning of command and add them to |serial|.
568 auto consume = [&full_service, &serial, &command](size_t count) {
569 CHECK_LE(count, command.size());
570 if (!serial.empty()) {
571 CHECK_EQ(serial.data() + serial.size(), command.data());
572 }
573
574 serial = full_service.substr(0, serial.size() + count);
575 command.remove_prefix(count);
576 };
577
578 // Remove the trailing : from serial, and assign the values to the output parameters.
579 auto finish = [out_serial, out_command, &serial, &command] {
580 if (serial.empty() || command.empty()) {
581 return false;
582 }
583
584 CHECK_EQ(':', serial.back());
585 serial.remove_suffix(1);
586
587 *out_serial = serial;
588 *out_command = command;
589 return true;
590 };
591
592 static constexpr std::string_view prefixes[] = {"usb:", "product:", "model:", "device:"};
593 for (std::string_view prefix : prefixes) {
594 if (command.starts_with(prefix)) {
595 consume(prefix.size());
596
597 size_t offset = command.find_first_of(':');
598 if (offset == std::string::npos) {
599 return false;
600 }
601 consume(offset + 1);
602 return finish();
David Pursell3f902aa2016-03-01 08:58:26 -0800603 }
Scott Anderson3608d832012-05-31 12:04:23 -0700604 }
605
David Pursell3f902aa2016-03-01 08:58:26 -0800606 // For fastboot compatibility, ignore protocol prefixes.
Josh Gaobd767202018-12-19 13:37:41 -0800607 if (command.starts_with("tcp:") || command.starts_with("udp:")) {
608 consume(4);
609 if (command.empty()) {
610 return false;
David Pursell73d55aa2016-09-21 12:08:37 -0700611 }
612 }
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800613 if (command.starts_with("vsock:")) {
614 // vsock serials are vsock:cid:port, which have an extra colon compared to tcp.
615 size_t next_colon = command.find(':');
616 if (next_colon == std::string::npos) {
617 return false;
618 }
619 consume(next_colon + 1);
620 }
David Pursell73d55aa2016-09-21 12:08:37 -0700621
Josh Gaobd767202018-12-19 13:37:41 -0800622 bool found_address = false;
623 if (command[0] == '[') {
624 // Read an IPv6 address. `adb connect` creates the serial number from the canonical
625 // network address so it will always have the [] delimiters.
626 size_t ipv6_end = command.find_first_of(']');
627 if (ipv6_end != std::string::npos) {
628 consume(ipv6_end + 1);
629 if (command.empty()) {
630 // Nothing after the IPv6 address.
631 return false;
632 } else if (command[0] != ':') {
633 // Garbage after the IPv6 address.
634 return false;
635 }
636 consume(1);
637 found_address = true;
638 }
Terence Haddock28e13902011-03-16 09:43:56 +0100639 }
David Pursell3f902aa2016-03-01 08:58:26 -0800640
Josh Gaobd767202018-12-19 13:37:41 -0800641 if (!found_address) {
642 // Scan ahead to the next colon.
643 size_t offset = command.find_first_of(':');
644 if (offset == std::string::npos) {
645 return false;
Terence Haddock28e13902011-03-16 09:43:56 +0100646 }
Josh Gaobd767202018-12-19 13:37:41 -0800647 consume(offset + 1);
648 }
649
650 // We're either at the beginning of a port, or the command itself.
651 // Look for a port in between colons.
652 size_t next_colon = command.find_first_of(':');
653 if (next_colon == std::string::npos) {
654 // No colon, we must be at the command.
655 return finish();
656 }
657
658 bool port_valid = true;
659 if (command.size() <= next_colon) {
660 return false;
661 }
662
663 std::string_view port = command.substr(0, next_colon);
664 for (auto digit : port) {
665 if (!isdigit(digit)) {
666 // Port isn't a number.
667 port_valid = false;
668 break;
Terence Haddock28e13902011-03-16 09:43:56 +0100669 }
670 }
Josh Gaobd767202018-12-19 13:37:41 -0800671
672 if (port_valid) {
673 consume(next_colon + 1);
674 }
675 return finish();
Terence Haddock28e13902011-03-16 09:43:56 +0100676}
677
David Pursell3f902aa2016-03-01 08:58:26 -0800678} // namespace internal
679
Josh Gao52bd8522016-05-17 16:55:06 -0700680#endif // ADB_HOST
Elliott Hughese67f1f82015-04-30 17:32:03 -0700681
Josh Gao1ce99572018-03-07 16:52:28 -0800682static int smart_socket_enqueue(asocket* s, apacket::payload_type data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683#if ADB_HOST
Josh Gaobd767202018-12-19 13:37:41 -0800684 std::string_view service;
685 std::string_view serial;
Josh Gaob122b172017-08-16 16:57:01 -0700686 TransportId transport_id = 0;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700687 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688#endif
689
Josh Gao27cb7dc2018-02-01 13:17:50 -0800690 D("SS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691
Josh Gao27cb7dc2018-02-01 13:17:50 -0800692 if (s->smart_socket_data.empty()) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700693 // TODO: Make this an IOVector?
Josh Gao1ce99572018-03-07 16:52:28 -0800694 s->smart_socket_data.assign(data.begin(), data.end());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 } else {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800696 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697 }
698
Josh Gao7e6683c2016-01-15 14:35:54 -0800699 /* don't bother if we can't decode the length */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800700 if (s->smart_socket_data.size() < 4) {
Josh Gao52bd8522016-05-17 16:55:06 -0700701 return 0;
702 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703
Josh Gao27cb7dc2018-02-01 13:17:50 -0800704 uint32_t len = unhex(s->smart_socket_data.data(), 4);
705 if (len == 0 || len > MAX_PAYLOAD) {
706 D("SS(%d): bad size (%u)", s->id, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 goto fail;
708 }
709
Josh Gao27cb7dc2018-02-01 13:17:50 -0800710 D("SS(%d): len is %u", s->id, len);
Josh Gao7e6683c2016-01-15 14:35:54 -0800711 /* can't do anything until we have the full header */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800712 if ((len + 4) > s->smart_socket_data.size()) {
713 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 -0800714 return 0;
715 }
716
Josh Gao27cb7dc2018-02-01 13:17:50 -0800717 s->smart_socket_data[len + 4] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718
Josh Gao27cb7dc2018-02-01 13:17:50 -0800719 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720
721#if ADB_HOST
Josh Gaobd767202018-12-19 13:37:41 -0800722 service = std::string_view(s->smart_socket_data).substr(4);
723 if (service.starts_with("host-serial:")) {
724 service.remove_prefix(strlen("host-serial:"));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725
Terence Haddock28e13902011-03-16 09:43:56 +0100726 // serial number should follow "host:" and could be a host:port string.
Josh Gaobd767202018-12-19 13:37:41 -0800727 if (!internal::parse_host_service(&serial, &service, service)) {
728 LOG(ERROR) << "SS(" << s->id << "): failed to parse host service: " << service;
729 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800730 }
Josh Gaobd767202018-12-19 13:37:41 -0800731 } else if (service.starts_with("host-transport-id:")) {
732 service.remove_prefix(strlen("host-transport-id:"));
733 if (!ParseUint(&transport_id, service, &service)) {
734 LOG(ERROR) << "SS(" << s->id << "): failed to parse host transport id: " << service;
Josh Gaob122b172017-08-16 16:57:01 -0700735 return -1;
736 }
Josh Gaobd767202018-12-19 13:37:41 -0800737 if (!service.starts_with(":")) {
738 LOG(ERROR) << "SS(" << s->id << "): host-transport-id without command";
739 return -1;
740 }
741 service.remove_prefix(1);
742 } else if (service.starts_with("host-usb:")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700743 type = kTransportUsb;
Josh Gaobd767202018-12-19 13:37:41 -0800744 service.remove_prefix(strlen("host-usb:"));
745 } else if (service.starts_with("host-local:")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700746 type = kTransportLocal;
Josh Gaobd767202018-12-19 13:37:41 -0800747 service.remove_prefix(strlen("host-local:"));
748 } else if (service.starts_with("host:")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700749 type = kTransportAny;
Josh Gaobd767202018-12-19 13:37:41 -0800750 service.remove_prefix(strlen("host:"));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800751 } else {
Josh Gaobd767202018-12-19 13:37:41 -0800752 service = std::string_view{};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753 }
754
Josh Gaobd767202018-12-19 13:37:41 -0800755 if (!service.empty()) {
Josh Gao52bd8522016-05-17 16:55:06 -0700756 asocket* s2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800757
Josh Gao40390512018-08-07 14:14:21 -0700758 // Some requests are handled immediately -- in that case the handle_host_request() routine
759 // has sent the OKAY or FAIL message and all we have to do is clean up.
Josh Gaobd767202018-12-19 13:37:41 -0800760 // TODO: Convert to string_view.
761 if (handle_host_request(std::string(service).c_str(), type,
762 serial.empty() ? nullptr : std::string(serial).c_str(),
763 transport_id, s->peer->fd, s)) {
764 LOG(VERBOSE) << "SS(" << s->id << "): handled host service '" << service << "'";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765 goto fail;
766 }
Josh Gaobd767202018-12-19 13:37:41 -0800767 if (service.starts_with("transport")) {
Josh Gao52bd8522016-05-17 16:55:06 -0700768 D("SS(%d): okay transport", s->id);
Josh Gao27cb7dc2018-02-01 13:17:50 -0800769 s->smart_socket_data.clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800770 return 0;
771 }
772
Josh Gao52bd8522016-05-17 16:55:06 -0700773 /* try to find a local service with this name.
774 ** if no such service exists, we'll fail out
775 ** and tear down here.
776 */
Josh Gaobd767202018-12-19 13:37:41 -0800777 // TODO: Convert to string_view.
778 s2 = create_host_service_socket(std::string(service).c_str(), std::string(serial).c_str(),
779 transport_id);
Yi Kongaed415c2018-07-13 18:15:16 -0700780 if (s2 == nullptr) {
Josh Gaobd767202018-12-19 13:37:41 -0800781 LOG(VERBOSE) << "SS(" << s->id << "): couldn't create host service '" << service << "'";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700782 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800783 goto fail;
784 }
785
Josh Gao52bd8522016-05-17 16:55:06 -0700786 /* we've connected to a local host service,
787 ** so we make our peer back into a regular
788 ** local socket and bind it to the new local
789 ** service socket, acknowledge the successful
790 ** connection, and close this smart socket now
791 ** that its work is done.
792 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700793 SendOkay(s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800794
795 s->peer->ready = local_socket_ready;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700796 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800797 s->peer->close = local_socket_close;
798 s->peer->peer = s2;
799 s2->peer = s->peer;
Yi Kongaed415c2018-07-13 18:15:16 -0700800 s->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700801 D("SS(%d): okay", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802 s->close(s);
803
Josh Gao52bd8522016-05-17 16:55:06 -0700804 /* initial state is "ready" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805 s2->ready(s2);
806 return 0;
807 }
808#else /* !ADB_HOST */
Elliott Hughes8d28e192015-10-07 14:55:10 -0700809 if (s->transport == nullptr) {
Elliott Hughes7be29c82015-04-16 22:54:44 -0700810 std::string error_msg = "unknown failure";
Josh Gaob122b172017-08-16 16:57:01 -0700811 s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700812 if (s->transport == nullptr) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700813 SendFail(s->peer->fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800814 goto fail;
815 }
816 }
817#endif
818
Josh Gao22d2b3e2016-10-27 14:01:08 -0700819 if (!s->transport) {
820 SendFail(s->peer->fd, "device offline (no transport)");
821 goto fail;
Josh Gao704494b2018-05-04 16:04:49 -0700822 } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) {
Josh Gao52bd8522016-05-17 16:55:06 -0700823 /* if there's no remote we fail the connection
824 ** right here and terminate it
825 */
Josh Gao22d2b3e2016-10-27 14:01:08 -0700826 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827 goto fail;
828 }
829
Josh Gao52bd8522016-05-17 16:55:06 -0700830 /* instrument our peer to pass the success or fail
831 ** message back once it connects or closes, then
832 ** detach from it, request the connection, and
833 ** tear down
834 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800835 s->peer->ready = local_socket_ready_notify;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700836 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800837 s->peer->close = local_socket_close_notify;
Yi Kongaed415c2018-07-13 18:15:16 -0700838 s->peer->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700839 /* give him our transport and upref it */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800840 s->peer->transport = s->transport;
841
Josh Gaod0fa13e2018-12-20 17:00:13 -0800842 connect_to_remote(s->peer, std::string_view(s->smart_socket_data).substr(4));
Yi Kongaed415c2018-07-13 18:15:16 -0700843 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800844 s->close(s);
845 return 1;
846
847fail:
Josh Gao52bd8522016-05-17 16:55:06 -0700848 /* we're going to close our peer as a side-effect, so
849 ** return -1 to signal that state to the local socket
850 ** who is enqueueing against us
851 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852 s->close(s);
853 return -1;
854}
855
Josh Gao52bd8522016-05-17 16:55:06 -0700856static void smart_socket_ready(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700857 D("SS(%d): ready", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800858}
859
Josh Gao52bd8522016-05-17 16:55:06 -0700860static void smart_socket_close(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700861 D("SS(%d): closed", s->id);
Josh Gao52bd8522016-05-17 16:55:06 -0700862 if (s->peer) {
Yi Kongaed415c2018-07-13 18:15:16 -0700863 s->peer->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800864 s->peer->close(s->peer);
Yi Kongaed415c2018-07-13 18:15:16 -0700865 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800866 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800867 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800868}
869
Josh Gao52bd8522016-05-17 16:55:06 -0700870static asocket* create_smart_socket(void) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700871 D("Creating smart socket");
Josh Gaoe0361d12018-02-12 17:24:00 -0800872 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800873 s->enqueue = smart_socket_enqueue;
874 s->ready = smart_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700875 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800876 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800877
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700878 D("SS(%d)", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800879 return s;
880}
881
Josh Gao52bd8522016-05-17 16:55:06 -0700882void connect_to_smartsocket(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700883 D("Connecting to smart socket");
Josh Gao52bd8522016-05-17 16:55:06 -0700884 asocket* ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800885 s->peer = ss;
886 ss->peer = s;
887 s->ready(s);
888}
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100889
890size_t asocket::get_max_payload() const {
891 size_t max_payload = MAX_PAYLOAD;
892 if (transport) {
893 max_payload = std::min(max_payload, transport->get_max_payload());
894 }
895 if (peer && peer->transport) {
896 max_payload = std::min(max_payload, peer->transport->get_max_payload());
897 }
898 return max_payload;
899}