Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 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> |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 20 | #include <string_view> |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 21 | #include <unordered_map> |
| 22 | #include <vector> |
| 23 | |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 24 | #include <android-base/parseint.h> |
| 25 | #include <android-base/parsenetaddress.h> |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 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 | |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 33 | using namespace std::string_literals; |
| 34 | |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 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 | // Not static because it is used in commandline.c. |
| 50 | int gListenAll = 0; |
| 51 | |
| 52 | struct LocalSocketType { |
| 53 | int socket_namespace; |
| 54 | bool available; |
| 55 | }; |
| 56 | |
| 57 | static 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 Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 69 | bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port, |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 70 | std::string* error) { |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 71 | if (!spec.starts_with("tcp:")) { |
| 72 | *error = "specification is not tcp: "; |
| 73 | *error += spec; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 77 | std::string hostname_value; |
| 78 | int port_value; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 79 | |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 80 | // 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 Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 87 | return false; |
| 88 | } |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 89 | } else { |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 90 | std::string addr(spec.substr(4)); |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 91 | 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 Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 97 | } |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 98 | |
| 99 | if (port_value == -1) { |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 100 | *error = "missing port in specification: "; |
| 101 | *error += spec; |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (hostname) { |
| 107 | *hostname = std::move(hostname_value); |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if (port) { |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 111 | *port = port_value; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 117 | static bool tcp_host_is_local(std::string_view hostname) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 118 | // FIXME |
| 119 | return hostname.empty() || hostname == "localhost"; |
| 120 | } |
| 121 | |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 122 | bool is_socket_spec(std::string_view spec) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 123 | for (const auto& it : kLocalSocketTypes) { |
| 124 | std::string prefix = it.first + ":"; |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 125 | if (spec.starts_with(prefix)) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | } |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 129 | return spec.starts_with("tcp:"); |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 132 | bool is_local_socket_spec(std::string_view spec) { |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 133 | for (const auto& it : kLocalSocketTypes) { |
| 134 | std::string prefix = it.first + ":"; |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 135 | if (spec.starts_with(prefix)) { |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | std::string error; |
| 141 | std::string hostname; |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 142 | if (!parse_tcp_socket_spec(spec, &hostname, nullptr, &error)) { |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | return tcp_host_is_local(hostname); |
| 146 | } |
| 147 | |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 148 | int socket_spec_connect(std::string_view spec, std::string* error) { |
| 149 | if (spec.starts_with("tcp:")) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 150 | std::string hostname; |
| 151 | int port; |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 152 | if (!parse_tcp_socket_spec(spec, &hostname, &port, error)) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 153 | 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 Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 177 | if (spec.starts_with(prefix)) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 178 | 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 Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 189 | *error = "unknown socket specification: "; |
| 190 | *error += spec; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 191 | return -1; |
| 192 | } |
| 193 | |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 194 | int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_tcp_port) { |
| 195 | if (spec.starts_with("tcp:")) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 196 | std::string hostname; |
| 197 | int port; |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 198 | if (!parse_tcp_socket_spec(spec, &hostname, &port, error)) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 199 | 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 Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 221 | if (spec.starts_with(prefix)) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 222 | if (!it.second.available) { |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 223 | *error = "attempted to listen on unavailable socket type: "; |
| 224 | *error += spec; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | return network_local_server(&spec[prefix.length()], it.second.socket_namespace, |
| 229 | SOCK_STREAM, error); |
| 230 | } |
| 231 | } |
| 232 | |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame^] | 233 | *error = "unknown socket specification:"; |
| 234 | *error += spec; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 235 | return -1; |
| 236 | } |