blob: 9ce443e36873484a65c42d258e831182b3e1d4f2 [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
Josh Gaoab9958e2018-12-13 14:04:04 -0800125static bool tcp_host_is_local(std::string_view hostname) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700126 // FIXME
127 return hostname.empty() || hostname == "localhost";
128}
129
Josh Gaoab9958e2018-12-13 14:04:04 -0800130bool is_socket_spec(std::string_view spec) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700131 for (const auto& it : kLocalSocketTypes) {
132 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800133 if (spec.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700134 return true;
135 }
136 }
Daniel Colascione3e124692019-11-13 17:49:37 -0800137 return spec.starts_with("tcp:") || spec.starts_with("acceptfd:");
Josh Gao4a5a95d2016-08-24 18:38:44 -0700138}
139
Josh Gaoab9958e2018-12-13 14:04:04 -0800140bool is_local_socket_spec(std::string_view spec) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700141 for (const auto& it : kLocalSocketTypes) {
142 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800143 if (spec.starts_with(prefix)) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700144 return true;
145 }
146 }
147
148 std::string error;
149 std::string hostname;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800150 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700151 return false;
152 }
153 return tcp_host_is_local(hostname);
154}
155
Cody Schuffelen331a9082019-01-02 14:17:29 -0800156bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial,
157 std::string* error) {
158 if (address.starts_with("tcp:")) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700159 std::string hostname;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800160 int port_value = port ? *port : 0;
161 if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) {
162 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700163 }
164
Josh Gao4a5a95d2016-08-24 18:38:44 -0700165 if (tcp_host_is_local(hostname)) {
Cody Schuffelen331a9082019-01-02 14:17:29 -0800166 fd->reset(network_loopback_client(port_value, SOCK_STREAM, error));
Josh Gao4a5a95d2016-08-24 18:38:44 -0700167 } else {
168#if ADB_HOST
Cody Schuffelen331a9082019-01-02 14:17:29 -0800169 fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error));
Josh Gao4a5a95d2016-08-24 18:38:44 -0700170#else
171 // Disallow arbitrary connections in adbd.
172 *error = "adbd does not support arbitrary tcp connections";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800173 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700174#endif
175 }
176
Cody Schuffelen331a9082019-01-02 14:17:29 -0800177 if (fd->get() > 0) {
178 disable_tcp_nagle(fd->get());
179 if (port) {
180 *port = port_value;
181 }
182 return true;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700183 }
Cody Schuffelen331a9082019-01-02 14:17:29 -0800184 return false;
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800185 } else if (address.starts_with("vsock:")) {
186#if ADB_LINUX
187 std::string spec_str(address);
188 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
189 unsigned int port_value = port ? *port : 0;
190 if (fragments.size() != 2 && fragments.size() != 3) {
191 *error = android::base::StringPrintf("expected vsock:cid or vsock:port:cid in '%s'",
192 spec_str.c_str());
193 errno = EINVAL;
194 return false;
195 }
196 unsigned int cid = 0;
197 if (!android::base::ParseUint(fragments[1], &cid)) {
198 *error = android::base::StringPrintf("could not parse vsock cid in '%s'",
199 spec_str.c_str());
200 errno = EINVAL;
201 return false;
202 }
203 if (fragments.size() == 3 && !android::base::ParseUint(fragments[2], &port_value)) {
204 *error = android::base::StringPrintf("could not parse vsock port in '%s'",
205 spec_str.c_str());
206 errno = EINVAL;
207 return false;
208 }
209 if (port_value == 0) {
210 *error = android::base::StringPrintf("vsock port was not provided.");
211 errno = EINVAL;
212 return false;
213 }
214 fd->reset(socket(AF_VSOCK, SOCK_STREAM, 0));
215 if (fd->get() == -1) {
216 *error = "could not open vsock socket";
217 return false;
218 }
219 sockaddr_vm addr{};
220 addr.svm_family = AF_VSOCK;
221 addr.svm_port = port_value;
222 addr.svm_cid = cid;
223 if (serial) {
224 *serial = android::base::StringPrintf("vsock:%u:%d", cid, port_value);
225 }
226 if (connect(fd->get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) {
227 int error_num = errno;
228 *error = android::base::StringPrintf("could not connect to vsock address '%s'",
229 spec_str.c_str());
230 errno = error_num;
231 return false;
232 }
233 if (port) {
234 *port = port_value;
235 }
236 return true;
237#else // ADB_LINUX
238 *error = "vsock is only supported on linux";
239 return false;
240#endif // ADB_LINUX
Daniel Colascione3e124692019-11-13 17:49:37 -0800241 } else if (address.starts_with("acceptfd:")) {
242 *error = "cannot connect to acceptfd";
243 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700244 }
245
246 for (const auto& it : kLocalSocketTypes) {
247 std::string prefix = it.first + ":";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800248 if (address.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700249 if (!it.second.available) {
250 *error = StringPrintf("socket type %s is unavailable on this platform",
251 it.first.c_str());
Cody Schuffelen331a9082019-01-02 14:17:29 -0800252 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700253 }
254
Cody Schuffelen331a9082019-01-02 14:17:29 -0800255 fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace,
256 SOCK_STREAM, error));
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800257 if (serial) {
258 *serial = address;
259 }
Cody Schuffelen331a9082019-01-02 14:17:29 -0800260 return true;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700261 }
262 }
263
Josh Gaoab9958e2018-12-13 14:04:04 -0800264 *error = "unknown socket specification: ";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800265 *error += address;
266 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700267}
268
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800269int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_port) {
Josh Gaoab9958e2018-12-13 14:04:04 -0800270 if (spec.starts_with("tcp:")) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700271 std::string hostname;
272 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800273 if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700274 return -1;
275 }
276
277 int result;
278 if (hostname.empty() && gListenAll) {
279 result = network_inaddr_any_server(port, SOCK_STREAM, error);
280 } else if (tcp_host_is_local(hostname)) {
Callum Ryan04efea32019-10-31 07:21:42 -0700281 result = network_loopback_server(port, SOCK_STREAM, error, true);
282 } else if (hostname == "::1") {
283 result = network_loopback_server(port, SOCK_STREAM, error, false);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700284 } else {
285 // TODO: Implement me.
286 *error = "listening on specified hostname currently unsupported";
287 return -1;
288 }
289
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800290 if (result >= 0 && resolved_port) {
291 *resolved_port = adb_socket_get_local_port(result);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700292 }
293 return result;
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800294 } else if (spec.starts_with("vsock:")) {
295#if ADB_LINUX
296 std::string spec_str(spec);
297 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
298 if (fragments.size() != 2) {
299 *error = "given vsock server socket string was invalid";
300 return -1;
301 }
302 int port;
303 if (!android::base::ParseInt(fragments[1], &port)) {
304 *error = "could not parse vsock port";
305 errno = EINVAL;
306 return -1;
307 } else if (port < 0) {
308 *error = "vsock port was negative.";
309 errno = EINVAL;
310 return -1;
311 }
312 unique_fd serverfd(socket(AF_VSOCK, SOCK_STREAM, 0));
313 if (serverfd == -1) {
314 int error_num = errno;
315 *error = android::base::StringPrintf("could not create vsock server: '%s'",
316 strerror(error_num));
317 errno = error_num;
318 return -1;
319 }
320 sockaddr_vm addr{};
321 addr.svm_family = AF_VSOCK;
322 addr.svm_port = port == 0 ? VMADDR_PORT_ANY : port;
323 addr.svm_cid = VMADDR_CID_ANY;
324 socklen_t addr_len = sizeof(addr);
Josh Gao90228a62019-04-25 14:04:57 -0700325 if (bind(serverfd.get(), reinterpret_cast<struct sockaddr*>(&addr), addr_len)) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800326 return -1;
327 }
Josh Gao90228a62019-04-25 14:04:57 -0700328 if (listen(serverfd.get(), 4)) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800329 return -1;
330 }
331 if (serverfd >= 0 && resolved_port) {
Josh Gao90228a62019-04-25 14:04:57 -0700332 if (getsockname(serverfd.get(), reinterpret_cast<sockaddr*>(&addr), &addr_len) == 0) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800333 *resolved_port = addr.svm_port;
334 } else {
335 return -1;
336 }
337 }
338 return serverfd.release();
339#else // ADB_LINUX
340 *error = "vsock is only supported on linux";
341 return -1;
342#endif // ADB_LINUX
Daniel Colascione3e124692019-11-13 17:49:37 -0800343 } else if (ConsumePrefix(&spec, "acceptfd:")) {
344#if ADB_WINDOWS
345 *error = "socket activation not supported under Windows";
346 return -1;
347#else
348 // We inherited the socket from some kind of launcher. It's already bound and
349 // listening. Return a copy of the FD instead of the FD itself so we implement the
350 // normal "listen" contract and can succeed more than once.
351 unsigned int fd_u;
352 if (!ParseUint(&fd_u, spec) || fd_u > std::numeric_limits<int>::max()) {
353 *error = "invalid fd";
354 return -1;
355 }
356 int fd = static_cast<int>(fd_u);
357 int flags = get_fd_flags(fd);
358 if (flags < 0) {
359 *error = android::base::StringPrintf("could not get flags of inherited fd %d: '%s'", fd,
360 strerror(errno));
361 return -1;
362 }
363 if (flags & FD_CLOEXEC) {
364 *error = android::base::StringPrintf("fd %d was not inherited from parent", fd);
365 return -1;
366 }
367
368 int dummy_sock_type;
369 socklen_t dummy_sock_type_size = sizeof(dummy_sock_type);
370 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &dummy_sock_type, &dummy_sock_type_size)) {
371 *error = android::base::StringPrintf("fd %d does not refer to a socket", fd);
372 return -1;
373 }
374
375 int new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
376 if (new_fd < 0) {
377 *error = android::base::StringPrintf("could not dup inherited fd %d: '%s'", fd,
378 strerror(errno));
379 return -1;
380 }
381 return new_fd;
382#endif
Josh Gao4a5a95d2016-08-24 18:38:44 -0700383 }
384
385 for (const auto& it : kLocalSocketTypes) {
386 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800387 if (spec.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700388 if (!it.second.available) {
Josh Gaoab9958e2018-12-13 14:04:04 -0800389 *error = "attempted to listen on unavailable socket type: ";
390 *error += spec;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700391 return -1;
392 }
393
394 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
395 SOCK_STREAM, error);
396 }
397 }
398
Josh Gaoab9958e2018-12-13 14:04:04 -0800399 *error = "unknown socket specification:";
400 *error += spec;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700401 return -1;
402}