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