blob: 4cddc8444ecd651e093c4e114658fc93a80b9733 [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,
Josh Gaocfb21412016-08-24 18:38:44 -070070 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.
95 if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, nullptr, error)) {
96 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;
Josh Gao10992152016-09-19 17:31:55 -0700142 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, &error)) {
Josh Gao9c869b52016-08-25 16:00:22 -0700143 return false;
144 }
145 return tcp_host_is_local(hostname);
146}
147
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800148int socket_spec_connect(std::string_view spec, std::string* error) {
149 if (spec.starts_with("tcp:")) {
Josh Gaocfb21412016-08-24 18:38:44 -0700150 std::string hostname;
151 int port;
Josh Gao10992152016-09-19 17:31:55 -0700152 if (!parse_tcp_socket_spec(spec, &hostname, &port, error)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700153 return -1;
154 }
155
156 int result;
157 if (tcp_host_is_local(hostname)) {
158 result = network_loopback_client(port, SOCK_STREAM, error);
159 } else {
160#if ADB_HOST
161 result = network_connect(hostname, port, SOCK_STREAM, 0, error);
162#else
163 // Disallow arbitrary connections in adbd.
164 *error = "adbd does not support arbitrary tcp connections";
165 return -1;
166#endif
167 }
168
169 if (result >= 0) {
170 disable_tcp_nagle(result);
171 }
172 return result;
173 }
174
175 for (const auto& it : kLocalSocketTypes) {
176 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800177 if (spec.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700178 if (!it.second.available) {
179 *error = StringPrintf("socket type %s is unavailable on this platform",
180 it.first.c_str());
181 return -1;
182 }
183
184 return network_local_client(&spec[prefix.length()], it.second.socket_namespace,
185 SOCK_STREAM, error);
186 }
187 }
188
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800189 *error = "unknown socket specification: ";
190 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -0700191 return -1;
192}
193
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800194int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_tcp_port) {
195 if (spec.starts_with("tcp:")) {
Josh Gaocfb21412016-08-24 18:38:44 -0700196 std::string hostname;
197 int port;
Josh Gao10992152016-09-19 17:31:55 -0700198 if (!parse_tcp_socket_spec(spec, &hostname, &port, error)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700199 return -1;
200 }
201
202 int result;
203 if (hostname.empty() && gListenAll) {
204 result = network_inaddr_any_server(port, SOCK_STREAM, error);
205 } else if (tcp_host_is_local(hostname)) {
206 result = network_loopback_server(port, SOCK_STREAM, error);
207 } else {
208 // TODO: Implement me.
209 *error = "listening on specified hostname currently unsupported";
210 return -1;
211 }
212
213 if (result >= 0 && port == 0 && resolved_tcp_port) {
214 *resolved_tcp_port = adb_socket_get_local_port(result);
215 }
216 return result;
217 }
218
219 for (const auto& it : kLocalSocketTypes) {
220 std::string prefix = it.first + ":";
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800221 if (spec.starts_with(prefix)) {
Josh Gaocfb21412016-08-24 18:38:44 -0700222 if (!it.second.available) {
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800223 *error = "attempted to listen on unavailable socket type: ";
224 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -0700225 return -1;
226 }
227
228 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
229 SOCK_STREAM, error);
230 }
231 }
232
Josh Gao9dd1f5c2018-12-13 14:04:04 -0800233 *error = "unknown socket specification:";
234 *error += spec;
Josh Gaocfb21412016-08-24 18:38:44 -0700235 return -1;
236}