blob: 47ae883f44f03b1444462e4dc5229801dc12c48d [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG SOCKETS
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
Dan Albertb302d122015-02-24 15:51:19 -080021#include <ctype.h>
22#include <errno.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080023#include <stdio.h>
24#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080025#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080026#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080027
Spencer Low28bc2cb2015-11-07 18:51:54 -080028#include <algorithm>
Josh Gao0f1a20a2016-05-17 17:46:27 -070029#include <mutex>
David Pursellc929c6f2016-03-01 08:58:26 -080030#include <string>
31#include <vector>
Spencer Low28bc2cb2015-11-07 18:51:54 -080032
Dan Albertb302d122015-02-24 15:51:19 -080033#if !ADB_HOST
Elliott Hughes8b249d22016-09-23 15:40:03 -070034#include <android-base/properties.h>
Steven Morelandb087d302017-04-13 23:48:57 -070035#include <log/log_properties.h>
Dan Albertb302d122015-02-24 15:51:19 -080036#endif
Dan Albertdb6fe642015-03-19 15:21:08 -070037
38#include "adb.h"
39#include "adb_io.h"
Josh Gao8d49e122018-12-19 13:37:41 -080040#include "adb_utils.h"
Dan Albertb302d122015-02-24 15:51:19 -080041#include "transport.h"
Josh Gaocd2a5292018-03-07 16:52:28 -080042#include "types.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080043
Josh Gao0f1a20a2016-05-17 17:46:27 -070044static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080045static unsigned local_socket_next_id = 1;
46
Josh Gao2b3db9e2018-01-31 13:15:51 -080047static auto& local_socket_list = *new std::vector<asocket*>();
The Android Open Source Project9ca14dc2009-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 Gao2b3db9e2018-01-31 13:15:51 -080053static auto& local_socket_closing_list = *new std::vector<asocket*>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080054
David 'Digit' Turnere92344d2013-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 Gaoef550fe2016-05-17 16:55:06 -070058asocket* find_local_socket(unsigned local_id, unsigned peer_id) {
Josh Gao2b3db9e2018-01-31 13:15:51 -080059 asocket* result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080060
Josh Gao0f1a20a2016-05-17 17:46:27 -070061 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao2b3db9e2018-01-31 13:15:51 -080062 for (asocket* s : local_socket_list) {
Josh Gaoef550fe2016-05-17 16:55:06 -070063 if (s->id != local_id) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010064 continue;
Josh Gaoef550fe2016-05-17 16:55:06 -070065 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010066 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030067 result = s;
André Goddard Rosa5720e6e2010-06-12 11:40:20 -030068 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010069 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080070 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080071
72 return result;
73}
74
Josh Gaoef550fe2016-05-17 16:55:06 -070075void install_local_socket(asocket* s) {
Josh Gao0f1a20a2016-05-17 17:46:27 -070076 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080077
78 s->id = local_socket_next_id++;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010079
80 // Socket ids should never be 0.
Josh Gaoef550fe2016-05-17 16:55:06 -070081 if (local_socket_next_id == 0) {
Elliott Hughese64126b2018-10-19 13:59:44 -070082 LOG(FATAL) << "local socket id overflow";
Josh Gaoef550fe2016-05-17 16:55:06 -070083 }
David 'Digit' Turnere92344d2013-12-13 14:09:44 +010084
Josh Gao2b3db9e2018-01-31 13:15:51 -080085 local_socket_list.push_back(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080086}
87
Josh Gaoef550fe2016-05-17 16:55:06 -070088void remove_socket(asocket* s) {
Josh Gaob69c78b2017-09-13 11:17:33 -070089 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao2b3db9e2018-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 Project9ca14dc2009-03-03 19:32:55 -080093 }
94}
95
Josh Gaoef550fe2016-05-17 16:55:06 -070096void close_all_sockets(atransport* t) {
Josh Gaoef550fe2016-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 Gao0f1a20a2016-05-17 17:46:27 -0700100 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800101restart:
Josh Gao2b3db9e2018-01-31 13:15:51 -0800102 for (asocket* s : local_socket_list) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700103 if (s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gao80814e12016-05-18 10:39:48 -0700104 s->close(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800105 goto restart;
106 }
107 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800108}
109
Josh Gao4b808502018-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 Gao9f155db2018-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 Gao4b808502018-03-19 13:20:29 -0700122 } else if (rc > 0) {
Josh Gao9f155db2018-04-03 14:37:11 -0700123 // TODO: Implement a faster drop_front?
124 s->packet_queue.take_front(rc);
Josh Gao9528df22018-05-14 11:14:33 -0700125 fdevent_add(s->fde, FDE_WRITE);
Josh Gao4b808502018-03-19 13:20:29 -0700126 return SocketFlushResult::TryAgain;
127 } else if (rc == -1 && errno == EAGAIN) {
Josh Gao9528df22018-05-14 11:14:33 -0700128 fdevent_add(s->fde, FDE_WRITE);
Josh Gao4b808502018-03-19 13:20:29 -0700129 return SocketFlushResult::TryAgain;
Josh Gaob50b92f2018-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 Gao4b808502018-03-19 13:20:29 -0700134 }
Josh Gao4b808502018-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 Gao9528df22018-05-14 11:14:33 -0700143 fdevent_del(s->fde, FDE_WRITE);
Josh Gao4b808502018-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 Gaocd2a5292018-03-07 16:52:28 -0800150 apacket::payload_type data;
Josh Gao4b808502018-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 Gao9528df22018-05-14 11:14:33 -0700176 s->fde->force_eof);
Josh Gao4b808502018-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 Gao9528df22018-05-14 11:14:33 -0700203 fdevent_del(s->fde, FDE_READ);
Josh Gao4b808502018-03-19 13:20:29 -0700204 }
205 }
206
207 // Don't allow a forced eof if data is still there.
Josh Gao9528df22018-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 Gao4b808502018-03-19 13:20:29 -0700210 s->close(s);
211 return false;
212 }
213
214 return true;
215}
216
Josh Gaocd2a5292018-03-07 16:52:28 -0800217static int local_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800218 D("LS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800219
Josh Gao9f155db2018-04-03 14:37:11 -0700220 s->packet_queue.append(std::move(data));
Josh Gao4b808502018-03-19 13:20:29 -0700221 switch (local_socket_flush_incoming(s)) {
222 case SocketFlushResult::Destroyed:
223 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800224
Josh Gao4b808502018-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 Project9ca14dc2009-03-03 19:32:55 -0800233}
234
Josh Gaoef550fe2016-05-17 16:55:06 -0700235static void local_socket_ready(asocket* s) {
Nanik Tolaramc624a7e2015-02-18 22:53:37 +1100236 /* far side is ready for data, pay attention to
237 readable events */
Josh Gao9528df22018-05-14 11:14:33 -0700238 fdevent_add(s->fde, FDE_READ);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800239}
240
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800241// be sure to hold the socket list lock when calling this
Josh Gaoef550fe2016-05-17 16:55:06 -0700242static void local_socket_destroy(asocket* s) {
Benoit Goby88468f32012-03-16 14:50:07 -0700243 int exit_on_close = s->exit_on_close;
244
Josh Gao9528df22018-05-14 11:14:33 -0700245 D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800246
Josh Gao94250272018-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 Project9ca14dc2009-03-03 19:32:55 -0800251
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800252 remove_socket(s);
Josh Gao5cb76ce2018-02-12 17:24:00 -0800253 delete s;
Benoit Goby88468f32012-03-16 14:50:07 -0700254
255 if (exit_on_close) {
Yabin Cui815ad882015-09-02 17:44:28 -0700256 D("local_socket_destroy: exiting");
Benoit Goby88468f32012-03-16 14:50:07 -0700257 exit(1);
258 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800259}
260
Josh Gao0f1a20a2016-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 Gaoef550fe2016-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' Turnere92344d2013-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 Gaoef550fe2016-05-17 16:55:06 -0700270 if (s->peer->shutdown) {
271 s->peer->shutdown(s->peer);
272 }
Josh Gao0f1a20a2016-05-17 17:46:27 -0700273 s->peer->peer = nullptr;
274 s->peer->close(s->peer);
275 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800276 }
277
Josh Gaoef550fe2016-05-17 16:55:06 -0700278 /* If we are already closing, or if there are no
279 ** pending packets, destroy immediately
280 */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800281 if (s->closing || s->has_write_error || s->packet_queue.empty()) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700282 int id = s->id;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800283 local_socket_destroy(s);
Yabin Cui815ad882015-09-02 17:44:28 -0700284 D("LS(%d): closed", id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800285 return;
286 }
287
Josh Gaoef550fe2016-05-17 16:55:06 -0700288 /* otherwise, put on the closing list
289 */
Yabin Cui815ad882015-09-02 17:44:28 -0700290 D("LS(%d): closing", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800291 s->closing = 1;
Josh Gao9528df22018-05-14 11:14:33 -0700292 fdevent_del(s->fde, FDE_READ);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800293 remove_socket(s);
Yabin Cui815ad882015-09-02 17:44:28 -0700294 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
Josh Gao2b3db9e2018-01-31 13:15:51 -0800295 local_socket_closing_list.push_back(s);
Josh Gao9528df22018-05-14 11:14:33 -0700296 CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800297}
298
Josh Gaoef550fe2016-05-17 16:55:06 -0700299static void local_socket_event_func(int fd, unsigned ev, void* _s) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800300 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui815ad882015-09-02 17:44:28 -0700301 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700302
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800303 /* put the FDE_WRITE processing before the FDE_READ
304 ** in order to simplify the code.
305 */
Dan Albertf30d73c2015-02-25 17:51:28 -0800306 if (ev & FDE_WRITE) {
Josh Gao4b808502018-03-19 13:20:29 -0700307 switch (local_socket_flush_incoming(s)) {
308 case SocketFlushResult::Destroyed:
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800309 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800310
Josh Gao4b808502018-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 Project9ca14dc2009-03-03 19:32:55 -0800317 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800318 }
319
Dan Albertf30d73c2015-02-25 17:51:28 -0800320 if (ev & FDE_READ) {
Josh Gao4b808502018-03-19 13:20:29 -0700321 if (!local_socket_flush_outgoing(s)) {
Yabin Cui2ce9d562015-09-15 16:27:09 -0700322 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800323 }
324 }
325
Josh Gaoef550fe2016-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 Cui815ad882015-09-02 17:44:28 -0700331 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800332 return;
333 }
334}
335
Josh Gaoef550fe2016-05-17 16:55:06 -0700336asocket* create_local_socket(int fd) {
Josh Gao5cb76ce2018-02-12 17:24:00 -0800337 asocket* s = new asocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800338 s->fd = fd;
339 s->enqueue = local_socket_enqueue;
340 s->ready = local_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700341 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800342 s->close = local_socket_close;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700343 install_local_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800344
Josh Gao9528df22018-05-14 11:14:33 -0700345 s->fde = fdevent_create(fd, local_socket_event_func, s);
Yabin Cui815ad882015-09-02 17:44:28 -0700346 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800347 return s;
348}
349
Josh Gao6dbf5792018-12-13 14:21:00 -0800350asocket* create_local_service_socket(std::string_view name, atransport* transport) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800351#if !ADB_HOST
Josh Gao3edf8072018-11-16 15:40:16 -0800352 if (asocket* s = daemon_service_to_socket(name); s) {
353 return s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800354 }
355#endif
David Pursell8da19a42015-08-31 10:42:13 -0700356 int fd = service_to_fd(name, transport);
Josh Gaoef550fe2016-05-17 16:55:06 -0700357 if (fd < 0) {
Elliott Hughesc3d1c112016-06-15 14:46:56 -0700358 return nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700359 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800360
Dan Pasanenfb787d92014-10-06 12:57:20 -0500361 asocket* s = create_local_socket(fd);
Josh Gao6dbf5792018-12-13 14:21:00 -0800362 LOG(VERBOSE) << "LS(" << s->id << "): bound to '" << name << "' via " << fd;
Benoit Goby88468f32012-03-16 14:50:07 -0700363
JP Abgralla84bd682012-03-30 13:19:11 -0700364#if !ADB_HOST
Josh Gao6dbf5792018-12-13 14:21:00 -0800365 if ((name.starts_with("root:") && getuid() != 0 && __android_log_is_debuggable()) ||
366 (name.starts_with("unroot:") && getuid() == 0) || name.starts_with("usb:") ||
367 name.starts_with("tcpip:")) {
Yabin Cui815ad882015-09-02 17:44:28 -0700368 D("LS(%d): enabling exit_on_close", s->id);
Benoit Goby88468f32012-03-16 14:50:07 -0700369 s->exit_on_close = 1;
370 }
JP Abgralla84bd682012-03-30 13:19:11 -0700371#endif
Benoit Goby88468f32012-03-16 14:50:07 -0700372
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800373 return s;
374}
375
376#if ADB_HOST
Josh Gaob39e4152017-08-16 16:57:01 -0700377static asocket* create_host_service_socket(const char* name, const char* serial,
378 TransportId transport_id) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700379 asocket* s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800380
Josh Gaob39e4152017-08-16 16:57:01 -0700381 s = host_service_to_socket(name, serial, transport_id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800382
Yi Kong86e67182018-07-13 18:15:16 -0700383 if (s != nullptr) {
Yabin Cui815ad882015-09-02 17:44:28 -0700384 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800385 return s;
386 }
387
388 return s;
389}
390#endif /* ADB_HOST */
391
Josh Gaocd2a5292018-03-07 16:52:28 -0800392static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700393 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
Josh Gaoa7d9d712018-02-01 13:17:50 -0800394 apacket* p = get_apacket();
395
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800396 p->msg.command = A_WRTE;
397 p->msg.arg0 = s->peer->id;
398 p->msg.arg1 = s->id;
Josh Gaoa7d9d712018-02-01 13:17:50 -0800399
Josh Gao839b9322018-02-05 18:49:10 -0800400 if (data.size() > MAX_PAYLOAD) {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800401 put_apacket(p);
402 return -1;
403 }
404
Josh Gao839b9322018-02-05 18:49:10 -0800405 p->payload = std::move(data);
406 p->msg.data_length = p->payload.size();
407
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800408 send_packet(p, s->transport);
409 return 1;
410}
411
Josh Gaoef550fe2016-05-17 16:55:06 -0700412static void remote_socket_ready(asocket* s) {
413 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
414 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800415 p->msg.command = A_OKAY;
416 p->msg.arg0 = s->peer->id;
417 p->msg.arg1 = s->id;
418 send_packet(p, s->transport);
419}
420
Josh Gaoef550fe2016-05-17 16:55:06 -0700421static void remote_socket_shutdown(asocket* s) {
422 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
423 s->peer ? s->peer->fd : -1);
424 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800425 p->msg.command = A_CLSE;
Josh Gaoef550fe2016-05-17 16:55:06 -0700426 if (s->peer) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800427 p->msg.arg0 = s->peer->id;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100428 }
429 p->msg.arg1 = s->id;
430 send_packet(p, s->transport);
431}
432
Josh Gaoef550fe2016-05-17 16:55:06 -0700433static void remote_socket_close(asocket* s) {
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100434 if (s->peer) {
Yi Kong86e67182018-07-13 18:15:16 -0700435 s->peer->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700436 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800437 s->peer->close(s->peer);
438 }
Josh Gaoef550fe2016-05-17 16:55:06 -0700439 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
440 s->peer ? s->peer->fd : -1);
Yabin Cui815ad882015-09-02 17:44:28 -0700441 D("RS(%d): closed", s->id);
Josh Gao5cb76ce2018-02-12 17:24:00 -0800442 delete s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800443}
444
Yabin Cui70ec57b2015-08-27 18:50:04 -0700445// Create a remote socket to exchange packets with a remote service through transport
446// |t|. Where |id| is the socket id of the corresponding service on the other
447// side of the transport (it is allocated by the remote side and _cannot_ be 0).
448// Returns a new non-NULL asocket handle.
Josh Gaoef550fe2016-05-17 16:55:06 -0700449asocket* create_remote_socket(unsigned id, atransport* t) {
450 if (id == 0) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700451 LOG(FATAL) << "invalid remote socket id (0)";
Josh Gaoef550fe2016-05-17 16:55:06 -0700452 }
Josh Gao5cb76ce2018-02-12 17:24:00 -0800453 asocket* s = new asocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800454 s->id = id;
455 s->enqueue = remote_socket_enqueue;
456 s->ready = remote_socket_ready;
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100457 s->shutdown = remote_socket_shutdown;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800458 s->close = remote_socket_close;
459 s->transport = t;
460
Yabin Cui815ad882015-09-02 17:44:28 -0700461 D("RS(%d): created", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800462 return s;
463}
464
Josh Gao4a037e22018-12-20 17:00:13 -0800465void connect_to_remote(asocket* s, std::string_view destination) {
Yabin Cui815ad882015-09-02 17:44:28 -0700466 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gaoef550fe2016-05-17 16:55:06 -0700467 apacket* p = get_apacket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800468
Josh Gao4a037e22018-12-20 17:00:13 -0800469 LOG(VERBOSE) << "LS(" << s->id << ": connect(" << destination << ")";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800470 p->msg.command = A_OPEN;
471 p->msg.arg0 = s->id;
Josh Gao839b9322018-02-05 18:49:10 -0800472
Josh Gao4a037e22018-12-20 17:00:13 -0800473 // adbd used to expect a null-terminated string.
474 // Keep doing so to maintain backward compatibility.
475 p->payload.resize(destination.size() + 1);
476 memcpy(p->payload.data(), destination.data(), destination.size());
477 p->payload[destination.size()] = '\0';
Josh Gao839b9322018-02-05 18:49:10 -0800478 p->msg.data_length = p->payload.size();
479
Elliott Hughese64126b2018-10-19 13:59:44 -0700480 CHECK_LE(p->msg.data_length, s->get_max_payload());
Josh Gao839b9322018-02-05 18:49:10 -0800481
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800482 send_packet(p, s->transport);
483}
484
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800485/* this is used by magic sockets to rig local sockets to
486 send the go-ahead message when they connect */
Josh Gaoef550fe2016-05-17 16:55:06 -0700487static void local_socket_ready_notify(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800488 s->ready = local_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700489 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800490 s->close = local_socket_close;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700491 SendOkay(s->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800492 s->ready(s);
493}
494
495/* this is used by magic sockets to rig local sockets to
496 send the failure message if they are closed before
497 connected (to avoid closing them without a status message) */
Josh Gaoef550fe2016-05-17 16:55:06 -0700498static void local_socket_close_notify(asocket* s) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800499 s->ready = local_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700500 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800501 s->close = local_socket_close;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700502 SendFail(s->fd, "closed");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800503 s->close(s);
504}
505
Josh Gaoa7d9d712018-02-01 13:17:50 -0800506static unsigned unhex(const char* s, int len) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800507 unsigned n = 0, c;
508
Josh Gaoef550fe2016-05-17 16:55:06 -0700509 while (len-- > 0) {
510 switch ((c = *s++)) {
511 case '0':
512 case '1':
513 case '2':
514 case '3':
515 case '4':
516 case '5':
517 case '6':
518 case '7':
519 case '8':
520 case '9':
521 c -= '0';
522 break;
523 case 'a':
524 case 'b':
525 case 'c':
526 case 'd':
527 case 'e':
528 case 'f':
529 c = c - 'a' + 10;
530 break;
531 case 'A':
532 case 'B':
533 case 'C':
534 case 'D':
535 case 'E':
536 case 'F':
537 c = c - 'A' + 10;
538 break;
539 default:
540 return 0xffffffff;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800541 }
542
543 n = (n << 4) | c;
544 }
545
546 return n;
547}
548
Elliott Hughes88b4c852015-04-30 17:32:03 -0700549#if ADB_HOST
550
David Pursellc929c6f2016-03-01 08:58:26 -0800551namespace internal {
Scott Anderson27042382012-05-30 18:11:27 -0700552
Josh Gao8d49e122018-12-19 13:37:41 -0800553// Parses a host service string of the following format:
David Pursellc929c6f2016-03-01 08:58:26 -0800554// * [tcp:|udp:]<serial>[:<port>]:<command>
555// * <prefix>:<serial>:<command>
556// Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}.
Josh Gao8d49e122018-12-19 13:37:41 -0800557bool parse_host_service(std::string_view* out_serial, std::string_view* out_command,
558 std::string_view full_service) {
559 if (full_service.empty()) {
560 return false;
561 }
Terence Haddock6c670402011-03-16 09:43:56 +0100562
Josh Gao8d49e122018-12-19 13:37:41 -0800563 std::string_view serial;
564 std::string_view command = full_service;
565 // Remove |count| bytes from the beginning of command and add them to |serial|.
566 auto consume = [&full_service, &serial, &command](size_t count) {
567 CHECK_LE(count, command.size());
568 if (!serial.empty()) {
569 CHECK_EQ(serial.data() + serial.size(), command.data());
570 }
571
572 serial = full_service.substr(0, serial.size() + count);
573 command.remove_prefix(count);
574 };
575
576 // Remove the trailing : from serial, and assign the values to the output parameters.
577 auto finish = [out_serial, out_command, &serial, &command] {
578 if (serial.empty() || command.empty()) {
579 return false;
580 }
581
582 CHECK_EQ(':', serial.back());
583 serial.remove_suffix(1);
584
585 *out_serial = serial;
586 *out_command = command;
587 return true;
588 };
589
590 static constexpr std::string_view prefixes[] = {"usb:", "product:", "model:", "device:"};
591 for (std::string_view prefix : prefixes) {
592 if (command.starts_with(prefix)) {
593 consume(prefix.size());
594
595 size_t offset = command.find_first_of(':');
596 if (offset == std::string::npos) {
597 return false;
598 }
599 consume(offset + 1);
600 return finish();
David Pursellc929c6f2016-03-01 08:58:26 -0800601 }
Scott Anderson090e5cb2012-05-31 12:04:23 -0700602 }
603
David Pursellc929c6f2016-03-01 08:58:26 -0800604 // For fastboot compatibility, ignore protocol prefixes.
Josh Gao8d49e122018-12-19 13:37:41 -0800605 if (command.starts_with("tcp:") || command.starts_with("udp:")) {
606 consume(4);
607 if (command.empty()) {
608 return false;
David Pursell24b62a72016-09-21 12:08:37 -0700609 }
610 }
611
Josh Gao8d49e122018-12-19 13:37:41 -0800612 bool found_address = false;
613 if (command[0] == '[') {
614 // Read an IPv6 address. `adb connect` creates the serial number from the canonical
615 // network address so it will always have the [] delimiters.
616 size_t ipv6_end = command.find_first_of(']');
617 if (ipv6_end != std::string::npos) {
618 consume(ipv6_end + 1);
619 if (command.empty()) {
620 // Nothing after the IPv6 address.
621 return false;
622 } else if (command[0] != ':') {
623 // Garbage after the IPv6 address.
624 return false;
625 }
626 consume(1);
627 found_address = true;
628 }
Terence Haddock6c670402011-03-16 09:43:56 +0100629 }
David Pursellc929c6f2016-03-01 08:58:26 -0800630
Josh Gao8d49e122018-12-19 13:37:41 -0800631 if (!found_address) {
632 // Scan ahead to the next colon.
633 size_t offset = command.find_first_of(':');
634 if (offset == std::string::npos) {
635 return false;
Terence Haddock6c670402011-03-16 09:43:56 +0100636 }
Josh Gao8d49e122018-12-19 13:37:41 -0800637 consume(offset + 1);
638 }
639
640 // We're either at the beginning of a port, or the command itself.
641 // Look for a port in between colons.
642 size_t next_colon = command.find_first_of(':');
643 if (next_colon == std::string::npos) {
644 // No colon, we must be at the command.
645 return finish();
646 }
647
648 bool port_valid = true;
649 if (command.size() <= next_colon) {
650 return false;
651 }
652
653 std::string_view port = command.substr(0, next_colon);
654 for (auto digit : port) {
655 if (!isdigit(digit)) {
656 // Port isn't a number.
657 port_valid = false;
658 break;
Terence Haddock6c670402011-03-16 09:43:56 +0100659 }
660 }
Josh Gao8d49e122018-12-19 13:37:41 -0800661
662 if (port_valid) {
663 consume(next_colon + 1);
664 }
665 return finish();
Terence Haddock6c670402011-03-16 09:43:56 +0100666}
667
David Pursellc929c6f2016-03-01 08:58:26 -0800668} // namespace internal
669
Josh Gaoef550fe2016-05-17 16:55:06 -0700670#endif // ADB_HOST
Elliott Hughes88b4c852015-04-30 17:32:03 -0700671
Josh Gaocd2a5292018-03-07 16:52:28 -0800672static int smart_socket_enqueue(asocket* s, apacket::payload_type data) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800673#if ADB_HOST
Josh Gao8d49e122018-12-19 13:37:41 -0800674 std::string_view service;
675 std::string_view serial;
Josh Gaob39e4152017-08-16 16:57:01 -0700676 TransportId transport_id = 0;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700677 TransportType type = kTransportAny;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800678#endif
679
Josh Gaoa7d9d712018-02-01 13:17:50 -0800680 D("SS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800681
Josh Gaoa7d9d712018-02-01 13:17:50 -0800682 if (s->smart_socket_data.empty()) {
Josh Gao9f155db2018-04-03 14:37:11 -0700683 // TODO: Make this an IOVector?
Josh Gaocd2a5292018-03-07 16:52:28 -0800684 s->smart_socket_data.assign(data.begin(), data.end());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800685 } else {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800686 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800687 }
688
Josh Gao9055a582016-01-15 14:35:54 -0800689 /* don't bother if we can't decode the length */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800690 if (s->smart_socket_data.size() < 4) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700691 return 0;
692 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800693
Josh Gaoa7d9d712018-02-01 13:17:50 -0800694 uint32_t len = unhex(s->smart_socket_data.data(), 4);
695 if (len == 0 || len > MAX_PAYLOAD) {
696 D("SS(%d): bad size (%u)", s->id, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800697 goto fail;
698 }
699
Josh Gaoa7d9d712018-02-01 13:17:50 -0800700 D("SS(%d): len is %u", s->id, len);
Josh Gao9055a582016-01-15 14:35:54 -0800701 /* can't do anything until we have the full header */
Josh Gaoa7d9d712018-02-01 13:17:50 -0800702 if ((len + 4) > s->smart_socket_data.size()) {
703 D("SS(%d): waiting for %zu more bytes", s->id, len + 4 - s->smart_socket_data.size());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800704 return 0;
705 }
706
Josh Gaoa7d9d712018-02-01 13:17:50 -0800707 s->smart_socket_data[len + 4] = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800708
Josh Gaoa7d9d712018-02-01 13:17:50 -0800709 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800710
711#if ADB_HOST
Josh Gao8d49e122018-12-19 13:37:41 -0800712 service = std::string_view(s->smart_socket_data).substr(4);
713 if (service.starts_with("host-serial:")) {
714 service.remove_prefix(strlen("host-serial:"));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800715
Terence Haddock6c670402011-03-16 09:43:56 +0100716 // serial number should follow "host:" and could be a host:port string.
Josh Gao8d49e122018-12-19 13:37:41 -0800717 if (!internal::parse_host_service(&serial, &service, service)) {
718 LOG(ERROR) << "SS(" << s->id << "): failed to parse host service: " << service;
719 goto fail;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800720 }
Josh Gao8d49e122018-12-19 13:37:41 -0800721 } else if (service.starts_with("host-transport-id:")) {
722 service.remove_prefix(strlen("host-transport-id:"));
723 if (!ParseUint(&transport_id, service, &service)) {
724 LOG(ERROR) << "SS(" << s->id << "): failed to parse host transport id: " << service;
Josh Gaob39e4152017-08-16 16:57:01 -0700725 return -1;
726 }
Josh Gao8d49e122018-12-19 13:37:41 -0800727 if (!service.starts_with(":")) {
728 LOG(ERROR) << "SS(" << s->id << "): host-transport-id without command";
729 return -1;
730 }
731 service.remove_prefix(1);
732 } else if (service.starts_with("host-usb:")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700733 type = kTransportUsb;
Josh Gao8d49e122018-12-19 13:37:41 -0800734 service.remove_prefix(strlen("host-usb:"));
735 } else if (service.starts_with("host-local:")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700736 type = kTransportLocal;
Josh Gao8d49e122018-12-19 13:37:41 -0800737 service.remove_prefix(strlen("host-local:"));
738 } else if (service.starts_with("host:")) {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700739 type = kTransportAny;
Josh Gao8d49e122018-12-19 13:37:41 -0800740 service.remove_prefix(strlen("host:"));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800741 } else {
Josh Gao8d49e122018-12-19 13:37:41 -0800742 service = std::string_view{};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800743 }
744
Josh Gao8d49e122018-12-19 13:37:41 -0800745 if (!service.empty()) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700746 asocket* s2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800747
Josh Gaod3067472018-08-07 14:14:21 -0700748 // Some requests are handled immediately -- in that case the handle_host_request() routine
749 // has sent the OKAY or FAIL message and all we have to do is clean up.
Josh Gao8d49e122018-12-19 13:37:41 -0800750 // TODO: Convert to string_view.
751 if (handle_host_request(std::string(service).c_str(), type,
752 serial.empty() ? nullptr : std::string(serial).c_str(),
753 transport_id, s->peer->fd, s)) {
754 LOG(VERBOSE) << "SS(" << s->id << "): handled host service '" << service << "'";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800755 goto fail;
756 }
Josh Gao8d49e122018-12-19 13:37:41 -0800757 if (service.starts_with("transport")) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700758 D("SS(%d): okay transport", s->id);
Josh Gaoa7d9d712018-02-01 13:17:50 -0800759 s->smart_socket_data.clear();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800760 return 0;
761 }
762
Josh Gaoef550fe2016-05-17 16:55:06 -0700763 /* try to find a local service with this name.
764 ** if no such service exists, we'll fail out
765 ** and tear down here.
766 */
Josh Gao8d49e122018-12-19 13:37:41 -0800767 // TODO: Convert to string_view.
768 s2 = create_host_service_socket(std::string(service).c_str(), std::string(serial).c_str(),
769 transport_id);
Yi Kong86e67182018-07-13 18:15:16 -0700770 if (s2 == nullptr) {
Josh Gao8d49e122018-12-19 13:37:41 -0800771 LOG(VERBOSE) << "SS(" << s->id << "): couldn't create host service '" << service << "'";
Elliott Hughes88b4c852015-04-30 17:32:03 -0700772 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800773 goto fail;
774 }
775
Josh Gaoef550fe2016-05-17 16:55:06 -0700776 /* we've connected to a local host service,
777 ** so we make our peer back into a regular
778 ** local socket and bind it to the new local
779 ** service socket, acknowledge the successful
780 ** connection, and close this smart socket now
781 ** that its work is done.
782 */
Elliott Hughes88b4c852015-04-30 17:32:03 -0700783 SendOkay(s->peer->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800784
785 s->peer->ready = local_socket_ready;
Elliott Hughes67943d12015-10-07 14:55:10 -0700786 s->peer->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800787 s->peer->close = local_socket_close;
788 s->peer->peer = s2;
789 s2->peer = s->peer;
Yi Kong86e67182018-07-13 18:15:16 -0700790 s->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700791 D("SS(%d): okay", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800792 s->close(s);
793
Josh Gaoef550fe2016-05-17 16:55:06 -0700794 /* initial state is "ready" */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800795 s2->ready(s2);
796 return 0;
797 }
798#else /* !ADB_HOST */
Elliott Hughes67943d12015-10-07 14:55:10 -0700799 if (s->transport == nullptr) {
Elliott Hughesab882422015-04-16 22:54:44 -0700800 std::string error_msg = "unknown failure";
Josh Gaob39e4152017-08-16 16:57:01 -0700801 s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
Elliott Hughes67943d12015-10-07 14:55:10 -0700802 if (s->transport == nullptr) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700803 SendFail(s->peer->fd, error_msg);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800804 goto fail;
805 }
806 }
807#endif
808
Josh Gao4e562502016-10-27 14:01:08 -0700809 if (!s->transport) {
810 SendFail(s->peer->fd, "device offline (no transport)");
811 goto fail;
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700812 } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) {
Josh Gaoef550fe2016-05-17 16:55:06 -0700813 /* if there's no remote we fail the connection
814 ** right here and terminate it
815 */
Josh Gao4e562502016-10-27 14:01:08 -0700816 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800817 goto fail;
818 }
819
Josh Gaoef550fe2016-05-17 16:55:06 -0700820 /* instrument our peer to pass the success or fail
821 ** message back once it connects or closes, then
822 ** detach from it, request the connection, and
823 ** tear down
824 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800825 s->peer->ready = local_socket_ready_notify;
Elliott Hughes67943d12015-10-07 14:55:10 -0700826 s->peer->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800827 s->peer->close = local_socket_close_notify;
Yi Kong86e67182018-07-13 18:15:16 -0700828 s->peer->peer = nullptr;
Josh Gaoef550fe2016-05-17 16:55:06 -0700829 /* give him our transport and upref it */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800830 s->peer->transport = s->transport;
831
Josh Gao4a037e22018-12-20 17:00:13 -0800832 connect_to_remote(s->peer, std::string_view(s->smart_socket_data).substr(4));
Yi Kong86e67182018-07-13 18:15:16 -0700833 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800834 s->close(s);
835 return 1;
836
837fail:
Josh Gaoef550fe2016-05-17 16:55:06 -0700838 /* we're going to close our peer as a side-effect, so
839 ** return -1 to signal that state to the local socket
840 ** who is enqueueing against us
841 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800842 s->close(s);
843 return -1;
844}
845
Josh Gaoef550fe2016-05-17 16:55:06 -0700846static void smart_socket_ready(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700847 D("SS(%d): ready", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800848}
849
Josh Gaoef550fe2016-05-17 16:55:06 -0700850static void smart_socket_close(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700851 D("SS(%d): closed", s->id);
Josh Gaoef550fe2016-05-17 16:55:06 -0700852 if (s->peer) {
Yi Kong86e67182018-07-13 18:15:16 -0700853 s->peer->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800854 s->peer->close(s->peer);
Yi Kong86e67182018-07-13 18:15:16 -0700855 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800856 }
Josh Gao5cb76ce2018-02-12 17:24:00 -0800857 delete s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800858}
859
Josh Gaoef550fe2016-05-17 16:55:06 -0700860static asocket* create_smart_socket(void) {
Yabin Cui815ad882015-09-02 17:44:28 -0700861 D("Creating smart socket");
Josh Gao5cb76ce2018-02-12 17:24:00 -0800862 asocket* s = new asocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800863 s->enqueue = smart_socket_enqueue;
864 s->ready = smart_socket_ready;
Yi Kong86e67182018-07-13 18:15:16 -0700865 s->shutdown = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800866 s->close = smart_socket_close;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800867
Yabin Cui815ad882015-09-02 17:44:28 -0700868 D("SS(%d)", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800869 return s;
870}
871
Josh Gaoef550fe2016-05-17 16:55:06 -0700872void connect_to_smartsocket(asocket* s) {
Yabin Cui815ad882015-09-02 17:44:28 -0700873 D("Connecting to smart socket");
Josh Gaoef550fe2016-05-17 16:55:06 -0700874 asocket* ss = create_smart_socket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800875 s->peer = ss;
876 ss->peer = s;
877 s->ready(s);
878}
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100879
880size_t asocket::get_max_payload() const {
881 size_t max_payload = MAX_PAYLOAD;
882 if (transport) {
883 max_payload = std::min(max_payload, transport->get_max_payload());
884 }
885 if (peer && peer->transport) {
886 max_payload = std::min(max_payload, peer->transport->get_max_payload());
887 }
888 return max_payload;
889}