blob: 5cad70d09490d8cd3989da4c728e93393b1e71d4 [file] [log] [blame]
Josh Gaocfb21412016-08-24 18:38:44 -07001/*
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 Colascione3ec7be72019-11-13 17:49:37 -080019#include <limits>
Josh Gaocfb21412016-08-24 18:38:44 -070020#include <string>
Josh Gao9dd1f5c2018-12-13 14:04:04 -080021#include <string_view>
Josh Gaocfb21412016-08-24 18:38:44 -070022#include <unordered_map>
23#include <vector>
24
Josh Gao10992152016-09-19 17:31:55 -070025#include <android-base/parseint.h>
26#include <android-base/parsenetaddress.h>
Josh Gaocfb21412016-08-24 18:38:44 -070027#include <android-base/stringprintf.h>
28#include <android-base/strings.h>
29#include <cutils/sockets.h>
30
31#include "adb.h"
Daniel Colascione3ec7be72019-11-13 17:49:37 -080032#include "adb_utils.h"
Joshua Duong7ebc5952020-05-01 09:25:12 -070033#include "adb_wifi.h"
Josh Gaocfb21412016-08-24 18:38:44 -070034#include "sysdeps.h"
35
Josh Gao9dd1f5c2018-12-13 14:04:04 -080036using namespace std::string_literals;
37
Daniel Colascione3ec7be72019-11-13 17:49:37 -080038using android::base::ConsumePrefix;
Josh Gaocfb21412016-08-24 18:38:44 -070039using android::base::StringPrintf;
40
41#if defined(__linux__)
42#define ADB_LINUX 1
43#else
44#define ADB_LINUX 0
45#endif
46
47#if defined(_WIN32)
48#define ADB_WINDOWS 1
49#else
50#define ADB_WINDOWS 0
51#endif
52
Cody Schuffelena05b64d2019-01-04 18:51:11 -080053#if ADB_LINUX
54#include <sys/socket.h>
55#include "sysdeps/vm_sockets.h"
56#endif
57
Josh Gaocfb21412016-08-24 18:38:44 -070058// Not static because it is used in commandline.c.
59int gListenAll = 0;
60
61struct LocalSocketType {
62 int socket_namespace;
63 bool available;
64};
65
66static 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 Gao9dd1f5c2018-12-13 14:04:04 -080078bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port,
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080079 std::string* serial, std::string* error) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -080080 if (!spec.starts_with("tcp:")) {
81 *error = "specification is not tcp: ";
82 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -070083 return false;
84 }
85
Josh Gao10992152016-09-19 17:31:55 -070086 std::string hostname_value;
87 int port_value;
Josh Gaocfb21412016-08-24 18:38:44 -070088
Josh Gao10992152016-09-19 17:31:55 -070089 // 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 Gaocfb21412016-08-24 18:38:44 -070096 return false;
97 }
Josh Gao10992152016-09-19 17:31:55 -070098 } else {
Josh Gao9dd1f5c2018-12-13 14:04:04 -080099 std::string addr(spec.substr(4));
Elliott Hughes573e67c2019-07-30 13:51:03 -0700100 port_value = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
Josh Gao10992152016-09-19 17:31:55 -0700101
102 // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening
103 // on an address that isn't 'localhost' is unsupported.
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800104 if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) {
Josh Gao10992152016-09-19 17:31:55 -0700105 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700106 }
Josh Gao10992152016-09-19 17:31:55 -0700107 }
108
109 if (hostname) {
110 *hostname = std::move(hostname_value);
Josh Gaocfb21412016-08-24 18:38:44 -0700111 }
112
113 if (port) {
Josh Gao10992152016-09-19 17:31:55 -0700114 *port = port_value;
Josh Gaocfb21412016-08-24 18:38:44 -0700115 }
116
117 return true;
118}
119
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900120int 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 Gao9dd1f5c2018-12-13 14:04:04 -0800155static bool tcp_host_is_local(std::string_view hostname) {
Josh Gaocfb21412016-08-24 18:38:44 -0700156 // FIXME
157 return hostname.empty() || hostname == "localhost";
158}
159
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800160bool is_socket_spec(std::string_view spec) {
Josh Gaocfb21412016-08-24 18:38:44 -0700161 for (const auto& it : kLocalSocketTypes) {
162 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800163 if (spec.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700164 return true;
165 }
166 }
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800167 return spec.starts_with("tcp:") || spec.starts_with("acceptfd:");
Josh Gaocfb21412016-08-24 18:38:44 -0700168}
169
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800170bool is_local_socket_spec(std::string_view spec) {
Josh Gao9c869b52016-08-25 16:00:22 -0700171 for (const auto& it : kLocalSocketTypes) {
172 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800173 if (spec.starts_with(prefix)) {
Josh Gao9c869b52016-08-25 16:00:22 -0700174 return true;
175 }
176 }
177
178 std::string error;
179 std::string hostname;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800180 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) {
Josh Gao9c869b52016-08-25 16:00:22 -0700181 return false;
182 }
183 return tcp_host_is_local(hostname);
184}
185
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800186bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial,
187 std::string* error) {
188 if (address.starts_with("tcp:")) {
Josh Gaocfb21412016-08-24 18:38:44 -0700189 std::string hostname;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800190 int port_value = port ? *port : 0;
191 if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) {
192 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700193 }
194
Josh Gaocfb21412016-08-24 18:38:44 -0700195 if (tcp_host_is_local(hostname)) {
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800196 fd->reset(network_loopback_client(port_value, SOCK_STREAM, error));
Josh Gaocfb21412016-08-24 18:38:44 -0700197 } else {
198#if ADB_HOST
Joshua Duong7ebc5952020-05-01 09:25:12 -0700199 // Check if the address is an mdns service we can connect to.
200 if (auto mdns_info = mdns_get_connect_service_info(address.substr(4));
201 mdns_info != std::nullopt) {
202 fd->reset(network_connect(mdns_info->addr, mdns_info->port, SOCK_STREAM, 0, error));
203 if (fd->get() != -1) {
204 // TODO(joshuaduong): We still show the ip address for the serial. Change it to
205 // use the mdns instance name, so we can adjust to address changes on
206 // reconnects.
207 port_value = mdns_info->port;
208 if (serial) {
209 *serial = android::base::StringPrintf("%s.%s",
210 mdns_info->service_name.c_str(),
211 mdns_info->service_type.c_str());
212 }
213 }
214 } else {
215 fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error));
216 }
Josh Gaocfb21412016-08-24 18:38:44 -0700217#else
218 // Disallow arbitrary connections in adbd.
219 *error = "adbd does not support arbitrary tcp connections";
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800220 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700221#endif
222 }
223
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800224 if (fd->get() > 0) {
225 disable_tcp_nagle(fd->get());
226 if (port) {
227 *port = port_value;
228 }
229 return true;
Josh Gaocfb21412016-08-24 18:38:44 -0700230 }
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800231 return false;
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800232 } else if (address.starts_with("vsock:")) {
233#if ADB_LINUX
234 std::string spec_str(address);
235 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
236 unsigned int port_value = port ? *port : 0;
237 if (fragments.size() != 2 && fragments.size() != 3) {
238 *error = android::base::StringPrintf("expected vsock:cid or vsock:port:cid in '%s'",
239 spec_str.c_str());
240 errno = EINVAL;
241 return false;
242 }
243 unsigned int cid = 0;
244 if (!android::base::ParseUint(fragments[1], &cid)) {
245 *error = android::base::StringPrintf("could not parse vsock cid in '%s'",
246 spec_str.c_str());
247 errno = EINVAL;
248 return false;
249 }
250 if (fragments.size() == 3 && !android::base::ParseUint(fragments[2], &port_value)) {
251 *error = android::base::StringPrintf("could not parse vsock port in '%s'",
252 spec_str.c_str());
253 errno = EINVAL;
254 return false;
255 }
256 if (port_value == 0) {
257 *error = android::base::StringPrintf("vsock port was not provided.");
258 errno = EINVAL;
259 return false;
260 }
261 fd->reset(socket(AF_VSOCK, SOCK_STREAM, 0));
262 if (fd->get() == -1) {
263 *error = "could not open vsock socket";
264 return false;
265 }
266 sockaddr_vm addr{};
267 addr.svm_family = AF_VSOCK;
268 addr.svm_port = port_value;
269 addr.svm_cid = cid;
270 if (serial) {
271 *serial = android::base::StringPrintf("vsock:%u:%d", cid, port_value);
272 }
273 if (connect(fd->get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) {
274 int error_num = errno;
275 *error = android::base::StringPrintf("could not connect to vsock address '%s'",
276 spec_str.c_str());
277 errno = error_num;
278 return false;
279 }
280 if (port) {
281 *port = port_value;
282 }
283 return true;
284#else // ADB_LINUX
285 *error = "vsock is only supported on linux";
286 return false;
287#endif // ADB_LINUX
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800288 } else if (address.starts_with("acceptfd:")) {
289 *error = "cannot connect to acceptfd";
290 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700291 }
292
293 for (const auto& it : kLocalSocketTypes) {
294 std::string prefix = it.first + ":";
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800295 if (address.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700296 if (!it.second.available) {
297 *error = StringPrintf("socket type %s is unavailable on this platform",
298 it.first.c_str());
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800299 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700300 }
301
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800302 fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace,
303 SOCK_STREAM, error));
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900304
305 if (fd->get() < 0) {
306 *error =
307 android::base::StringPrintf("could not connect to %s address '%s'",
308 it.first.c_str(), std::string(address).c_str());
309 return false;
310 }
311
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800312 if (serial) {
313 *serial = address;
314 }
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800315 return true;
Josh Gaocfb21412016-08-24 18:38:44 -0700316 }
317 }
318
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800319 *error = "unknown socket specification: ";
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800320 *error += address;
321 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700322}
323
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800324int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_port) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800325 if (spec.starts_with("tcp:")) {
Josh Gaocfb21412016-08-24 18:38:44 -0700326 std::string hostname;
327 int port;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800328 if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700329 return -1;
330 }
331
332 int result;
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900333#if ADB_HOST
Josh Gaocfb21412016-08-24 18:38:44 -0700334 if (hostname.empty() && gListenAll) {
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900335#else
336 if (hostname.empty()) {
337#endif
Josh Gaocfb21412016-08-24 18:38:44 -0700338 result = network_inaddr_any_server(port, SOCK_STREAM, error);
339 } else if (tcp_host_is_local(hostname)) {
Callum Ryan8539cb32019-10-31 07:21:42 -0700340 result = network_loopback_server(port, SOCK_STREAM, error, true);
341 } else if (hostname == "::1") {
342 result = network_loopback_server(port, SOCK_STREAM, error, false);
Josh Gaocfb21412016-08-24 18:38:44 -0700343 } else {
344 // TODO: Implement me.
345 *error = "listening on specified hostname currently unsupported";
346 return -1;
347 }
348
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800349 if (result >= 0 && resolved_port) {
350 *resolved_port = adb_socket_get_local_port(result);
Josh Gaocfb21412016-08-24 18:38:44 -0700351 }
352 return result;
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800353 } else if (spec.starts_with("vsock:")) {
354#if ADB_LINUX
355 std::string spec_str(spec);
356 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
357 if (fragments.size() != 2) {
358 *error = "given vsock server socket string was invalid";
359 return -1;
360 }
361 int port;
362 if (!android::base::ParseInt(fragments[1], &port)) {
363 *error = "could not parse vsock port";
364 errno = EINVAL;
365 return -1;
366 } else if (port < 0) {
367 *error = "vsock port was negative.";
368 errno = EINVAL;
369 return -1;
370 }
371 unique_fd serverfd(socket(AF_VSOCK, SOCK_STREAM, 0));
372 if (serverfd == -1) {
373 int error_num = errno;
374 *error = android::base::StringPrintf("could not create vsock server: '%s'",
375 strerror(error_num));
376 errno = error_num;
377 return -1;
378 }
379 sockaddr_vm addr{};
380 addr.svm_family = AF_VSOCK;
381 addr.svm_port = port == 0 ? VMADDR_PORT_ANY : port;
382 addr.svm_cid = VMADDR_CID_ANY;
383 socklen_t addr_len = sizeof(addr);
Josh Gao27241a72019-04-25 14:04:57 -0700384 if (bind(serverfd.get(), reinterpret_cast<struct sockaddr*>(&addr), addr_len)) {
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800385 return -1;
386 }
Josh Gao27241a72019-04-25 14:04:57 -0700387 if (listen(serverfd.get(), 4)) {
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800388 return -1;
389 }
390 if (serverfd >= 0 && resolved_port) {
Josh Gao27241a72019-04-25 14:04:57 -0700391 if (getsockname(serverfd.get(), reinterpret_cast<sockaddr*>(&addr), &addr_len) == 0) {
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800392 *resolved_port = addr.svm_port;
393 } else {
394 return -1;
395 }
396 }
397 return serverfd.release();
398#else // ADB_LINUX
399 *error = "vsock is only supported on linux";
400 return -1;
401#endif // ADB_LINUX
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800402 } else if (ConsumePrefix(&spec, "acceptfd:")) {
403#if ADB_WINDOWS
404 *error = "socket activation not supported under Windows";
405 return -1;
406#else
407 // We inherited the socket from some kind of launcher. It's already bound and
408 // listening. Return a copy of the FD instead of the FD itself so we implement the
409 // normal "listen" contract and can succeed more than once.
410 unsigned int fd_u;
411 if (!ParseUint(&fd_u, spec) || fd_u > std::numeric_limits<int>::max()) {
412 *error = "invalid fd";
413 return -1;
414 }
415 int fd = static_cast<int>(fd_u);
416 int flags = get_fd_flags(fd);
417 if (flags < 0) {
418 *error = android::base::StringPrintf("could not get flags of inherited fd %d: '%s'", fd,
419 strerror(errno));
420 return -1;
421 }
422 if (flags & FD_CLOEXEC) {
423 *error = android::base::StringPrintf("fd %d was not inherited from parent", fd);
424 return -1;
425 }
426
427 int dummy_sock_type;
428 socklen_t dummy_sock_type_size = sizeof(dummy_sock_type);
429 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &dummy_sock_type, &dummy_sock_type_size)) {
430 *error = android::base::StringPrintf("fd %d does not refer to a socket", fd);
431 return -1;
432 }
433
434 int new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
435 if (new_fd < 0) {
436 *error = android::base::StringPrintf("could not dup inherited fd %d: '%s'", fd,
437 strerror(errno));
438 return -1;
439 }
440 return new_fd;
441#endif
Josh Gaocfb21412016-08-24 18:38:44 -0700442 }
443
444 for (const auto& it : kLocalSocketTypes) {
445 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800446 if (spec.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700447 if (!it.second.available) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800448 *error = "attempted to listen on unavailable socket type: ";
449 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -0700450 return -1;
451 }
452
453 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
454 SOCK_STREAM, error);
455 }
456 }
457
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800458 *error = "unknown socket specification:";
459 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -0700460 return -1;
461}