blob: 28da975f56fe367adad473ff5804d83ca274cdbb [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
Josh Gao74ccdf92019-01-23 15:36:42 -0800362 asocket* s = create_local_socket(std::move(fd));
363 LOG(VERBOSE) << "LS(" << s->id << "): bound to '" << name << "' via " << fd.get();
Benoit Gobyf366b362012-03-16 14:50:07 -0700364
JP Abgrallf91259a2012-03-30 13:19:11 -0700365#if !ADB_HOST
Josh Gaod19b77a2018-12-13 14:21:00 -0800366 if ((name.starts_with("root:") && getuid() != 0 && __android_log_is_debuggable()) ||
367 (name.starts_with("unroot:") && getuid() == 0) || name.starts_with("usb:") ||
368 name.starts_with("tcpip:")) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700369 D("LS(%d): enabling exit_on_close", s->id);
Benoit Gobyf366b362012-03-16 14:50:07 -0700370 s->exit_on_close = 1;
371 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700372#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700373
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 return s;
375}
376
377#if ADB_HOST
Josh Gaob122b172017-08-16 16:57:01 -0700378static asocket* create_host_service_socket(const char* name, const char* serial,
379 TransportId transport_id) {
Josh Gao52bd8522016-05-17 16:55:06 -0700380 asocket* s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381
Josh Gaob122b172017-08-16 16:57:01 -0700382 s = host_service_to_socket(name, serial, transport_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383
Yi Kongaed415c2018-07-13 18:15:16 -0700384 if (s != nullptr) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700385 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386 return s;
387 }
388
389 return s;
390}
391#endif /* ADB_HOST */
392
Josh Gao1ce99572018-03-07 16:52:28 -0800393static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gao52bd8522016-05-17 16:55:06 -0700394 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 -0800395 apacket* p = get_apacket();
396
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 p->msg.command = A_WRTE;
398 p->msg.arg0 = s->peer->id;
399 p->msg.arg1 = s->id;
Josh Gao27cb7dc2018-02-01 13:17:50 -0800400
Josh Gaof571fcb2018-02-05 18:49:10 -0800401 if (data.size() > MAX_PAYLOAD) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800402 put_apacket(p);
403 return -1;
404 }
405
Josh Gaof571fcb2018-02-05 18:49:10 -0800406 p->payload = std::move(data);
407 p->msg.data_length = p->payload.size();
408
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 send_packet(p, s->transport);
410 return 1;
411}
412
Josh Gao52bd8522016-05-17 16:55:06 -0700413static void remote_socket_ready(asocket* s) {
414 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
415 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 p->msg.command = A_OKAY;
417 p->msg.arg0 = s->peer->id;
418 p->msg.arg1 = s->id;
419 send_packet(p, s->transport);
420}
421
Josh Gao52bd8522016-05-17 16:55:06 -0700422static void remote_socket_shutdown(asocket* s) {
423 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
424 s->peer ? s->peer->fd : -1);
425 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 p->msg.command = A_CLSE;
Josh Gao52bd8522016-05-17 16:55:06 -0700427 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100429 }
430 p->msg.arg1 = s->id;
431 send_packet(p, s->transport);
432}
433
Josh Gao52bd8522016-05-17 16:55:06 -0700434static void remote_socket_close(asocket* s) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100435 if (s->peer) {
Yi Kongaed415c2018-07-13 18:15:16 -0700436 s->peer->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700437 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 -0800438 s->peer->close(s->peer);
439 }
Josh Gao52bd8522016-05-17 16:55:06 -0700440 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
441 s->peer ? s->peer->fd : -1);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700442 D("RS(%d): closed", s->id);
Josh Gaoe0361d12018-02-12 17:24:00 -0800443 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444}
445
Yabin Cuifd28f322015-08-27 18:50:04 -0700446// Create a remote socket to exchange packets with a remote service through transport
447// |t|. Where |id| is the socket id of the corresponding service on the other
448// side of the transport (it is allocated by the remote side and _cannot_ be 0).
449// Returns a new non-NULL asocket handle.
Josh Gao52bd8522016-05-17 16:55:06 -0700450asocket* create_remote_socket(unsigned id, atransport* t) {
451 if (id == 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700452 LOG(FATAL) << "invalid remote socket id (0)";
Josh Gao52bd8522016-05-17 16:55:06 -0700453 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800454 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 s->id = id;
456 s->enqueue = remote_socket_enqueue;
457 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100458 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 s->close = remote_socket_close;
460 s->transport = t;
461
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700462 D("RS(%d): created", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 return s;
464}
465
Josh Gaod0fa13e2018-12-20 17:00:13 -0800466void connect_to_remote(asocket* s, std::string_view destination) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700467 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gao52bd8522016-05-17 16:55:06 -0700468 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469
Josh Gaod0fa13e2018-12-20 17:00:13 -0800470 LOG(VERBOSE) << "LS(" << s->id << ": connect(" << destination << ")";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 p->msg.command = A_OPEN;
472 p->msg.arg0 = s->id;
Josh Gaof571fcb2018-02-05 18:49:10 -0800473
Josh Gaod0fa13e2018-12-20 17:00:13 -0800474 // adbd used to expect a null-terminated string.
475 // Keep doing so to maintain backward compatibility.
476 p->payload.resize(destination.size() + 1);
477 memcpy(p->payload.data(), destination.data(), destination.size());
478 p->payload[destination.size()] = '\0';
Josh Gaof571fcb2018-02-05 18:49:10 -0800479 p->msg.data_length = p->payload.size();
480
Elliott Hughes4679a392018-10-19 13:59:44 -0700481 CHECK_LE(p->msg.data_length, s->get_max_payload());
Josh Gaof571fcb2018-02-05 18:49:10 -0800482
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 send_packet(p, s->transport);
484}
485
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486/* this is used by magic sockets to rig local sockets to
487 send the go-ahead message when they connect */
Josh Gao52bd8522016-05-17 16:55:06 -0700488static void local_socket_ready_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700490 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700492 SendOkay(s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 s->ready(s);
494}
495
496/* this is used by magic sockets to rig local sockets to
497 send the failure message if they are closed before
498 connected (to avoid closing them without a status message) */
Josh Gao52bd8522016-05-17 16:55:06 -0700499static void local_socket_close_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700501 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700503 SendFail(s->fd, "closed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 s->close(s);
505}
506
Josh Gao27cb7dc2018-02-01 13:17:50 -0800507static unsigned unhex(const char* s, int len) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 unsigned n = 0, c;
509
Josh Gao52bd8522016-05-17 16:55:06 -0700510 while (len-- > 0) {
511 switch ((c = *s++)) {
512 case '0':
513 case '1':
514 case '2':
515 case '3':
516 case '4':
517 case '5':
518 case '6':
519 case '7':
520 case '8':
521 case '9':
522 c -= '0';
523 break;
524 case 'a':
525 case 'b':
526 case 'c':
527 case 'd':
528 case 'e':
529 case 'f':
530 c = c - 'a' + 10;
531 break;
532 case 'A':
533 case 'B':
534 case 'C':
535 case 'D':
536 case 'E':
537 case 'F':
538 c = c - 'A' + 10;
539 break;
540 default:
541 return 0xffffffff;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 }
543
544 n = (n << 4) | c;
545 }
546
547 return n;
548}
549
Elliott Hughese67f1f82015-04-30 17:32:03 -0700550#if ADB_HOST
551
David Pursell3f902aa2016-03-01 08:58:26 -0800552namespace internal {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700553
Josh Gaobd767202018-12-19 13:37:41 -0800554// Parses a host service string of the following format:
David Pursell3f902aa2016-03-01 08:58:26 -0800555// * [tcp:|udp:]<serial>[:<port>]:<command>
556// * <prefix>:<serial>:<command>
557// 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 -0800558bool parse_host_service(std::string_view* out_serial, std::string_view* out_command,
559 std::string_view full_service) {
560 if (full_service.empty()) {
561 return false;
562 }
Terence Haddock28e13902011-03-16 09:43:56 +0100563
Josh Gaobd767202018-12-19 13:37:41 -0800564 std::string_view serial;
565 std::string_view command = full_service;
566 // Remove |count| bytes from the beginning of command and add them to |serial|.
567 auto consume = [&full_service, &serial, &command](size_t count) {
568 CHECK_LE(count, command.size());
569 if (!serial.empty()) {
570 CHECK_EQ(serial.data() + serial.size(), command.data());
571 }
572
573 serial = full_service.substr(0, serial.size() + count);
574 command.remove_prefix(count);
575 };
576
577 // Remove the trailing : from serial, and assign the values to the output parameters.
578 auto finish = [out_serial, out_command, &serial, &command] {
579 if (serial.empty() || command.empty()) {
580 return false;
581 }
582
583 CHECK_EQ(':', serial.back());
584 serial.remove_suffix(1);
585
586 *out_serial = serial;
587 *out_command = command;
588 return true;
589 };
590
591 static constexpr std::string_view prefixes[] = {"usb:", "product:", "model:", "device:"};
592 for (std::string_view prefix : prefixes) {
593 if (command.starts_with(prefix)) {
594 consume(prefix.size());
595
596 size_t offset = command.find_first_of(':');
597 if (offset == std::string::npos) {
598 return false;
599 }
600 consume(offset + 1);
601 return finish();
David Pursell3f902aa2016-03-01 08:58:26 -0800602 }
Scott Anderson3608d832012-05-31 12:04:23 -0700603 }
604
David Pursell3f902aa2016-03-01 08:58:26 -0800605 // For fastboot compatibility, ignore protocol prefixes.
Josh Gaobd767202018-12-19 13:37:41 -0800606 if (command.starts_with("tcp:") || command.starts_with("udp:")) {
607 consume(4);
608 if (command.empty()) {
609 return false;
David Pursell73d55aa2016-09-21 12:08:37 -0700610 }
611 }
612
Josh Gaobd767202018-12-19 13:37:41 -0800613 bool found_address = false;
614 if (command[0] == '[') {
615 // Read an IPv6 address. `adb connect` creates the serial number from the canonical
616 // network address so it will always have the [] delimiters.
617 size_t ipv6_end = command.find_first_of(']');
618 if (ipv6_end != std::string::npos) {
619 consume(ipv6_end + 1);
620 if (command.empty()) {
621 // Nothing after the IPv6 address.
622 return false;
623 } else if (command[0] != ':') {
624 // Garbage after the IPv6 address.
625 return false;
626 }
627 consume(1);
628 found_address = true;
629 }
Terence Haddock28e13902011-03-16 09:43:56 +0100630 }
David Pursell3f902aa2016-03-01 08:58:26 -0800631
Josh Gaobd767202018-12-19 13:37:41 -0800632 if (!found_address) {
633 // Scan ahead to the next colon.
634 size_t offset = command.find_first_of(':');
635 if (offset == std::string::npos) {
636 return false;
Terence Haddock28e13902011-03-16 09:43:56 +0100637 }
Josh Gaobd767202018-12-19 13:37:41 -0800638 consume(offset + 1);
639 }
640
641 // We're either at the beginning of a port, or the command itself.
642 // Look for a port in between colons.
643 size_t next_colon = command.find_first_of(':');
644 if (next_colon == std::string::npos) {
645 // No colon, we must be at the command.
646 return finish();
647 }
648
649 bool port_valid = true;
650 if (command.size() <= next_colon) {
651 return false;
652 }
653
654 std::string_view port = command.substr(0, next_colon);
655 for (auto digit : port) {
656 if (!isdigit(digit)) {
657 // Port isn't a number.
658 port_valid = false;
659 break;
Terence Haddock28e13902011-03-16 09:43:56 +0100660 }
661 }
Josh Gaobd767202018-12-19 13:37:41 -0800662
663 if (port_valid) {
664 consume(next_colon + 1);
665 }
666 return finish();
Terence Haddock28e13902011-03-16 09:43:56 +0100667}
668
David Pursell3f902aa2016-03-01 08:58:26 -0800669} // namespace internal
670
Josh Gao52bd8522016-05-17 16:55:06 -0700671#endif // ADB_HOST
Elliott Hughese67f1f82015-04-30 17:32:03 -0700672
Josh Gao1ce99572018-03-07 16:52:28 -0800673static int smart_socket_enqueue(asocket* s, apacket::payload_type data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800674#if ADB_HOST
Josh Gaobd767202018-12-19 13:37:41 -0800675 std::string_view service;
676 std::string_view serial;
Josh Gaob122b172017-08-16 16:57:01 -0700677 TransportId transport_id = 0;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700678 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679#endif
680
Josh Gao27cb7dc2018-02-01 13:17:50 -0800681 D("SS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682
Josh Gao27cb7dc2018-02-01 13:17:50 -0800683 if (s->smart_socket_data.empty()) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700684 // TODO: Make this an IOVector?
Josh Gao1ce99572018-03-07 16:52:28 -0800685 s->smart_socket_data.assign(data.begin(), data.end());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 } else {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800687 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 }
689
Josh Gao7e6683c2016-01-15 14:35:54 -0800690 /* don't bother if we can't decode the length */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800691 if (s->smart_socket_data.size() < 4) {
Josh Gao52bd8522016-05-17 16:55:06 -0700692 return 0;
693 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694
Josh Gao27cb7dc2018-02-01 13:17:50 -0800695 uint32_t len = unhex(s->smart_socket_data.data(), 4);
696 if (len == 0 || len > MAX_PAYLOAD) {
697 D("SS(%d): bad size (%u)", s->id, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698 goto fail;
699 }
700
Josh Gao27cb7dc2018-02-01 13:17:50 -0800701 D("SS(%d): len is %u", s->id, len);
Josh Gao7e6683c2016-01-15 14:35:54 -0800702 /* can't do anything until we have the full header */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800703 if ((len + 4) > s->smart_socket_data.size()) {
704 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 -0800705 return 0;
706 }
707
Josh Gao27cb7dc2018-02-01 13:17:50 -0800708 s->smart_socket_data[len + 4] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709
Josh Gao27cb7dc2018-02-01 13:17:50 -0800710 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711
712#if ADB_HOST
Josh Gaobd767202018-12-19 13:37:41 -0800713 service = std::string_view(s->smart_socket_data).substr(4);
714 if (service.starts_with("host-serial:")) {
715 service.remove_prefix(strlen("host-serial:"));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716
Terence Haddock28e13902011-03-16 09:43:56 +0100717 // serial number should follow "host:" and could be a host:port string.
Josh Gaobd767202018-12-19 13:37:41 -0800718 if (!internal::parse_host_service(&serial, &service, service)) {
719 LOG(ERROR) << "SS(" << s->id << "): failed to parse host service: " << service;
720 goto fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721 }
Josh Gaobd767202018-12-19 13:37:41 -0800722 } else if (service.starts_with("host-transport-id:")) {
723 service.remove_prefix(strlen("host-transport-id:"));
724 if (!ParseUint(&transport_id, service, &service)) {
725 LOG(ERROR) << "SS(" << s->id << "): failed to parse host transport id: " << service;
Josh Gaob122b172017-08-16 16:57:01 -0700726 return -1;
727 }
Josh Gaobd767202018-12-19 13:37:41 -0800728 if (!service.starts_with(":")) {
729 LOG(ERROR) << "SS(" << s->id << "): host-transport-id without command";
730 return -1;
731 }
732 service.remove_prefix(1);
733 } else if (service.starts_with("host-usb:")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700734 type = kTransportUsb;
Josh Gaobd767202018-12-19 13:37:41 -0800735 service.remove_prefix(strlen("host-usb:"));
736 } else if (service.starts_with("host-local:")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700737 type = kTransportLocal;
Josh Gaobd767202018-12-19 13:37:41 -0800738 service.remove_prefix(strlen("host-local:"));
739 } else if (service.starts_with("host:")) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700740 type = kTransportAny;
Josh Gaobd767202018-12-19 13:37:41 -0800741 service.remove_prefix(strlen("host:"));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800742 } else {
Josh Gaobd767202018-12-19 13:37:41 -0800743 service = std::string_view{};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800744 }
745
Josh Gaobd767202018-12-19 13:37:41 -0800746 if (!service.empty()) {
Josh Gao52bd8522016-05-17 16:55:06 -0700747 asocket* s2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748
Josh Gao40390512018-08-07 14:14:21 -0700749 // Some requests are handled immediately -- in that case the handle_host_request() routine
750 // has sent the OKAY or FAIL message and all we have to do is clean up.
Josh Gaobd767202018-12-19 13:37:41 -0800751 // TODO: Convert to string_view.
752 if (handle_host_request(std::string(service).c_str(), type,
753 serial.empty() ? nullptr : std::string(serial).c_str(),
754 transport_id, s->peer->fd, s)) {
755 LOG(VERBOSE) << "SS(" << s->id << "): handled host service '" << service << "'";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756 goto fail;
757 }
Josh Gaobd767202018-12-19 13:37:41 -0800758 if (service.starts_with("transport")) {
Josh Gao52bd8522016-05-17 16:55:06 -0700759 D("SS(%d): okay transport", s->id);
Josh Gao27cb7dc2018-02-01 13:17:50 -0800760 s->smart_socket_data.clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800761 return 0;
762 }
763
Josh Gao52bd8522016-05-17 16:55:06 -0700764 /* try to find a local service with this name.
765 ** if no such service exists, we'll fail out
766 ** and tear down here.
767 */
Josh Gaobd767202018-12-19 13:37:41 -0800768 // TODO: Convert to string_view.
769 s2 = create_host_service_socket(std::string(service).c_str(), std::string(serial).c_str(),
770 transport_id);
Yi Kongaed415c2018-07-13 18:15:16 -0700771 if (s2 == nullptr) {
Josh Gaobd767202018-12-19 13:37:41 -0800772 LOG(VERBOSE) << "SS(" << s->id << "): couldn't create host service '" << service << "'";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700773 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800774 goto fail;
775 }
776
Josh Gao52bd8522016-05-17 16:55:06 -0700777 /* we've connected to a local host service,
778 ** so we make our peer back into a regular
779 ** local socket and bind it to the new local
780 ** service socket, acknowledge the successful
781 ** connection, and close this smart socket now
782 ** that its work is done.
783 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700784 SendOkay(s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800785
786 s->peer->ready = local_socket_ready;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700787 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800788 s->peer->close = local_socket_close;
789 s->peer->peer = s2;
790 s2->peer = s->peer;
Yi Kongaed415c2018-07-13 18:15:16 -0700791 s->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700792 D("SS(%d): okay", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800793 s->close(s);
794
Josh Gao52bd8522016-05-17 16:55:06 -0700795 /* initial state is "ready" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796 s2->ready(s2);
797 return 0;
798 }
799#else /* !ADB_HOST */
Elliott Hughes8d28e192015-10-07 14:55:10 -0700800 if (s->transport == nullptr) {
Elliott Hughes7be29c82015-04-16 22:54:44 -0700801 std::string error_msg = "unknown failure";
Josh Gaob122b172017-08-16 16:57:01 -0700802 s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700803 if (s->transport == nullptr) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700804 SendFail(s->peer->fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805 goto fail;
806 }
807 }
808#endif
809
Josh Gao22d2b3e2016-10-27 14:01:08 -0700810 if (!s->transport) {
811 SendFail(s->peer->fd, "device offline (no transport)");
812 goto fail;
Josh Gao704494b2018-05-04 16:04:49 -0700813 } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) {
Josh Gao52bd8522016-05-17 16:55:06 -0700814 /* if there's no remote we fail the connection
815 ** right here and terminate it
816 */
Josh Gao22d2b3e2016-10-27 14:01:08 -0700817 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800818 goto fail;
819 }
820
Josh Gao52bd8522016-05-17 16:55:06 -0700821 /* instrument our peer to pass the success or fail
822 ** message back once it connects or closes, then
823 ** detach from it, request the connection, and
824 ** tear down
825 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800826 s->peer->ready = local_socket_ready_notify;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700827 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800828 s->peer->close = local_socket_close_notify;
Yi Kongaed415c2018-07-13 18:15:16 -0700829 s->peer->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700830 /* give him our transport and upref it */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800831 s->peer->transport = s->transport;
832
Josh Gaod0fa13e2018-12-20 17:00:13 -0800833 connect_to_remote(s->peer, std::string_view(s->smart_socket_data).substr(4));
Yi Kongaed415c2018-07-13 18:15:16 -0700834 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800835 s->close(s);
836 return 1;
837
838fail:
Josh Gao52bd8522016-05-17 16:55:06 -0700839 /* we're going to close our peer as a side-effect, so
840 ** return -1 to signal that state to the local socket
841 ** who is enqueueing against us
842 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800843 s->close(s);
844 return -1;
845}
846
Josh Gao52bd8522016-05-17 16:55:06 -0700847static void smart_socket_ready(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700848 D("SS(%d): ready", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800849}
850
Josh Gao52bd8522016-05-17 16:55:06 -0700851static void smart_socket_close(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700852 D("SS(%d): closed", s->id);
Josh Gao52bd8522016-05-17 16:55:06 -0700853 if (s->peer) {
Yi Kongaed415c2018-07-13 18:15:16 -0700854 s->peer->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800855 s->peer->close(s->peer);
Yi Kongaed415c2018-07-13 18:15:16 -0700856 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800857 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800858 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800859}
860
Josh Gao52bd8522016-05-17 16:55:06 -0700861static asocket* create_smart_socket(void) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700862 D("Creating smart socket");
Josh Gaoe0361d12018-02-12 17:24:00 -0800863 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800864 s->enqueue = smart_socket_enqueue;
865 s->ready = smart_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700866 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800867 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800868
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700869 D("SS(%d)", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800870 return s;
871}
872
Josh Gao52bd8522016-05-17 16:55:06 -0700873void connect_to_smartsocket(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700874 D("Connecting to smart socket");
Josh Gao52bd8522016-05-17 16:55:06 -0700875 asocket* ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800876 s->peer = ss;
877 ss->peer = s;
878 s->ready(s);
879}
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100880
881size_t asocket::get_max_payload() const {
882 size_t max_payload = MAX_PAYLOAD;
883 if (transport) {
884 max_payload = std::min(max_payload, transport->get_max_payload());
885 }
886 if (peer && peer->transport) {
887 max_payload = std::min(max_payload, peer->transport->get_max_payload());
888 }
889 return max_payload;
890}