blob: b7b25fa8802d2c37e17e94e26bb1e7fdd3bce84e [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"
Josh Gaocfb21412016-08-24 18:38:44 -070033#include "sysdeps.h"
34
Josh Gao9dd1f5c2018-12-13 14:04:04 -080035using namespace std::string_literals;
36
Daniel Colascione3ec7be72019-11-13 17:49:37 -080037using android::base::ConsumePrefix;
Josh Gaocfb21412016-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 Schuffelena05b64d2019-01-04 18:51:11 -080052#if ADB_LINUX
53#include <sys/socket.h>
54#include "sysdeps/vm_sockets.h"
55#endif
56
Josh Gaocfb21412016-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 Gao9dd1f5c2018-12-13 14:04:04 -080077bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port,
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080078 std::string* serial, std::string* error) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -080079 if (!spec.starts_with("tcp:")) {
80 *error = "specification is not tcp: ";
81 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -070082 return false;
83 }
84
Josh Gao10992152016-09-19 17:31:55 -070085 std::string hostname_value;
86 int port_value;
Josh Gaocfb21412016-08-24 18:38:44 -070087
Josh Gao10992152016-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 Gaocfb21412016-08-24 18:38:44 -070095 return false;
96 }
Josh Gao10992152016-09-19 17:31:55 -070097 } else {
Josh Gao9dd1f5c2018-12-13 14:04:04 -080098 std::string addr(spec.substr(4));
Elliott Hughes573e67c2019-07-30 13:51:03 -070099 port_value = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
Josh Gao10992152016-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 Schuffelenaf0e2202019-01-02 14:17:29 -0800103 if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) {
Josh Gao10992152016-09-19 17:31:55 -0700104 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700105 }
Josh Gao10992152016-09-19 17:31:55 -0700106 }
107
108 if (hostname) {
109 *hostname = std::move(hostname_value);
Josh Gaocfb21412016-08-24 18:38:44 -0700110 }
111
112 if (port) {
Josh Gao10992152016-09-19 17:31:55 -0700113 *port = port_value;
Josh Gaocfb21412016-08-24 18:38:44 -0700114 }
115
116 return true;
117}
118
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900119int get_host_socket_spec_port(std::string_view spec, std::string* error) {
120 int port;
121 if (spec.starts_with("tcp:")) {
122 if (!parse_tcp_socket_spec(spec, nullptr, &port, nullptr, error)) {
123 return -1;
124 }
125 } else if (spec.starts_with("vsock:")) {
126#if ADB_LINUX
127 std::string spec_str(spec);
128 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
129 if (fragments.size() != 2) {
130 *error = "given vsock server socket string was invalid";
131 return -1;
132 }
133 if (!android::base::ParseInt(fragments[1], &port)) {
134 *error = "could not parse vsock port";
135 errno = EINVAL;
136 return -1;
137 }
138 if (port < 0) {
139 *error = "vsock port was negative.";
140 errno = EINVAL;
141 return -1;
142 }
143#else // ADB_LINUX
144 *error = "vsock is only supported on linux";
145 return -1;
146#endif // ADB_LINUX
147 } else {
148 *error = "given socket spec string was invalid";
149 return -1;
150 }
151 return port;
152}
153
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800154static bool tcp_host_is_local(std::string_view hostname) {
Josh Gaocfb21412016-08-24 18:38:44 -0700155 // FIXME
156 return hostname.empty() || hostname == "localhost";
157}
158
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800159bool is_socket_spec(std::string_view spec) {
Josh Gaocfb21412016-08-24 18:38:44 -0700160 for (const auto& it : kLocalSocketTypes) {
161 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800162 if (spec.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700163 return true;
164 }
165 }
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800166 return spec.starts_with("tcp:") || spec.starts_with("acceptfd:");
Josh Gaocfb21412016-08-24 18:38:44 -0700167}
168
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800169bool is_local_socket_spec(std::string_view spec) {
Josh Gao9c869b52016-08-25 16:00:22 -0700170 for (const auto& it : kLocalSocketTypes) {
171 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800172 if (spec.starts_with(prefix)) {
Josh Gao9c869b52016-08-25 16:00:22 -0700173 return true;
174 }
175 }
176
177 std::string error;
178 std::string hostname;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800179 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) {
Josh Gao9c869b52016-08-25 16:00:22 -0700180 return false;
181 }
182 return tcp_host_is_local(hostname);
183}
184
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800185bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial,
186 std::string* error) {
187 if (address.starts_with("tcp:")) {
Josh Gaocfb21412016-08-24 18:38:44 -0700188 std::string hostname;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800189 int port_value = port ? *port : 0;
190 if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) {
191 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700192 }
193
Josh Gaocfb21412016-08-24 18:38:44 -0700194 if (tcp_host_is_local(hostname)) {
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800195 fd->reset(network_loopback_client(port_value, SOCK_STREAM, error));
Josh Gaocfb21412016-08-24 18:38:44 -0700196 } else {
197#if ADB_HOST
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800198 fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error));
Josh Gaocfb21412016-08-24 18:38:44 -0700199#else
200 // Disallow arbitrary connections in adbd.
201 *error = "adbd does not support arbitrary tcp connections";
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800202 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700203#endif
204 }
205
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800206 if (fd->get() > 0) {
207 disable_tcp_nagle(fd->get());
208 if (port) {
209 *port = port_value;
210 }
211 return true;
Josh Gaocfb21412016-08-24 18:38:44 -0700212 }
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800213 return false;
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800214 } else if (address.starts_with("vsock:")) {
215#if ADB_LINUX
216 std::string spec_str(address);
217 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
218 unsigned int port_value = port ? *port : 0;
219 if (fragments.size() != 2 && fragments.size() != 3) {
220 *error = android::base::StringPrintf("expected vsock:cid or vsock:port:cid in '%s'",
221 spec_str.c_str());
222 errno = EINVAL;
223 return false;
224 }
225 unsigned int cid = 0;
226 if (!android::base::ParseUint(fragments[1], &cid)) {
227 *error = android::base::StringPrintf("could not parse vsock cid in '%s'",
228 spec_str.c_str());
229 errno = EINVAL;
230 return false;
231 }
232 if (fragments.size() == 3 && !android::base::ParseUint(fragments[2], &port_value)) {
233 *error = android::base::StringPrintf("could not parse vsock port in '%s'",
234 spec_str.c_str());
235 errno = EINVAL;
236 return false;
237 }
238 if (port_value == 0) {
239 *error = android::base::StringPrintf("vsock port was not provided.");
240 errno = EINVAL;
241 return false;
242 }
243 fd->reset(socket(AF_VSOCK, SOCK_STREAM, 0));
244 if (fd->get() == -1) {
245 *error = "could not open vsock socket";
246 return false;
247 }
248 sockaddr_vm addr{};
249 addr.svm_family = AF_VSOCK;
250 addr.svm_port = port_value;
251 addr.svm_cid = cid;
252 if (serial) {
253 *serial = android::base::StringPrintf("vsock:%u:%d", cid, port_value);
254 }
255 if (connect(fd->get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) {
256 int error_num = errno;
257 *error = android::base::StringPrintf("could not connect to vsock address '%s'",
258 spec_str.c_str());
259 errno = error_num;
260 return false;
261 }
262 if (port) {
263 *port = port_value;
264 }
265 return true;
266#else // ADB_LINUX
267 *error = "vsock is only supported on linux";
268 return false;
269#endif // ADB_LINUX
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800270 } else if (address.starts_with("acceptfd:")) {
271 *error = "cannot connect to acceptfd";
272 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700273 }
274
275 for (const auto& it : kLocalSocketTypes) {
276 std::string prefix = it.first + ":";
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800277 if (address.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700278 if (!it.second.available) {
279 *error = StringPrintf("socket type %s is unavailable on this platform",
280 it.first.c_str());
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800281 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700282 }
283
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800284 fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace,
285 SOCK_STREAM, error));
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900286
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 Schuffelena05b64d2019-01-04 18:51:11 -0800294 if (serial) {
295 *serial = address;
296 }
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800297 return true;
Josh Gaocfb21412016-08-24 18:38:44 -0700298 }
299 }
300
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800301 *error = "unknown socket specification: ";
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800302 *error += address;
303 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700304}
305
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800306int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_port) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800307 if (spec.starts_with("tcp:")) {
Josh Gaocfb21412016-08-24 18:38:44 -0700308 std::string hostname;
309 int port;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800310 if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700311 return -1;
312 }
313
314 int result;
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900315#if ADB_HOST
Josh Gaocfb21412016-08-24 18:38:44 -0700316 if (hostname.empty() && gListenAll) {
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900317#else
318 if (hostname.empty()) {
319#endif
Josh Gaocfb21412016-08-24 18:38:44 -0700320 result = network_inaddr_any_server(port, SOCK_STREAM, error);
321 } else if (tcp_host_is_local(hostname)) {
Callum Ryan8539cb32019-10-31 07:21:42 -0700322 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 Gaocfb21412016-08-24 18:38:44 -0700325 } else {
326 // TODO: Implement me.
327 *error = "listening on specified hostname currently unsupported";
328 return -1;
329 }
330
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800331 if (result >= 0 && resolved_port) {
332 *resolved_port = adb_socket_get_local_port(result);
Josh Gaocfb21412016-08-24 18:38:44 -0700333 }
334 return result;
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800335 } 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 Gao27241a72019-04-25 14:04:57 -0700366 if (bind(serverfd.get(), reinterpret_cast<struct sockaddr*>(&addr), addr_len)) {
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800367 return -1;
368 }
Josh Gao27241a72019-04-25 14:04:57 -0700369 if (listen(serverfd.get(), 4)) {
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800370 return -1;
371 }
372 if (serverfd >= 0 && resolved_port) {
Josh Gao27241a72019-04-25 14:04:57 -0700373 if (getsockname(serverfd.get(), reinterpret_cast<sockaddr*>(&addr), &addr_len) == 0) {
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800374 *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
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800384 } else if (ConsumePrefix(&spec, "acceptfd:")) {
385#if ADB_WINDOWS
386 *error = "socket activation not supported under Windows";
387 return -1;
388#else
389 // We inherited the socket from some kind of launcher. It's already bound and
390 // listening. Return a copy of the FD instead of the FD itself so we implement the
391 // normal "listen" contract and can succeed more than once.
392 unsigned int fd_u;
393 if (!ParseUint(&fd_u, spec) || fd_u > std::numeric_limits<int>::max()) {
394 *error = "invalid fd";
395 return -1;
396 }
397 int fd = static_cast<int>(fd_u);
398 int flags = get_fd_flags(fd);
399 if (flags < 0) {
400 *error = android::base::StringPrintf("could not get flags of inherited fd %d: '%s'", fd,
401 strerror(errno));
402 return -1;
403 }
404 if (flags & FD_CLOEXEC) {
405 *error = android::base::StringPrintf("fd %d was not inherited from parent", fd);
406 return -1;
407 }
408
409 int dummy_sock_type;
410 socklen_t dummy_sock_type_size = sizeof(dummy_sock_type);
411 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &dummy_sock_type, &dummy_sock_type_size)) {
412 *error = android::base::StringPrintf("fd %d does not refer to a socket", fd);
413 return -1;
414 }
415
416 int new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
417 if (new_fd < 0) {
418 *error = android::base::StringPrintf("could not dup inherited fd %d: '%s'", fd,
419 strerror(errno));
420 return -1;
421 }
422 return new_fd;
423#endif
Josh Gaocfb21412016-08-24 18:38:44 -0700424 }
425
426 for (const auto& it : kLocalSocketTypes) {
427 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800428 if (spec.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700429 if (!it.second.available) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800430 *error = "attempted to listen on unavailable socket type: ";
431 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -0700432 return -1;
433 }
434
435 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
436 SOCK_STREAM, error);
437 }
438 }
439
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800440 *error = "unknown socket specification:";
441 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -0700442 return -1;
443}