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