The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG SOCKETS |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 21 | #include <ctype.h> |
| 22 | #include <errno.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <string.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 28 | #include <algorithm> |
Josh Gao | ffc11d3 | 2018-09-20 17:38:14 -0700 | [diff] [blame^] | 29 | #include <map> |
Josh Gao | 9b587de | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 30 | #include <mutex> |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 31 | #include <string> |
Josh Gao | ffc11d3 | 2018-09-20 17:38:14 -0700 | [diff] [blame^] | 32 | #include <thread> |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 33 | #include <vector> |
Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 34 | |
Josh Gao | ffc11d3 | 2018-09-20 17:38:14 -0700 | [diff] [blame^] | 35 | #include <android-base/thread_annotations.h> |
| 36 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 37 | #if !ADB_HOST |
Elliott Hughes | ffdec18 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 38 | #include <android-base/properties.h> |
Steven Moreland | d73be1b | 2017-04-13 23:48:57 -0700 | [diff] [blame] | 39 | #include <log/log_properties.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 40 | #endif |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 41 | |
| 42 | #include "adb.h" |
| 43 | #include "adb_io.h" |
Josh Gao | ffc11d3 | 2018-09-20 17:38:14 -0700 | [diff] [blame^] | 44 | #include "adb_utils.h" |
| 45 | #include "sysdeps/chrono.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 46 | #include "transport.h" |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 47 | #include "types.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | |
Josh Gao | ffc11d3 | 2018-09-20 17:38:14 -0700 | [diff] [blame^] | 49 | // 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. |
| 56 | struct 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 | |
| 186 | static auto& socket_closer = *new LingeringSocketCloser(); |
| 187 | |
Josh Gao | 9b587de | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 188 | static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | static unsigned local_socket_next_id = 1; |
| 190 | |
Josh Gao | 5e50764 | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 191 | static auto& local_socket_list = *new std::vector<asocket*>(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | |
| 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 Gao | 5e50764 | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 197 | static auto& local_socket_closing_list = *new std::vector<asocket*>(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 | |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 199 | // 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 Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 202 | asocket* find_local_socket(unsigned local_id, unsigned peer_id) { |
Josh Gao | 5e50764 | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 203 | asocket* result = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | |
Josh Gao | 9b587de | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 205 | std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock); |
Josh Gao | 5e50764 | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 206 | for (asocket* s : local_socket_list) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 207 | if (s->id != local_id) { |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 208 | continue; |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 209 | } |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 210 | if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) { |
André Goddard Rosa | 8182829 | 2010-06-12 11:40:20 -0300 | [diff] [blame] | 211 | result = s; |
André Goddard Rosa | 8182829 | 2010-06-12 11:40:20 -0300 | [diff] [blame] | 212 | } |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 213 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 215 | |
| 216 | return result; |
| 217 | } |
| 218 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 219 | void install_local_socket(asocket* s) { |
Josh Gao | 9b587de | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 220 | std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 221 | |
| 222 | s->id = local_socket_next_id++; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 223 | |
| 224 | // Socket ids should never be 0. |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 225 | if (local_socket_next_id == 0) { |
Josh Gao | 9b587de | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 226 | fatal("local socket id overflow"); |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 227 | } |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 228 | |
Josh Gao | 5e50764 | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 229 | local_socket_list.push_back(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 232 | void remove_socket(asocket* s) { |
Josh Gao | 62c92f0 | 2017-09-13 11:17:33 -0700 | [diff] [blame] | 233 | std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock); |
Josh Gao | 5e50764 | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 234 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 240 | void close_all_sockets(atransport* t) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 241 | /* this is a little gross, but since s->close() *will* modify |
| 242 | ** the list out from under you, your options are limited. |
| 243 | */ |
Josh Gao | 9b587de | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 244 | std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 245 | restart: |
Josh Gao | 5e50764 | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 246 | for (asocket* s : local_socket_list) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 247 | if (s->transport == t || (s->peer && s->peer->transport == t)) { |
Josh Gao | 53eb31d | 2016-05-18 10:39:48 -0700 | [diff] [blame] | 248 | s->close(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 249 | goto restart; |
| 250 | } |
| 251 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 254 | enum class SocketFlushResult { |
| 255 | Destroyed, |
| 256 | TryAgain, |
| 257 | Completed, |
| 258 | }; |
| 259 | |
| 260 | static SocketFlushResult local_socket_flush_incoming(asocket* s) { |
Josh Gao | 7c738cd | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 261 | 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 Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 266 | } else if (rc > 0) { |
Josh Gao | 7c738cd | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 267 | // TODO: Implement a faster drop_front? |
| 268 | s->packet_queue.take_front(rc); |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 269 | fdevent_add(s->fde, FDE_WRITE); |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 270 | return SocketFlushResult::TryAgain; |
| 271 | } else if (rc == -1 && errno == EAGAIN) { |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 272 | fdevent_add(s->fde, FDE_WRITE); |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 273 | return SocketFlushResult::TryAgain; |
Josh Gao | 954e128 | 2018-03-30 13:56:24 -0700 | [diff] [blame] | 274 | } 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 Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 278 | } |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 279 | } |
| 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 Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 287 | fdevent_del(s->fde, FDE_WRITE); |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 288 | return SocketFlushResult::Completed; |
| 289 | } |
| 290 | |
| 291 | // Returns false if the socket has been closed and destroyed as a side-effect of this function. |
| 292 | static bool local_socket_flush_outgoing(asocket* s) { |
| 293 | const size_t max_payload = s->get_max_payload(); |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 294 | apacket::payload_type data; |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 295 | 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 Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 320 | s->fde->force_eof); |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 321 | |
| 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 Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 347 | fdevent_del(s->fde, FDE_READ); |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | // Don't allow a forced eof if data is still there. |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 352 | 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 Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 354 | s->close(s); |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | return true; |
| 359 | } |
| 360 | |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 361 | static int local_socket_enqueue(asocket* s, apacket::payload_type data) { |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 362 | D("LS(%d): enqueue %zu", s->id, data.size()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | |
Josh Gao | 7c738cd | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 364 | s->packet_queue.append(std::move(data)); |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 365 | switch (local_socket_flush_incoming(s)) { |
| 366 | case SocketFlushResult::Destroyed: |
| 367 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 368 | |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 369 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 379 | static void local_socket_ready(asocket* s) { |
Nanik Tolaram | b627a0e | 2015-02-18 22:53:37 +1100 | [diff] [blame] | 380 | /* far side is ready for data, pay attention to |
| 381 | readable events */ |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 382 | fdevent_add(s->fde, FDE_READ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 383 | } |
| 384 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 | // be sure to hold the socket list lock when calling this |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 386 | static void local_socket_destroy(asocket* s) { |
Benoit Goby | f366b36 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 387 | int exit_on_close = s->exit_on_close; |
| 388 | |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 389 | D("LS(%d): destroying fde.fd=%d", s->id, s->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 390 | |
Josh Gao | ffc11d3 | 2018-09-20 17:38:14 -0700 | [diff] [blame^] | 391 | // 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 397 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 398 | remove_socket(s); |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 399 | delete s; |
Benoit Goby | f366b36 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 400 | |
| 401 | if (exit_on_close) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 402 | D("local_socket_destroy: exiting"); |
Benoit Goby | f366b36 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 403 | exit(1); |
| 404 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Josh Gao | 9b587de | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 407 | static 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 Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 410 | 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' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 412 | /* 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 Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 416 | if (s->peer->shutdown) { |
| 417 | s->peer->shutdown(s->peer); |
| 418 | } |
Josh Gao | 9b587de | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 419 | s->peer->peer = nullptr; |
| 420 | s->peer->close(s->peer); |
| 421 | s->peer = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 424 | /* If we are already closing, or if there are no |
| 425 | ** pending packets, destroy immediately |
| 426 | */ |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 427 | if (s->closing || s->has_write_error || s->packet_queue.empty()) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 428 | int id = s->id; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | local_socket_destroy(s); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 430 | D("LS(%d): closed", id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 434 | /* otherwise, put on the closing list |
| 435 | */ |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 436 | D("LS(%d): closing", s->id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 437 | s->closing = 1; |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 438 | fdevent_del(s->fde, FDE_READ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 439 | remove_socket(s); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 440 | D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd); |
Josh Gao | 5e50764 | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 441 | local_socket_closing_list.push_back(s); |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 442 | CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 445 | static void local_socket_event_func(int fd, unsigned ev, void* _s) { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 446 | asocket* s = reinterpret_cast<asocket*>(_s); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 447 | D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 448 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 449 | /* put the FDE_WRITE processing before the FDE_READ |
| 450 | ** in order to simplify the code. |
| 451 | */ |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 452 | if (ev & FDE_WRITE) { |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 453 | switch (local_socket_flush_incoming(s)) { |
| 454 | case SocketFlushResult::Destroyed: |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 455 | return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 456 | |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 457 | case SocketFlushResult::TryAgain: |
| 458 | break; |
| 459 | |
| 460 | case SocketFlushResult::Completed: |
| 461 | s->peer->ready(s->peer); |
| 462 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 463 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 466 | if (ev & FDE_READ) { |
Josh Gao | 184f480 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 467 | if (!local_socket_flush_outgoing(s)) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 468 | return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 472 | 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 Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 477 | D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 478 | return; |
| 479 | } |
| 480 | } |
| 481 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 482 | asocket* create_local_socket(int fd) { |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 483 | asocket* s = new asocket(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 484 | s->fd = fd; |
| 485 | s->enqueue = local_socket_enqueue; |
| 486 | s->ready = local_socket_ready; |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 487 | s->shutdown = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 488 | s->close = local_socket_close; |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 489 | install_local_socket(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 490 | |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 491 | s->fde = fdevent_create(fd, local_socket_event_func, s); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 492 | D("LS(%d): created (fd=%d)", s->id, s->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 493 | return s; |
| 494 | } |
| 495 | |
Josh Gao | 44899ee | 2018-04-13 12:17:03 -0700 | [diff] [blame] | 496 | asocket* create_local_service_socket(const char* name, atransport* transport) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 497 | #if !ADB_HOST |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 498 | if (!strcmp(name, "jdwp")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 499 | return create_jdwp_service_socket(); |
| 500 | } |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 501 | if (!strcmp(name, "track-jdwp")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 502 | return create_jdwp_tracker_service_socket(); |
| 503 | } |
| 504 | #endif |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 505 | int fd = service_to_fd(name, transport); |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 506 | if (fd < 0) { |
Elliott Hughes | ffc73a3 | 2016-06-15 14:46:56 -0700 | [diff] [blame] | 507 | return nullptr; |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 508 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 509 | |
Dan Pasanen | 9885881 | 2014-10-06 12:57:20 -0500 | [diff] [blame] | 510 | asocket* s = create_local_socket(fd); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 511 | D("LS(%d): bound to '%s' via %d", s->id, name, fd); |
Benoit Goby | f366b36 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 512 | |
JP Abgrall | f91259a | 2012-03-30 13:19:11 -0700 | [diff] [blame] | 513 | #if !ADB_HOST |
Mark Salyzyn | 97787a0 | 2016-03-28 15:52:13 -0700 | [diff] [blame] | 514 | if ((!strncmp(name, "root:", 5) && getuid() != 0 && __android_log_is_debuggable()) || |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 515 | (!strncmp(name, "unroot:", 7) && getuid() == 0) || |
| 516 | !strncmp(name, "usb:", 4) || |
| 517 | !strncmp(name, "tcpip:", 6)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 518 | D("LS(%d): enabling exit_on_close", s->id); |
Benoit Goby | f366b36 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 519 | s->exit_on_close = 1; |
| 520 | } |
JP Abgrall | f91259a | 2012-03-30 13:19:11 -0700 | [diff] [blame] | 521 | #endif |
Benoit Goby | f366b36 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 522 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 523 | return s; |
| 524 | } |
| 525 | |
| 526 | #if ADB_HOST |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 527 | static asocket* create_host_service_socket(const char* name, const char* serial, |
| 528 | TransportId transport_id) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 529 | asocket* s; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 530 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 531 | s = host_service_to_socket(name, serial, transport_id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 532 | |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 533 | if (s != nullptr) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 534 | D("LS(%d) bound to '%s'", s->id, name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 535 | return s; |
| 536 | } |
| 537 | |
| 538 | return s; |
| 539 | } |
| 540 | #endif /* ADB_HOST */ |
| 541 | |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 542 | static int remote_socket_enqueue(asocket* s, apacket::payload_type data) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 543 | D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd); |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 544 | apacket* p = get_apacket(); |
| 545 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 546 | p->msg.command = A_WRTE; |
| 547 | p->msg.arg0 = s->peer->id; |
| 548 | p->msg.arg1 = s->id; |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 549 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 550 | if (data.size() > MAX_PAYLOAD) { |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 551 | put_apacket(p); |
| 552 | return -1; |
| 553 | } |
| 554 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 555 | p->payload = std::move(data); |
| 556 | p->msg.data_length = p->payload.size(); |
| 557 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 558 | send_packet(p, s->transport); |
| 559 | return 1; |
| 560 | } |
| 561 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 562 | static 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 565 | 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 Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 571 | static 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 575 | p->msg.command = A_CLSE; |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 576 | if (s->peer) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 577 | p->msg.arg0 = s->peer->id; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 578 | } |
| 579 | p->msg.arg1 = s->id; |
| 580 | send_packet(p, s->transport); |
| 581 | } |
| 582 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 583 | static void remote_socket_close(asocket* s) { |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 584 | if (s->peer) { |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 585 | s->peer->peer = nullptr; |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 586 | D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 587 | s->peer->close(s->peer); |
| 588 | } |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 589 | 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 Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 591 | D("RS(%d): closed", s->id); |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 592 | delete s; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Yabin Cui | fd28f32 | 2015-08-27 18:50:04 -0700 | [diff] [blame] | 595 | // 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 Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 599 | asocket* create_remote_socket(unsigned id, atransport* t) { |
| 600 | if (id == 0) { |
| 601 | fatal("invalid remote socket id (0)"); |
| 602 | } |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 603 | asocket* s = new asocket(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 604 | s->id = id; |
| 605 | s->enqueue = remote_socket_enqueue; |
| 606 | s->ready = remote_socket_ready; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 607 | s->shutdown = remote_socket_shutdown; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 608 | s->close = remote_socket_close; |
| 609 | s->transport = t; |
| 610 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 611 | D("RS(%d): created", s->id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 612 | return s; |
| 613 | } |
| 614 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 615 | void connect_to_remote(asocket* s, const char* destination) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 616 | D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd); |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 617 | apacket* p = get_apacket(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 618 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 619 | D("LS(%d): connect('%s')", s->id, destination); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 620 | p->msg.command = A_OPEN; |
| 621 | p->msg.arg0 = s->id; |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 622 | |
| 623 | // adbd expects a null-terminated string. |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 624 | p->payload.assign(destination, destination + strlen(destination) + 1); |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 625 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 631 | send_packet(p, s->transport); |
| 632 | } |
| 633 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 634 | /* this is used by magic sockets to rig local sockets to |
| 635 | send the go-ahead message when they connect */ |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 636 | static void local_socket_ready_notify(asocket* s) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 637 | s->ready = local_socket_ready; |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 638 | s->shutdown = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 639 | s->close = local_socket_close; |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 640 | SendOkay(s->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 641 | 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 Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 647 | static void local_socket_close_notify(asocket* s) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 648 | s->ready = local_socket_ready; |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 649 | s->shutdown = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 650 | s->close = local_socket_close; |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 651 | SendFail(s->fd, "closed"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 652 | s->close(s); |
| 653 | } |
| 654 | |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 655 | static unsigned unhex(const char* s, int len) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 656 | unsigned n = 0, c; |
| 657 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 658 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | n = (n << 4) | c; |
| 693 | } |
| 694 | |
| 695 | return n; |
| 696 | } |
| 697 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 698 | #if ADB_HOST |
| 699 | |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 700 | namespace internal { |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 701 | |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 702 | // 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 Austin | b4cff49 | 2016-03-28 15:32:37 -0700 | [diff] [blame] | 709 | char* skip_host_serial(char* service) { |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 710 | static const std::vector<std::string>& prefixes = |
| 711 | *(new std::vector<std::string>{"usb:", "product:", "model:", "device:"}); |
Terence Haddock | 28e1390 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 712 | |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 713 | for (const std::string& prefix : prefixes) { |
| 714 | if (!strncmp(service, prefix.c_str(), prefix.length())) { |
| 715 | return strchr(service + prefix.length(), ':'); |
| 716 | } |
Scott Anderson | 3608d83 | 2012-05-31 12:04:23 -0700 | [diff] [blame] | 717 | } |
| 718 | |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 719 | // For fastboot compatibility, ignore protocol prefixes. |
| 720 | if (!strncmp(service, "tcp:", 4) || !strncmp(service, "udp:", 4)) { |
| 721 | service += 4; |
| 722 | } |
| 723 | |
David Pursell | 73d55aa | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 724 | // 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 Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 736 | // No colon in service string. |
| 737 | return nullptr; |
Terence Haddock | 28e1390 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 738 | } |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 739 | |
David Pursell | 73d55aa | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 740 | // If the next field is only decimal digits and ends with another colon, it's a port. |
| 741 | char* serial_end = colon_ptr; |
Terence Haddock | 28e1390 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 742 | if (isdigit(serial_end[1])) { |
| 743 | serial_end++; |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 744 | while (*serial_end && isdigit(*serial_end)) { |
Terence Haddock | 28e1390 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 745 | serial_end++; |
| 746 | } |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 747 | if (*serial_end != ':') { |
David Pursell | 73d55aa | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 748 | // Something other than "<port>:" was found, this must be the command field instead. |
| 749 | serial_end = colon_ptr; |
Terence Haddock | 28e1390 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | return serial_end; |
| 753 | } |
| 754 | |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 755 | } // namespace internal |
| 756 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 757 | #endif // ADB_HOST |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 758 | |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 759 | static int smart_socket_enqueue(asocket* s, apacket::payload_type data) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 760 | #if ADB_HOST |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 761 | char* service = nullptr; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 762 | char* serial = nullptr; |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 763 | TransportId transport_id = 0; |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 764 | TransportType type = kTransportAny; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 765 | #endif |
| 766 | |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 767 | D("SS(%d): enqueue %zu", s->id, data.size()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 768 | |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 769 | if (s->smart_socket_data.empty()) { |
Josh Gao | 7c738cd | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 770 | // TODO: Make this an IOVector? |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 771 | s->smart_socket_data.assign(data.begin(), data.end()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 772 | } else { |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 773 | std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Josh Gao | 7e6683c | 2016-01-15 14:35:54 -0800 | [diff] [blame] | 776 | /* don't bother if we can't decode the length */ |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 777 | if (s->smart_socket_data.size() < 4) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 778 | return 0; |
| 779 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 780 | |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 781 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 784 | goto fail; |
| 785 | } |
| 786 | |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 787 | D("SS(%d): len is %u", s->id, len); |
Josh Gao | 7e6683c | 2016-01-15 14:35:54 -0800 | [diff] [blame] | 788 | /* can't do anything until we have the full header */ |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 789 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 794 | s->smart_socket_data[len + 4] = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 795 | |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 796 | D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 797 | |
| 798 | #if ADB_HOST |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 799 | service = &s->smart_socket_data[4]; |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 800 | if (!strncmp(service, "host-serial:", strlen("host-serial:"))) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 801 | char* serial_end; |
| 802 | service += strlen("host-serial:"); |
| 803 | |
Terence Haddock | 28e1390 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 804 | // serial number should follow "host:" and could be a host:port string. |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 805 | serial_end = internal::skip_host_serial(service); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 806 | if (serial_end) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 807 | *serial_end = 0; // terminate string |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 808 | serial = service; |
| 809 | service = serial_end + 1; |
| 810 | } |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 811 | } 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 819 | } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) { |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 820 | type = kTransportUsb; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 821 | service += strlen("host-usb:"); |
| 822 | } else if (!strncmp(service, "host-local:", strlen("host-local:"))) { |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 823 | type = kTransportLocal; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 824 | service += strlen("host-local:"); |
| 825 | } else if (!strncmp(service, "host:", strlen("host:"))) { |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 826 | type = kTransportAny; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 827 | service += strlen("host:"); |
| 828 | } else { |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 829 | service = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | if (service) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 833 | asocket* s2; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 834 | |
Josh Gao | 4039051 | 2018-08-07 14:14:21 -0700 | [diff] [blame] | 835 | // 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 Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 838 | D("SS(%d): handled host service '%s'", s->id, service); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 839 | goto fail; |
| 840 | } |
| 841 | if (!strncmp(service, "transport", strlen("transport"))) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 842 | D("SS(%d): okay transport", s->id); |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 843 | s->smart_socket_data.clear(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 844 | return 0; |
| 845 | } |
| 846 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 847 | /* 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 Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 851 | s2 = create_host_service_socket(service, serial, transport_id); |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 852 | if (s2 == nullptr) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 853 | D("SS(%d): couldn't create host service '%s'", s->id, service); |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 854 | SendFail(s->peer->fd, "unknown host service"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 855 | goto fail; |
| 856 | } |
| 857 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 858 | /* 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 Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 865 | SendOkay(s->peer->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 866 | |
| 867 | s->peer->ready = local_socket_ready; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 868 | s->peer->shutdown = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 869 | s->peer->close = local_socket_close; |
| 870 | s->peer->peer = s2; |
| 871 | s2->peer = s->peer; |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 872 | s->peer = nullptr; |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 873 | D("SS(%d): okay", s->id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 874 | s->close(s); |
| 875 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 876 | /* initial state is "ready" */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 877 | s2->ready(s2); |
| 878 | return 0; |
| 879 | } |
| 880 | #else /* !ADB_HOST */ |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 881 | if (s->transport == nullptr) { |
Elliott Hughes | 7be29c8 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 882 | std::string error_msg = "unknown failure"; |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 883 | s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg); |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 884 | if (s->transport == nullptr) { |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 885 | SendFail(s->peer->fd, error_msg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 886 | goto fail; |
| 887 | } |
| 888 | } |
| 889 | #endif |
| 890 | |
Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 891 | if (!s->transport) { |
| 892 | SendFail(s->peer->fd, "device offline (no transport)"); |
| 893 | goto fail; |
Josh Gao | 704494b | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 894 | } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) { |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 895 | /* if there's no remote we fail the connection |
| 896 | ** right here and terminate it |
| 897 | */ |
Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 898 | SendFail(s->peer->fd, "device offline (transport offline)"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 899 | goto fail; |
| 900 | } |
| 901 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 902 | /* 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 907 | s->peer->ready = local_socket_ready_notify; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 908 | s->peer->shutdown = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 909 | s->peer->close = local_socket_close_notify; |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 910 | s->peer->peer = nullptr; |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 911 | /* give him our transport and upref it */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 912 | s->peer->transport = s->transport; |
| 913 | |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 914 | connect_to_remote(s->peer, s->smart_socket_data.data() + 4); |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 915 | s->peer = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 916 | s->close(s); |
| 917 | return 1; |
| 918 | |
| 919 | fail: |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 920 | /* 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 924 | s->close(s); |
| 925 | return -1; |
| 926 | } |
| 927 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 928 | static void smart_socket_ready(asocket* s) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 929 | D("SS(%d): ready", s->id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 930 | } |
| 931 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 932 | static void smart_socket_close(asocket* s) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 933 | D("SS(%d): closed", s->id); |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 934 | if (s->peer) { |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 935 | s->peer->peer = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 936 | s->peer->close(s->peer); |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 937 | s->peer = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 938 | } |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 939 | delete s; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 940 | } |
| 941 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 942 | static asocket* create_smart_socket(void) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 943 | D("Creating smart socket"); |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 944 | asocket* s = new asocket(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 945 | s->enqueue = smart_socket_enqueue; |
| 946 | s->ready = smart_socket_ready; |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 947 | s->shutdown = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 948 | s->close = smart_socket_close; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 949 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 950 | D("SS(%d)", s->id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 951 | return s; |
| 952 | } |
| 953 | |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 954 | void connect_to_smartsocket(asocket* s) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 955 | D("Connecting to smart socket"); |
Josh Gao | 52bd852 | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 956 | asocket* ss = create_smart_socket(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 957 | s->peer = ss; |
| 958 | ss->peer = s; |
| 959 | s->ready(s); |
| 960 | } |
Tamas Berghammer | 3d2904c | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 961 | |
| 962 | size_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 | } |