blob: 2d1ea56faa9cf371791284d7a64cc8333331b666 [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
19#include <string>
Josh Gao9dd1f5c2018-12-13 14:04:04 -080020#include <string_view>
Josh Gaocfb21412016-08-24 18:38:44 -070021#include <unordered_map>
22#include <vector>
23
Josh Gao10992152016-09-19 17:31:55 -070024#include <android-base/parseint.h>
25#include <android-base/parsenetaddress.h>
Josh Gaocfb21412016-08-24 18:38:44 -070026#include <android-base/stringprintf.h>
27#include <android-base/strings.h>
28#include <cutils/sockets.h>
29
30#include "adb.h"
31#include "sysdeps.h"
32
Josh Gao9dd1f5c2018-12-13 14:04:04 -080033using namespace std::string_literals;
34
Josh Gaocfb21412016-08-24 18:38:44 -070035using android::base::StringPrintf;
36
37#if defined(__linux__)
38#define ADB_LINUX 1
39#else
40#define ADB_LINUX 0
41#endif
42
43#if defined(_WIN32)
44#define ADB_WINDOWS 1
45#else
46#define ADB_WINDOWS 0
47#endif
48
Cody Schuffelena05b64d2019-01-04 18:51:11 -080049#if ADB_LINUX
50#include <sys/socket.h>
51#include "sysdeps/vm_sockets.h"
52#endif
53
Josh Gaocfb21412016-08-24 18:38:44 -070054// Not static because it is used in commandline.c.
55int gListenAll = 0;
56
57struct LocalSocketType {
58 int socket_namespace;
59 bool available;
60};
61
62static auto& kLocalSocketTypes = *new std::unordered_map<std::string, LocalSocketType>({
63#if ADB_HOST
64 { "local", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
65#else
66 { "local", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_WINDOWS } },
67#endif
68
69 { "localreserved", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_HOST } },
70 { "localabstract", { ANDROID_SOCKET_NAMESPACE_ABSTRACT, ADB_LINUX } },
71 { "localfilesystem", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
72});
73
Josh Gao9dd1f5c2018-12-13 14:04:04 -080074bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port,
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080075 std::string* serial, std::string* error) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -080076 if (!spec.starts_with("tcp:")) {
77 *error = "specification is not tcp: ";
78 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -070079 return false;
80 }
81
Josh Gao10992152016-09-19 17:31:55 -070082 std::string hostname_value;
83 int port_value;
Josh Gaocfb21412016-08-24 18:38:44 -070084
Josh Gao10992152016-09-19 17:31:55 -070085 // If the spec is tcp:<port>, parse it ourselves.
86 // Otherwise, delegate to android::base::ParseNetAddress.
87 if (android::base::ParseInt(&spec[4], &port_value)) {
88 // Do the range checking ourselves, because ParseInt rejects 'tcp:65536' and 'tcp:foo:1234'
89 // identically.
90 if (port_value < 0 || port_value > 65535) {
91 *error = StringPrintf("bad port number '%d'", port_value);
Josh Gaocfb21412016-08-24 18:38:44 -070092 return false;
93 }
Josh Gao10992152016-09-19 17:31:55 -070094 } else {
Josh Gao9dd1f5c2018-12-13 14:04:04 -080095 std::string addr(spec.substr(4));
Elliott Hughes573e67c2019-07-30 13:51:03 -070096 port_value = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
Josh Gao10992152016-09-19 17:31:55 -070097
98 // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening
99 // on an address that isn't 'localhost' is unsupported.
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800100 if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) {
Josh Gao10992152016-09-19 17:31:55 -0700101 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700102 }
Josh Gao10992152016-09-19 17:31:55 -0700103
104 if (port_value == -1) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800105 *error = "missing port in specification: ";
106 *error += spec;
Josh Gao10992152016-09-19 17:31:55 -0700107 return false;
108 }
109 }
110
111 if (hostname) {
112 *hostname = std::move(hostname_value);
Josh Gaocfb21412016-08-24 18:38:44 -0700113 }
114
115 if (port) {
Josh Gao10992152016-09-19 17:31:55 -0700116 *port = port_value;
Josh Gaocfb21412016-08-24 18:38:44 -0700117 }
118
119 return true;
120}
121
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900122int get_host_socket_spec_port(std::string_view spec, std::string* error) {
123 int port;
124 if (spec.starts_with("tcp:")) {
125 if (!parse_tcp_socket_spec(spec, nullptr, &port, nullptr, error)) {
126 return -1;
127 }
128 } else if (spec.starts_with("vsock:")) {
129#if ADB_LINUX
130 std::string spec_str(spec);
131 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
132 if (fragments.size() != 2) {
133 *error = "given vsock server socket string was invalid";
134 return -1;
135 }
136 if (!android::base::ParseInt(fragments[1], &port)) {
137 *error = "could not parse vsock port";
138 errno = EINVAL;
139 return -1;
140 }
141 if (port < 0) {
142 *error = "vsock port was negative.";
143 errno = EINVAL;
144 return -1;
145 }
146#else // ADB_LINUX
147 *error = "vsock is only supported on linux";
148 return -1;
149#endif // ADB_LINUX
150 } else {
151 *error = "given socket spec string was invalid";
152 return -1;
153 }
154 return port;
155}
156
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800157static bool tcp_host_is_local(std::string_view hostname) {
Josh Gaocfb21412016-08-24 18:38:44 -0700158 // FIXME
159 return hostname.empty() || hostname == "localhost";
160}
161
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800162bool is_socket_spec(std::string_view spec) {
Josh Gaocfb21412016-08-24 18:38:44 -0700163 for (const auto& it : kLocalSocketTypes) {
164 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800165 if (spec.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700166 return true;
167 }
168 }
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800169 return spec.starts_with("tcp:");
Josh Gaocfb21412016-08-24 18:38:44 -0700170}
171
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800172bool is_local_socket_spec(std::string_view spec) {
Josh Gao9c869b52016-08-25 16:00:22 -0700173 for (const auto& it : kLocalSocketTypes) {
174 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800175 if (spec.starts_with(prefix)) {
Josh Gao9c869b52016-08-25 16:00:22 -0700176 return true;
177 }
178 }
179
180 std::string error;
181 std::string hostname;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800182 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) {
Josh Gao9c869b52016-08-25 16:00:22 -0700183 return false;
184 }
185 return tcp_host_is_local(hostname);
186}
187
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800188bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial,
189 std::string* error) {
190 if (address.starts_with("tcp:")) {
Josh Gaocfb21412016-08-24 18:38:44 -0700191 std::string hostname;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800192 int port_value = port ? *port : 0;
193 if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) {
194 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700195 }
196
Josh Gaocfb21412016-08-24 18:38:44 -0700197 if (tcp_host_is_local(hostname)) {
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800198 fd->reset(network_loopback_client(port_value, SOCK_STREAM, error));
Josh Gaocfb21412016-08-24 18:38:44 -0700199 } else {
200#if ADB_HOST
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800201 fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error));
Josh Gaocfb21412016-08-24 18:38:44 -0700202#else
203 // Disallow arbitrary connections in adbd.
204 *error = "adbd does not support arbitrary tcp connections";
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800205 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700206#endif
207 }
208
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800209 if (fd->get() > 0) {
210 disable_tcp_nagle(fd->get());
211 if (port) {
212 *port = port_value;
213 }
214 return true;
Josh Gaocfb21412016-08-24 18:38:44 -0700215 }
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800216 return false;
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800217 } else if (address.starts_with("vsock:")) {
218#if ADB_LINUX
219 std::string spec_str(address);
220 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
221 unsigned int port_value = port ? *port : 0;
222 if (fragments.size() != 2 && fragments.size() != 3) {
223 *error = android::base::StringPrintf("expected vsock:cid or vsock:port:cid in '%s'",
224 spec_str.c_str());
225 errno = EINVAL;
226 return false;
227 }
228 unsigned int cid = 0;
229 if (!android::base::ParseUint(fragments[1], &cid)) {
230 *error = android::base::StringPrintf("could not parse vsock cid in '%s'",
231 spec_str.c_str());
232 errno = EINVAL;
233 return false;
234 }
235 if (fragments.size() == 3 && !android::base::ParseUint(fragments[2], &port_value)) {
236 *error = android::base::StringPrintf("could not parse vsock port in '%s'",
237 spec_str.c_str());
238 errno = EINVAL;
239 return false;
240 }
241 if (port_value == 0) {
242 *error = android::base::StringPrintf("vsock port was not provided.");
243 errno = EINVAL;
244 return false;
245 }
246 fd->reset(socket(AF_VSOCK, SOCK_STREAM, 0));
247 if (fd->get() == -1) {
248 *error = "could not open vsock socket";
249 return false;
250 }
251 sockaddr_vm addr{};
252 addr.svm_family = AF_VSOCK;
253 addr.svm_port = port_value;
254 addr.svm_cid = cid;
255 if (serial) {
256 *serial = android::base::StringPrintf("vsock:%u:%d", cid, port_value);
257 }
258 if (connect(fd->get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) {
259 int error_num = errno;
260 *error = android::base::StringPrintf("could not connect to vsock address '%s'",
261 spec_str.c_str());
262 errno = error_num;
263 return false;
264 }
265 if (port) {
266 *port = port_value;
267 }
268 return true;
269#else // ADB_LINUX
270 *error = "vsock is only supported on linux";
271 return false;
272#endif // ADB_LINUX
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
Josh Gaocfb21412016-08-24 18:38:44 -0700384 }
385
386 for (const auto& it : kLocalSocketTypes) {
387 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800388 if (spec.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700389 if (!it.second.available) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800390 *error = "attempted to listen on unavailable socket type: ";
391 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -0700392 return -1;
393 }
394
395 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
396 SOCK_STREAM, error);
397 }
398 }
399
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800400 *error = "unknown socket specification:";
401 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -0700402 return -1;
403}