blob: d17036cec9a5c7c51aaaa86e2cfc2dcdc5e392bc [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"
Daniel Colascione3e124692019-11-13 17:49:37 -080032#include "adb_utils.h"
Josh Gao4a5a95d2016-08-24 18:38:44 -070033#include "sysdeps.h"
34
Josh Gaoab9958e2018-12-13 14:04:04 -080035using namespace std::string_literals;
36
Daniel Colascione3e124692019-11-13 17:49:37 -080037using android::base::ConsumePrefix;
Josh Gao4a5a95d2016-08-24 18:38:44 -070038using 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 Schuffelen637aaf52019-01-04 18:51:11 -080052#if ADB_LINUX
53#include <sys/socket.h>
54#include "sysdeps/vm_sockets.h"
55#endif
56
Josh Gao4a5a95d2016-08-24 18:38:44 -070057// Not static because it is used in commandline.c.
58int gListenAll = 0;
59
60struct LocalSocketType {
61 int socket_namespace;
62 bool available;
63};
64
65static 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 Gaoab9958e2018-12-13 14:04:04 -080077bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port,
Cody Schuffelen331a9082019-01-02 14:17:29 -080078 std::string* serial, std::string* error) {
Josh Gaoab9958e2018-12-13 14:04:04 -080079 if (!spec.starts_with("tcp:")) {
80 *error = "specification is not tcp: ";
81 *error += spec;
Josh Gao4a5a95d2016-08-24 18:38:44 -070082 return false;
83 }
84
Josh Gao3fb517c2016-09-19 17:31:55 -070085 std::string hostname_value;
86 int port_value;
Josh Gao4a5a95d2016-08-24 18:38:44 -070087
Josh Gao3fb517c2016-09-19 17:31:55 -070088 // 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 Gao4a5a95d2016-08-24 18:38:44 -070095 return false;
96 }
Josh Gao3fb517c2016-09-19 17:31:55 -070097 } else {
Josh Gaoab9958e2018-12-13 14:04:04 -080098 std::string addr(spec.substr(4));
Elliott Hughes14d673e2019-07-30 13:51:03 -070099 port_value = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
Josh Gao3fb517c2016-09-19 17:31:55 -0700100
101 // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening
102 // on an address that isn't 'localhost' is unsupported.
Cody Schuffelen331a9082019-01-02 14:17:29 -0800103 if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) {
Josh Gao3fb517c2016-09-19 17:31:55 -0700104 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700105 }
Josh Gao3fb517c2016-09-19 17:31:55 -0700106
107 if (port_value == -1) {
Josh Gaoab9958e2018-12-13 14:04:04 -0800108 *error = "missing port in specification: ";
109 *error += spec;
Josh Gao3fb517c2016-09-19 17:31:55 -0700110 return false;
111 }
112 }
113
114 if (hostname) {
115 *hostname = std::move(hostname_value);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700116 }
117
118 if (port) {
Josh Gao3fb517c2016-09-19 17:31:55 -0700119 *port = port_value;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700120 }
121
122 return true;
123}
124
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900125int 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 Gaoab9958e2018-12-13 14:04:04 -0800160static bool tcp_host_is_local(std::string_view hostname) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700161 // FIXME
162 return hostname.empty() || hostname == "localhost";
163}
164
Josh Gaoab9958e2018-12-13 14:04:04 -0800165bool is_socket_spec(std::string_view spec) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700166 for (const auto& it : kLocalSocketTypes) {
167 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800168 if (spec.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700169 return true;
170 }
171 }
Daniel Colascione3e124692019-11-13 17:49:37 -0800172 return spec.starts_with("tcp:") || spec.starts_with("acceptfd:");
Josh Gao4a5a95d2016-08-24 18:38:44 -0700173}
174
Josh Gaoab9958e2018-12-13 14:04:04 -0800175bool is_local_socket_spec(std::string_view spec) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700176 for (const auto& it : kLocalSocketTypes) {
177 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800178 if (spec.starts_with(prefix)) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700179 return true;
180 }
181 }
182
183 std::string error;
184 std::string hostname;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800185 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700186 return false;
187 }
188 return tcp_host_is_local(hostname);
189}
190
Cody Schuffelen331a9082019-01-02 14:17:29 -0800191bool 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 Gao4a5a95d2016-08-24 18:38:44 -0700194 std::string hostname;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800195 int port_value = port ? *port : 0;
196 if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) {
197 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700198 }
199
Josh Gao4a5a95d2016-08-24 18:38:44 -0700200 if (tcp_host_is_local(hostname)) {
Cody Schuffelen331a9082019-01-02 14:17:29 -0800201 fd->reset(network_loopback_client(port_value, SOCK_STREAM, error));
Josh Gao4a5a95d2016-08-24 18:38:44 -0700202 } else {
203#if ADB_HOST
Cody Schuffelen331a9082019-01-02 14:17:29 -0800204 fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error));
Josh Gao4a5a95d2016-08-24 18:38:44 -0700205#else
206 // Disallow arbitrary connections in adbd.
207 *error = "adbd does not support arbitrary tcp connections";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800208 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700209#endif
210 }
211
Cody Schuffelen331a9082019-01-02 14:17:29 -0800212 if (fd->get() > 0) {
213 disable_tcp_nagle(fd->get());
214 if (port) {
215 *port = port_value;
216 }
217 return true;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700218 }
Cody Schuffelen331a9082019-01-02 14:17:29 -0800219 return false;
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800220 } 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 Colascione3e124692019-11-13 17:49:37 -0800276 } else if (address.starts_with("acceptfd:")) {
277 *error = "cannot connect to acceptfd";
278 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700279 }
280
281 for (const auto& it : kLocalSocketTypes) {
282 std::string prefix = it.first + ":";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800283 if (address.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700284 if (!it.second.available) {
285 *error = StringPrintf("socket type %s is unavailable on this platform",
286 it.first.c_str());
Cody Schuffelen331a9082019-01-02 14:17:29 -0800287 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700288 }
289
Cody Schuffelen331a9082019-01-02 14:17:29 -0800290 fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace,
291 SOCK_STREAM, error));
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900292
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 Schuffelen637aaf52019-01-04 18:51:11 -0800300 if (serial) {
301 *serial = address;
302 }
Cody Schuffelen331a9082019-01-02 14:17:29 -0800303 return true;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700304 }
305 }
306
Josh Gaoab9958e2018-12-13 14:04:04 -0800307 *error = "unknown socket specification: ";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800308 *error += address;
309 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700310}
311
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800312int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_port) {
Josh Gaoab9958e2018-12-13 14:04:04 -0800313 if (spec.starts_with("tcp:")) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700314 std::string hostname;
315 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800316 if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700317 return -1;
318 }
319
320 int result;
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900321#if ADB_HOST
Josh Gao4a5a95d2016-08-24 18:38:44 -0700322 if (hostname.empty() && gListenAll) {
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900323#else
324 if (hostname.empty()) {
325#endif
Josh Gao4a5a95d2016-08-24 18:38:44 -0700326 result = network_inaddr_any_server(port, SOCK_STREAM, error);
327 } else if (tcp_host_is_local(hostname)) {
Callum Ryan04efea32019-10-31 07:21:42 -0700328 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 Gao4a5a95d2016-08-24 18:38:44 -0700331 } else {
332 // TODO: Implement me.
333 *error = "listening on specified hostname currently unsupported";
334 return -1;
335 }
336
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800337 if (result >= 0 && resolved_port) {
338 *resolved_port = adb_socket_get_local_port(result);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700339 }
340 return result;
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800341 } 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 Gao90228a62019-04-25 14:04:57 -0700372 if (bind(serverfd.get(), reinterpret_cast<struct sockaddr*>(&addr), addr_len)) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800373 return -1;
374 }
Josh Gao90228a62019-04-25 14:04:57 -0700375 if (listen(serverfd.get(), 4)) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800376 return -1;
377 }
378 if (serverfd >= 0 && resolved_port) {
Josh Gao90228a62019-04-25 14:04:57 -0700379 if (getsockname(serverfd.get(), reinterpret_cast<sockaddr*>(&addr), &addr_len) == 0) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800380 *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 Colascione3e124692019-11-13 17:49:37 -0800390 } 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 Gao4a5a95d2016-08-24 18:38:44 -0700430 }
431
432 for (const auto& it : kLocalSocketTypes) {
433 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800434 if (spec.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700435 if (!it.second.available) {
Josh Gaoab9958e2018-12-13 14:04:04 -0800436 *error = "attempted to listen on unavailable socket type: ";
437 *error += spec;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700438 return -1;
439 }
440
441 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
442 SOCK_STREAM, error);
443 }
444 }
445
Josh Gaoab9958e2018-12-13 14:04:04 -0800446 *error = "unknown socket specification:";
447 *error += spec;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700448 return -1;
449}