The Android Open Source Project | 9ca14dc | 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 | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG SOCKETS |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 21 | #include <ctype.h> |
| 22 | #include <errno.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <string.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
Spencer Low | 28bc2cb | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 28 | #include <algorithm> |
Josh Gao | 0f1a20a | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 29 | #include <mutex> |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <vector> |
Spencer Low | 28bc2cb | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 32 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 33 | #if !ADB_HOST |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 34 | #include <android-base/properties.h> |
Steven Moreland | b087d30 | 2017-04-13 23:48:57 -0700 | [diff] [blame] | 35 | #include <log/log_properties.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 36 | #endif |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 37 | |
| 38 | #include "adb.h" |
| 39 | #include "adb_io.h" |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 40 | #include "adb_utils.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 41 | #include "transport.h" |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 42 | #include "types.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
Josh Gao | 0f1a20a | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 44 | static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | static unsigned local_socket_next_id = 1; |
| 46 | |
Josh Gao | 2b3db9e | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 47 | static auto& local_socket_list = *new std::vector<asocket*>(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | |
| 49 | /* the the list of currently closing local sockets. |
| 50 | ** these have no peer anymore, but still packets to |
| 51 | ** write to their fd. |
| 52 | */ |
Josh Gao | 2b3db9e | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 53 | static auto& local_socket_closing_list = *new std::vector<asocket*>(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | |
David 'Digit' Turner | e92344d | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 55 | // Parse the global list of sockets to find one with id |local_id|. |
| 56 | // If |peer_id| is not 0, also check that it is connected to a peer |
| 57 | // with id |peer_id|. Returns an asocket handle on success, NULL on failure. |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 58 | asocket* find_local_socket(unsigned local_id, unsigned peer_id) { |
Josh Gao | 2b3db9e | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 59 | asocket* result = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | |
Josh Gao | 0f1a20a | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 61 | std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock); |
Josh Gao | 2b3db9e | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 62 | for (asocket* s : local_socket_list) { |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 63 | if (s->id != local_id) { |
David 'Digit' Turner | e92344d | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 64 | continue; |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 65 | } |
David 'Digit' Turner | e92344d | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 66 | if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) { |
André Goddard Rosa | 5720e6e | 2010-06-12 11:40:20 -0300 | [diff] [blame] | 67 | result = s; |
André Goddard Rosa | 5720e6e | 2010-06-12 11:40:20 -0300 | [diff] [blame] | 68 | } |
David 'Digit' Turner | e92344d | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 69 | break; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | |
| 72 | return result; |
| 73 | } |
| 74 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 75 | void install_local_socket(asocket* s) { |
Josh Gao | 0f1a20a | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 76 | std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | |
| 78 | s->id = local_socket_next_id++; |
David 'Digit' Turner | e92344d | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 79 | |
| 80 | // Socket ids should never be 0. |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 81 | if (local_socket_next_id == 0) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 82 | LOG(FATAL) << "local socket id overflow"; |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 83 | } |
David 'Digit' Turner | e92344d | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 84 | |
Josh Gao | 2b3db9e | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 85 | local_socket_list.push_back(s); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 88 | void remove_socket(asocket* s) { |
Josh Gao | b69c78b | 2017-09-13 11:17:33 -0700 | [diff] [blame] | 89 | std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock); |
Josh Gao | 2b3db9e | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 90 | for (auto list : { &local_socket_list, &local_socket_closing_list }) { |
| 91 | list->erase(std::remove_if(list->begin(), list->end(), [s](asocket* x) { return x == s; }), |
| 92 | list->end()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 96 | void close_all_sockets(atransport* t) { |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 97 | /* this is a little gross, but since s->close() *will* modify |
| 98 | ** the list out from under you, your options are limited. |
| 99 | */ |
Josh Gao | 0f1a20a | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 100 | std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 101 | restart: |
Josh Gao | 2b3db9e | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 102 | for (asocket* s : local_socket_list) { |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 103 | if (s->transport == t || (s->peer && s->peer->transport == t)) { |
Josh Gao | 80814e1 | 2016-05-18 10:39:48 -0700 | [diff] [blame] | 104 | s->close(s); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | goto restart; |
| 106 | } |
| 107 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 110 | enum class SocketFlushResult { |
| 111 | Destroyed, |
| 112 | TryAgain, |
| 113 | Completed, |
| 114 | }; |
| 115 | |
| 116 | static SocketFlushResult local_socket_flush_incoming(asocket* s) { |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 117 | if (!s->packet_queue.empty()) { |
| 118 | std::vector<adb_iovec> iov = s->packet_queue.iovecs(); |
| 119 | ssize_t rc = adb_writev(s->fd, iov.data(), iov.size()); |
| 120 | if (rc > 0 && static_cast<size_t>(rc) == s->packet_queue.size()) { |
| 121 | s->packet_queue.clear(); |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 122 | } else if (rc > 0) { |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 123 | // TODO: Implement a faster drop_front? |
| 124 | s->packet_queue.take_front(rc); |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 125 | fdevent_add(s->fde, FDE_WRITE); |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 126 | return SocketFlushResult::TryAgain; |
| 127 | } else if (rc == -1 && errno == EAGAIN) { |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 128 | fdevent_add(s->fde, FDE_WRITE); |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 129 | return SocketFlushResult::TryAgain; |
Josh Gao | b50b92f | 2018-03-30 13:56:24 -0700 | [diff] [blame] | 130 | } else { |
| 131 | // We failed to write, but it's possible that we can still read from the socket. |
| 132 | // Give that a try before giving up. |
| 133 | s->has_write_error = true; |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 134 | } |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // If we sent the last packet of a closing socket, we can now destroy it. |
| 138 | if (s->closing) { |
| 139 | s->close(s); |
| 140 | return SocketFlushResult::Destroyed; |
| 141 | } |
| 142 | |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 143 | fdevent_del(s->fde, FDE_WRITE); |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 144 | return SocketFlushResult::Completed; |
| 145 | } |
| 146 | |
| 147 | // Returns false if the socket has been closed and destroyed as a side-effect of this function. |
| 148 | static bool local_socket_flush_outgoing(asocket* s) { |
| 149 | const size_t max_payload = s->get_max_payload(); |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 150 | apacket::payload_type data; |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 151 | data.resize(max_payload); |
| 152 | char* x = &data[0]; |
| 153 | size_t avail = max_payload; |
| 154 | int r = 0; |
| 155 | int is_eof = 0; |
| 156 | |
| 157 | while (avail > 0) { |
| 158 | r = adb_read(s->fd, x, avail); |
| 159 | D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r, |
| 160 | r < 0 ? errno : 0, avail); |
| 161 | if (r == -1) { |
| 162 | if (errno == EAGAIN) { |
| 163 | break; |
| 164 | } |
| 165 | } else if (r > 0) { |
| 166 | avail -= r; |
| 167 | x += r; |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | /* r = 0 or unhandled error */ |
| 172 | is_eof = 1; |
| 173 | break; |
| 174 | } |
| 175 | D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d", s->id, s->fd, r, is_eof, |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 176 | s->fde->force_eof); |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 177 | |
| 178 | if (avail != max_payload && s->peer) { |
| 179 | data.resize(max_payload - avail); |
| 180 | |
| 181 | // s->peer->enqueue() may call s->close() and free s, |
| 182 | // so save variables for debug printing below. |
| 183 | unsigned saved_id = s->id; |
| 184 | int saved_fd = s->fd; |
| 185 | r = s->peer->enqueue(s->peer, std::move(data)); |
| 186 | D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r); |
| 187 | |
| 188 | if (r < 0) { |
| 189 | // Error return means they closed us as a side-effect and we must |
| 190 | // return immediately. |
| 191 | // |
| 192 | // Note that if we still have buffered packets, the socket will be |
| 193 | // placed on the closing socket list. This handler function will be |
| 194 | // called again to process FDE_WRITE events. |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | if (r > 0) { |
| 199 | /* if the remote cannot accept further events, |
| 200 | ** we disable notification of READs. They'll |
| 201 | ** be enabled again when we get a call to ready() |
| 202 | */ |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 203 | fdevent_del(s->fde, FDE_READ); |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | // Don't allow a forced eof if data is still there. |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 208 | if ((s->fde->force_eof && !r) || is_eof) { |
| 209 | D(" closing because is_eof=%d r=%d s->fde.force_eof=%d", is_eof, r, s->fde->force_eof); |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 210 | s->close(s); |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | return true; |
| 215 | } |
| 216 | |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 217 | static int local_socket_enqueue(asocket* s, apacket::payload_type data) { |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 218 | D("LS(%d): enqueue %zu", s->id, data.size()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 219 | |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 220 | s->packet_queue.append(std::move(data)); |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 221 | switch (local_socket_flush_incoming(s)) { |
| 222 | case SocketFlushResult::Destroyed: |
| 223 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 224 | |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 225 | case SocketFlushResult::TryAgain: |
| 226 | return 1; |
| 227 | |
| 228 | case SocketFlushResult::Completed: |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | return !s->packet_queue.empty(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 235 | static void local_socket_ready(asocket* s) { |
Nanik Tolaram | c624a7e | 2015-02-18 22:53:37 +1100 | [diff] [blame] | 236 | /* far side is ready for data, pay attention to |
| 237 | readable events */ |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 238 | fdevent_add(s->fde, FDE_READ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | } |
| 240 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 241 | // be sure to hold the socket list lock when calling this |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 242 | static void local_socket_destroy(asocket* s) { |
Benoit Goby | 88468f3 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 243 | int exit_on_close = s->exit_on_close; |
| 244 | |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 245 | D("LS(%d): destroying fde.fd=%d", s->id, s->fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 246 | |
Josh Gao | 9425027 | 2018-10-12 05:08:45 +0000 | [diff] [blame] | 247 | /* IMPORTANT: the remove closes the fd |
| 248 | ** that belongs to this socket |
| 249 | */ |
| 250 | fdevent_destroy(s->fde); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 252 | remove_socket(s); |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 253 | delete s; |
Benoit Goby | 88468f3 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 254 | |
| 255 | if (exit_on_close) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 256 | D("local_socket_destroy: exiting"); |
Benoit Goby | 88468f3 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 257 | exit(1); |
| 258 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Josh Gao | 0f1a20a | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 261 | static void local_socket_close(asocket* s) { |
| 262 | D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd); |
| 263 | std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock); |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 264 | if (s->peer) { |
| 265 | D("LS(%d): closing peer. peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); |
David 'Digit' Turner | e92344d | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 266 | /* Note: it's important to call shutdown before disconnecting from |
| 267 | * the peer, this ensures that remote sockets can still get the id |
| 268 | * of the local socket they're connected to, to send a CLOSE() |
| 269 | * protocol event. */ |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 270 | if (s->peer->shutdown) { |
| 271 | s->peer->shutdown(s->peer); |
| 272 | } |
Josh Gao | 0f1a20a | 2016-05-17 17:46:27 -0700 | [diff] [blame] | 273 | s->peer->peer = nullptr; |
| 274 | s->peer->close(s->peer); |
| 275 | s->peer = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 278 | /* If we are already closing, or if there are no |
| 279 | ** pending packets, destroy immediately |
| 280 | */ |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 281 | if (s->closing || s->has_write_error || s->packet_queue.empty()) { |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 282 | int id = s->id; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 283 | local_socket_destroy(s); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 284 | D("LS(%d): closed", id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 288 | /* otherwise, put on the closing list |
| 289 | */ |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 290 | D("LS(%d): closing", s->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 291 | s->closing = 1; |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 292 | fdevent_del(s->fde, FDE_READ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 293 | remove_socket(s); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 294 | D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd); |
Josh Gao | 2b3db9e | 2018-01-31 13:15:51 -0800 | [diff] [blame] | 295 | local_socket_closing_list.push_back(s); |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 296 | CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 299 | static void local_socket_event_func(int fd, unsigned ev, void* _s) { |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 300 | asocket* s = reinterpret_cast<asocket*>(_s); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 301 | D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 302 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 303 | /* put the FDE_WRITE processing before the FDE_READ |
| 304 | ** in order to simplify the code. |
| 305 | */ |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 306 | if (ev & FDE_WRITE) { |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 307 | switch (local_socket_flush_incoming(s)) { |
| 308 | case SocketFlushResult::Destroyed: |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | return; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 310 | |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 311 | case SocketFlushResult::TryAgain: |
| 312 | break; |
| 313 | |
| 314 | case SocketFlushResult::Completed: |
| 315 | s->peer->ready(s->peer); |
| 316 | break; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 317 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 320 | if (ev & FDE_READ) { |
Josh Gao | 4b80850 | 2018-03-19 13:20:29 -0700 | [diff] [blame] | 321 | if (!local_socket_flush_outgoing(s)) { |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 322 | return; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 326 | if (ev & FDE_ERROR) { |
| 327 | /* this should be caught be the next read or write |
| 328 | ** catching it here means we may skip the last few |
| 329 | ** bytes of readable data. |
| 330 | */ |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 331 | D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | return; |
| 333 | } |
| 334 | } |
| 335 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 336 | asocket* create_local_socket(int fd) { |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 337 | asocket* s = new asocket(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 338 | s->fd = fd; |
| 339 | s->enqueue = local_socket_enqueue; |
| 340 | s->ready = local_socket_ready; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 341 | s->shutdown = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 342 | s->close = local_socket_close; |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 343 | install_local_socket(s); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 345 | s->fde = fdevent_create(fd, local_socket_event_func, s); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 346 | D("LS(%d): created (fd=%d)", s->id, s->fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 347 | return s; |
| 348 | } |
| 349 | |
Josh Gao | 6dbf579 | 2018-12-13 14:21:00 -0800 | [diff] [blame] | 350 | asocket* create_local_service_socket(std::string_view name, atransport* transport) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 351 | #if !ADB_HOST |
Josh Gao | 3edf807 | 2018-11-16 15:40:16 -0800 | [diff] [blame] | 352 | if (asocket* s = daemon_service_to_socket(name); s) { |
| 353 | return s; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 354 | } |
| 355 | #endif |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 356 | int fd = service_to_fd(name, transport); |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 357 | if (fd < 0) { |
Elliott Hughes | c3d1c11 | 2016-06-15 14:46:56 -0700 | [diff] [blame] | 358 | return nullptr; |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 359 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 360 | |
Dan Pasanen | fb787d9 | 2014-10-06 12:57:20 -0500 | [diff] [blame] | 361 | asocket* s = create_local_socket(fd); |
Josh Gao | 6dbf579 | 2018-12-13 14:21:00 -0800 | [diff] [blame] | 362 | LOG(VERBOSE) << "LS(" << s->id << "): bound to '" << name << "' via " << fd; |
Benoit Goby | 88468f3 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 363 | |
JP Abgrall | a84bd68 | 2012-03-30 13:19:11 -0700 | [diff] [blame] | 364 | #if !ADB_HOST |
Josh Gao | 6dbf579 | 2018-12-13 14:21:00 -0800 | [diff] [blame] | 365 | if ((name.starts_with("root:") && getuid() != 0 && __android_log_is_debuggable()) || |
| 366 | (name.starts_with("unroot:") && getuid() == 0) || name.starts_with("usb:") || |
| 367 | name.starts_with("tcpip:")) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 368 | D("LS(%d): enabling exit_on_close", s->id); |
Benoit Goby | 88468f3 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 369 | s->exit_on_close = 1; |
| 370 | } |
JP Abgrall | a84bd68 | 2012-03-30 13:19:11 -0700 | [diff] [blame] | 371 | #endif |
Benoit Goby | 88468f3 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 372 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 373 | return s; |
| 374 | } |
| 375 | |
| 376 | #if ADB_HOST |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 377 | static asocket* create_host_service_socket(const char* name, const char* serial, |
| 378 | TransportId transport_id) { |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 379 | asocket* s; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 380 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 381 | s = host_service_to_socket(name, serial, transport_id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 382 | |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 383 | if (s != nullptr) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 384 | D("LS(%d) bound to '%s'", s->id, name); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 | return s; |
| 386 | } |
| 387 | |
| 388 | return s; |
| 389 | } |
| 390 | #endif /* ADB_HOST */ |
| 391 | |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 392 | static int remote_socket_enqueue(asocket* s, apacket::payload_type data) { |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 393 | D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd); |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 394 | apacket* p = get_apacket(); |
| 395 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 396 | p->msg.command = A_WRTE; |
| 397 | p->msg.arg0 = s->peer->id; |
| 398 | p->msg.arg1 = s->id; |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 399 | |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 400 | if (data.size() > MAX_PAYLOAD) { |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 401 | put_apacket(p); |
| 402 | return -1; |
| 403 | } |
| 404 | |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 405 | p->payload = std::move(data); |
| 406 | p->msg.data_length = p->payload.size(); |
| 407 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 408 | send_packet(p, s->transport); |
| 409 | return 1; |
| 410 | } |
| 411 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 412 | static void remote_socket_ready(asocket* s) { |
| 413 | D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd); |
| 414 | apacket* p = get_apacket(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 415 | p->msg.command = A_OKAY; |
| 416 | p->msg.arg0 = s->peer->id; |
| 417 | p->msg.arg1 = s->id; |
| 418 | send_packet(p, s->transport); |
| 419 | } |
| 420 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 421 | static void remote_socket_shutdown(asocket* s) { |
| 422 | D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd, |
| 423 | s->peer ? s->peer->fd : -1); |
| 424 | apacket* p = get_apacket(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 425 | p->msg.command = A_CLSE; |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 426 | if (s->peer) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 427 | p->msg.arg0 = s->peer->id; |
David 'Digit' Turner | e92344d | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 428 | } |
| 429 | p->msg.arg1 = s->id; |
| 430 | send_packet(p, s->transport); |
| 431 | } |
| 432 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 433 | static void remote_socket_close(asocket* s) { |
David 'Digit' Turner | e92344d | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 434 | if (s->peer) { |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 435 | s->peer->peer = nullptr; |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 436 | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 437 | s->peer->close(s->peer); |
| 438 | } |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 439 | D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd, |
| 440 | s->peer ? s->peer->fd : -1); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 441 | D("RS(%d): closed", s->id); |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 442 | delete s; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Yabin Cui | 70ec57b | 2015-08-27 18:50:04 -0700 | [diff] [blame] | 445 | // Create a remote socket to exchange packets with a remote service through transport |
| 446 | // |t|. Where |id| is the socket id of the corresponding service on the other |
| 447 | // side of the transport (it is allocated by the remote side and _cannot_ be 0). |
| 448 | // Returns a new non-NULL asocket handle. |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 449 | asocket* create_remote_socket(unsigned id, atransport* t) { |
| 450 | if (id == 0) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 451 | LOG(FATAL) << "invalid remote socket id (0)"; |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 452 | } |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 453 | asocket* s = new asocket(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 454 | s->id = id; |
| 455 | s->enqueue = remote_socket_enqueue; |
| 456 | s->ready = remote_socket_ready; |
David 'Digit' Turner | e92344d | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 457 | s->shutdown = remote_socket_shutdown; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 458 | s->close = remote_socket_close; |
| 459 | s->transport = t; |
| 460 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 461 | D("RS(%d): created", s->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 462 | return s; |
| 463 | } |
| 464 | |
Josh Gao | 4a037e2 | 2018-12-20 17:00:13 -0800 | [diff] [blame^] | 465 | void connect_to_remote(asocket* s, std::string_view destination) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 466 | D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd); |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 467 | apacket* p = get_apacket(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 468 | |
Josh Gao | 4a037e2 | 2018-12-20 17:00:13 -0800 | [diff] [blame^] | 469 | LOG(VERBOSE) << "LS(" << s->id << ": connect(" << destination << ")"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 470 | p->msg.command = A_OPEN; |
| 471 | p->msg.arg0 = s->id; |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 472 | |
Josh Gao | 4a037e2 | 2018-12-20 17:00:13 -0800 | [diff] [blame^] | 473 | // adbd used to expect a null-terminated string. |
| 474 | // Keep doing so to maintain backward compatibility. |
| 475 | p->payload.resize(destination.size() + 1); |
| 476 | memcpy(p->payload.data(), destination.data(), destination.size()); |
| 477 | p->payload[destination.size()] = '\0'; |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 478 | p->msg.data_length = p->payload.size(); |
| 479 | |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 480 | CHECK_LE(p->msg.data_length, s->get_max_payload()); |
Josh Gao | 839b932 | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 481 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 482 | send_packet(p, s->transport); |
| 483 | } |
| 484 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 485 | /* this is used by magic sockets to rig local sockets to |
| 486 | send the go-ahead message when they connect */ |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 487 | static void local_socket_ready_notify(asocket* s) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 488 | s->ready = local_socket_ready; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 489 | s->shutdown = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 490 | s->close = local_socket_close; |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 491 | SendOkay(s->fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 492 | s->ready(s); |
| 493 | } |
| 494 | |
| 495 | /* this is used by magic sockets to rig local sockets to |
| 496 | send the failure message if they are closed before |
| 497 | connected (to avoid closing them without a status message) */ |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 498 | static void local_socket_close_notify(asocket* s) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 499 | s->ready = local_socket_ready; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 500 | s->shutdown = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 501 | s->close = local_socket_close; |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 502 | SendFail(s->fd, "closed"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 503 | s->close(s); |
| 504 | } |
| 505 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 506 | static unsigned unhex(const char* s, int len) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 507 | unsigned n = 0, c; |
| 508 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 509 | while (len-- > 0) { |
| 510 | switch ((c = *s++)) { |
| 511 | case '0': |
| 512 | case '1': |
| 513 | case '2': |
| 514 | case '3': |
| 515 | case '4': |
| 516 | case '5': |
| 517 | case '6': |
| 518 | case '7': |
| 519 | case '8': |
| 520 | case '9': |
| 521 | c -= '0'; |
| 522 | break; |
| 523 | case 'a': |
| 524 | case 'b': |
| 525 | case 'c': |
| 526 | case 'd': |
| 527 | case 'e': |
| 528 | case 'f': |
| 529 | c = c - 'a' + 10; |
| 530 | break; |
| 531 | case 'A': |
| 532 | case 'B': |
| 533 | case 'C': |
| 534 | case 'D': |
| 535 | case 'E': |
| 536 | case 'F': |
| 537 | c = c - 'A' + 10; |
| 538 | break; |
| 539 | default: |
| 540 | return 0xffffffff; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | n = (n << 4) | c; |
| 544 | } |
| 545 | |
| 546 | return n; |
| 547 | } |
| 548 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 549 | #if ADB_HOST |
| 550 | |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 551 | namespace internal { |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 552 | |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 553 | // Parses a host service string of the following format: |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 554 | // * [tcp:|udp:]<serial>[:<port>]:<command> |
| 555 | // * <prefix>:<serial>:<command> |
| 556 | // Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}. |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 557 | bool parse_host_service(std::string_view* out_serial, std::string_view* out_command, |
| 558 | std::string_view full_service) { |
| 559 | if (full_service.empty()) { |
| 560 | return false; |
| 561 | } |
Terence Haddock | 6c67040 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 562 | |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 563 | std::string_view serial; |
| 564 | std::string_view command = full_service; |
| 565 | // Remove |count| bytes from the beginning of command and add them to |serial|. |
| 566 | auto consume = [&full_service, &serial, &command](size_t count) { |
| 567 | CHECK_LE(count, command.size()); |
| 568 | if (!serial.empty()) { |
| 569 | CHECK_EQ(serial.data() + serial.size(), command.data()); |
| 570 | } |
| 571 | |
| 572 | serial = full_service.substr(0, serial.size() + count); |
| 573 | command.remove_prefix(count); |
| 574 | }; |
| 575 | |
| 576 | // Remove the trailing : from serial, and assign the values to the output parameters. |
| 577 | auto finish = [out_serial, out_command, &serial, &command] { |
| 578 | if (serial.empty() || command.empty()) { |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | CHECK_EQ(':', serial.back()); |
| 583 | serial.remove_suffix(1); |
| 584 | |
| 585 | *out_serial = serial; |
| 586 | *out_command = command; |
| 587 | return true; |
| 588 | }; |
| 589 | |
| 590 | static constexpr std::string_view prefixes[] = {"usb:", "product:", "model:", "device:"}; |
| 591 | for (std::string_view prefix : prefixes) { |
| 592 | if (command.starts_with(prefix)) { |
| 593 | consume(prefix.size()); |
| 594 | |
| 595 | size_t offset = command.find_first_of(':'); |
| 596 | if (offset == std::string::npos) { |
| 597 | return false; |
| 598 | } |
| 599 | consume(offset + 1); |
| 600 | return finish(); |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 601 | } |
Scott Anderson | 090e5cb | 2012-05-31 12:04:23 -0700 | [diff] [blame] | 602 | } |
| 603 | |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 604 | // For fastboot compatibility, ignore protocol prefixes. |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 605 | if (command.starts_with("tcp:") || command.starts_with("udp:")) { |
| 606 | consume(4); |
| 607 | if (command.empty()) { |
| 608 | return false; |
David Pursell | 24b62a7 | 2016-09-21 12:08:37 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 612 | bool found_address = false; |
| 613 | if (command[0] == '[') { |
| 614 | // Read an IPv6 address. `adb connect` creates the serial number from the canonical |
| 615 | // network address so it will always have the [] delimiters. |
| 616 | size_t ipv6_end = command.find_first_of(']'); |
| 617 | if (ipv6_end != std::string::npos) { |
| 618 | consume(ipv6_end + 1); |
| 619 | if (command.empty()) { |
| 620 | // Nothing after the IPv6 address. |
| 621 | return false; |
| 622 | } else if (command[0] != ':') { |
| 623 | // Garbage after the IPv6 address. |
| 624 | return false; |
| 625 | } |
| 626 | consume(1); |
| 627 | found_address = true; |
| 628 | } |
Terence Haddock | 6c67040 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 629 | } |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 630 | |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 631 | if (!found_address) { |
| 632 | // Scan ahead to the next colon. |
| 633 | size_t offset = command.find_first_of(':'); |
| 634 | if (offset == std::string::npos) { |
| 635 | return false; |
Terence Haddock | 6c67040 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 636 | } |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 637 | consume(offset + 1); |
| 638 | } |
| 639 | |
| 640 | // We're either at the beginning of a port, or the command itself. |
| 641 | // Look for a port in between colons. |
| 642 | size_t next_colon = command.find_first_of(':'); |
| 643 | if (next_colon == std::string::npos) { |
| 644 | // No colon, we must be at the command. |
| 645 | return finish(); |
| 646 | } |
| 647 | |
| 648 | bool port_valid = true; |
| 649 | if (command.size() <= next_colon) { |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | std::string_view port = command.substr(0, next_colon); |
| 654 | for (auto digit : port) { |
| 655 | if (!isdigit(digit)) { |
| 656 | // Port isn't a number. |
| 657 | port_valid = false; |
| 658 | break; |
Terence Haddock | 6c67040 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 659 | } |
| 660 | } |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 661 | |
| 662 | if (port_valid) { |
| 663 | consume(next_colon + 1); |
| 664 | } |
| 665 | return finish(); |
Terence Haddock | 6c67040 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 666 | } |
| 667 | |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 668 | } // namespace internal |
| 669 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 670 | #endif // ADB_HOST |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 671 | |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 672 | static int smart_socket_enqueue(asocket* s, apacket::payload_type data) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 673 | #if ADB_HOST |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 674 | std::string_view service; |
| 675 | std::string_view serial; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 676 | TransportId transport_id = 0; |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 677 | TransportType type = kTransportAny; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 678 | #endif |
| 679 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 680 | D("SS(%d): enqueue %zu", s->id, data.size()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 681 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 682 | if (s->smart_socket_data.empty()) { |
Josh Gao | 9f155db | 2018-04-03 14:37:11 -0700 | [diff] [blame] | 683 | // TODO: Make this an IOVector? |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 684 | s->smart_socket_data.assign(data.begin(), data.end()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 685 | } else { |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 686 | std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Josh Gao | 9055a58 | 2016-01-15 14:35:54 -0800 | [diff] [blame] | 689 | /* don't bother if we can't decode the length */ |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 690 | if (s->smart_socket_data.size() < 4) { |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 691 | return 0; |
| 692 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 693 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 694 | uint32_t len = unhex(s->smart_socket_data.data(), 4); |
| 695 | if (len == 0 || len > MAX_PAYLOAD) { |
| 696 | D("SS(%d): bad size (%u)", s->id, len); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 697 | goto fail; |
| 698 | } |
| 699 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 700 | D("SS(%d): len is %u", s->id, len); |
Josh Gao | 9055a58 | 2016-01-15 14:35:54 -0800 | [diff] [blame] | 701 | /* can't do anything until we have the full header */ |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 702 | if ((len + 4) > s->smart_socket_data.size()) { |
| 703 | D("SS(%d): waiting for %zu more bytes", s->id, len + 4 - s->smart_socket_data.size()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 707 | s->smart_socket_data[len + 4] = 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 708 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 709 | D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 710 | |
| 711 | #if ADB_HOST |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 712 | service = std::string_view(s->smart_socket_data).substr(4); |
| 713 | if (service.starts_with("host-serial:")) { |
| 714 | service.remove_prefix(strlen("host-serial:")); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 715 | |
Terence Haddock | 6c67040 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 716 | // serial number should follow "host:" and could be a host:port string. |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 717 | if (!internal::parse_host_service(&serial, &service, service)) { |
| 718 | LOG(ERROR) << "SS(" << s->id << "): failed to parse host service: " << service; |
| 719 | goto fail; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 720 | } |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 721 | } else if (service.starts_with("host-transport-id:")) { |
| 722 | service.remove_prefix(strlen("host-transport-id:")); |
| 723 | if (!ParseUint(&transport_id, service, &service)) { |
| 724 | LOG(ERROR) << "SS(" << s->id << "): failed to parse host transport id: " << service; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 725 | return -1; |
| 726 | } |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 727 | if (!service.starts_with(":")) { |
| 728 | LOG(ERROR) << "SS(" << s->id << "): host-transport-id without command"; |
| 729 | return -1; |
| 730 | } |
| 731 | service.remove_prefix(1); |
| 732 | } else if (service.starts_with("host-usb:")) { |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 733 | type = kTransportUsb; |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 734 | service.remove_prefix(strlen("host-usb:")); |
| 735 | } else if (service.starts_with("host-local:")) { |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 736 | type = kTransportLocal; |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 737 | service.remove_prefix(strlen("host-local:")); |
| 738 | } else if (service.starts_with("host:")) { |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 739 | type = kTransportAny; |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 740 | service.remove_prefix(strlen("host:")); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 741 | } else { |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 742 | service = std::string_view{}; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 745 | if (!service.empty()) { |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 746 | asocket* s2; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 747 | |
Josh Gao | d306747 | 2018-08-07 14:14:21 -0700 | [diff] [blame] | 748 | // Some requests are handled immediately -- in that case the handle_host_request() routine |
| 749 | // has sent the OKAY or FAIL message and all we have to do is clean up. |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 750 | // TODO: Convert to string_view. |
| 751 | if (handle_host_request(std::string(service).c_str(), type, |
| 752 | serial.empty() ? nullptr : std::string(serial).c_str(), |
| 753 | transport_id, s->peer->fd, s)) { |
| 754 | LOG(VERBOSE) << "SS(" << s->id << "): handled host service '" << service << "'"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 755 | goto fail; |
| 756 | } |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 757 | if (service.starts_with("transport")) { |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 758 | D("SS(%d): okay transport", s->id); |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 759 | s->smart_socket_data.clear(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 763 | /* try to find a local service with this name. |
| 764 | ** if no such service exists, we'll fail out |
| 765 | ** and tear down here. |
| 766 | */ |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 767 | // TODO: Convert to string_view. |
| 768 | s2 = create_host_service_socket(std::string(service).c_str(), std::string(serial).c_str(), |
| 769 | transport_id); |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 770 | if (s2 == nullptr) { |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 771 | LOG(VERBOSE) << "SS(" << s->id << "): couldn't create host service '" << service << "'"; |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 772 | SendFail(s->peer->fd, "unknown host service"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 773 | goto fail; |
| 774 | } |
| 775 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 776 | /* we've connected to a local host service, |
| 777 | ** so we make our peer back into a regular |
| 778 | ** local socket and bind it to the new local |
| 779 | ** service socket, acknowledge the successful |
| 780 | ** connection, and close this smart socket now |
| 781 | ** that its work is done. |
| 782 | */ |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 783 | SendOkay(s->peer->fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 784 | |
| 785 | s->peer->ready = local_socket_ready; |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 786 | s->peer->shutdown = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 787 | s->peer->close = local_socket_close; |
| 788 | s->peer->peer = s2; |
| 789 | s2->peer = s->peer; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 790 | s->peer = nullptr; |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 791 | D("SS(%d): okay", s->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 792 | s->close(s); |
| 793 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 794 | /* initial state is "ready" */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 795 | s2->ready(s2); |
| 796 | return 0; |
| 797 | } |
| 798 | #else /* !ADB_HOST */ |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 799 | if (s->transport == nullptr) { |
Elliott Hughes | ab88242 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 800 | std::string error_msg = "unknown failure"; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 801 | s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg); |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 802 | if (s->transport == nullptr) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 803 | SendFail(s->peer->fd, error_msg); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 804 | goto fail; |
| 805 | } |
| 806 | } |
| 807 | #endif |
| 808 | |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 809 | if (!s->transport) { |
| 810 | SendFail(s->peer->fd, "device offline (no transport)"); |
| 811 | goto fail; |
Josh Gao | 7a7c5cb | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 812 | } else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) { |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 813 | /* if there's no remote we fail the connection |
| 814 | ** right here and terminate it |
| 815 | */ |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 816 | SendFail(s->peer->fd, "device offline (transport offline)"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 817 | goto fail; |
| 818 | } |
| 819 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 820 | /* instrument our peer to pass the success or fail |
| 821 | ** message back once it connects or closes, then |
| 822 | ** detach from it, request the connection, and |
| 823 | ** tear down |
| 824 | */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 825 | s->peer->ready = local_socket_ready_notify; |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 826 | s->peer->shutdown = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 827 | s->peer->close = local_socket_close_notify; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 828 | s->peer->peer = nullptr; |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 829 | /* give him our transport and upref it */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 830 | s->peer->transport = s->transport; |
| 831 | |
Josh Gao | 4a037e2 | 2018-12-20 17:00:13 -0800 | [diff] [blame^] | 832 | connect_to_remote(s->peer, std::string_view(s->smart_socket_data).substr(4)); |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 833 | s->peer = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 834 | s->close(s); |
| 835 | return 1; |
| 836 | |
| 837 | fail: |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 838 | /* we're going to close our peer as a side-effect, so |
| 839 | ** return -1 to signal that state to the local socket |
| 840 | ** who is enqueueing against us |
| 841 | */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 842 | s->close(s); |
| 843 | return -1; |
| 844 | } |
| 845 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 846 | static void smart_socket_ready(asocket* s) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 847 | D("SS(%d): ready", s->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 848 | } |
| 849 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 850 | static void smart_socket_close(asocket* s) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 851 | D("SS(%d): closed", s->id); |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 852 | if (s->peer) { |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 853 | s->peer->peer = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 854 | s->peer->close(s->peer); |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 855 | s->peer = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 856 | } |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 857 | delete s; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 860 | static asocket* create_smart_socket(void) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 861 | D("Creating smart socket"); |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 862 | asocket* s = new asocket(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 863 | s->enqueue = smart_socket_enqueue; |
| 864 | s->ready = smart_socket_ready; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 865 | s->shutdown = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 866 | s->close = smart_socket_close; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 867 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 868 | D("SS(%d)", s->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 869 | return s; |
| 870 | } |
| 871 | |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 872 | void connect_to_smartsocket(asocket* s) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 873 | D("Connecting to smart socket"); |
Josh Gao | ef550fe | 2016-05-17 16:55:06 -0700 | [diff] [blame] | 874 | asocket* ss = create_smart_socket(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 875 | s->peer = ss; |
| 876 | ss->peer = s; |
| 877 | s->ready(s); |
| 878 | } |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 879 | |
| 880 | size_t asocket::get_max_payload() const { |
| 881 | size_t max_payload = MAX_PAYLOAD; |
| 882 | if (transport) { |
| 883 | max_payload = std::min(max_payload, transport->get_max_payload()); |
| 884 | } |
| 885 | if (peer && peer->transport) { |
| 886 | max_payload = std::min(max_payload, peer->transport->get_max_payload()); |
| 887 | } |
| 888 | return max_payload; |
| 889 | } |