Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #include "socket_spec.h" |
| 18 | |
Daniel Colascione | 3e12469 | 2019-11-13 17:49:37 -0800 | [diff] [blame] | 19 | #include <limits> |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 20 | #include <string> |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 21 | #include <string_view> |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 22 | #include <unordered_map> |
| 23 | #include <vector> |
| 24 | |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 25 | #include <android-base/parseint.h> |
| 26 | #include <android-base/parsenetaddress.h> |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
| 28 | #include <android-base/strings.h> |
| 29 | #include <cutils/sockets.h> |
| 30 | |
| 31 | #include "adb.h" |
Shaju Mathew | cb8d887 | 2021-11-28 17:29:21 -0800 | [diff] [blame] | 32 | #include "adb_auth.h" |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 33 | #include "adb_mdns.h" |
Daniel Colascione | 3e12469 | 2019-11-13 17:49:37 -0800 | [diff] [blame] | 34 | #include "adb_utils.h" |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 35 | #include "sysdeps.h" |
| 36 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 37 | using namespace std::string_literals; |
| 38 | |
Daniel Colascione | 3e12469 | 2019-11-13 17:49:37 -0800 | [diff] [blame] | 39 | using android::base::ConsumePrefix; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 40 | using android::base::StringPrintf; |
| 41 | |
| 42 | #if defined(__linux__) |
| 43 | #define ADB_LINUX 1 |
| 44 | #else |
| 45 | #define ADB_LINUX 0 |
| 46 | #endif |
| 47 | |
| 48 | #if defined(_WIN32) |
| 49 | #define ADB_WINDOWS 1 |
| 50 | #else |
| 51 | #define ADB_WINDOWS 0 |
| 52 | #endif |
| 53 | |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 54 | #if ADB_LINUX |
Elliott Hughes | d690167 | 2023-12-06 00:35:41 +0000 | [diff] [blame] | 55 | #include <linux/vm_sockets.h> |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 56 | #include <sys/socket.h> |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 57 | #endif |
| 58 | |
Ryan Prichard | c36807b | 2023-06-21 22:08:16 -0700 | [diff] [blame] | 59 | bool gListenAll = false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 60 | |
| 61 | struct LocalSocketType { |
| 62 | int socket_namespace; |
| 63 | bool available; |
| 64 | }; |
| 65 | |
| 66 | static auto& kLocalSocketTypes = *new std::unordered_map<std::string, LocalSocketType>({ |
| 67 | #if ADB_HOST |
| 68 | { "local", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } }, |
| 69 | #else |
| 70 | { "local", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_WINDOWS } }, |
| 71 | #endif |
| 72 | |
| 73 | { "localreserved", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_HOST } }, |
| 74 | { "localabstract", { ANDROID_SOCKET_NAMESPACE_ABSTRACT, ADB_LINUX } }, |
| 75 | { "localfilesystem", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } }, |
| 76 | }); |
| 77 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 78 | bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port, |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 79 | std::string* serial, std::string* error) { |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 80 | if (!spec.starts_with("tcp:")) { |
| 81 | *error = "specification is not tcp: "; |
| 82 | *error += spec; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 86 | std::string hostname_value; |
| 87 | int port_value; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 88 | |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 89 | // If the spec is tcp:<port>, parse it ourselves. |
| 90 | // Otherwise, delegate to android::base::ParseNetAddress. |
| 91 | if (android::base::ParseInt(&spec[4], &port_value)) { |
| 92 | // Do the range checking ourselves, because ParseInt rejects 'tcp:65536' and 'tcp:foo:1234' |
| 93 | // identically. |
| 94 | if (port_value < 0 || port_value > 65535) { |
| 95 | *error = StringPrintf("bad port number '%d'", port_value); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 96 | return false; |
| 97 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 98 | } else { |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 99 | std::string addr(spec.substr(4)); |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 100 | port_value = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 101 | |
| 102 | // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening |
| 103 | // on an address that isn't 'localhost' is unsupported. |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 104 | if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) { |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 105 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 106 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (hostname) { |
| 110 | *hostname = std::move(hostname_value); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | if (port) { |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 114 | *port = port_value; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 120 | int get_host_socket_spec_port(std::string_view spec, std::string* error) { |
| 121 | int port; |
| 122 | if (spec.starts_with("tcp:")) { |
| 123 | if (!parse_tcp_socket_spec(spec, nullptr, &port, nullptr, error)) { |
| 124 | return -1; |
| 125 | } |
| 126 | } else if (spec.starts_with("vsock:")) { |
| 127 | #if ADB_LINUX |
| 128 | std::string spec_str(spec); |
| 129 | std::vector<std::string> fragments = android::base::Split(spec_str, ":"); |
| 130 | if (fragments.size() != 2) { |
| 131 | *error = "given vsock server socket string was invalid"; |
| 132 | return -1; |
| 133 | } |
| 134 | if (!android::base::ParseInt(fragments[1], &port)) { |
| 135 | *error = "could not parse vsock port"; |
| 136 | errno = EINVAL; |
| 137 | return -1; |
| 138 | } |
| 139 | if (port < 0) { |
| 140 | *error = "vsock port was negative."; |
| 141 | errno = EINVAL; |
| 142 | return -1; |
| 143 | } |
| 144 | #else // ADB_LINUX |
| 145 | *error = "vsock is only supported on linux"; |
| 146 | return -1; |
| 147 | #endif // ADB_LINUX |
| 148 | } else { |
| 149 | *error = "given socket spec string was invalid"; |
| 150 | return -1; |
| 151 | } |
| 152 | return port; |
| 153 | } |
| 154 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 155 | static bool tcp_host_is_local(std::string_view hostname) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 156 | // FIXME |
| 157 | return hostname.empty() || hostname == "localhost"; |
| 158 | } |
| 159 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 160 | bool is_socket_spec(std::string_view spec) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 161 | for (const auto& it : kLocalSocketTypes) { |
| 162 | std::string prefix = it.first + ":"; |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 163 | if (spec.starts_with(prefix)) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | } |
Andrew Walbran | ebf09dd | 2021-03-03 18:06:12 +0000 | [diff] [blame] | 167 | return spec.starts_with("tcp:") || spec.starts_with("acceptfd:") || spec.starts_with("vsock:"); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 170 | bool is_local_socket_spec(std::string_view spec) { |
Josh Gao | bb4f860 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 171 | for (const auto& it : kLocalSocketTypes) { |
| 172 | std::string prefix = it.first + ":"; |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 173 | if (spec.starts_with(prefix)) { |
Josh Gao | bb4f860 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | std::string error; |
| 179 | std::string hostname; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 180 | if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) { |
Josh Gao | bb4f860 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | return tcp_host_is_local(hostname); |
| 184 | } |
| 185 | |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 186 | bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial, |
| 187 | std::string* error) { |
Shaju Mathew | cb8d887 | 2021-11-28 17:29:21 -0800 | [diff] [blame] | 188 | #if !ADB_HOST |
| 189 | if (!socket_access_allowed) { // Check whether this security suppression is |
| 190 | // active (initiated from minadbd), and if so disable socket communications |
| 191 | // for the (min)deamon. |
| 192 | *error = "Suppressing minadbd socket communications"; |
| 193 | return false; |
| 194 | } |
| 195 | #endif |
| 196 | |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 197 | if (address.starts_with("tcp:")) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 198 | std::string hostname; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 199 | int port_value = port ? *port : 0; |
| 200 | if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) { |
| 201 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 204 | if (tcp_host_is_local(hostname)) { |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 205 | fd->reset(network_loopback_client(port_value, SOCK_STREAM, error)); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 206 | } else { |
| 207 | #if ADB_HOST |
Joshua Duong | 7ea62d8 | 2020-05-01 09:25:12 -0700 | [diff] [blame] | 208 | // Check if the address is an mdns service we can connect to. |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 209 | if (auto mdns_info = mdns_get_connect_service_info(std::string(address.substr(4))); |
Joshua Duong | 7ea62d8 | 2020-05-01 09:25:12 -0700 | [diff] [blame] | 210 | mdns_info != std::nullopt) { |
| 211 | fd->reset(network_connect(mdns_info->addr, mdns_info->port, SOCK_STREAM, 0, error)); |
| 212 | if (fd->get() != -1) { |
| 213 | // TODO(joshuaduong): We still show the ip address for the serial. Change it to |
| 214 | // use the mdns instance name, so we can adjust to address changes on |
| 215 | // reconnects. |
| 216 | port_value = mdns_info->port; |
| 217 | if (serial) { |
| 218 | *serial = android::base::StringPrintf("%s.%s", |
| 219 | mdns_info->service_name.c_str(), |
| 220 | mdns_info->service_type.c_str()); |
| 221 | } |
| 222 | } |
| 223 | } else { |
| 224 | fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error)); |
| 225 | } |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 226 | #else |
| 227 | // Disallow arbitrary connections in adbd. |
| 228 | *error = "adbd does not support arbitrary tcp connections"; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 229 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 230 | #endif |
| 231 | } |
| 232 | |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 233 | if (fd->get() > 0) { |
Josh Gao | 15d0d1a | 2021-05-04 16:29:25 -0700 | [diff] [blame] | 234 | int keepalive_interval = 1; |
| 235 | if (const char* keepalive_env = getenv("ADB_TCP_KEEPALIVE_INTERVAL")) { |
| 236 | android::base::ParseInt(keepalive_env, &keepalive_interval, 0); |
| 237 | } |
| 238 | |
| 239 | set_tcp_keepalive(fd->get(), keepalive_interval); |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 240 | disable_tcp_nagle(fd->get()); |
| 241 | if (port) { |
| 242 | *port = port_value; |
| 243 | } |
| 244 | return true; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 245 | } |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 246 | return false; |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 247 | } else if (address.starts_with("vsock:")) { |
| 248 | #if ADB_LINUX |
| 249 | std::string spec_str(address); |
| 250 | std::vector<std::string> fragments = android::base::Split(spec_str, ":"); |
| 251 | unsigned int port_value = port ? *port : 0; |
| 252 | if (fragments.size() != 2 && fragments.size() != 3) { |
Andrew Walbran | ebf09dd | 2021-03-03 18:06:12 +0000 | [diff] [blame] | 253 | *error = android::base::StringPrintf("expected vsock:cid or vsock:cid:port in '%s'", |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 254 | spec_str.c_str()); |
| 255 | errno = EINVAL; |
| 256 | return false; |
| 257 | } |
| 258 | unsigned int cid = 0; |
| 259 | if (!android::base::ParseUint(fragments[1], &cid)) { |
| 260 | *error = android::base::StringPrintf("could not parse vsock cid in '%s'", |
| 261 | spec_str.c_str()); |
| 262 | errno = EINVAL; |
| 263 | return false; |
| 264 | } |
| 265 | if (fragments.size() == 3 && !android::base::ParseUint(fragments[2], &port_value)) { |
| 266 | *error = android::base::StringPrintf("could not parse vsock port in '%s'", |
| 267 | spec_str.c_str()); |
| 268 | errno = EINVAL; |
| 269 | return false; |
| 270 | } |
| 271 | if (port_value == 0) { |
| 272 | *error = android::base::StringPrintf("vsock port was not provided."); |
| 273 | errno = EINVAL; |
| 274 | return false; |
| 275 | } |
Jeff Vander Stoep | 58a73ad | 2021-09-30 15:54:36 +0200 | [diff] [blame] | 276 | fd->reset(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0)); |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 277 | if (fd->get() == -1) { |
| 278 | *error = "could not open vsock socket"; |
| 279 | return false; |
| 280 | } |
| 281 | sockaddr_vm addr{}; |
| 282 | addr.svm_family = AF_VSOCK; |
| 283 | addr.svm_port = port_value; |
| 284 | addr.svm_cid = cid; |
| 285 | if (serial) { |
| 286 | *serial = android::base::StringPrintf("vsock:%u:%d", cid, port_value); |
| 287 | } |
| 288 | if (connect(fd->get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) { |
| 289 | int error_num = errno; |
| 290 | *error = android::base::StringPrintf("could not connect to vsock address '%s'", |
| 291 | spec_str.c_str()); |
| 292 | errno = error_num; |
| 293 | return false; |
| 294 | } |
| 295 | if (port) { |
| 296 | *port = port_value; |
| 297 | } |
| 298 | return true; |
| 299 | #else // ADB_LINUX |
Andrew Walbran | ebf09dd | 2021-03-03 18:06:12 +0000 | [diff] [blame] | 300 | *error = "vsock is only supported on Linux"; |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 301 | return false; |
| 302 | #endif // ADB_LINUX |
Daniel Colascione | 3e12469 | 2019-11-13 17:49:37 -0800 | [diff] [blame] | 303 | } else if (address.starts_with("acceptfd:")) { |
| 304 | *error = "cannot connect to acceptfd"; |
| 305 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | for (const auto& it : kLocalSocketTypes) { |
| 309 | std::string prefix = it.first + ":"; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 310 | if (address.starts_with(prefix)) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 311 | if (!it.second.available) { |
| 312 | *error = StringPrintf("socket type %s is unavailable on this platform", |
| 313 | it.first.c_str()); |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 314 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 317 | fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace, |
| 318 | SOCK_STREAM, error)); |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 319 | |
| 320 | if (fd->get() < 0) { |
| 321 | *error = |
| 322 | android::base::StringPrintf("could not connect to %s address '%s'", |
| 323 | it.first.c_str(), std::string(address).c_str()); |
| 324 | return false; |
| 325 | } |
| 326 | |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 327 | if (serial) { |
| 328 | *serial = address; |
| 329 | } |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 330 | return true; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 334 | *error = "unknown socket specification: "; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 335 | *error += address; |
| 336 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 339 | int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_port) { |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 340 | if (spec.starts_with("tcp:")) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 341 | std::string hostname; |
| 342 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 343 | if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 344 | return -1; |
| 345 | } |
| 346 | |
| 347 | int result; |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 348 | #if ADB_HOST |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 349 | if (hostname.empty() && gListenAll) { |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 350 | #else |
| 351 | if (hostname.empty()) { |
| 352 | #endif |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 353 | result = network_inaddr_any_server(port, SOCK_STREAM, error); |
| 354 | } else if (tcp_host_is_local(hostname)) { |
Callum Ryan | 04efea3 | 2019-10-31 07:21:42 -0700 | [diff] [blame] | 355 | result = network_loopback_server(port, SOCK_STREAM, error, true); |
| 356 | } else if (hostname == "::1") { |
| 357 | result = network_loopback_server(port, SOCK_STREAM, error, false); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 358 | } else { |
| 359 | // TODO: Implement me. |
| 360 | *error = "listening on specified hostname currently unsupported"; |
| 361 | return -1; |
| 362 | } |
| 363 | |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 364 | if (result >= 0 && resolved_port) { |
| 365 | *resolved_port = adb_socket_get_local_port(result); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 366 | } |
| 367 | return result; |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 368 | } else if (spec.starts_with("vsock:")) { |
| 369 | #if ADB_LINUX |
| 370 | std::string spec_str(spec); |
| 371 | std::vector<std::string> fragments = android::base::Split(spec_str, ":"); |
| 372 | if (fragments.size() != 2) { |
| 373 | *error = "given vsock server socket string was invalid"; |
| 374 | return -1; |
| 375 | } |
| 376 | int port; |
| 377 | if (!android::base::ParseInt(fragments[1], &port)) { |
| 378 | *error = "could not parse vsock port"; |
| 379 | errno = EINVAL; |
| 380 | return -1; |
| 381 | } else if (port < 0) { |
| 382 | *error = "vsock port was negative."; |
| 383 | errno = EINVAL; |
| 384 | return -1; |
| 385 | } |
Jeff Vander Stoep | 58a73ad | 2021-09-30 15:54:36 +0200 | [diff] [blame] | 386 | unique_fd serverfd(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0)); |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 387 | if (serverfd == -1) { |
| 388 | int error_num = errno; |
| 389 | *error = android::base::StringPrintf("could not create vsock server: '%s'", |
| 390 | strerror(error_num)); |
| 391 | errno = error_num; |
| 392 | return -1; |
| 393 | } |
| 394 | sockaddr_vm addr{}; |
| 395 | addr.svm_family = AF_VSOCK; |
| 396 | addr.svm_port = port == 0 ? VMADDR_PORT_ANY : port; |
| 397 | addr.svm_cid = VMADDR_CID_ANY; |
| 398 | socklen_t addr_len = sizeof(addr); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 399 | if (bind(serverfd.get(), reinterpret_cast<struct sockaddr*>(&addr), addr_len)) { |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 400 | return -1; |
| 401 | } |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 402 | if (listen(serverfd.get(), 4)) { |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 403 | return -1; |
| 404 | } |
| 405 | if (serverfd >= 0 && resolved_port) { |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 406 | if (getsockname(serverfd.get(), reinterpret_cast<sockaddr*>(&addr), &addr_len) == 0) { |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 407 | *resolved_port = addr.svm_port; |
| 408 | } else { |
| 409 | return -1; |
| 410 | } |
| 411 | } |
| 412 | return serverfd.release(); |
| 413 | #else // ADB_LINUX |
| 414 | *error = "vsock is only supported on linux"; |
| 415 | return -1; |
| 416 | #endif // ADB_LINUX |
Daniel Colascione | 3e12469 | 2019-11-13 17:49:37 -0800 | [diff] [blame] | 417 | } else if (ConsumePrefix(&spec, "acceptfd:")) { |
| 418 | #if ADB_WINDOWS |
| 419 | *error = "socket activation not supported under Windows"; |
| 420 | return -1; |
| 421 | #else |
| 422 | // We inherited the socket from some kind of launcher. It's already bound and |
| 423 | // listening. Return a copy of the FD instead of the FD itself so we implement the |
| 424 | // normal "listen" contract and can succeed more than once. |
| 425 | unsigned int fd_u; |
| 426 | if (!ParseUint(&fd_u, spec) || fd_u > std::numeric_limits<int>::max()) { |
| 427 | *error = "invalid fd"; |
| 428 | return -1; |
| 429 | } |
| 430 | int fd = static_cast<int>(fd_u); |
| 431 | int flags = get_fd_flags(fd); |
| 432 | if (flags < 0) { |
| 433 | *error = android::base::StringPrintf("could not get flags of inherited fd %d: '%s'", fd, |
| 434 | strerror(errno)); |
| 435 | return -1; |
| 436 | } |
| 437 | if (flags & FD_CLOEXEC) { |
| 438 | *error = android::base::StringPrintf("fd %d was not inherited from parent", fd); |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | int dummy_sock_type; |
| 443 | socklen_t dummy_sock_type_size = sizeof(dummy_sock_type); |
| 444 | if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &dummy_sock_type, &dummy_sock_type_size)) { |
| 445 | *error = android::base::StringPrintf("fd %d does not refer to a socket", fd); |
| 446 | return -1; |
| 447 | } |
| 448 | |
| 449 | int new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
| 450 | if (new_fd < 0) { |
| 451 | *error = android::base::StringPrintf("could not dup inherited fd %d: '%s'", fd, |
| 452 | strerror(errno)); |
| 453 | return -1; |
| 454 | } |
| 455 | return new_fd; |
| 456 | #endif |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | for (const auto& it : kLocalSocketTypes) { |
| 460 | std::string prefix = it.first + ":"; |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 461 | if (spec.starts_with(prefix)) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 462 | if (!it.second.available) { |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 463 | *error = "attempted to listen on unavailable socket type: "; |
| 464 | *error += spec; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | return network_local_server(&spec[prefix.length()], it.second.socket_namespace, |
| 469 | SOCK_STREAM, error); |
| 470 | } |
| 471 | } |
| 472 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 473 | *error = "unknown socket specification:"; |
| 474 | *error += spec; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 475 | return -1; |
| 476 | } |