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