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