| 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 |  | 
| Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 122 | static bool tcp_host_is_local(std::string_view hostname) { | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 123 | // FIXME | 
|  | 124 | return hostname.empty() || hostname == "localhost"; | 
|  | 125 | } | 
|  | 126 |  | 
| Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 127 | bool is_socket_spec(std::string_view spec) { | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 128 | for (const auto& it : kLocalSocketTypes) { | 
|  | 129 | std::string prefix = it.first + ":"; | 
| Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 130 | if (spec.starts_with(prefix)) { | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 131 | return true; | 
|  | 132 | } | 
|  | 133 | } | 
| Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 134 | return spec.starts_with("tcp:"); | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 137 | bool is_local_socket_spec(std::string_view spec) { | 
| Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 138 | for (const auto& it : kLocalSocketTypes) { | 
|  | 139 | std::string prefix = it.first + ":"; | 
| Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 140 | if (spec.starts_with(prefix)) { | 
| Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 141 | return true; | 
|  | 142 | } | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | std::string error; | 
|  | 146 | std::string hostname; | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 147 | if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) { | 
| Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 148 | return false; | 
|  | 149 | } | 
|  | 150 | return tcp_host_is_local(hostname); | 
|  | 151 | } | 
|  | 152 |  | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 153 | bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial, | 
|  | 154 | std::string* error) { | 
|  | 155 | if (address.starts_with("tcp:")) { | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 156 | std::string hostname; | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 157 | int port_value = port ? *port : 0; | 
|  | 158 | if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) { | 
|  | 159 | return false; | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 160 | } | 
|  | 161 |  | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 162 | if (tcp_host_is_local(hostname)) { | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 163 | fd->reset(network_loopback_client(port_value, SOCK_STREAM, error)); | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 164 | } else { | 
|  | 165 | #if ADB_HOST | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 166 | fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error)); | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 167 | #else | 
|  | 168 | // Disallow arbitrary connections in adbd. | 
|  | 169 | *error = "adbd does not support arbitrary tcp connections"; | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 170 | return false; | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 171 | #endif | 
|  | 172 | } | 
|  | 173 |  | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 174 | if (fd->get() > 0) { | 
|  | 175 | disable_tcp_nagle(fd->get()); | 
|  | 176 | if (port) { | 
|  | 177 | *port = port_value; | 
|  | 178 | } | 
|  | 179 | return true; | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 180 | } | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 181 | return false; | 
| Cody Schuffelen | a05b64d | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 182 | } else if (address.starts_with("vsock:")) { | 
|  | 183 | #if ADB_LINUX | 
|  | 184 | std::string spec_str(address); | 
|  | 185 | std::vector<std::string> fragments = android::base::Split(spec_str, ":"); | 
|  | 186 | unsigned int port_value = port ? *port : 0; | 
|  | 187 | if (fragments.size() != 2 && fragments.size() != 3) { | 
|  | 188 | *error = android::base::StringPrintf("expected vsock:cid or vsock:port:cid in '%s'", | 
|  | 189 | spec_str.c_str()); | 
|  | 190 | errno = EINVAL; | 
|  | 191 | return false; | 
|  | 192 | } | 
|  | 193 | unsigned int cid = 0; | 
|  | 194 | if (!android::base::ParseUint(fragments[1], &cid)) { | 
|  | 195 | *error = android::base::StringPrintf("could not parse vsock cid in '%s'", | 
|  | 196 | spec_str.c_str()); | 
|  | 197 | errno = EINVAL; | 
|  | 198 | return false; | 
|  | 199 | } | 
|  | 200 | if (fragments.size() == 3 && !android::base::ParseUint(fragments[2], &port_value)) { | 
|  | 201 | *error = android::base::StringPrintf("could not parse vsock port in '%s'", | 
|  | 202 | spec_str.c_str()); | 
|  | 203 | errno = EINVAL; | 
|  | 204 | return false; | 
|  | 205 | } | 
|  | 206 | if (port_value == 0) { | 
|  | 207 | *error = android::base::StringPrintf("vsock port was not provided."); | 
|  | 208 | errno = EINVAL; | 
|  | 209 | return false; | 
|  | 210 | } | 
|  | 211 | fd->reset(socket(AF_VSOCK, SOCK_STREAM, 0)); | 
|  | 212 | if (fd->get() == -1) { | 
|  | 213 | *error = "could not open vsock socket"; | 
|  | 214 | return false; | 
|  | 215 | } | 
|  | 216 | sockaddr_vm addr{}; | 
|  | 217 | addr.svm_family = AF_VSOCK; | 
|  | 218 | addr.svm_port = port_value; | 
|  | 219 | addr.svm_cid = cid; | 
|  | 220 | if (serial) { | 
|  | 221 | *serial = android::base::StringPrintf("vsock:%u:%d", cid, port_value); | 
|  | 222 | } | 
|  | 223 | if (connect(fd->get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) { | 
|  | 224 | int error_num = errno; | 
|  | 225 | *error = android::base::StringPrintf("could not connect to vsock address '%s'", | 
|  | 226 | spec_str.c_str()); | 
|  | 227 | errno = error_num; | 
|  | 228 | return false; | 
|  | 229 | } | 
|  | 230 | if (port) { | 
|  | 231 | *port = port_value; | 
|  | 232 | } | 
|  | 233 | return true; | 
|  | 234 | #else   // ADB_LINUX | 
|  | 235 | *error = "vsock is only supported on linux"; | 
|  | 236 | return false; | 
|  | 237 | #endif  // ADB_LINUX | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 238 | } | 
|  | 239 |  | 
|  | 240 | for (const auto& it : kLocalSocketTypes) { | 
|  | 241 | std::string prefix = it.first + ":"; | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 242 | if (address.starts_with(prefix)) { | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 243 | if (!it.second.available) { | 
|  | 244 | *error = StringPrintf("socket type %s is unavailable on this platform", | 
|  | 245 | it.first.c_str()); | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 246 | return false; | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 249 | fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace, | 
|  | 250 | SOCK_STREAM, error)); | 
| Cody Schuffelen | a05b64d | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 251 | if (serial) { | 
|  | 252 | *serial = address; | 
|  | 253 | } | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 254 | return true; | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 255 | } | 
|  | 256 | } | 
|  | 257 |  | 
| Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 258 | *error = "unknown socket specification: "; | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 259 | *error += address; | 
|  | 260 | return false; | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
| Cody Schuffelen | a05b64d | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 263 | 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] | 264 | if (spec.starts_with("tcp:")) { | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 265 | std::string hostname; | 
|  | 266 | int port; | 
| Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 267 | if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) { | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 268 | return -1; | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | int result; | 
|  | 272 | if (hostname.empty() && gListenAll) { | 
|  | 273 | result = network_inaddr_any_server(port, SOCK_STREAM, error); | 
|  | 274 | } else if (tcp_host_is_local(hostname)) { | 
|  | 275 | result = network_loopback_server(port, SOCK_STREAM, error); | 
|  | 276 | } else { | 
|  | 277 | // TODO: Implement me. | 
|  | 278 | *error = "listening on specified hostname currently unsupported"; | 
|  | 279 | return -1; | 
|  | 280 | } | 
|  | 281 |  | 
| Cody Schuffelen | a05b64d | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 282 | if (result >= 0 && resolved_port) { | 
|  | 283 | *resolved_port = adb_socket_get_local_port(result); | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 284 | } | 
|  | 285 | return result; | 
| Cody Schuffelen | a05b64d | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 286 | } else if (spec.starts_with("vsock:")) { | 
|  | 287 | #if ADB_LINUX | 
|  | 288 | std::string spec_str(spec); | 
|  | 289 | std::vector<std::string> fragments = android::base::Split(spec_str, ":"); | 
|  | 290 | if (fragments.size() != 2) { | 
|  | 291 | *error = "given vsock server socket string was invalid"; | 
|  | 292 | return -1; | 
|  | 293 | } | 
|  | 294 | int port; | 
|  | 295 | if (!android::base::ParseInt(fragments[1], &port)) { | 
|  | 296 | *error = "could not parse vsock port"; | 
|  | 297 | errno = EINVAL; | 
|  | 298 | return -1; | 
|  | 299 | } else if (port < 0) { | 
|  | 300 | *error = "vsock port was negative."; | 
|  | 301 | errno = EINVAL; | 
|  | 302 | return -1; | 
|  | 303 | } | 
|  | 304 | unique_fd serverfd(socket(AF_VSOCK, SOCK_STREAM, 0)); | 
|  | 305 | if (serverfd == -1) { | 
|  | 306 | int error_num = errno; | 
|  | 307 | *error = android::base::StringPrintf("could not create vsock server: '%s'", | 
|  | 308 | strerror(error_num)); | 
|  | 309 | errno = error_num; | 
|  | 310 | return -1; | 
|  | 311 | } | 
|  | 312 | sockaddr_vm addr{}; | 
|  | 313 | addr.svm_family = AF_VSOCK; | 
|  | 314 | addr.svm_port = port == 0 ? VMADDR_PORT_ANY : port; | 
|  | 315 | addr.svm_cid = VMADDR_CID_ANY; | 
|  | 316 | socklen_t addr_len = sizeof(addr); | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 317 | if (bind(serverfd.get(), reinterpret_cast<struct sockaddr*>(&addr), addr_len)) { | 
| Cody Schuffelen | a05b64d | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 318 | return -1; | 
|  | 319 | } | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 320 | if (listen(serverfd.get(), 4)) { | 
| Cody Schuffelen | a05b64d | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 321 | return -1; | 
|  | 322 | } | 
|  | 323 | if (serverfd >= 0 && resolved_port) { | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 324 | if (getsockname(serverfd.get(), reinterpret_cast<sockaddr*>(&addr), &addr_len) == 0) { | 
| Cody Schuffelen | a05b64d | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 325 | *resolved_port = addr.svm_port; | 
|  | 326 | } else { | 
|  | 327 | return -1; | 
|  | 328 | } | 
|  | 329 | } | 
|  | 330 | return serverfd.release(); | 
|  | 331 | #else   // ADB_LINUX | 
|  | 332 | *error = "vsock is only supported on linux"; | 
|  | 333 | return -1; | 
|  | 334 | #endif  // ADB_LINUX | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 335 | } | 
|  | 336 |  | 
|  | 337 | for (const auto& it : kLocalSocketTypes) { | 
|  | 338 | std::string prefix = it.first + ":"; | 
| Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 339 | if (spec.starts_with(prefix)) { | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 340 | if (!it.second.available) { | 
| Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 341 | *error = "attempted to listen on unavailable socket type: "; | 
|  | 342 | *error += spec; | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 343 | return -1; | 
|  | 344 | } | 
|  | 345 |  | 
|  | 346 | return network_local_server(&spec[prefix.length()], it.second.socket_namespace, | 
|  | 347 | SOCK_STREAM, error); | 
|  | 348 | } | 
|  | 349 | } | 
|  | 350 |  | 
| Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 351 | *error = "unknown socket specification:"; | 
|  | 352 | *error += spec; | 
| Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 353 | return -1; | 
|  | 354 | } |