blob: f8bbbb325a37370759b9dc5e943c6e83d43cdfa9 [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
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
134int socket_spec_connect(const std::string& spec, std::string* error) {
135 if (StartsWith(spec, "tcp:")) {
136 std::string hostname;
137 int port;
138 if (!parse_tcp_spec(spec, &hostname, &port, error)) {
139 return -1;
140 }
141
142 int result;
143 if (tcp_host_is_local(hostname)) {
144 result = network_loopback_client(port, SOCK_STREAM, error);
145 } else {
146#if ADB_HOST
147 result = network_connect(hostname, port, SOCK_STREAM, 0, error);
148#else
149 // Disallow arbitrary connections in adbd.
150 *error = "adbd does not support arbitrary tcp connections";
151 return -1;
152#endif
153 }
154
155 if (result >= 0) {
156 disable_tcp_nagle(result);
157 }
158 return result;
159 }
160
161 for (const auto& it : kLocalSocketTypes) {
162 std::string prefix = it.first + ":";
163 if (StartsWith(spec, prefix.c_str())) {
164 if (!it.second.available) {
165 *error = StringPrintf("socket type %s is unavailable on this platform",
166 it.first.c_str());
167 return -1;
168 }
169
170 return network_local_client(&spec[prefix.length()], it.second.socket_namespace,
171 SOCK_STREAM, error);
172 }
173 }
174
175 *error = StringPrintf("unknown socket specification '%s'", spec.c_str());
176 return -1;
177}
178
179int socket_spec_listen(const std::string& spec, std::string* error, int* resolved_tcp_port) {
180 if (StartsWith(spec, "tcp:")) {
181 std::string hostname;
182 int port;
183 if (!parse_tcp_spec(spec, &hostname, &port, error)) {
184 return -1;
185 }
186
187 int result;
188 if (hostname.empty() && gListenAll) {
189 result = network_inaddr_any_server(port, SOCK_STREAM, error);
190 } else if (tcp_host_is_local(hostname)) {
191 result = network_loopback_server(port, SOCK_STREAM, error);
192 } else {
193 // TODO: Implement me.
194 *error = "listening on specified hostname currently unsupported";
195 return -1;
196 }
197
198 if (result >= 0 && port == 0 && resolved_tcp_port) {
199 *resolved_tcp_port = adb_socket_get_local_port(result);
200 }
201 return result;
202 }
203
204 for (const auto& it : kLocalSocketTypes) {
205 std::string prefix = it.first + ":";
206 if (StartsWith(spec, prefix.c_str())) {
207 if (!it.second.available) {
208 *error = StringPrintf("attempted to listen on unavailable socket type: '%s'",
209 spec.c_str());
210 return -1;
211 }
212
213 return network_local_server(&spec[prefix.length()], it.second.socket_namespace,
214 SOCK_STREAM, error);
215 }
216 }
217
218 *error = StringPrintf("unknown socket specification '%s'", spec.c_str());
219 return -1;
220}