blob: dfd9a0afa6db48c55162e4b8b3c5f9e4d843d914 [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 Gaoffc11d32018-09-20 17:38:14 -070029#include <map>
Josh Gao9b587de2016-05-17 17:46:27 -070030#include <mutex>
David Pursell3f902aa2016-03-01 08:58:26 -080031#include <string>
Josh Gaoffc11d32018-09-20 17:38:14 -070032#include <thread>
David Pursell3f902aa2016-03-01 08:58:26 -080033#include <vector>
Spencer Low363af562015-11-07 18:51:54 -080034
Josh Gaoffc11d32018-09-20 17:38:14 -070035#include <android-base/thread_annotations.h>
36
Dan Albert76649012015-02-24 15:51:19 -080037#if !ADB_HOST
Elliott Hughesffdec182016-09-23 15:40:03 -070038#include <android-base/properties.h>
Steven Morelandd73be1b2017-04-13 23:48:57 -070039#include <log/log_properties.h>
Dan Albert76649012015-02-24 15:51:19 -080040#endif
Dan Albert33134262015-03-19 15:21:08 -070041
42#include "adb.h"
43#include "adb_io.h"
Josh Gaoffc11d32018-09-20 17:38:14 -070044#include "adb_utils.h"
45#include "sysdeps/chrono.h"
Dan Albert76649012015-02-24 15:51:19 -080046#include "transport.h"
Josh Gao1ce99572018-03-07 16:52:28 -080047#include "types.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
Josh Gaoffc11d32018-09-20 17:38:14 -070049// The standard (RFC 1122 - 4.2.2.13) says that if we call close on a
50// socket while we have pending data, a TCP RST should be sent to the
51// other end to notify it that we didn't read all of its data. However,
52// this can result in data that we've successfully written out to be dropped
53// on the other end. To avoid this, instead of immediately closing a
54// socket, call shutdown on it instead, and then read from the file
55// descriptor until we hit EOF or an error before closing.
56struct LingeringSocketCloser {
57 LingeringSocketCloser() = default;
58 ~LingeringSocketCloser() = delete;
59
60 // Defer thread creation until it's needed, because we need for there to
61 // only be one thread when dropping privileges in adbd.
62 void Start() {
63 CHECK(!thread_.joinable());
64
65 int fds[2];
66 if (adb_socketpair(fds) != 0) {
67 PLOG(FATAL) << "adb_socketpair failed";
68 }
69
70 set_file_block_mode(fds[0], false);
71 set_file_block_mode(fds[1], false);
72
73 notify_fd_read_.reset(fds[0]);
74 notify_fd_write_.reset(fds[1]);
75
76 thread_ = std::thread([this]() { Run(); });
77 }
78
79 void EnqueueSocket(unique_fd socket) {
80 // Shutdown the socket in the outgoing direction only, so that
81 // we don't have the same problem on the opposite end.
82 adb_shutdown(socket.get(), SHUT_WR);
83 set_file_block_mode(socket.get(), false);
84
85 std::lock_guard<std::mutex> lock(mutex_);
86 int fd = socket.get();
87 SocketInfo info = {
88 .fd = std::move(socket),
89 .deadline = std::chrono::steady_clock::now() + 1s,
90 };
91
92 D("LingeringSocketCloser received fd %d", fd);
93
94 fds_.emplace(fd, std::move(info));
95 if (adb_write(notify_fd_write_, "", 1) == -1 && errno != EAGAIN) {
96 PLOG(FATAL) << "failed to write to LingeringSocketCloser notify fd";
97 }
98 }
99
100 private:
101 std::vector<adb_pollfd> GeneratePollFds() {
102 std::lock_guard<std::mutex> lock(mutex_);
103 std::vector<adb_pollfd> result;
104 result.push_back(adb_pollfd{.fd = notify_fd_read_, .events = POLLIN});
105 for (auto& [fd, _] : fds_) {
106 result.push_back(adb_pollfd{.fd = fd, .events = POLLIN});
107 }
108 return result;
109 }
110
111 void Run() {
112 while (true) {
113 std::vector<adb_pollfd> pfds = GeneratePollFds();
114 int rc = adb_poll(pfds.data(), pfds.size(), 1000);
115 if (rc == -1) {
116 PLOG(FATAL) << "poll failed in LingeringSocketCloser";
117 }
118
119 std::lock_guard<std::mutex> lock(mutex_);
120 if (rc == 0) {
121 // Check deadlines.
122 auto now = std::chrono::steady_clock::now();
123 for (auto it = fds_.begin(); it != fds_.end();) {
124 if (now > it->second.deadline) {
125 D("LingeringSocketCloser closing fd %d due to deadline", it->first);
126 it = fds_.erase(it);
127 } else {
128 D("deadline still not expired for fd %d", it->first);
129 ++it;
130 }
131 }
132 continue;
133 }
134
135 for (auto& pfd : pfds) {
136 if ((pfd.revents & POLLIN) == 0) {
137 continue;
138 }
139
140 // Empty the fd.
141 ssize_t rc;
142 char buf[32768];
143 while ((rc = adb_read(pfd.fd, buf, sizeof(buf))) > 0) {
144 continue;
145 }
146
147 if (pfd.fd == notify_fd_read_) {
148 continue;
149 }
150
151 auto it = fds_.find(pfd.fd);
152 if (it == fds_.end()) {
153 LOG(FATAL) << "fd is missing";
154 }
155
156 if (rc == -1 && errno == EAGAIN) {
157 if (std::chrono::steady_clock::now() > it->second.deadline) {
158 D("LingeringSocketCloser closing fd %d due to deadline", pfd.fd);
159 } else {
160 continue;
161 }
162 } else if (rc == -1) {
163 D("LingeringSocketCloser closing fd %d due to error %d", pfd.fd, errno);
164 } else {
165 D("LingeringSocketCloser closing fd %d due to EOF", pfd.fd);
166 }
167
168 fds_.erase(it);
169 }
170 }
171 }
172
173 std::thread thread_;
174 unique_fd notify_fd_read_;
175 unique_fd notify_fd_write_;
176
177 struct SocketInfo {
178 unique_fd fd;
179 std::chrono::steady_clock::time_point deadline;
180 };
181
182 std::mutex mutex_;
183 std::map<int, SocketInfo> fds_ GUARDED_BY(mutex_);
184};
185
186static auto& socket_closer = *new LingeringSocketCloser();
187
Josh Gao9b587de2016-05-17 17:46:27 -0700188static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189static unsigned local_socket_next_id = 1;
190
Josh Gao5e507642018-01-31 13:15:51 -0800191static auto& local_socket_list = *new std::vector<asocket*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192
193/* the the list of currently closing local sockets.
194** these have no peer anymore, but still packets to
195** write to their fd.
196*/
Josh Gao5e507642018-01-31 13:15:51 -0800197static auto& local_socket_closing_list = *new std::vector<asocket*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100199// Parse the global list of sockets to find one with id |local_id|.
200// If |peer_id| is not 0, also check that it is connected to a peer
201// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
Josh Gao52bd8522016-05-17 16:55:06 -0700202asocket* find_local_socket(unsigned local_id, unsigned peer_id) {
Josh Gao5e507642018-01-31 13:15:51 -0800203 asocket* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204
Josh Gao9b587de2016-05-17 17:46:27 -0700205 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao5e507642018-01-31 13:15:51 -0800206 for (asocket* s : local_socket_list) {
Josh Gao52bd8522016-05-17 16:55:06 -0700207 if (s->id != local_id) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100208 continue;
Josh Gao52bd8522016-05-17 16:55:06 -0700209 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100210 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa81828292010-06-12 11:40:20 -0300211 result = s;
André Goddard Rosa81828292010-06-12 11:40:20 -0300212 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100213 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215
216 return result;
217}
218
Josh Gao52bd8522016-05-17 16:55:06 -0700219void install_local_socket(asocket* s) {
Josh Gao9b587de2016-05-17 17:46:27 -0700220 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221
222 s->id = local_socket_next_id++;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100223
224 // Socket ids should never be 0.
Josh Gao52bd8522016-05-17 16:55:06 -0700225 if (local_socket_next_id == 0) {
Josh Gao9b587de2016-05-17 17:46:27 -0700226 fatal("local socket id overflow");
Josh Gao52bd8522016-05-17 16:55:06 -0700227 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100228
Josh Gao5e507642018-01-31 13:15:51 -0800229 local_socket_list.push_back(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230}
231
Josh Gao52bd8522016-05-17 16:55:06 -0700232void remove_socket(asocket* s) {
Josh Gao62c92f02017-09-13 11:17:33 -0700233 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao5e507642018-01-31 13:15:51 -0800234 for (auto list : { &local_socket_list, &local_socket_closing_list }) {
235 list->erase(std::remove_if(list->begin(), list->end(), [s](asocket* x) { return x == s; }),
236 list->end());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 }
238}
239
Josh Gao52bd8522016-05-17 16:55:06 -0700240void close_all_sockets(atransport* t) {
Josh Gao52bd8522016-05-17 16:55:06 -0700241 /* this is a little gross, but since s->close() *will* modify
242 ** the list out from under you, your options are limited.
243 */
Josh Gao9b587de2016-05-17 17:46:27 -0700244 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245restart:
Josh Gao5e507642018-01-31 13:15:51 -0800246 for (asocket* s : local_socket_list) {
Josh Gao52bd8522016-05-17 16:55:06 -0700247 if (s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gao53eb31d2016-05-18 10:39:48 -0700248 s->close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 goto restart;
250 }
251 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252}
253
Josh Gao184f4802018-03-19 13:20:29 -0700254enum class SocketFlushResult {
255 Destroyed,
256 TryAgain,
257 Completed,
258};
259
260static SocketFlushResult local_socket_flush_incoming(asocket* s) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700261 if (!s->packet_queue.empty()) {
262 std::vector<adb_iovec> iov = s->packet_queue.iovecs();
263 ssize_t rc = adb_writev(s->fd, iov.data(), iov.size());
264 if (rc > 0 && static_cast<size_t>(rc) == s->packet_queue.size()) {
265 s->packet_queue.clear();
Josh Gao184f4802018-03-19 13:20:29 -0700266 } else if (rc > 0) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700267 // TODO: Implement a faster drop_front?
268 s->packet_queue.take_front(rc);
Josh Gao71f775a2018-05-14 11:14:33 -0700269 fdevent_add(s->fde, FDE_WRITE);
Josh Gao184f4802018-03-19 13:20:29 -0700270 return SocketFlushResult::TryAgain;
271 } else if (rc == -1 && errno == EAGAIN) {
Josh Gao71f775a2018-05-14 11:14:33 -0700272 fdevent_add(s->fde, FDE_WRITE);
Josh Gao184f4802018-03-19 13:20:29 -0700273 return SocketFlushResult::TryAgain;
Josh Gao954e1282018-03-30 13:56:24 -0700274 } else {
275 // We failed to write, but it's possible that we can still read from the socket.
276 // Give that a try before giving up.
277 s->has_write_error = true;
Josh Gao184f4802018-03-19 13:20:29 -0700278 }
Josh Gao184f4802018-03-19 13:20:29 -0700279 }
280
281 // If we sent the last packet of a closing socket, we can now destroy it.
282 if (s->closing) {
283 s->close(s);
284 return SocketFlushResult::Destroyed;
285 }
286
Josh Gao71f775a2018-05-14 11:14:33 -0700287 fdevent_del(s->fde, FDE_WRITE);
Josh Gao184f4802018-03-19 13:20:29 -0700288 return SocketFlushResult::Completed;
289}
290
291// Returns false if the socket has been closed and destroyed as a side-effect of this function.
292static bool local_socket_flush_outgoing(asocket* s) {
293 const size_t max_payload = s->get_max_payload();
Josh Gao1ce99572018-03-07 16:52:28 -0800294 apacket::payload_type data;
Josh Gao184f4802018-03-19 13:20:29 -0700295 data.resize(max_payload);
296 char* x = &data[0];
297 size_t avail = max_payload;
298 int r = 0;
299 int is_eof = 0;
300
301 while (avail > 0) {
302 r = adb_read(s->fd, x, avail);
303 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r,
304 r < 0 ? errno : 0, avail);
305 if (r == -1) {
306 if (errno == EAGAIN) {
307 break;
308 }
309 } else if (r > 0) {
310 avail -= r;
311 x += r;
312 continue;
313 }
314
315 /* r = 0 or unhandled error */
316 is_eof = 1;
317 break;
318 }
319 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 -0700320 s->fde->force_eof);
Josh Gao184f4802018-03-19 13:20:29 -0700321
322 if (avail != max_payload && s->peer) {
323 data.resize(max_payload - avail);
324
325 // s->peer->enqueue() may call s->close() and free s,
326 // so save variables for debug printing below.
327 unsigned saved_id = s->id;
328 int saved_fd = s->fd;
329 r = s->peer->enqueue(s->peer, std::move(data));
330 D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
331
332 if (r < 0) {
333 // Error return means they closed us as a side-effect and we must
334 // return immediately.
335 //
336 // Note that if we still have buffered packets, the socket will be
337 // placed on the closing socket list. This handler function will be
338 // called again to process FDE_WRITE events.
339 return false;
340 }
341
342 if (r > 0) {
343 /* if the remote cannot accept further events,
344 ** we disable notification of READs. They'll
345 ** be enabled again when we get a call to ready()
346 */
Josh Gao71f775a2018-05-14 11:14:33 -0700347 fdevent_del(s->fde, FDE_READ);
Josh Gao184f4802018-03-19 13:20:29 -0700348 }
349 }
350
351 // Don't allow a forced eof if data is still there.
Josh Gao71f775a2018-05-14 11:14:33 -0700352 if ((s->fde->force_eof && !r) || is_eof) {
353 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 -0700354 s->close(s);
355 return false;
356 }
357
358 return true;
359}
360
Josh Gao1ce99572018-03-07 16:52:28 -0800361static int local_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800362 D("LS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363
Josh Gao7c738cd2018-04-03 14:37:11 -0700364 s->packet_queue.append(std::move(data));
Josh Gao184f4802018-03-19 13:20:29 -0700365 switch (local_socket_flush_incoming(s)) {
366 case SocketFlushResult::Destroyed:
367 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368
Josh Gao184f4802018-03-19 13:20:29 -0700369 case SocketFlushResult::TryAgain:
370 return 1;
371
372 case SocketFlushResult::Completed:
373 return 0;
374 }
375
376 return !s->packet_queue.empty();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377}
378
Josh Gao52bd8522016-05-17 16:55:06 -0700379static void local_socket_ready(asocket* s) {
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100380 /* far side is ready for data, pay attention to
381 readable events */
Josh Gao71f775a2018-05-14 11:14:33 -0700382 fdevent_add(s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383}
384
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385// be sure to hold the socket list lock when calling this
Josh Gao52bd8522016-05-17 16:55:06 -0700386static void local_socket_destroy(asocket* s) {
Benoit Gobyf366b362012-03-16 14:50:07 -0700387 int exit_on_close = s->exit_on_close;
388
Josh Gao71f775a2018-05-14 11:14:33 -0700389 D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390
Josh Gaoffc11d32018-09-20 17:38:14 -0700391 // Defer thread creation until it's needed, because we need for there to
392 // only be one thread when dropping privileges in adbd.
393 static std::once_flag once;
394 std::call_once(once, []() { socket_closer.Start(); });
395
396 socket_closer.EnqueueSocket(fdevent_release(s->fde));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 remove_socket(s);
Josh Gaoe0361d12018-02-12 17:24:00 -0800399 delete s;
Benoit Gobyf366b362012-03-16 14:50:07 -0700400
401 if (exit_on_close) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700402 D("local_socket_destroy: exiting");
Benoit Gobyf366b362012-03-16 14:50:07 -0700403 exit(1);
404 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405}
406
Josh Gao9b587de2016-05-17 17:46:27 -0700407static void local_socket_close(asocket* s) {
408 D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd);
409 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao52bd8522016-05-17 16:55:06 -0700410 if (s->peer) {
411 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 +0100412 /* Note: it's important to call shutdown before disconnecting from
413 * the peer, this ensures that remote sockets can still get the id
414 * of the local socket they're connected to, to send a CLOSE()
415 * protocol event. */
Josh Gao52bd8522016-05-17 16:55:06 -0700416 if (s->peer->shutdown) {
417 s->peer->shutdown(s->peer);
418 }
Josh Gao9b587de2016-05-17 17:46:27 -0700419 s->peer->peer = nullptr;
420 s->peer->close(s->peer);
421 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 }
423
Josh Gao52bd8522016-05-17 16:55:06 -0700424 /* If we are already closing, or if there are no
425 ** pending packets, destroy immediately
426 */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800427 if (s->closing || s->has_write_error || s->packet_queue.empty()) {
Josh Gao52bd8522016-05-17 16:55:06 -0700428 int id = s->id;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 local_socket_destroy(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700430 D("LS(%d): closed", id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 return;
432 }
433
Josh Gao52bd8522016-05-17 16:55:06 -0700434 /* otherwise, put on the closing list
435 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700436 D("LS(%d): closing", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 s->closing = 1;
Josh Gao71f775a2018-05-14 11:14:33 -0700438 fdevent_del(s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 remove_socket(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700440 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
Josh Gao5e507642018-01-31 13:15:51 -0800441 local_socket_closing_list.push_back(s);
Josh Gao71f775a2018-05-14 11:14:33 -0700442 CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443}
444
Josh Gao52bd8522016-05-17 16:55:06 -0700445static void local_socket_event_func(int fd, unsigned ev, void* _s) {
Dan Albertbac34742015-02-25 17:51:28 -0800446 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700447 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall408fa572011-03-16 15:57:42 -0700448
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 /* put the FDE_WRITE processing before the FDE_READ
450 ** in order to simplify the code.
451 */
Dan Albertbac34742015-02-25 17:51:28 -0800452 if (ev & FDE_WRITE) {
Josh Gao184f4802018-03-19 13:20:29 -0700453 switch (local_socket_flush_incoming(s)) {
454 case SocketFlushResult::Destroyed:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456
Josh Gao184f4802018-03-19 13:20:29 -0700457 case SocketFlushResult::TryAgain:
458 break;
459
460 case SocketFlushResult::Completed:
461 s->peer->ready(s->peer);
462 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 }
465
Dan Albertbac34742015-02-25 17:51:28 -0800466 if (ev & FDE_READ) {
Josh Gao184f4802018-03-19 13:20:29 -0700467 if (!local_socket_flush_outgoing(s)) {
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700468 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 }
470 }
471
Josh Gao52bd8522016-05-17 16:55:06 -0700472 if (ev & FDE_ERROR) {
473 /* this should be caught be the next read or write
474 ** catching it here means we may skip the last few
475 ** bytes of readable data.
476 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700477 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 return;
479 }
480}
481
Josh Gao52bd8522016-05-17 16:55:06 -0700482asocket* create_local_socket(int fd) {
Josh Gaoe0361d12018-02-12 17:24:00 -0800483 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484 s->fd = fd;
485 s->enqueue = local_socket_enqueue;
486 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700487 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700489 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490
Josh Gao71f775a2018-05-14 11:14:33 -0700491 s->fde = fdevent_create(fd, local_socket_event_func, s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700492 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 return s;
494}
495
Josh Gao44899ee2018-04-13 12:17:03 -0700496asocket* create_local_service_socket(const char* name, atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497#if !ADB_HOST
Josh Gao52bd8522016-05-17 16:55:06 -0700498 if (!strcmp(name, "jdwp")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 return create_jdwp_service_socket();
500 }
Josh Gao52bd8522016-05-17 16:55:06 -0700501 if (!strcmp(name, "track-jdwp")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 return create_jdwp_tracker_service_socket();
503 }
504#endif
David Pursell0955c662015-08-31 10:42:13 -0700505 int fd = service_to_fd(name, transport);
Josh Gao52bd8522016-05-17 16:55:06 -0700506 if (fd < 0) {
Elliott Hughesffc73a32016-06-15 14:46:56 -0700507 return nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700508 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509
Dan Pasanen98858812014-10-06 12:57:20 -0500510 asocket* s = create_local_socket(fd);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700511 D("LS(%d): bound to '%s' via %d", s->id, name, fd);
Benoit Gobyf366b362012-03-16 14:50:07 -0700512
JP Abgrallf91259a2012-03-30 13:19:11 -0700513#if !ADB_HOST
Mark Salyzyn97787a02016-03-28 15:52:13 -0700514 if ((!strncmp(name, "root:", 5) && getuid() != 0 && __android_log_is_debuggable()) ||
Josh Gao52bd8522016-05-17 16:55:06 -0700515 (!strncmp(name, "unroot:", 7) && getuid() == 0) ||
516 !strncmp(name, "usb:", 4) ||
517 !strncmp(name, "tcpip:", 6)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700518 D("LS(%d): enabling exit_on_close", s->id);
Benoit Gobyf366b362012-03-16 14:50:07 -0700519 s->exit_on_close = 1;
520 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700521#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700522
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 return s;
524}
525
526#if ADB_HOST
Josh Gaob122b172017-08-16 16:57:01 -0700527static asocket* create_host_service_socket(const char* name, const char* serial,
528 TransportId transport_id) {
Josh Gao52bd8522016-05-17 16:55:06 -0700529 asocket* s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530
Josh Gaob122b172017-08-16 16:57:01 -0700531 s = host_service_to_socket(name, serial, transport_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532
Yi Kongaed415c2018-07-13 18:15:16 -0700533 if (s != nullptr) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700534 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 return s;
536 }
537
538 return s;
539}
540#endif /* ADB_HOST */
541
Josh Gao1ce99572018-03-07 16:52:28 -0800542static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
Josh Gao52bd8522016-05-17 16:55:06 -0700543 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 -0800544 apacket* p = get_apacket();
545
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546 p->msg.command = A_WRTE;
547 p->msg.arg0 = s->peer->id;
548 p->msg.arg1 = s->id;
Josh Gao27cb7dc2018-02-01 13:17:50 -0800549
Josh Gaof571fcb2018-02-05 18:49:10 -0800550 if (data.size() > MAX_PAYLOAD) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800551 put_apacket(p);
552 return -1;
553 }
554
Josh Gaof571fcb2018-02-05 18:49:10 -0800555 p->payload = std::move(data);
556 p->msg.data_length = p->payload.size();
557
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 send_packet(p, s->transport);
559 return 1;
560}
561
Josh Gao52bd8522016-05-17 16:55:06 -0700562static void remote_socket_ready(asocket* s) {
563 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
564 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565 p->msg.command = A_OKAY;
566 p->msg.arg0 = s->peer->id;
567 p->msg.arg1 = s->id;
568 send_packet(p, s->transport);
569}
570
Josh Gao52bd8522016-05-17 16:55:06 -0700571static void remote_socket_shutdown(asocket* s) {
572 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
573 s->peer ? s->peer->fd : -1);
574 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575 p->msg.command = A_CLSE;
Josh Gao52bd8522016-05-17 16:55:06 -0700576 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100578 }
579 p->msg.arg1 = s->id;
580 send_packet(p, s->transport);
581}
582
Josh Gao52bd8522016-05-17 16:55:06 -0700583static void remote_socket_close(asocket* s) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100584 if (s->peer) {
Yi Kongaed415c2018-07-13 18:15:16 -0700585 s->peer->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700586 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 -0800587 s->peer->close(s->peer);
588 }
Josh Gao52bd8522016-05-17 16:55:06 -0700589 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
590 s->peer ? s->peer->fd : -1);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700591 D("RS(%d): closed", s->id);
Josh Gaoe0361d12018-02-12 17:24:00 -0800592 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593}
594
Yabin Cuifd28f322015-08-27 18:50:04 -0700595// Create a remote socket to exchange packets with a remote service through transport
596// |t|. Where |id| is the socket id of the corresponding service on the other
597// side of the transport (it is allocated by the remote side and _cannot_ be 0).
598// Returns a new non-NULL asocket handle.
Josh Gao52bd8522016-05-17 16:55:06 -0700599asocket* create_remote_socket(unsigned id, atransport* t) {
600 if (id == 0) {
601 fatal("invalid remote socket id (0)");
602 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800603 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604 s->id = id;
605 s->enqueue = remote_socket_enqueue;
606 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100607 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608 s->close = remote_socket_close;
609 s->transport = t;
610
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700611 D("RS(%d): created", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612 return s;
613}
614
Josh Gao52bd8522016-05-17 16:55:06 -0700615void connect_to_remote(asocket* s, const char* destination) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700616 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gao52bd8522016-05-17 16:55:06 -0700617 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700619 D("LS(%d): connect('%s')", s->id, destination);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620 p->msg.command = A_OPEN;
621 p->msg.arg0 = s->id;
Josh Gaof571fcb2018-02-05 18:49:10 -0800622
623 // adbd expects a null-terminated string.
Josh Gao1ce99572018-03-07 16:52:28 -0800624 p->payload.assign(destination, destination + strlen(destination) + 1);
Josh Gaof571fcb2018-02-05 18:49:10 -0800625 p->msg.data_length = p->payload.size();
626
627 if (p->msg.data_length > s->get_max_payload()) {
628 fatal("destination oversized");
629 }
630
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631 send_packet(p, s->transport);
632}
633
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634/* this is used by magic sockets to rig local sockets to
635 send the go-ahead message when they connect */
Josh Gao52bd8522016-05-17 16:55:06 -0700636static void local_socket_ready_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800637 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700638 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800639 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700640 SendOkay(s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800641 s->ready(s);
642}
643
644/* this is used by magic sockets to rig local sockets to
645 send the failure message if they are closed before
646 connected (to avoid closing them without a status message) */
Josh Gao52bd8522016-05-17 16:55:06 -0700647static void local_socket_close_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648 s->ready = local_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700649 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700651 SendFail(s->fd, "closed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800652 s->close(s);
653}
654
Josh Gao27cb7dc2018-02-01 13:17:50 -0800655static unsigned unhex(const char* s, int len) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 unsigned n = 0, c;
657
Josh Gao52bd8522016-05-17 16:55:06 -0700658 while (len-- > 0) {
659 switch ((c = *s++)) {
660 case '0':
661 case '1':
662 case '2':
663 case '3':
664 case '4':
665 case '5':
666 case '6':
667 case '7':
668 case '8':
669 case '9':
670 c -= '0';
671 break;
672 case 'a':
673 case 'b':
674 case 'c':
675 case 'd':
676 case 'e':
677 case 'f':
678 c = c - 'a' + 10;
679 break;
680 case 'A':
681 case 'B':
682 case 'C':
683 case 'D':
684 case 'E':
685 case 'F':
686 c = c - 'A' + 10;
687 break;
688 default:
689 return 0xffffffff;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 }
691
692 n = (n << 4) | c;
693 }
694
695 return n;
696}
697
Elliott Hughese67f1f82015-04-30 17:32:03 -0700698#if ADB_HOST
699
David Pursell3f902aa2016-03-01 08:58:26 -0800700namespace internal {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700701
David Pursell3f902aa2016-03-01 08:58:26 -0800702// Returns the position in |service| following the target serial parameter. Serial format can be
703// any of:
704// * [tcp:|udp:]<serial>[:<port>]:<command>
705// * <prefix>:<serial>:<command>
706// Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}.
707//
708// The returned pointer will point to the ':' just before <command>, or nullptr if not found.
Dan Austinb4cff492016-03-28 15:32:37 -0700709char* skip_host_serial(char* service) {
David Pursell3f902aa2016-03-01 08:58:26 -0800710 static const std::vector<std::string>& prefixes =
711 *(new std::vector<std::string>{"usb:", "product:", "model:", "device:"});
Terence Haddock28e13902011-03-16 09:43:56 +0100712
David Pursell3f902aa2016-03-01 08:58:26 -0800713 for (const std::string& prefix : prefixes) {
714 if (!strncmp(service, prefix.c_str(), prefix.length())) {
715 return strchr(service + prefix.length(), ':');
716 }
Scott Anderson3608d832012-05-31 12:04:23 -0700717 }
718
David Pursell3f902aa2016-03-01 08:58:26 -0800719 // For fastboot compatibility, ignore protocol prefixes.
720 if (!strncmp(service, "tcp:", 4) || !strncmp(service, "udp:", 4)) {
721 service += 4;
722 }
723
David Pursell73d55aa2016-09-21 12:08:37 -0700724 // Check for an IPv6 address. `adb connect` creates the serial number from the canonical
725 // network address so it will always have the [] delimiters.
726 if (service[0] == '[') {
727 char* ipv6_end = strchr(service, ']');
728 if (ipv6_end != nullptr) {
729 service = ipv6_end;
730 }
731 }
732
733 // The next colon we find must either begin the port field or the command field.
734 char* colon_ptr = strchr(service, ':');
735 if (!colon_ptr) {
David Pursell3f902aa2016-03-01 08:58:26 -0800736 // No colon in service string.
737 return nullptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100738 }
David Pursell3f902aa2016-03-01 08:58:26 -0800739
David Pursell73d55aa2016-09-21 12:08:37 -0700740 // If the next field is only decimal digits and ends with another colon, it's a port.
741 char* serial_end = colon_ptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100742 if (isdigit(serial_end[1])) {
743 serial_end++;
David Pursell3f902aa2016-03-01 08:58:26 -0800744 while (*serial_end && isdigit(*serial_end)) {
Terence Haddock28e13902011-03-16 09:43:56 +0100745 serial_end++;
746 }
David Pursell3f902aa2016-03-01 08:58:26 -0800747 if (*serial_end != ':') {
David Pursell73d55aa2016-09-21 12:08:37 -0700748 // Something other than "<port>:" was found, this must be the command field instead.
749 serial_end = colon_ptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100750 }
751 }
752 return serial_end;
753}
754
David Pursell3f902aa2016-03-01 08:58:26 -0800755} // namespace internal
756
Josh Gao52bd8522016-05-17 16:55:06 -0700757#endif // ADB_HOST
Elliott Hughese67f1f82015-04-30 17:32:03 -0700758
Josh Gao1ce99572018-03-07 16:52:28 -0800759static int smart_socket_enqueue(asocket* s, apacket::payload_type data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760#if ADB_HOST
Josh Gao52bd8522016-05-17 16:55:06 -0700761 char* service = nullptr;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700762 char* serial = nullptr;
Josh Gaob122b172017-08-16 16:57:01 -0700763 TransportId transport_id = 0;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700764 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765#endif
766
Josh Gao27cb7dc2018-02-01 13:17:50 -0800767 D("SS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800768
Josh Gao27cb7dc2018-02-01 13:17:50 -0800769 if (s->smart_socket_data.empty()) {
Josh Gao7c738cd2018-04-03 14:37:11 -0700770 // TODO: Make this an IOVector?
Josh Gao1ce99572018-03-07 16:52:28 -0800771 s->smart_socket_data.assign(data.begin(), data.end());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800772 } else {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800773 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800774 }
775
Josh Gao7e6683c2016-01-15 14:35:54 -0800776 /* don't bother if we can't decode the length */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800777 if (s->smart_socket_data.size() < 4) {
Josh Gao52bd8522016-05-17 16:55:06 -0700778 return 0;
779 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780
Josh Gao27cb7dc2018-02-01 13:17:50 -0800781 uint32_t len = unhex(s->smart_socket_data.data(), 4);
782 if (len == 0 || len > MAX_PAYLOAD) {
783 D("SS(%d): bad size (%u)", s->id, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800784 goto fail;
785 }
786
Josh Gao27cb7dc2018-02-01 13:17:50 -0800787 D("SS(%d): len is %u", s->id, len);
Josh Gao7e6683c2016-01-15 14:35:54 -0800788 /* can't do anything until we have the full header */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800789 if ((len + 4) > s->smart_socket_data.size()) {
790 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 -0800791 return 0;
792 }
793
Josh Gao27cb7dc2018-02-01 13:17:50 -0800794 s->smart_socket_data[len + 4] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800795
Josh Gao27cb7dc2018-02-01 13:17:50 -0800796 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800797
798#if ADB_HOST
Josh Gao27cb7dc2018-02-01 13:17:50 -0800799 service = &s->smart_socket_data[4];
Josh Gao52bd8522016-05-17 16:55:06 -0700800 if (!strncmp(service, "host-serial:", strlen("host-serial:"))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800801 char* serial_end;
802 service += strlen("host-serial:");
803
Terence Haddock28e13902011-03-16 09:43:56 +0100804 // serial number should follow "host:" and could be a host:port string.
David Pursell3f902aa2016-03-01 08:58:26 -0800805 serial_end = internal::skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800806 if (serial_end) {
Josh Gao52bd8522016-05-17 16:55:06 -0700807 *serial_end = 0; // terminate string
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800808 serial = service;
809 service = serial_end + 1;
810 }
Josh Gaob122b172017-08-16 16:57:01 -0700811 } else if (!strncmp(service, "host-transport-id:", strlen("host-transport-id:"))) {
812 service += strlen("host-transport-id:");
813 transport_id = strtoll(service, &service, 10);
814
815 if (*service != ':') {
816 return -1;
817 }
818 service++;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800819 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700820 type = kTransportUsb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800821 service += strlen("host-usb:");
822 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700823 type = kTransportLocal;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800824 service += strlen("host-local:");
825 } else if (!strncmp(service, "host:", strlen("host:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700826 type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827 service += strlen("host:");
828 } else {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700829 service = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800830 }
831
832 if (service) {
Josh Gao52bd8522016-05-17 16:55:06 -0700833 asocket* s2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800834
Josh Gao40390512018-08-07 14:14:21 -0700835 // Some requests are handled immediately -- in that case the handle_host_request() routine
836 // has sent the OKAY or FAIL message and all we have to do is clean up.
837 if (handle_host_request(service, type, serial, transport_id, s->peer->fd, s)) {
Josh Gao52bd8522016-05-17 16:55:06 -0700838 D("SS(%d): handled host service '%s'", s->id, service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800839 goto fail;
840 }
841 if (!strncmp(service, "transport", strlen("transport"))) {
Josh Gao52bd8522016-05-17 16:55:06 -0700842 D("SS(%d): okay transport", s->id);
Josh Gao27cb7dc2018-02-01 13:17:50 -0800843 s->smart_socket_data.clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800844 return 0;
845 }
846
Josh Gao52bd8522016-05-17 16:55:06 -0700847 /* try to find a local service with this name.
848 ** if no such service exists, we'll fail out
849 ** and tear down here.
850 */
Josh Gaob122b172017-08-16 16:57:01 -0700851 s2 = create_host_service_socket(service, serial, transport_id);
Yi Kongaed415c2018-07-13 18:15:16 -0700852 if (s2 == nullptr) {
Josh Gao52bd8522016-05-17 16:55:06 -0700853 D("SS(%d): couldn't create host service '%s'", s->id, service);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700854 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800855 goto fail;
856 }
857
Josh Gao52bd8522016-05-17 16:55:06 -0700858 /* we've connected to a local host service,
859 ** so we make our peer back into a regular
860 ** local socket and bind it to the new local
861 ** service socket, acknowledge the successful
862 ** connection, and close this smart socket now
863 ** that its work is done.
864 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700865 SendOkay(s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800866
867 s->peer->ready = local_socket_ready;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700868 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800869 s->peer->close = local_socket_close;
870 s->peer->peer = s2;
871 s2->peer = s->peer;
Yi Kongaed415c2018-07-13 18:15:16 -0700872 s->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700873 D("SS(%d): okay", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800874 s->close(s);
875
Josh Gao52bd8522016-05-17 16:55:06 -0700876 /* initial state is "ready" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800877 s2->ready(s2);
878 return 0;
879 }
880#else /* !ADB_HOST */
Elliott Hughes8d28e192015-10-07 14:55:10 -0700881 if (s->transport == nullptr) {
Elliott Hughes7be29c82015-04-16 22:54:44 -0700882 std::string error_msg = "unknown failure";
Josh Gaob122b172017-08-16 16:57:01 -0700883 s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700884 if (s->transport == nullptr) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700885 SendFail(s->peer->fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800886 goto fail;
887 }
888 }
889#endif
890
Josh Gao22d2b3e2016-10-27 14:01:08 -0700891 if (!s->transport) {
892 SendFail(s->peer->fd, "device offline (no transport)");
893 goto fail;
Josh Gao704494b2018-05-04 16:04:49 -0700894 } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) {
Josh Gao52bd8522016-05-17 16:55:06 -0700895 /* if there's no remote we fail the connection
896 ** right here and terminate it
897 */
Josh Gao22d2b3e2016-10-27 14:01:08 -0700898 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800899 goto fail;
900 }
901
Josh Gao52bd8522016-05-17 16:55:06 -0700902 /* instrument our peer to pass the success or fail
903 ** message back once it connects or closes, then
904 ** detach from it, request the connection, and
905 ** tear down
906 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800907 s->peer->ready = local_socket_ready_notify;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700908 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800909 s->peer->close = local_socket_close_notify;
Yi Kongaed415c2018-07-13 18:15:16 -0700910 s->peer->peer = nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700911 /* give him our transport and upref it */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800912 s->peer->transport = s->transport;
913
Josh Gao27cb7dc2018-02-01 13:17:50 -0800914 connect_to_remote(s->peer, s->smart_socket_data.data() + 4);
Yi Kongaed415c2018-07-13 18:15:16 -0700915 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800916 s->close(s);
917 return 1;
918
919fail:
Josh Gao52bd8522016-05-17 16:55:06 -0700920 /* we're going to close our peer as a side-effect, so
921 ** return -1 to signal that state to the local socket
922 ** who is enqueueing against us
923 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800924 s->close(s);
925 return -1;
926}
927
Josh Gao52bd8522016-05-17 16:55:06 -0700928static void smart_socket_ready(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700929 D("SS(%d): ready", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800930}
931
Josh Gao52bd8522016-05-17 16:55:06 -0700932static void smart_socket_close(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700933 D("SS(%d): closed", s->id);
Josh Gao52bd8522016-05-17 16:55:06 -0700934 if (s->peer) {
Yi Kongaed415c2018-07-13 18:15:16 -0700935 s->peer->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936 s->peer->close(s->peer);
Yi Kongaed415c2018-07-13 18:15:16 -0700937 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800938 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800939 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800940}
941
Josh Gao52bd8522016-05-17 16:55:06 -0700942static asocket* create_smart_socket(void) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700943 D("Creating smart socket");
Josh Gaoe0361d12018-02-12 17:24:00 -0800944 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800945 s->enqueue = smart_socket_enqueue;
946 s->ready = smart_socket_ready;
Yi Kongaed415c2018-07-13 18:15:16 -0700947 s->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800948 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800949
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700950 D("SS(%d)", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800951 return s;
952}
953
Josh Gao52bd8522016-05-17 16:55:06 -0700954void connect_to_smartsocket(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700955 D("Connecting to smart socket");
Josh Gao52bd8522016-05-17 16:55:06 -0700956 asocket* ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800957 s->peer = ss;
958 ss->peer = s;
959 s->ready(s);
960}
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100961
962size_t asocket::get_max_payload() const {
963 size_t max_payload = MAX_PAYLOAD;
964 if (transport) {
965 max_payload = std::min(max_payload, transport->get_max_payload());
966 }
967 if (peer && peer->transport) {
968 max_payload = std::min(max_payload, peer->transport->get_max_payload());
969 }
970 return max_payload;
971}