blob: eb4df97faa0b294e4991b7a6793553025f3e93ad [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>
20#include <unordered_map>
21#include <vector>
22
Josh Gao3fb517c2016-09-19 17:31:55 -070023#include <android-base/parseint.h>
24#include <android-base/parsenetaddress.h>
Josh Gao4a5a95d2016-08-24 18:38:44 -070025#include <android-base/stringprintf.h>
26#include <android-base/strings.h>
27#include <cutils/sockets.h>
28
29#include "adb.h"
30#include "sysdeps.h"
31
32using android::base::StartsWith;
33using android::base::StringPrintf;
34
35#if defined(__linux__)
36#define ADB_LINUX 1
37#else
38#define ADB_LINUX 0
39#endif
40
41#if defined(_WIN32)
42#define ADB_WINDOWS 1
43#else
44#define ADB_WINDOWS 0
45#endif
46
47// Not static because it is used in commandline.c.
48int gListenAll = 0;
49
50struct LocalSocketType {
51 int socket_namespace;
52 bool available;
53};
54
55static auto& kLocalSocketTypes = *new std::unordered_map<std::string, LocalSocketType>({
56#if ADB_HOST
57 { "local", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
58#else
59 { "local", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_WINDOWS } },
60#endif
61
62 { "localreserved", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_HOST } },
63 { "localabstract", { ANDROID_SOCKET_NAMESPACE_ABSTRACT, ADB_LINUX } },
64 { "localfilesystem", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
65});
66
Josh Gao3fb517c2016-09-19 17:31:55 -070067bool parse_tcp_socket_spec(const std::string& spec, std::string* hostname, int* port,
Josh Gao4a5a95d2016-08-24 18:38:44 -070068 std::string* error) {
Josh Gao3fb517c2016-09-19 17:31:55 -070069 if (!StartsWith(spec, "tcp:")) {
Josh Gao4a5a95d2016-08-24 18:38:44 -070070 *error = StringPrintf("specification is not tcp: '%s'", spec.c_str());
71 return false;
72 }
73
Josh Gao3fb517c2016-09-19 17:31:55 -070074 std::string hostname_value;
75 int port_value;
Josh Gao4a5a95d2016-08-24 18:38:44 -070076
Josh Gao3fb517c2016-09-19 17:31:55 -070077 // If the spec is tcp:<port>, parse it ourselves.
78 // Otherwise, delegate to android::base::ParseNetAddress.
79 if (android::base::ParseInt(&spec[4], &port_value)) {
80 // Do the range checking ourselves, because ParseInt rejects 'tcp:65536' and 'tcp:foo:1234'
81 // identically.
82 if (port_value < 0 || port_value > 65535) {
83 *error = StringPrintf("bad port number '%d'", port_value);
Josh Gao4a5a95d2016-08-24 18:38:44 -070084 return false;
85 }
Josh Gao3fb517c2016-09-19 17:31:55 -070086 } else {
87 std::string addr = spec.substr(4);
88 port_value = -1;
89
90 // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening
91 // on an address that isn't 'localhost' is unsupported.
92 if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, nullptr, error)) {
93 return false;
Josh Gao4a5a95d2016-08-24 18:38:44 -070094 }
Josh Gao3fb517c2016-09-19 17:31:55 -070095
96 if (port_value == -1) {
97 *error = StringPrintf("missing port in specification: '%s'", spec.c_str());
98 return false;
99 }
100 }
101
102 if (hostname) {
103 *hostname = std::move(hostname_value);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700104 }
105
106 if (port) {
Josh Gao3fb517c2016-09-19 17:31:55 -0700107 *port = port_value;
Josh Gao4a5a95d2016-08-24 18:38:44 -0700108 }
109
110 return true;
111}
112
113static bool tcp_host_is_local(const std::string& hostname) {
114 // FIXME
115 return hostname.empty() || hostname == "localhost";
116}
117
118bool is_socket_spec(const std::string& spec) {
119 for (const auto& it : kLocalSocketTypes) {
120 std::string prefix = it.first + ":";
Elliott Hughes8efc5202017-12-20 09:41:00 -0800121 if (StartsWith(spec, prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700122 return true;
123 }
124 }
125 return StartsWith(spec, "tcp:");
126}
127
Josh Gaobb4f8602016-08-25 16:00:22 -0700128bool is_local_socket_spec(const std::string& spec) {
129 for (const auto& it : kLocalSocketTypes) {
130 std::string prefix = it.first + ":";
Elliott Hughes8efc5202017-12-20 09:41:00 -0800131 if (StartsWith(spec, prefix)) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700132 return true;
133 }
134 }
135
136 std::string error;
137 std::string hostname;
Josh Gao3fb517c2016-09-19 17:31:55 -0700138 if (!parse_tcp_socket_spec(spec, &hostname, nullptr, &error)) {
Josh Gaobb4f8602016-08-25 16:00:22 -0700139 return false;
140 }
141 return tcp_host_is_local(hostname);
142}
143
Josh Gao4a5a95d2016-08-24 18:38:44 -0700144int socket_spec_connect(const std::string& spec, std::string* error) {
145 if (StartsWith(spec, "tcp:")) {
146 std::string hostname;
147 int port;
Josh Gao3fb517c2016-09-19 17:31:55 -0700148 if (!parse_tcp_socket_spec(spec, &hostname, &port, error)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700149 return -1;
150 }
151
152 int result;
153 if (tcp_host_is_local(hostname)) {
154 result = network_loopback_client(port, SOCK_STREAM, error);
155 } else {
156#if ADB_HOST
157 result = network_connect(hostname, port, SOCK_STREAM, 0, error);
158#else
159 // Disallow arbitrary connections in adbd.
160 *error = "adbd does not support arbitrary tcp connections";
161 return -1;
162#endif
163 }
164
165 if (result >= 0) {
166 disable_tcp_nagle(result);
167 }
168 return result;
169 }
170
171 for (const auto& it : kLocalSocketTypes) {
172 std::string prefix = it.first + ":";
Elliott Hughes8efc5202017-12-20 09:41:00 -0800173 if (StartsWith(spec, prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700174 if (!it.second.available) {
175 *error = StringPrintf("socket type %s is unavailable on this platform",
176 it.first.c_str());
177 return -1;
178 }
179
180 return network_local_client(&spec[prefix.length()], it.second.socket_namespace,
181 SOCK_STREAM, error);
182 }
183 }
184
185 *error = StringPrintf("unknown socket specification '%s'", spec.c_str());
186 return -1;
187}
188
189int socket_spec_listen(const std::string& spec, std::string* error, int* resolved_tcp_port) {
190 if (StartsWith(spec, "tcp:")) {
191 std::string hostname;
192 int port;
Josh Gao3fb517c2016-09-19 17:31:55 -0700193 if (!parse_tcp_socket_spec(spec, &hostname, &port, error)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700194 return -1;
195 }
196
197 int result;
198 if (hostname.empty() && gListenAll) {
199 result = network_inaddr_any_server(port, SOCK_STREAM, error);
200 } else if (tcp_host_is_local(hostname)) {
201 result = network_loopback_server(port, SOCK_STREAM, error);
202 } else {
203 // TODO: Implement me.
204 *error = "listening on specified hostname currently unsupported";
205 return -1;
206 }
207
208 if (result >= 0 && port == 0 && resolved_tcp_port) {
209 *resolved_tcp_port = adb_socket_get_local_port(result);
210 }
211 return result;
212 }
213
214 for (const auto& it : kLocalSocketTypes) {
215 std::string prefix = it.first + ":";
Elliott Hughes8efc5202017-12-20 09:41:00 -0800216 if (StartsWith(spec, prefix)) {
Josh Gao4a5a95d2016-08-24 18:38:44 -0700217 if (!it.second.available) {
218 *error = StringPrintf("attempted to listen on unavailable socket type: '%s'",
219 spec.c_str());
220 return -1;
221 }
222
223 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
224 SOCK_STREAM, error);
225 }
226 }
227
228 *error = StringPrintf("unknown socket specification '%s'", spec.c_str());
229 return -1;
230}