blob: 5e0678aa6f2f9f8d2464614c35acdc8e665f8deb [file] [log] [blame]
Josh Gao4a5a95d2016-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 Colascione3e124692019-11-13 17:49:37 -080019#include <limits>
Josh Gao4a5a95d2016-08-24 18:38:44 -070020#include <string>
Josh Gaoab9958e2018-12-13 14:04:04 -080021#include <string_view>
Josh Gao4a5a95d2016-08-24 18:38:44 -070022#include <unordered_map>
23#include <vector>
24
Josh Gao3fb517c2016-09-19 17:31:55 -070025#include <android-base/parseint.h>
26#include <android-base/parsenetaddress.h>
Josh Gao4a5a95d2016-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"
Shaju Mathewcb8d8872021-11-28 17:29:21 -080032#include "adb_auth.h"
Joshua Duongf4ba8d72021-01-13 12:18:15 -080033#include "adb_mdns.h"
Daniel Colascione3e124692019-11-13 17:49:37 -080034#include "adb_utils.h"
Josh Gao4a5a95d2016-08-24 18:38:44 -070035#include "sysdeps.h"
36
Josh Gaoab9958e2018-12-13 14:04:04 -080037using namespace std::string_literals;
38
Daniel Colascione3e124692019-11-13 17:49:37 -080039using android::base::ConsumePrefix;
Josh Gao4a5a95d2016-08-24 18:38:44 -070040using 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 Schuffelen637aaf52019-01-04 18:51:11 -080054#if ADB_LINUX
55#include <sys/socket.h>
56#include "sysdeps/vm_sockets.h"
57#endif
58
Josh Gao4a5a95d2016-08-24 18:38:44 -070059// Not static because it is used in commandline.c.
60int gListenAll = 0;
61
62struct LocalSocketType {
63 int socket_namespace;
64 bool available;
65};
66
67static auto& kLocalSocketTypes = *new std::unordered_map<std::string, LocalSocketType>({
68#if ADB_HOST
69 { "local", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
70#else
71 { "local", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_WINDOWS } },
72#endif
73
74 { "localreserved", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_HOST } },
75 { "localabstract", { ANDROID_SOCKET_NAMESPACE_ABSTRACT, ADB_LINUX } },
76 { "localfilesystem", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
77});
78
Josh Gaoab9958e2018-12-13 14:04:04 -080079bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port,
Cody Schuffelen331a9082019-01-02 14:17:29 -080080 std::string* serial, std::string* error) {
Josh Gaoab9958e2018-12-13 14:04:04 -080081 if (!spec.starts_with("tcp:")) {
82 *error = "specification is not tcp: ";
83 *error += spec;
Josh Gao4a5a95d2016-08-24 18:38:44 -070084 return false;
85 }
86
Josh Gao3fb517c2016-09-19 17:31:55 -070087 std::string hostname_value;
88 int port_value;
Josh Gao4a5a95d2016-08-24 18:38:44 -070089
Josh Gao3fb517c2016-09-19 17:31:55 -070090 // If the spec is tcp:<port>, parse it ourselves.
91 // Otherwise, delegate to android::base::ParseNetAddress.
92 if (android::base::ParseInt(&spec[4], &port_value)) {
93 // Do the range checking ourselves, because ParseInt rejects 'tcp:65536' and 'tcp:foo:1234'
94 // identically.
95 if (port_value < 0 || port_value > 65535) {
96 *error = StringPrintf("bad port number '%d'", port_value);
Josh Gao4a5a95d2016-08-24 18:38:44 -070097 return false;
98 }
Josh Gao3fb517c2016-09-19 17:31:55 -070099 } else {
Josh Gaoab9958e2018-12-13 14:04:04 -0800100 std::string addr(spec.substr(4));
Elliott Hughes14d673e2019-07-30 13:51:03 -0700101 port_value = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
Josh Gao3fb517c2016-09-19 17:31:55 -0700102
103 // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening
104 // on an address that isn't 'localhost' is unsupported.
Cody Schuffelen331a9082019-01-02 14:17:29 -0800105 if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) {
Josh Gao3fb517c2016-09-19 17:31:55 -0700106 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700107 }
Josh Gao3fb517c2016-09-19 17:31:55 -0700108 }
109
110 if (hostname) {
111 *hostname = std::move(hostname_value);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700112 }
113
114 if (port) {
Josh Gao3fb517c2016-09-19 17:31:55 -0700115 *port = port_value;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700116 }
117
118 return true;
119}
120
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900121int get_host_socket_spec_port(std::string_view spec, std::string* error) {
122 int port;
123 if (spec.starts_with("tcp:")) {
124 if (!parse_tcp_socket_spec(spec, nullptr, &port, nullptr, error)) {
125 return -1;
126 }
127 } else if (spec.starts_with("vsock:")) {
128#if ADB_LINUX
129 std::string spec_str(spec);
130 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
131 if (fragments.size() != 2) {
132 *error = "given vsock server socket string was invalid";
133 return -1;
134 }
135 if (!android::base::ParseInt(fragments[1], &port)) {
136 *error = "could not parse vsock port";
137 errno = EINVAL;
138 return -1;
139 }
140 if (port < 0) {
141 *error = "vsock port was negative.";
142 errno = EINVAL;
143 return -1;
144 }
145#else // ADB_LINUX
146 *error = "vsock is only supported on linux";
147 return -1;
148#endif // ADB_LINUX
149 } else {
150 *error = "given socket spec string was invalid";
151 return -1;
152 }
153 return port;
154}
155
Josh Gaoab9958e2018-12-13 14:04:04 -0800156static bool tcp_host_is_local(std::string_view hostname) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700157 // FIXME
158 return hostname.empty() || hostname == "localhost";
159}
160
Josh Gaoab9958e2018-12-13 14:04:04 -0800161bool is_socket_spec(std::string_view spec) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700162 for (const auto& it : kLocalSocketTypes) {
163 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800164 if (spec.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700165 return true;
166 }
167 }
Andrew Walbranebf09dd2021-03-03 18:06:12 +0000168 return spec.starts_with("tcp:") || spec.starts_with("acceptfd:") || spec.starts_with("vsock:");
Josh Gao4a5a95d2016-08-24 18:38:44 -0700169}
170
Josh Gaoab9958e2018-12-13 14:04:04 -0800171bool is_local_socket_spec(std::string_view spec) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700172 for (const auto& it : kLocalSocketTypes) {
173 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800174 if (spec.starts_with(prefix)) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700175 return true;
176 }
177 }
178
179 std::string error;
180 std::string hostname;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800181 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700182 return false;
183 }
184 return tcp_host_is_local(hostname);
185}
186
Cody Schuffelen331a9082019-01-02 14:17:29 -0800187bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial,
188 std::string* error) {
Shaju Mathewcb8d8872021-11-28 17:29:21 -0800189#if !ADB_HOST
190 if (!socket_access_allowed) { // Check whether this security suppression is
191 // active (initiated from minadbd), and if so disable socket communications
192 // for the (min)deamon.
193 *error = "Suppressing minadbd socket communications";
194 return false;
195 }
196#endif
197
Cody Schuffelen331a9082019-01-02 14:17:29 -0800198 if (address.starts_with("tcp:")) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700199 std::string hostname;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800200 int port_value = port ? *port : 0;
201 if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) {
202 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700203 }
204
Josh Gao4a5a95d2016-08-24 18:38:44 -0700205 if (tcp_host_is_local(hostname)) {
Cody Schuffelen331a9082019-01-02 14:17:29 -0800206 fd->reset(network_loopback_client(port_value, SOCK_STREAM, error));
Josh Gao4a5a95d2016-08-24 18:38:44 -0700207 } else {
208#if ADB_HOST
Joshua Duong7ea62d82020-05-01 09:25:12 -0700209 // Check if the address is an mdns service we can connect to.
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800210 if (auto mdns_info = mdns_get_connect_service_info(std::string(address.substr(4)));
Joshua Duong7ea62d82020-05-01 09:25:12 -0700211 mdns_info != std::nullopt) {
212 fd->reset(network_connect(mdns_info->addr, mdns_info->port, SOCK_STREAM, 0, error));
213 if (fd->get() != -1) {
214 // TODO(joshuaduong): We still show the ip address for the serial. Change it to
215 // use the mdns instance name, so we can adjust to address changes on
216 // reconnects.
217 port_value = mdns_info->port;
218 if (serial) {
219 *serial = android::base::StringPrintf("%s.%s",
220 mdns_info->service_name.c_str(),
221 mdns_info->service_type.c_str());
222 }
223 }
224 } else {
225 fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error));
226 }
Josh Gao4a5a95d2016-08-24 18:38:44 -0700227#else
228 // Disallow arbitrary connections in adbd.
229 *error = "adbd does not support arbitrary tcp connections";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800230 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700231#endif
232 }
233
Cody Schuffelen331a9082019-01-02 14:17:29 -0800234 if (fd->get() > 0) {
Josh Gao15d0d1a2021-05-04 16:29:25 -0700235 int keepalive_interval = 1;
236 if (const char* keepalive_env = getenv("ADB_TCP_KEEPALIVE_INTERVAL")) {
237 android::base::ParseInt(keepalive_env, &keepalive_interval, 0);
238 }
239
240 set_tcp_keepalive(fd->get(), keepalive_interval);
Cody Schuffelen331a9082019-01-02 14:17:29 -0800241 disable_tcp_nagle(fd->get());
242 if (port) {
243 *port = port_value;
244 }
245 return true;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700246 }
Cody Schuffelen331a9082019-01-02 14:17:29 -0800247 return false;
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800248 } else if (address.starts_with("vsock:")) {
249#if ADB_LINUX
250 std::string spec_str(address);
251 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
252 unsigned int port_value = port ? *port : 0;
253 if (fragments.size() != 2 && fragments.size() != 3) {
Andrew Walbranebf09dd2021-03-03 18:06:12 +0000254 *error = android::base::StringPrintf("expected vsock:cid or vsock:cid:port in '%s'",
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800255 spec_str.c_str());
256 errno = EINVAL;
257 return false;
258 }
259 unsigned int cid = 0;
260 if (!android::base::ParseUint(fragments[1], &cid)) {
261 *error = android::base::StringPrintf("could not parse vsock cid in '%s'",
262 spec_str.c_str());
263 errno = EINVAL;
264 return false;
265 }
266 if (fragments.size() == 3 && !android::base::ParseUint(fragments[2], &port_value)) {
267 *error = android::base::StringPrintf("could not parse vsock port in '%s'",
268 spec_str.c_str());
269 errno = EINVAL;
270 return false;
271 }
272 if (port_value == 0) {
273 *error = android::base::StringPrintf("vsock port was not provided.");
274 errno = EINVAL;
275 return false;
276 }
Jeff Vander Stoep58a73ad2021-09-30 15:54:36 +0200277 fd->reset(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0));
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800278 if (fd->get() == -1) {
279 *error = "could not open vsock socket";
280 return false;
281 }
282 sockaddr_vm addr{};
283 addr.svm_family = AF_VSOCK;
284 addr.svm_port = port_value;
285 addr.svm_cid = cid;
286 if (serial) {
287 *serial = android::base::StringPrintf("vsock:%u:%d", cid, port_value);
288 }
289 if (connect(fd->get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) {
290 int error_num = errno;
291 *error = android::base::StringPrintf("could not connect to vsock address '%s'",
292 spec_str.c_str());
293 errno = error_num;
294 return false;
295 }
296 if (port) {
297 *port = port_value;
298 }
299 return true;
300#else // ADB_LINUX
Andrew Walbranebf09dd2021-03-03 18:06:12 +0000301 *error = "vsock is only supported on Linux";
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800302 return false;
303#endif // ADB_LINUX
Daniel Colascione3e124692019-11-13 17:49:37 -0800304 } else if (address.starts_with("acceptfd:")) {
305 *error = "cannot connect to acceptfd";
306 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700307 }
308
309 for (const auto& it : kLocalSocketTypes) {
310 std::string prefix = it.first + ":";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800311 if (address.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700312 if (!it.second.available) {
313 *error = StringPrintf("socket type %s is unavailable on this platform",
314 it.first.c_str());
Cody Schuffelen331a9082019-01-02 14:17:29 -0800315 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700316 }
317
Cody Schuffelen331a9082019-01-02 14:17:29 -0800318 fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace,
319 SOCK_STREAM, error));
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900320
321 if (fd->get() < 0) {
322 *error =
323 android::base::StringPrintf("could not connect to %s address '%s'",
324 it.first.c_str(), std::string(address).c_str());
325 return false;
326 }
327
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800328 if (serial) {
329 *serial = address;
330 }
Cody Schuffelen331a9082019-01-02 14:17:29 -0800331 return true;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700332 }
333 }
334
Josh Gaoab9958e2018-12-13 14:04:04 -0800335 *error = "unknown socket specification: ";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800336 *error += address;
337 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700338}
339
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800340int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_port) {
Josh Gaoab9958e2018-12-13 14:04:04 -0800341 if (spec.starts_with("tcp:")) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700342 std::string hostname;
343 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800344 if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700345 return -1;
346 }
347
348 int result;
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900349#if ADB_HOST
Josh Gao4a5a95d2016-08-24 18:38:44 -0700350 if (hostname.empty() && gListenAll) {
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900351#else
352 if (hostname.empty()) {
353#endif
Josh Gao4a5a95d2016-08-24 18:38:44 -0700354 result = network_inaddr_any_server(port, SOCK_STREAM, error);
355 } else if (tcp_host_is_local(hostname)) {
Callum Ryan04efea32019-10-31 07:21:42 -0700356 result = network_loopback_server(port, SOCK_STREAM, error, true);
357 } else if (hostname == "::1") {
358 result = network_loopback_server(port, SOCK_STREAM, error, false);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700359 } else {
360 // TODO: Implement me.
361 *error = "listening on specified hostname currently unsupported";
362 return -1;
363 }
364
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800365 if (result >= 0 && resolved_port) {
366 *resolved_port = adb_socket_get_local_port(result);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700367 }
368 return result;
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800369 } else if (spec.starts_with("vsock:")) {
370#if ADB_LINUX
371 std::string spec_str(spec);
372 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
373 if (fragments.size() != 2) {
374 *error = "given vsock server socket string was invalid";
375 return -1;
376 }
377 int port;
378 if (!android::base::ParseInt(fragments[1], &port)) {
379 *error = "could not parse vsock port";
380 errno = EINVAL;
381 return -1;
382 } else if (port < 0) {
383 *error = "vsock port was negative.";
384 errno = EINVAL;
385 return -1;
386 }
Jeff Vander Stoep58a73ad2021-09-30 15:54:36 +0200387 unique_fd serverfd(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0));
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800388 if (serverfd == -1) {
389 int error_num = errno;
390 *error = android::base::StringPrintf("could not create vsock server: '%s'",
391 strerror(error_num));
392 errno = error_num;
393 return -1;
394 }
395 sockaddr_vm addr{};
396 addr.svm_family = AF_VSOCK;
397 addr.svm_port = port == 0 ? VMADDR_PORT_ANY : port;
398 addr.svm_cid = VMADDR_CID_ANY;
399 socklen_t addr_len = sizeof(addr);
Josh Gao90228a62019-04-25 14:04:57 -0700400 if (bind(serverfd.get(), reinterpret_cast<struct sockaddr*>(&addr), addr_len)) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800401 return -1;
402 }
Josh Gao90228a62019-04-25 14:04:57 -0700403 if (listen(serverfd.get(), 4)) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800404 return -1;
405 }
406 if (serverfd >= 0 && resolved_port) {
Josh Gao90228a62019-04-25 14:04:57 -0700407 if (getsockname(serverfd.get(), reinterpret_cast<sockaddr*>(&addr), &addr_len) == 0) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800408 *resolved_port = addr.svm_port;
409 } else {
410 return -1;
411 }
412 }
413 return serverfd.release();
414#else // ADB_LINUX
415 *error = "vsock is only supported on linux";
416 return -1;
417#endif // ADB_LINUX
Daniel Colascione3e124692019-11-13 17:49:37 -0800418 } else if (ConsumePrefix(&spec, "acceptfd:")) {
419#if ADB_WINDOWS
420 *error = "socket activation not supported under Windows";
421 return -1;
422#else
423 // We inherited the socket from some kind of launcher. It's already bound and
424 // listening. Return a copy of the FD instead of the FD itself so we implement the
425 // normal "listen" contract and can succeed more than once.
426 unsigned int fd_u;
427 if (!ParseUint(&fd_u, spec) || fd_u > std::numeric_limits<int>::max()) {
428 *error = "invalid fd";
429 return -1;
430 }
431 int fd = static_cast<int>(fd_u);
432 int flags = get_fd_flags(fd);
433 if (flags < 0) {
434 *error = android::base::StringPrintf("could not get flags of inherited fd %d: '%s'", fd,
435 strerror(errno));
436 return -1;
437 }
438 if (flags & FD_CLOEXEC) {
439 *error = android::base::StringPrintf("fd %d was not inherited from parent", fd);
440 return -1;
441 }
442
443 int dummy_sock_type;
444 socklen_t dummy_sock_type_size = sizeof(dummy_sock_type);
445 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &dummy_sock_type, &dummy_sock_type_size)) {
446 *error = android::base::StringPrintf("fd %d does not refer to a socket", fd);
447 return -1;
448 }
449
450 int new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
451 if (new_fd < 0) {
452 *error = android::base::StringPrintf("could not dup inherited fd %d: '%s'", fd,
453 strerror(errno));
454 return -1;
455 }
456 return new_fd;
457#endif
Josh Gao4a5a95d2016-08-24 18:38:44 -0700458 }
459
460 for (const auto& it : kLocalSocketTypes) {
461 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800462 if (spec.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700463 if (!it.second.available) {
Josh Gaoab9958e2018-12-13 14:04:04 -0800464 *error = "attempted to listen on unavailable socket type: ";
465 *error += spec;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700466 return -1;
467 }
468
469 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
470 SOCK_STREAM, error);
471 }
472 }
473
Josh Gaoab9958e2018-12-13 14:04:04 -0800474 *error = "unknown socket specification:";
475 *error += spec;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700476 return -1;
477}