blob: cb8cd16ffd19b5842d39f92e1c2d44844d41810a [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"
Dan Albert76649012015-02-24 15:51:19 -080040#include "transport.h"
Josh Gao1ce99572018-03-07 16:52:28 -080041#include "types.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Josh Gao9b587de2016-05-17 17:46:27 -070043static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044static unsigned local_socket_next_id = 1;
45
Josh Gao5e507642018-01-31 13:15:51 -080046static auto& local_socket_list = *new std::vector<asocket*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
48/* the the list of currently closing local sockets.
49** these have no peer anymore, but still packets to
50** write to their fd.
51*/
Josh Gao5e507642018-01-31 13:15:51 -080052static auto& local_socket_closing_list = *new std::vector<asocket*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
David 'Digit' Turner818d6412013-12-13 14:09:44 +010054// Parse the global list of sockets to find one with id |local_id|.
55// If |peer_id| is not 0, also check that it is connected to a peer
56// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
Josh Gao52bd8522016-05-17 16:55:06 -070057asocket* find_local_socket(unsigned local_id, unsigned peer_id) {
Josh Gao5e507642018-01-31 13:15:51 -080058 asocket* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
Josh Gao9b587de2016-05-17 17:46:27 -070060 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao5e507642018-01-31 13:15:51 -080061 for (asocket* s : local_socket_list) {
Josh Gao52bd8522016-05-17 16:55:06 -070062 if (s->id != local_id) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +010063 continue;
Josh Gao52bd8522016-05-17 16:55:06 -070064 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010065 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa81828292010-06-12 11:40:20 -030066 result = s;
André Goddard Rosa81828292010-06-12 11:40:20 -030067 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010068 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
71 return result;
72}
73
Josh Gao52bd8522016-05-17 16:55:06 -070074void install_local_socket(asocket* s) {
Josh Gao9b587de2016-05-17 17:46:27 -070075 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
77 s->id = local_socket_next_id++;
David 'Digit' Turner818d6412013-12-13 14:09:44 +010078
79 // Socket ids should never be 0.
Josh Gao52bd8522016-05-17 16:55:06 -070080 if (local_socket_next_id == 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -070081 LOG(FATAL) << "local socket id overflow";
Josh Gao52bd8522016-05-17 16:55:06 -070082 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010083
Josh Gao5e507642018-01-31 13:15:51 -080084 local_socket_list.push_back(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085}
86
Josh Gao52bd8522016-05-17 16:55:06 -070087void remove_socket(asocket* s) {
Josh Gao62c92f02017-09-13 11:17:33 -070088 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao5e507642018-01-31 13:15:51 -080089 for (auto list : { &local_socket_list, &local_socket_closing_list }) {
90 list->erase(std::remove_if(list->begin(), list->end(), [s](asocket* x) { return x == s; }),
91 list->end());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092 }
93}
94
Josh Gao52bd8522016-05-17 16:55:06 -070095void close_all_sockets(atransport* t) {
Josh Gao52bd8522016-05-17 16:55:06 -070096 /* this is a little gross, but since s->close() *will* modify
97 ** the list out from under you, your options are limited.
98 */
Josh Gao9b587de2016-05-17 17:46:27 -070099 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100restart:
Josh Gao5e507642018-01-31 13:15:51 -0800101 for (asocket* s : local_socket_list) {
Josh Gao52bd8522016-05-17 16:55:06 -0700102 if (s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gao53eb31d2016-05-18 10:39:48 -0700103 s->close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 goto restart;
105 }
106 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107}
108
Josh Gao184f4802018-03-19 13:20:29 -0700109enum class SocketFlushResult {
110 Destroyed,
111 TryAgain,
112 Completed,
113};
114
115static SocketFlushResult local_socket_flush_incoming(asocket* s) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700116 if (!s->packet_queue.empty()) {
117 std::vector<adb_iovec> iov = s->packet_queue.iovecs();
118 ssize_t rc = adb_writev(s->fd, iov.data(), iov.size());
119 if (rc > 0 && static_cast<size_t>(rc) == s->packet_queue.size()) {
120 s->packet_queue.clear();
Josh Gao184f4802018-03-19 13:20:29 -0700121 } else if (rc > 0) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700122 // TODO: Implement a faster drop_front?
123 s->packet_queue.take_front(rc);
Josh Gao71f775a2018-05-14 11:14:33 -0700124 fdevent_add(s->fde, FDE_WRITE);
Josh Gao184f4802018-03-19 13:20:29 -0700125 return SocketFlushResult::TryAgain;
126 } else if (rc == -1 && errno == EAGAIN) {
Josh Gao71f775a2018-05-14 11:14:33 -0700127 fdevent_add(s->fde, FDE_WRITE);
Josh Gao184f4802018-03-19 13:20:29 -0700128 return SocketFlushResult::TryAgain;
Josh Gao954e1282018-03-30 13:56:24 -0700129 } else {
130 // We failed to write, but it's possible that we can still read from the socket.
131 // Give that a try before giving up.
132 s->has_write_error = true;
Josh Gao184f4802018-03-19 13:20:29 -0700133 }
Josh Gao184f4802018-03-19 13:20:29 -0700134 }
135
136 // If we sent the last packet of a closing socket, we can now destroy it.
137 if (s->closing) {
138 s->close(s);
139 return SocketFlushResult::Destroyed;
140 }
141
Josh Gao71f775a2018-05-14 11:14:33 -0700142 fdevent_del(s->fde, FDE_WRITE);
Josh Gao184f4802018-03-19 13:20:29 -0700143 return SocketFlushResult::Completed;
144}
145
146// Returns false if the socket has been closed and destroyed as a side-effect of this function.
147static bool local_socket_flush_outgoing(asocket* s) {
148 const size_t max_payload = s->get_max_payload();
Josh Gao1ce99572018-03-07 16:52:28 -0800149 apacket::payload_type data;
Josh Gao184f4802018-03-19 13:20:29 -0700150 data.resize(max_payload);
151 char* x = &data[0];
152 size_t avail = max_payload;
153 int r = 0;
154 int is_eof = 0;
155
156 while (avail > 0) {
157 r = adb_read(s->fd, x, avail);
158 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r,
159 r < 0 ? errno : 0, avail);
160 if (r == -1) {
161 if (errno == EAGAIN) {
162 break;
163 }
164 } else if (r > 0) {
165 avail -= r;
166 x += r;
167 continue;
168 }
169
170 /* r = 0 or unhandled error */
171 is_eof = 1;
172 break;
173 }
174 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 -0700175 s->fde->force_eof);
Josh Gao184f4802018-03-19 13:20:29 -0700176
177 if (avail != max_payload && s->peer) {
178 data.resize(max_payload - avail);
179
180 // s->peer->enqueue() may call s->close() and free s,
181 // so save variables for debug printing below.
182 unsigned saved_id = s->id;
183 int saved_fd = s->fd;
184 r = s->peer->enqueue(s->peer, std::move(data));
185 D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
186
187 if (r < 0) {
188 // Error return means they closed us as a side-effect and we must
189 // return immediately.
190 //
191 // Note that if we still have buffered packets, the socket will be
192 // placed on the closing socket list. This handler function will be
193 // called again to process FDE_WRITE events.
194 return false;
195 }
196
197 if (r > 0) {
198 /* if the remote cannot accept further events,
199 ** we disable notification of READs. They'll
200 ** be enabled again when we get a call to ready()
201 */
Josh Gao71f775a2018-05-14 11:14:33 -0700202 fdevent_del(s->fde, FDE_READ);
Josh Gao184f4802018-03-19 13:20:29 -0700203 }
204 }
205
206 // Don't allow a forced eof if data is still there.
Josh Gao71f775a2018-05-14 11:14:33 -0700207 if ((s->fde->force_eof && !r) || is_eof) {
208 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 -0700209 s->close(s);
210 return false;
211 }
212
213 return true;
214}
215
Josh Gao1ce99572018-03-07 16:52:28 -0800216static int local_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800217 D("LS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218
Josh Gao7c738cd2018-04-03 14:37:11 -0700219 s->packet_queue.append(std::move(data));
Josh Gao184f4802018-03-19 13:20:29 -0700220 switch (local_socket_flush_incoming(s)) {
221 case SocketFlushResult::Destroyed:
222 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223
Josh Gao184f4802018-03-19 13:20:29 -0700224 case SocketFlushResult::TryAgain:
225 return 1;
226
227 case SocketFlushResult::Completed:
228 return 0;
229 }
230
231 return !s->packet_queue.empty();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232}
233
Josh Gao52bd8522016-05-17 16:55:06 -0700234static void local_socket_ready(asocket* s) {
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100235 /* far side is ready for data, pay attention to
236 readable events */
Josh Gao71f775a2018-05-14 11:14:33 -0700237 fdevent_add(s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238}
239
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240// be sure to hold the socket list lock when calling this
Josh Gao52bd8522016-05-17 16:55:06 -0700241static void local_socket_destroy(asocket* s) {
Benoit Gobyf366b362012-03-16 14:50:07 -0700242 int exit_on_close = s->exit_on_close;
243
Josh Gao71f775a2018-05-14 11:14:33 -0700244 D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245
Josh Gaofaf13282018-10-12 05:08:45 +0000246 /* IMPORTANT: the remove closes the fd
247 ** that belongs to this socket
248 */
249 fdevent_destroy(s->fde);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 remove_socket(s);
Josh Gaoe0361d12018-02-12 17:24:00 -0800252 delete s;
Benoit Gobyf366b362012-03-16 14:50:07 -0700253
254 if (exit_on_close) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700255 D("local_socket_destroy: exiting");
Benoit Gobyf366b362012-03-16 14:50:07 -0700256 exit(1);
257 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258}
259
Josh Gao9b587de2016-05-17 17:46:27 -0700260static void local_socket_close(asocket* s) {
261 D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd);
262 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao52bd8522016-05-17 16:55:06 -0700263 if (s->peer) {
264 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 +0100265 /* Note: it's important to call shutdown before disconnecting from
266 * the peer, this ensures that remote sockets can still get the id
267 * of the local socket they're connected to, to send a CLOSE()
268 * protocol event. */
Josh Gao52bd8522016-05-17 16:55:06 -0700269 if (s->peer->shutdown) {
270 s->peer->shutdown(s->peer);
271 }
Josh Gao9b587de2016-05-17 17:46:27 -0700272 s->peer->peer = nullptr;
273 s->peer->close(s->peer);
274 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 }
276
Josh Gao52bd8522016-05-17 16:55:06 -0700277 /* If we are already closing, or if there are no
278 ** pending packets, destroy immediately
279 */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800280 if (s->closing || s->has_write_error || s->packet_queue.empty()) {
Josh Gao52bd8522016-05-17 16:55:06 -0700281 int id = s->id;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 local_socket_destroy(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700283 D("LS(%d): closed", id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 return;
285 }
286
Josh Gao52bd8522016-05-17 16:55:06 -0700287 /* otherwise, put on the closing list
288 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700289 D("LS(%d): closing", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290 s->closing = 1;
Josh Gao71f775a2018-05-14 11:14:33 -0700291 fdevent_del(s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 remove_socket(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700293 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
Josh Gao5e507642018-01-31 13:15:51 -0800294 local_socket_closing_list.push_back(s);
Josh Gao71f775a2018-05-14 11:14:33 -0700295 CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296}
297
Josh Gao52bd8522016-05-17 16:55:06 -0700298static void local_socket_event_func(int fd, unsigned ev, void* _s) {
Dan Albertbac34742015-02-25 17:51:28 -0800299 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700300 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall408fa572011-03-16 15:57:42 -0700301
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 /* put the FDE_WRITE processing before the FDE_READ
303 ** in order to simplify the code.
304 */
Dan Albertbac34742015-02-25 17:51:28 -0800305 if (ev & FDE_WRITE) {
Josh Gao184f4802018-03-19 13:20:29 -0700306 switch (local_socket_flush_incoming(s)) {
307 case SocketFlushResult::Destroyed:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309
Josh Gao184f4802018-03-19 13:20:29 -0700310 case SocketFlushResult::TryAgain:
311 break;
312
313 case SocketFlushResult::Completed:
314 s->peer->ready(s->peer);
315 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 }
318
Dan Albertbac34742015-02-25 17:51:28 -0800319 if (ev & FDE_READ) {
Josh Gao184f4802018-03-19 13:20:29 -0700320 if (!local_socket_flush_outgoing(s)) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700321 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 }
323 }
324
Josh Gao52bd8522016-05-17 16:55:06 -0700325 if (ev & FDE_ERROR) {
326 /* this should be caught be the next read or write
327 ** catching it here means we may skip the last few
328 ** bytes of readable data.
329 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700330 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 return;
332 }
333}
334
Josh Gao52bd8522016-05-17 16:55:06 -0700335asocket* create_local_socket(int fd) {
Josh Gaoe0361d12018-02-12 17:24:00 -0800336 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 s->fd = fd;
338 s->enqueue = local_socket_enqueue;
339 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700340 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700342 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343
Josh Gao71f775a2018-05-14 11:14:33 -0700344 s->fde = fdevent_create(fd, local_socket_event_func, s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700345 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 return s;
347}
348
Josh Gaod19b77a2018-12-13 14:21:00 -0800349asocket* create_local_service_socket(std::string_view name, atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350#if !ADB_HOST
Josh Gao6eb78822018-11-16 15:40:16 -0800351 if (asocket* s = daemon_service_to_socket(name); s) {
352 return s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 }
354#endif
David Pursell0955c662015-08-31 10:42:13 -0700355 int fd = service_to_fd(name, transport);
Josh Gao52bd8522016-05-17 16:55:06 -0700356 if (fd < 0) {
Elliott Hughesffc73a32016-06-15 14:46:56 -0700357 return nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700358 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359
Dan Pasanen98858812014-10-06 12:57:20 -0500360 asocket* s = create_local_socket(fd);
Josh Gaod19b77a2018-12-13 14:21:00 -0800361 LOG(VERBOSE) << "LS(" << s->id << "): bound to '" << name << "' via " << fd;
Benoit Gobyf366b362012-03-16 14:50:07 -0700362
JP Abgrallf91259a2012-03-30 13:19:11 -0700363#if !ADB_HOST
Josh Gaod19b77a2018-12-13 14:21:00 -0800364 if ((name.starts_with("root:") && getuid() != 0 && __android_log_is_debuggable()) ||
365 (name.starts_with("unroot:") && getuid() == 0) || name.starts_with("usb:") ||
366 name.starts_with("tcpip:")) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700367 D("LS(%d): enabling exit_on_close", s->id);
Benoit Gobyf366b362012-03-16 14:50:07 -0700368 s->exit_on_close = 1;
369 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700370#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700371
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 return s;
373}
374
375#if ADB_HOST
Josh Gaob122b172017-08-16 16:57:01 -0700376static asocket* create_host_service_socket(const char* name, const char* serial,
377 TransportId transport_id) {
Josh Gao52bd8522016-05-17 16:55:06 -0700378 asocket* s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379
Josh Gaob122b172017-08-16 16:57:01 -0700380 s = host_service_to_socket(name, serial, transport_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381
Yi Kongaed415c2018-07-13 18:15:16 -0700382 if (s != nullptr) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700383 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 return s;
385 }
386
387 return s;
388}
389#endif /* ADB_HOST */
390
Josh Gao1ce99572018-03-07 16:52:28 -0800391static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gao52bd8522016-05-17 16:55:06 -0700392 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 -0800393 apacket* p = get_apacket();
394
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 p->msg.command = A_WRTE;
396 p->msg.arg0 = s->peer->id;
397 p->msg.arg1 = s->id;
Josh Gao27cb7dc2018-02-01 13:17:50 -0800398
Josh Gaof571fcb2018-02-05 18:49:10 -0800399 if (data.size() > MAX_PAYLOAD) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800400 put_apacket(p);
401 return -1;
402 }
403
Josh Gaof571fcb2018-02-05 18:49:10 -0800404 p->payload = std::move(data);
405 p->msg.data_length = p->payload.size();
406
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 send_packet(p, s->transport);
408 return 1;
409}
410
Josh Gao52bd8522016-05-17 16:55:06 -0700411static void remote_socket_ready(asocket* s) {
412 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
413 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 p->msg.command = A_OKAY;
415 p->msg.arg0 = s->peer->id;
416 p->msg.arg1 = s->id;
417 send_packet(p, s->transport);
418}
419
Josh Gao52bd8522016-05-17 16:55:06 -0700420static void remote_socket_shutdown(asocket* s) {
421 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
422 s->peer ? s->peer->fd : -1);
423 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 p->msg.command = A_CLSE;
Josh Gao52bd8522016-05-17 16:55:06 -0700425 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100427 }
428 p->msg.arg1 = s->id;
429 send_packet(p, s->transport);
430}
431
Josh Gao52bd8522016-05-17 16:55:06 -0700432static void remote_socket_close(asocket* s) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100433 if (s->peer) {
Yi Kongaed415c2018-07-13 18:15:16 -0700434 s->peer->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700435 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 -0800436 s->peer->close(s->peer);
437 }
Josh Gao52bd8522016-05-17 16:55:06 -0700438 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
439 s->peer ? s->peer->fd : -1);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700440 D("RS(%d): closed", s->id);
Josh Gaoe0361d12018-02-12 17:24:00 -0800441 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442}
443
Yabin Cuifd28f322015-08-27 18:50:04 -0700444// Create a remote socket to exchange packets with a remote service through transport
445// |t|. Where |id| is the socket id of the corresponding service on the other
446// side of the transport (it is allocated by the remote side and _cannot_ be 0).
447// Returns a new non-NULL asocket handle.
Josh Gao52bd8522016-05-17 16:55:06 -0700448asocket* create_remote_socket(unsigned id, atransport* t) {
449 if (id == 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700450 LOG(FATAL) << "invalid remote socket id (0)";
Josh Gao52bd8522016-05-17 16:55:06 -0700451 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800452 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 s->id = id;
454 s->enqueue = remote_socket_enqueue;
455 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100456 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 s->close = remote_socket_close;
458 s->transport = t;
459
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700460 D("RS(%d): created", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 return s;
462}
463
Josh Gao52bd8522016-05-17 16:55:06 -0700464void connect_to_remote(asocket* s, const char* destination) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700465 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gao52bd8522016-05-17 16:55:06 -0700466 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700468 D("LS(%d): connect('%s')", s->id, destination);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 p->msg.command = A_OPEN;
470 p->msg.arg0 = s->id;
Josh Gaof571fcb2018-02-05 18:49:10 -0800471
472 // adbd expects a null-terminated string.
Josh Gao1ce99572018-03-07 16:52:28 -0800473 p->payload.assign(destination, destination + strlen(destination) + 1);
Josh Gaof571fcb2018-02-05 18:49:10 -0800474 p->msg.data_length = p->payload.size();
475
Elliott Hughes4679a392018-10-19 13:59:44 -0700476 CHECK_LE(p->msg.data_length, s->get_max_payload());
Josh Gaof571fcb2018-02-05 18:49:10 -0800477
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 send_packet(p, s->transport);
479}
480
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481/* this is used by magic sockets to rig local sockets to
482 send the go-ahead message when they connect */
Josh Gao52bd8522016-05-17 16:55:06 -0700483static void local_socket_ready_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700485 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700487 SendOkay(s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488 s->ready(s);
489}
490
491/* this is used by magic sockets to rig local sockets to
492 send the failure message if they are closed before
493 connected (to avoid closing them without a status message) */
Josh Gao52bd8522016-05-17 16:55:06 -0700494static void local_socket_close_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700496 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700498 SendFail(s->fd, "closed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 s->close(s);
500}
501
Josh Gao27cb7dc2018-02-01 13:17:50 -0800502static unsigned unhex(const char* s, int len) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 unsigned n = 0, c;
504
Josh Gao52bd8522016-05-17 16:55:06 -0700505 while (len-- > 0) {
506 switch ((c = *s++)) {
507 case '0':
508 case '1':
509 case '2':
510 case '3':
511 case '4':
512 case '5':
513 case '6':
514 case '7':
515 case '8':
516 case '9':
517 c -= '0';
518 break;
519 case 'a':
520 case 'b':
521 case 'c':
522 case 'd':
523 case 'e':
524 case 'f':
525 c = c - 'a' + 10;
526 break;
527 case 'A':
528 case 'B':
529 case 'C':
530 case 'D':
531 case 'E':
532 case 'F':
533 c = c - 'A' + 10;
534 break;
535 default:
536 return 0xffffffff;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537 }
538
539 n = (n << 4) | c;
540 }
541
542 return n;
543}
544
Elliott Hughese67f1f82015-04-30 17:32:03 -0700545#if ADB_HOST
546
David Pursell3f902aa2016-03-01 08:58:26 -0800547namespace internal {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700548
David Pursell3f902aa2016-03-01 08:58:26 -0800549// Returns the position in |service| following the target serial parameter. Serial format can be
550// any of:
551// * [tcp:|udp:]<serial>[:<port>]:<command>
552// * <prefix>:<serial>:<command>
553// Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}.
554//
555// The returned pointer will point to the ':' just before <command>, or nullptr if not found.
Dan Austinb4cff492016-03-28 15:32:37 -0700556char* skip_host_serial(char* service) {
David Pursell3f902aa2016-03-01 08:58:26 -0800557 static const std::vector<std::string>& prefixes =
558 *(new std::vector<std::string>{"usb:", "product:", "model:", "device:"});
Terence Haddock28e13902011-03-16 09:43:56 +0100559
David Pursell3f902aa2016-03-01 08:58:26 -0800560 for (const std::string& prefix : prefixes) {
561 if (!strncmp(service, prefix.c_str(), prefix.length())) {
562 return strchr(service + prefix.length(), ':');
563 }
Scott Anderson3608d832012-05-31 12:04:23 -0700564 }
565
David Pursell3f902aa2016-03-01 08:58:26 -0800566 // For fastboot compatibility, ignore protocol prefixes.
567 if (!strncmp(service, "tcp:", 4) || !strncmp(service, "udp:", 4)) {
568 service += 4;
569 }
570
David Pursell73d55aa2016-09-21 12:08:37 -0700571 // Check for an IPv6 address. `adb connect` creates the serial number from the canonical
572 // network address so it will always have the [] delimiters.
573 if (service[0] == '[') {
574 char* ipv6_end = strchr(service, ']');
575 if (ipv6_end != nullptr) {
576 service = ipv6_end;
577 }
578 }
579
580 // The next colon we find must either begin the port field or the command field.
581 char* colon_ptr = strchr(service, ':');
582 if (!colon_ptr) {
David Pursell3f902aa2016-03-01 08:58:26 -0800583 // No colon in service string.
584 return nullptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100585 }
David Pursell3f902aa2016-03-01 08:58:26 -0800586
David Pursell73d55aa2016-09-21 12:08:37 -0700587 // If the next field is only decimal digits and ends with another colon, it's a port.
588 char* serial_end = colon_ptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100589 if (isdigit(serial_end[1])) {
590 serial_end++;
David Pursell3f902aa2016-03-01 08:58:26 -0800591 while (*serial_end && isdigit(*serial_end)) {
Terence Haddock28e13902011-03-16 09:43:56 +0100592 serial_end++;
593 }
David Pursell3f902aa2016-03-01 08:58:26 -0800594 if (*serial_end != ':') {
David Pursell73d55aa2016-09-21 12:08:37 -0700595 // Something other than "<port>:" was found, this must be the command field instead.
596 serial_end = colon_ptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100597 }
598 }
599 return serial_end;
600}
601
David Pursell3f902aa2016-03-01 08:58:26 -0800602} // namespace internal
603
Josh Gao52bd8522016-05-17 16:55:06 -0700604#endif // ADB_HOST
Elliott Hughese67f1f82015-04-30 17:32:03 -0700605
Josh Gao1ce99572018-03-07 16:52:28 -0800606static int smart_socket_enqueue(asocket* s, apacket::payload_type data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607#if ADB_HOST
Josh Gao52bd8522016-05-17 16:55:06 -0700608 char* service = nullptr;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700609 char* serial = nullptr;
Josh Gaob122b172017-08-16 16:57:01 -0700610 TransportId transport_id = 0;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700611 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612#endif
613
Josh Gao27cb7dc2018-02-01 13:17:50 -0800614 D("SS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615
Josh Gao27cb7dc2018-02-01 13:17:50 -0800616 if (s->smart_socket_data.empty()) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700617 // TODO: Make this an IOVector?
Josh Gao1ce99572018-03-07 16:52:28 -0800618 s->smart_socket_data.assign(data.begin(), data.end());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800619 } else {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800620 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800621 }
622
Josh Gao7e6683c2016-01-15 14:35:54 -0800623 /* don't bother if we can't decode the length */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800624 if (s->smart_socket_data.size() < 4) {
Josh Gao52bd8522016-05-17 16:55:06 -0700625 return 0;
626 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800627
Josh Gao27cb7dc2018-02-01 13:17:50 -0800628 uint32_t len = unhex(s->smart_socket_data.data(), 4);
629 if (len == 0 || len > MAX_PAYLOAD) {
630 D("SS(%d): bad size (%u)", s->id, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631 goto fail;
632 }
633
Josh Gao27cb7dc2018-02-01 13:17:50 -0800634 D("SS(%d): len is %u", s->id, len);
Josh Gao7e6683c2016-01-15 14:35:54 -0800635 /* can't do anything until we have the full header */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800636 if ((len + 4) > s->smart_socket_data.size()) {
637 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 -0800638 return 0;
639 }
640
Josh Gao27cb7dc2018-02-01 13:17:50 -0800641 s->smart_socket_data[len + 4] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800642
Josh Gao27cb7dc2018-02-01 13:17:50 -0800643 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800644
645#if ADB_HOST
Josh Gao27cb7dc2018-02-01 13:17:50 -0800646 service = &s->smart_socket_data[4];
Josh Gao52bd8522016-05-17 16:55:06 -0700647 if (!strncmp(service, "host-serial:", strlen("host-serial:"))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648 char* serial_end;
649 service += strlen("host-serial:");
650
Terence Haddock28e13902011-03-16 09:43:56 +0100651 // serial number should follow "host:" and could be a host:port string.
David Pursell3f902aa2016-03-01 08:58:26 -0800652 serial_end = internal::skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653 if (serial_end) {
Josh Gao52bd8522016-05-17 16:55:06 -0700654 *serial_end = 0; // terminate string
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655 serial = service;
656 service = serial_end + 1;
657 }
Josh Gaob122b172017-08-16 16:57:01 -0700658 } else if (!strncmp(service, "host-transport-id:", strlen("host-transport-id:"))) {
659 service += strlen("host-transport-id:");
660 transport_id = strtoll(service, &service, 10);
661
662 if (*service != ':') {
663 return -1;
664 }
665 service++;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700667 type = kTransportUsb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 service += strlen("host-usb:");
669 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700670 type = kTransportLocal;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671 service += strlen("host-local:");
672 } else if (!strncmp(service, "host:", strlen("host:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700673 type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800674 service += strlen("host:");
675 } else {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700676 service = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677 }
678
679 if (service) {
Josh Gao52bd8522016-05-17 16:55:06 -0700680 asocket* s2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681
Josh Gao40390512018-08-07 14:14:21 -0700682 // Some requests are handled immediately -- in that case the handle_host_request() routine
683 // has sent the OKAY or FAIL message and all we have to do is clean up.
684 if (handle_host_request(service, type, serial, transport_id, s->peer->fd, s)) {
Josh Gao52bd8522016-05-17 16:55:06 -0700685 D("SS(%d): handled host service '%s'", s->id, service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 goto fail;
687 }
688 if (!strncmp(service, "transport", strlen("transport"))) {
Josh Gao52bd8522016-05-17 16:55:06 -0700689 D("SS(%d): okay transport", s->id);
Josh Gao27cb7dc2018-02-01 13:17:50 -0800690 s->smart_socket_data.clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 return 0;
692 }
693
Josh Gao52bd8522016-05-17 16:55:06 -0700694 /* try to find a local service with this name.
695 ** if no such service exists, we'll fail out
696 ** and tear down here.
697 */
Josh Gaob122b172017-08-16 16:57:01 -0700698 s2 = create_host_service_socket(service, serial, transport_id);
Yi Kongaed415c2018-07-13 18:15:16 -0700699 if (s2 == nullptr) {
Josh Gao52bd8522016-05-17 16:55:06 -0700700 D("SS(%d): couldn't create host service '%s'", s->id, service);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700701 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 goto fail;
703 }
704
Josh Gao52bd8522016-05-17 16:55:06 -0700705 /* we've connected to a local host service,
706 ** so we make our peer back into a regular
707 ** local socket and bind it to the new local
708 ** service socket, acknowledge the successful
709 ** connection, and close this smart socket now
710 ** that its work is done.
711 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700712 SendOkay(s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713
714 s->peer->ready = local_socket_ready;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700715 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716 s->peer->close = local_socket_close;
717 s->peer->peer = s2;
718 s2->peer = s->peer;
Yi Kongaed415c2018-07-13 18:15:16 -0700719 s->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700720 D("SS(%d): okay", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721 s->close(s);
722
Josh Gao52bd8522016-05-17 16:55:06 -0700723 /* initial state is "ready" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800724 s2->ready(s2);
725 return 0;
726 }
727#else /* !ADB_HOST */
Elliott Hughes8d28e192015-10-07 14:55:10 -0700728 if (s->transport == nullptr) {
Elliott Hughes7be29c82015-04-16 22:54:44 -0700729 std::string error_msg = "unknown failure";
Josh Gaob122b172017-08-16 16:57:01 -0700730 s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700731 if (s->transport == nullptr) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700732 SendFail(s->peer->fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800733 goto fail;
734 }
735 }
736#endif
737
Josh Gao22d2b3e2016-10-27 14:01:08 -0700738 if (!s->transport) {
739 SendFail(s->peer->fd, "device offline (no transport)");
740 goto fail;
Josh Gao704494b2018-05-04 16:04:49 -0700741 } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) {
Josh Gao52bd8522016-05-17 16:55:06 -0700742 /* if there's no remote we fail the connection
743 ** right here and terminate it
744 */
Josh Gao22d2b3e2016-10-27 14:01:08 -0700745 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746 goto fail;
747 }
748
Josh Gao52bd8522016-05-17 16:55:06 -0700749 /* instrument our peer to pass the success or fail
750 ** message back once it connects or closes, then
751 ** detach from it, request the connection, and
752 ** tear down
753 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800754 s->peer->ready = local_socket_ready_notify;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700755 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756 s->peer->close = local_socket_close_notify;
Yi Kongaed415c2018-07-13 18:15:16 -0700757 s->peer->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700758 /* give him our transport and upref it */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800759 s->peer->transport = s->transport;
760
Josh Gao27cb7dc2018-02-01 13:17:50 -0800761 connect_to_remote(s->peer, s->smart_socket_data.data() + 4);
Yi Kongaed415c2018-07-13 18:15:16 -0700762 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800763 s->close(s);
764 return 1;
765
766fail:
Josh Gao52bd8522016-05-17 16:55:06 -0700767 /* we're going to close our peer as a side-effect, so
768 ** return -1 to signal that state to the local socket
769 ** who is enqueueing against us
770 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800771 s->close(s);
772 return -1;
773}
774
Josh Gao52bd8522016-05-17 16:55:06 -0700775static void smart_socket_ready(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700776 D("SS(%d): ready", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800777}
778
Josh Gao52bd8522016-05-17 16:55:06 -0700779static void smart_socket_close(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700780 D("SS(%d): closed", s->id);
Josh Gao52bd8522016-05-17 16:55:06 -0700781 if (s->peer) {
Yi Kongaed415c2018-07-13 18:15:16 -0700782 s->peer->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800783 s->peer->close(s->peer);
Yi Kongaed415c2018-07-13 18:15:16 -0700784 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800785 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800786 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787}
788
Josh Gao52bd8522016-05-17 16:55:06 -0700789static asocket* create_smart_socket(void) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700790 D("Creating smart socket");
Josh Gaoe0361d12018-02-12 17:24:00 -0800791 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792 s->enqueue = smart_socket_enqueue;
793 s->ready = smart_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700794 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800795 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700797 D("SS(%d)", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 return s;
799}
800
Josh Gao52bd8522016-05-17 16:55:06 -0700801void connect_to_smartsocket(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700802 D("Connecting to smart socket");
Josh Gao52bd8522016-05-17 16:55:06 -0700803 asocket* ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800804 s->peer = ss;
805 ss->peer = s;
806 s->ready(s);
807}
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100808
809size_t asocket::get_max_payload() const {
810 size_t max_payload = MAX_PAYLOAD;
811 if (transport) {
812 max_payload = std::min(max_payload, transport->get_max_payload());
813 }
814 if (peer && peer->transport) {
815 max_payload = std::min(max_payload, peer->transport->get_max_payload());
816 }
817 return max_payload;
818}