blob: 18e6e6d881ab00e4dc41eae6d579885845ac8372 [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>
20#include <unordered_map>
21#include <vector>
22
23#include <android-base/stringprintf.h>
24#include <android-base/strings.h>
25#include <cutils/sockets.h>
26
27#include "adb.h"
28#include "sysdeps.h"
29
30using android::base::StartsWith;
31using android::base::StringPrintf;
32
33#if defined(__linux__)
34#define ADB_LINUX 1
35#else
36#define ADB_LINUX 0
37#endif
38
39#if defined(_WIN32)
40#define ADB_WINDOWS 1
41#else
42#define ADB_WINDOWS 0
43#endif
44
45// Not static because it is used in commandline.c.
46int gListenAll = 0;
47
48struct LocalSocketType {
49 int socket_namespace;
50 bool available;
51};
52
53static auto& kLocalSocketTypes = *new std::unordered_map<std::string, LocalSocketType>({
54#if ADB_HOST
55 { "local", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
56#else
57 { "local", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_WINDOWS } },
58#endif
59
60 { "localreserved", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_HOST } },
61 { "localabstract", { ANDROID_SOCKET_NAMESPACE_ABSTRACT, ADB_LINUX } },
62 { "localfilesystem", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
63});
64
65static bool parse_tcp_spec(const std::string& spec, std::string* hostname, int* port,
66 std::string* error) {
67 std::vector<std::string> fragments = android::base::Split(spec, ":");
68 if (fragments.size() == 1 || fragments.size() > 3) {
69 *error = StringPrintf("invalid tcp specification: '%s'", spec.c_str());
70 return false;
71 }
72
73 if (fragments[0] != "tcp") {
74 *error = StringPrintf("specification is not tcp: '%s'", spec.c_str());
75 return false;
76 }
77
78 // strtol accepts leading whitespace.
79 const std::string& port_str = fragments.back();
80 if (port_str.empty() || port_str[0] < '0' || port_str[0] > '9') {
81 *error = StringPrintf("invalid port '%s'", port_str.c_str());
82 return false;
83 }
84
85 char* parsed_end;
86 long parsed_port = strtol(port_str.c_str(), &parsed_end, 10);
87 if (*parsed_end != '\0') {
88 *error = StringPrintf("trailing chars in port: '%s'", port_str.c_str());
89 return false;
90 }
91 if (parsed_port > 65535) {
92 *error = StringPrintf("invalid port %ld", parsed_port);
93 return false;
94 }
95
96 // tcp:123 is valid, tcp::123 isn't.
97 if (fragments.size() == 2) {
98 // Empty hostname.
99 if (hostname) {
100 *hostname = "";
101 }
102 } else {
103 if (fragments[1].empty()) {
104 *error = StringPrintf("empty host in '%s'", spec.c_str());
105 return false;
106 }
107 if (hostname) {
108 *hostname = fragments[1];
109 }
110 }
111
112 if (port) {
113 *port = parsed_port;
114 }
115
116 return true;
117}
118
119static bool tcp_host_is_local(const std::string& hostname) {
120 // FIXME
121 return hostname.empty() || hostname == "localhost";
122}
123
124bool is_socket_spec(const std::string& spec) {
125 for (const auto& it : kLocalSocketTypes) {
126 std::string prefix = it.first + ":";
127 if (StartsWith(spec, prefix.c_str())) {
128 return true;
129 }
130 }
131 return StartsWith(spec, "tcp:");
132}
133
Josh Gao9c869b52016-08-25 16:00:22 -0700134bool is_local_socket_spec(const std::string& spec) {
135 for (const auto& it : kLocalSocketTypes) {
136 std::string prefix = it.first + ":";
137 if (StartsWith(spec, prefix.c_str())) {
138 return true;
139 }
140 }
141
142 std::string error;
143 std::string hostname;
144 if (!parse_tcp_spec(spec, &hostname, nullptr, &error)) {
145 return false;
146 }
147 return tcp_host_is_local(hostname);
148}
149
Josh Gaocfb21412016-08-24 18:38:44 -0700150int socket_spec_connect(const std::string& spec, std::string* error) {
151 if (StartsWith(spec, "tcp:")) {
152 std::string hostname;
153 int port;
154 if (!parse_tcp_spec(spec, &hostname, &port, error)) {
155 return -1;
156 }
157
158 int result;
159 if (tcp_host_is_local(hostname)) {
160 result = network_loopback_client(port, SOCK_STREAM, error);
161 } else {
162#if ADB_HOST
163 result = network_connect(hostname, port, SOCK_STREAM, 0, error);
164#else
165 // Disallow arbitrary connections in adbd.
166 *error = "adbd does not support arbitrary tcp connections";
167 return -1;
168#endif
169 }
170
171 if (result >= 0) {
172 disable_tcp_nagle(result);
173 }
174 return result;
175 }
176
177 for (const auto& it : kLocalSocketTypes) {
178 std::string prefix = it.first + ":";
179 if (StartsWith(spec, prefix.c_str())) {
180 if (!it.second.available) {
181 *error = StringPrintf("socket type %s is unavailable on this platform",
182 it.first.c_str());
183 return -1;
184 }
185
186 return network_local_client(&spec[prefix.length()], it.second.socket_namespace,
187 SOCK_STREAM, error);
188 }
189 }
190
191 *error = StringPrintf("unknown socket specification '%s'", spec.c_str());
192 return -1;
193}
194
195int socket_spec_listen(const std::string& spec, std::string* error, int* resolved_tcp_port) {
196 if (StartsWith(spec, "tcp:")) {
197 std::string hostname;
198 int port;
199 if (!parse_tcp_spec(spec, &hostname, &port, error)) {
200 return -1;
201 }
202
203 int result;
204 if (hostname.empty() && gListenAll) {
205 result = network_inaddr_any_server(port, SOCK_STREAM, error);
206 } else if (tcp_host_is_local(hostname)) {
207 result = network_loopback_server(port, SOCK_STREAM, error);
208 } else {
209 // TODO: Implement me.
210 *error = "listening on specified hostname currently unsupported";
211 return -1;
212 }
213
214 if (result >= 0 && port == 0 && resolved_tcp_port) {
215 *resolved_tcp_port = adb_socket_get_local_port(result);
216 }
217 return result;
218 }
219
220 for (const auto& it : kLocalSocketTypes) {
221 std::string prefix = it.first + ":";
222 if (StartsWith(spec, prefix.c_str())) {
223 if (!it.second.available) {
224 *error = StringPrintf("attempted to listen on unavailable socket type: '%s'",
225 spec.c_str());
226 return -1;
227 }
228
229 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
230 SOCK_STREAM, error);
231 }
232 }
233
234 *error = StringPrintf("unknown socket specification '%s'", spec.c_str());
235 return -1;
236}