blob: 98468b5cf3c20c23100de03395cf523732803b07 [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
19#include <string>
Josh Gaoab9958e2018-12-13 14:04:04 -080020#include <string_view>
Josh Gao4a5a95d2016-08-24 18:38:44 -070021#include <unordered_map>
22#include <vector>
23
Josh Gao3fb517c2016-09-19 17:31:55 -070024#include <android-base/parseint.h>
25#include <android-base/parsenetaddress.h>
Josh Gao4a5a95d2016-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 Gaoab9958e2018-12-13 14:04:04 -080033using namespace std::string_literals;
34
Josh Gao4a5a95d2016-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 Schuffelen637aaf52019-01-04 18:51:11 -080049#if ADB_LINUX
50#include <sys/socket.h>
51#include "sysdeps/vm_sockets.h"
52#endif
53
Josh Gao4a5a95d2016-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 Gaoab9958e2018-12-13 14:04:04 -080074bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port,
Cody Schuffelen331a9082019-01-02 14:17:29 -080075 std::string* serial, std::string* error) {
Josh Gaoab9958e2018-12-13 14:04:04 -080076 if (!spec.starts_with("tcp:")) {
77 *error = "specification is not tcp: ";
78 *error += spec;
Josh Gao4a5a95d2016-08-24 18:38:44 -070079 return false;
80 }
81
Josh Gao3fb517c2016-09-19 17:31:55 -070082 std::string hostname_value;
83 int port_value;
Josh Gao4a5a95d2016-08-24 18:38:44 -070084
Josh Gao3fb517c2016-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 Gao4a5a95d2016-08-24 18:38:44 -070092 return false;
93 }
Josh Gao3fb517c2016-09-19 17:31:55 -070094 } else {
Josh Gaoab9958e2018-12-13 14:04:04 -080095 std::string addr(spec.substr(4));
Elliott Hughes14d673e2019-07-30 13:51:03 -070096 port_value = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
Josh Gao3fb517c2016-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 Schuffelen331a9082019-01-02 14:17:29 -0800100 if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) {
Josh Gao3fb517c2016-09-19 17:31:55 -0700101 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700102 }
Josh Gao3fb517c2016-09-19 17:31:55 -0700103
104 if (port_value == -1) {
Josh Gaoab9958e2018-12-13 14:04:04 -0800105 *error = "missing port in specification: ";
106 *error += spec;
Josh Gao3fb517c2016-09-19 17:31:55 -0700107 return false;
108 }
109 }
110
111 if (hostname) {
112 *hostname = std::move(hostname_value);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700113 }
114
115 if (port) {
Josh Gao3fb517c2016-09-19 17:31:55 -0700116 *port = port_value;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700117 }
118
119 return true;
120}
121
Josh Gaoab9958e2018-12-13 14:04:04 -0800122static bool tcp_host_is_local(std::string_view hostname) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700123 // FIXME
124 return hostname.empty() || hostname == "localhost";
125}
126
Josh Gaoab9958e2018-12-13 14:04:04 -0800127bool is_socket_spec(std::string_view spec) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700128 for (const auto& it : kLocalSocketTypes) {
129 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800130 if (spec.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700131 return true;
132 }
133 }
Josh Gaoab9958e2018-12-13 14:04:04 -0800134 return spec.starts_with("tcp:");
Josh Gao4a5a95d2016-08-24 18:38:44 -0700135}
136
Josh Gaoab9958e2018-12-13 14:04:04 -0800137bool is_local_socket_spec(std::string_view spec) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700138 for (const auto& it : kLocalSocketTypes) {
139 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800140 if (spec.starts_with(prefix)) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700141 return true;
142 }
143 }
144
145 std::string error;
146 std::string hostname;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800147 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700148 return false;
149 }
150 return tcp_host_is_local(hostname);
151}
152
Cody Schuffelen331a9082019-01-02 14:17:29 -0800153bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial,
154 std::string* error) {
155 if (address.starts_with("tcp:")) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700156 std::string hostname;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800157 int port_value = port ? *port : 0;
158 if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) {
159 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700160 }
161
Josh Gao4a5a95d2016-08-24 18:38:44 -0700162 if (tcp_host_is_local(hostname)) {
Cody Schuffelen331a9082019-01-02 14:17:29 -0800163 fd->reset(network_loopback_client(port_value, SOCK_STREAM, error));
Josh Gao4a5a95d2016-08-24 18:38:44 -0700164 } else {
165#if ADB_HOST
Cody Schuffelen331a9082019-01-02 14:17:29 -0800166 fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error));
Josh Gao4a5a95d2016-08-24 18:38:44 -0700167#else
168 // Disallow arbitrary connections in adbd.
169 *error = "adbd does not support arbitrary tcp connections";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800170 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700171#endif
172 }
173
Cody Schuffelen331a9082019-01-02 14:17:29 -0800174 if (fd->get() > 0) {
175 disable_tcp_nagle(fd->get());
176 if (port) {
177 *port = port_value;
178 }
179 return true;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700180 }
Cody Schuffelen331a9082019-01-02 14:17:29 -0800181 return false;
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800182 } else if (address.starts_with("vsock:")) {
183#if ADB_LINUX
184 std::string spec_str(address);
185 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
186 unsigned int port_value = port ? *port : 0;
187 if (fragments.size() != 2 && fragments.size() != 3) {
188 *error = android::base::StringPrintf("expected vsock:cid or vsock:port:cid in '%s'",
189 spec_str.c_str());
190 errno = EINVAL;
191 return false;
192 }
193 unsigned int cid = 0;
194 if (!android::base::ParseUint(fragments[1], &cid)) {
195 *error = android::base::StringPrintf("could not parse vsock cid in '%s'",
196 spec_str.c_str());
197 errno = EINVAL;
198 return false;
199 }
200 if (fragments.size() == 3 && !android::base::ParseUint(fragments[2], &port_value)) {
201 *error = android::base::StringPrintf("could not parse vsock port in '%s'",
202 spec_str.c_str());
203 errno = EINVAL;
204 return false;
205 }
206 if (port_value == 0) {
207 *error = android::base::StringPrintf("vsock port was not provided.");
208 errno = EINVAL;
209 return false;
210 }
211 fd->reset(socket(AF_VSOCK, SOCK_STREAM, 0));
212 if (fd->get() == -1) {
213 *error = "could not open vsock socket";
214 return false;
215 }
216 sockaddr_vm addr{};
217 addr.svm_family = AF_VSOCK;
218 addr.svm_port = port_value;
219 addr.svm_cid = cid;
220 if (serial) {
221 *serial = android::base::StringPrintf("vsock:%u:%d", cid, port_value);
222 }
223 if (connect(fd->get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) {
224 int error_num = errno;
225 *error = android::base::StringPrintf("could not connect to vsock address '%s'",
226 spec_str.c_str());
227 errno = error_num;
228 return false;
229 }
230 if (port) {
231 *port = port_value;
232 }
233 return true;
234#else // ADB_LINUX
235 *error = "vsock is only supported on linux";
236 return false;
237#endif // ADB_LINUX
Josh Gao4a5a95d2016-08-24 18:38:44 -0700238 }
239
240 for (const auto& it : kLocalSocketTypes) {
241 std::string prefix = it.first + ":";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800242 if (address.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700243 if (!it.second.available) {
244 *error = StringPrintf("socket type %s is unavailable on this platform",
245 it.first.c_str());
Cody Schuffelen331a9082019-01-02 14:17:29 -0800246 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700247 }
248
Cody Schuffelen331a9082019-01-02 14:17:29 -0800249 fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace,
250 SOCK_STREAM, error));
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800251 if (serial) {
252 *serial = address;
253 }
Cody Schuffelen331a9082019-01-02 14:17:29 -0800254 return true;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700255 }
256 }
257
Josh Gaoab9958e2018-12-13 14:04:04 -0800258 *error = "unknown socket specification: ";
Cody Schuffelen331a9082019-01-02 14:17:29 -0800259 *error += address;
260 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700261}
262
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800263int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_port) {
Josh Gaoab9958e2018-12-13 14:04:04 -0800264 if (spec.starts_with("tcp:")) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700265 std::string hostname;
266 int port;
Cody Schuffelen331a9082019-01-02 14:17:29 -0800267 if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700268 return -1;
269 }
270
271 int result;
272 if (hostname.empty() && gListenAll) {
273 result = network_inaddr_any_server(port, SOCK_STREAM, error);
274 } else if (tcp_host_is_local(hostname)) {
275 result = network_loopback_server(port, SOCK_STREAM, error);
276 } else {
277 // TODO: Implement me.
278 *error = "listening on specified hostname currently unsupported";
279 return -1;
280 }
281
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800282 if (result >= 0 && resolved_port) {
283 *resolved_port = adb_socket_get_local_port(result);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700284 }
285 return result;
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800286 } else if (spec.starts_with("vsock:")) {
287#if ADB_LINUX
288 std::string spec_str(spec);
289 std::vector<std::string> fragments = android::base::Split(spec_str, ":");
290 if (fragments.size() != 2) {
291 *error = "given vsock server socket string was invalid";
292 return -1;
293 }
294 int port;
295 if (!android::base::ParseInt(fragments[1], &port)) {
296 *error = "could not parse vsock port";
297 errno = EINVAL;
298 return -1;
299 } else if (port < 0) {
300 *error = "vsock port was negative.";
301 errno = EINVAL;
302 return -1;
303 }
304 unique_fd serverfd(socket(AF_VSOCK, SOCK_STREAM, 0));
305 if (serverfd == -1) {
306 int error_num = errno;
307 *error = android::base::StringPrintf("could not create vsock server: '%s'",
308 strerror(error_num));
309 errno = error_num;
310 return -1;
311 }
312 sockaddr_vm addr{};
313 addr.svm_family = AF_VSOCK;
314 addr.svm_port = port == 0 ? VMADDR_PORT_ANY : port;
315 addr.svm_cid = VMADDR_CID_ANY;
316 socklen_t addr_len = sizeof(addr);
Josh Gao90228a62019-04-25 14:04:57 -0700317 if (bind(serverfd.get(), reinterpret_cast<struct sockaddr*>(&addr), addr_len)) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800318 return -1;
319 }
Josh Gao90228a62019-04-25 14:04:57 -0700320 if (listen(serverfd.get(), 4)) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800321 return -1;
322 }
323 if (serverfd >= 0 && resolved_port) {
Josh Gao90228a62019-04-25 14:04:57 -0700324 if (getsockname(serverfd.get(), reinterpret_cast<sockaddr*>(&addr), &addr_len) == 0) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800325 *resolved_port = addr.svm_port;
326 } else {
327 return -1;
328 }
329 }
330 return serverfd.release();
331#else // ADB_LINUX
332 *error = "vsock is only supported on linux";
333 return -1;
334#endif // ADB_LINUX
Josh Gao4a5a95d2016-08-24 18:38:44 -0700335 }
336
337 for (const auto& it : kLocalSocketTypes) {
338 std::string prefix = it.first + ":";
Josh Gaoab9958e2018-12-13 14:04:04 -0800339 if (spec.starts_with(prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700340 if (!it.second.available) {
Josh Gaoab9958e2018-12-13 14:04:04 -0800341 *error = "attempted to listen on unavailable socket type: ";
342 *error += spec;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700343 return -1;
344 }
345
346 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
347 SOCK_STREAM, error);
348 }
349 }
350
Josh Gaoab9958e2018-12-13 14:04:04 -0800351 *error = "unknown socket specification:";
352 *error += spec;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700353 return -1;
354}