blob: cc67b6b9f1c1423092791d1a1039a76d99fb2182 [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
49// Not static because it is used in commandline.c.
50int gListenAll = 0;
51
52struct LocalSocketType {
53 int socket_namespace;
54 bool available;
55};
56
57static auto& kLocalSocketTypes = *new std::unordered_map<std::string, LocalSocketType>({
58#if ADB_HOST
59 { "local", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
60#else
61 { "local", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_WINDOWS } },
62#endif
63
64 { "localreserved", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_HOST } },
65 { "localabstract", { ANDROID_SOCKET_NAMESPACE_ABSTRACT, ADB_LINUX } },
66 { "localfilesystem", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
67});
68
Josh Gao9dd1f5c2018-12-13 14:04:04 -080069bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port,
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080070 std::string* serial, std::string* error) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -080071 if (!spec.starts_with("tcp:")) {
72 *error = "specification is not tcp: ";
73 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -070074 return false;
75 }
76
Josh Gao10992152016-09-19 17:31:55 -070077 std::string hostname_value;
78 int port_value;
Josh Gaocfb21412016-08-24 18:38:44 -070079
Josh Gao10992152016-09-19 17:31:55 -070080 // If the spec is tcp:<port>, parse it ourselves.
81 // Otherwise, delegate to android::base::ParseNetAddress.
82 if (android::base::ParseInt(&spec[4], &port_value)) {
83 // Do the range checking ourselves, because ParseInt rejects 'tcp:65536' and 'tcp:foo:1234'
84 // identically.
85 if (port_value < 0 || port_value > 65535) {
86 *error = StringPrintf("bad port number '%d'", port_value);
Josh Gaocfb21412016-08-24 18:38:44 -070087 return false;
88 }
Josh Gao10992152016-09-19 17:31:55 -070089 } else {
Josh Gao9dd1f5c2018-12-13 14:04:04 -080090 std::string addr(spec.substr(4));
Josh Gao10992152016-09-19 17:31:55 -070091 port_value = -1;
92
93 // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening
94 // on an address that isn't 'localhost' is unsupported.
Cody Schuffelenaf0e2202019-01-02 14:17:29 -080095 if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) {
Josh Gao10992152016-09-19 17:31:55 -070096 return false;
Josh Gaocfb21412016-08-24 18:38:44 -070097 }
Josh Gao10992152016-09-19 17:31:55 -070098
99 if (port_value == -1) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800100 *error = "missing port in specification: ";
101 *error += spec;
Josh Gao10992152016-09-19 17:31:55 -0700102 return false;
103 }
104 }
105
106 if (hostname) {
107 *hostname = std::move(hostname_value);
Josh Gaocfb21412016-08-24 18:38:44 -0700108 }
109
110 if (port) {
Josh Gao10992152016-09-19 17:31:55 -0700111 *port = port_value;
Josh Gaocfb21412016-08-24 18:38:44 -0700112 }
113
114 return true;
115}
116
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800117static bool tcp_host_is_local(std::string_view hostname) {
Josh Gaocfb21412016-08-24 18:38:44 -0700118 // FIXME
119 return hostname.empty() || hostname == "localhost";
120}
121
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800122bool is_socket_spec(std::string_view spec) {
Josh Gaocfb21412016-08-24 18:38:44 -0700123 for (const auto& it : kLocalSocketTypes) {
124 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800125 if (spec.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700126 return true;
127 }
128 }
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800129 return spec.starts_with("tcp:");
Josh Gaocfb21412016-08-24 18:38:44 -0700130}
131
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800132bool is_local_socket_spec(std::string_view spec) {
Josh Gao9c869b52016-08-25 16:00:22 -0700133 for (const auto& it : kLocalSocketTypes) {
134 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800135 if (spec.starts_with(prefix)) {
Josh Gao9c869b52016-08-25 16:00:22 -0700136 return true;
137 }
138 }
139
140 std::string error;
141 std::string hostname;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800142 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) {
Josh Gao9c869b52016-08-25 16:00:22 -0700143 return false;
144 }
145 return tcp_host_is_local(hostname);
146}
147
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800148bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial,
149 std::string* error) {
150 if (address.starts_with("tcp:")) {
Josh Gaocfb21412016-08-24 18:38:44 -0700151 std::string hostname;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800152 int port_value = port ? *port : 0;
153 if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) {
154 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700155 }
156
Josh Gaocfb21412016-08-24 18:38:44 -0700157 if (tcp_host_is_local(hostname)) {
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800158 fd->reset(network_loopback_client(port_value, SOCK_STREAM, error));
Josh Gaocfb21412016-08-24 18:38:44 -0700159 } else {
160#if ADB_HOST
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800161 fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error));
Josh Gaocfb21412016-08-24 18:38:44 -0700162#else
163 // Disallow arbitrary connections in adbd.
164 *error = "adbd does not support arbitrary tcp connections";
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800165 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700166#endif
167 }
168
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800169 if (fd->get() > 0) {
170 disable_tcp_nagle(fd->get());
171 if (port) {
172 *port = port_value;
173 }
174 return true;
Josh Gaocfb21412016-08-24 18:38:44 -0700175 }
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800176 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700177 }
178
179 for (const auto& it : kLocalSocketTypes) {
180 std::string prefix = it.first + ":";
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800181 if (address.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700182 if (!it.second.available) {
183 *error = StringPrintf("socket type %s is unavailable on this platform",
184 it.first.c_str());
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800185 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700186 }
187
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800188 fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace,
189 SOCK_STREAM, error));
190 return true;
Josh Gaocfb21412016-08-24 18:38:44 -0700191 }
192 }
193
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800194 *error = "unknown socket specification: ";
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800195 *error += address;
196 return false;
Josh Gaocfb21412016-08-24 18:38:44 -0700197}
198
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800199int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_tcp_port) {
200 if (spec.starts_with("tcp:")) {
Josh Gaocfb21412016-08-24 18:38:44 -0700201 std::string hostname;
202 int port;
Cody Schuffelenaf0e2202019-01-02 14:17:29 -0800203 if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700204 return -1;
205 }
206
207 int result;
208 if (hostname.empty() && gListenAll) {
209 result = network_inaddr_any_server(port, SOCK_STREAM, error);
210 } else if (tcp_host_is_local(hostname)) {
211 result = network_loopback_server(port, SOCK_STREAM, error);
212 } else {
213 // TODO: Implement me.
214 *error = "listening on specified hostname currently unsupported";
215 return -1;
216 }
217
218 if (result >= 0 && port == 0 && resolved_tcp_port) {
219 *resolved_tcp_port = adb_socket_get_local_port(result);
220 }
221 return result;
222 }
223
224 for (const auto& it : kLocalSocketTypes) {
225 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800226 if (spec.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700227 if (!it.second.available) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800228 *error = "attempted to listen on unavailable socket type: ";
229 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -0700230 return -1;
231 }
232
233 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
234 SOCK_STREAM, error);
235 }
236 }
237
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800238 *error = "unknown socket specification:";
239 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -0700240 return -1;
241}