1 /*
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>
20 #include <string_view>
21 #include <unordered_map>
22 #include <vector>
23
24 #include <android-base/parseint.h>
25 #include <android-base/parsenetaddress.h>
26 #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
33 using namespace std::string_literals;
34
35 using 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 #if ADB_LINUX
50 #include <sys/socket.h>
51 #include "sysdeps/vm_sockets.h"
52 #endif
53
54 // Not static because it is used in commandline.c.
55 int gListenAll = 0;
56
57 struct LocalSocketType {
58 int socket_namespace;
59 bool available;
60 };
61
62 static 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
parse_tcp_socket_spec(std::string_view spec,std::string * hostname,int * port,std::string * serial,std::string * error)74 bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port,
75 std::string* serial, std::string* error) {
76 if (!spec.starts_with("tcp:")) {
77 *error = "specification is not tcp: ";
78 *error += spec;
79 return false;
80 }
81
82 std::string hostname_value;
83 int port_value;
84
85 // 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);
92 return false;
93 }
94 } else {
95 std::string addr(spec.substr(4));
96 port_value = -1;
97
98 // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening
99 // on an address that isn't 'localhost' is unsupported.
100 if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) {
101 return false;
102 }
103
104 if (port_value == -1) {
105 *error = "missing port in specification: ";
106 *error += spec;
107 return false;
108 }
109 }
110
111 if (hostname) {
112 *hostname = std::move(hostname_value);
113 }
114
115 if (port) {
116 *port = port_value;
117 }
118
119 return true;
120 }
121
tcp_host_is_local(std::string_view hostname)122 static bool tcp_host_is_local(std::string_view hostname) {
123 // FIXME
124 return hostname.empty() || hostname == "localhost";
125 }
126
is_socket_spec(std::string_view spec)127 bool is_socket_spec(std::string_view spec) {
128 for (const auto& it : kLocalSocketTypes) {
129 std::string prefix = it.first + ":";
130 if (spec.starts_with(prefix)) {
131 return true;
132 }
133 }
134 return spec.starts_with("tcp:");
135 }
136
is_local_socket_spec(std::string_view spec)137 bool is_local_socket_spec(std::string_view spec) {
138 for (const auto& it : kLocalSocketTypes) {
139 std::string prefix = it.first + ":";
140 if (spec.starts_with(prefix)) {
141 return true;
142 }
143 }
144
145 std::string error;
146 std::string hostname;
147 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) {
148 return false;
149 }
150 return tcp_host_is_local(hostname);
151 }
152
socket_spec_connect(unique_fd * fd,std::string_view address,int * port,std::string * serial,std::string * error)153 bool 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:")) {
156 std::string hostname;
157 int port_value = port ? *port : 0;
158 if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) {
159 return false;
160 }
161
162 if (tcp_host_is_local(hostname)) {
163 fd->reset(network_loopback_client(port_value, SOCK_STREAM, error));
164 } else {
165 #if ADB_HOST
166 fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error));
167 #else
168 // Disallow arbitrary connections in adbd.
169 *error = "adbd does not support arbitrary tcp connections";
170 return false;
171 #endif
172 }
173
174 if (fd->get() > 0) {
175 disable_tcp_nagle(fd->get());
176 if (port) {
177 *port = port_value;
178 }
179 return true;
180 }
181 return false;
182 } 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
238 }
239
240 for (const auto& it : kLocalSocketTypes) {
241 std::string prefix = it.first + ":";
242 if (address.starts_with(prefix)) {
243 if (!it.second.available) {
244 *error = StringPrintf("socket type %s is unavailable on this platform",
245 it.first.c_str());
246 return false;
247 }
248
249 fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace,
250 SOCK_STREAM, error));
251 if (serial) {
252 *serial = address;
253 }
254 return true;
255 }
256 }
257
258 *error = "unknown socket specification: ";
259 *error += address;
260 return false;
261 }
262
socket_spec_listen(std::string_view spec,std::string * error,int * resolved_port)263 int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_port) {
264 if (spec.starts_with("tcp:")) {
265 std::string hostname;
266 int port;
267 if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) {
268 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
282 if (result >= 0 && resolved_port) {
283 *resolved_port = adb_socket_get_local_port(result);
284 }
285 return result;
286 } 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);
317 if (bind(serverfd, reinterpret_cast<struct sockaddr*>(&addr), addr_len)) {
318 return -1;
319 }
320 if (listen(serverfd, 4)) {
321 return -1;
322 }
323 if (serverfd >= 0 && resolved_port) {
324 if (getsockname(serverfd, reinterpret_cast<sockaddr*>(&addr), &addr_len) == 0) {
325 *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
335 }
336
337 for (const auto& it : kLocalSocketTypes) {
338 std::string prefix = it.first + ":";
339 if (spec.starts_with(prefix)) {
340 if (!it.second.available) {
341 *error = "attempted to listen on unavailable socket type: ";
342 *error += spec;
343 return -1;
344 }
345
346 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
347 SOCK_STREAM, error);
348 }
349 }
350
351 *error = "unknown socket specification:";
352 *error += spec;
353 return -1;
354 }
355