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