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, |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 70 | std::string* serial, 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. |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 95 | if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) { |
Josh Gao | 1099215 | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 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; |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 142 | if (!parse_tcp_socket_spec(spec, &hostname, nullptr, 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 | |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 148 | bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial, |
| 149 | std::string* error) { |
| 150 | if (address.starts_with("tcp:")) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 151 | std::string hostname; |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 152 | int port_value = port ? *port : 0; |
| 153 | if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) { |
| 154 | return false; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 157 | if (tcp_host_is_local(hostname)) { |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 158 | fd->reset(network_loopback_client(port_value, SOCK_STREAM, error)); |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 159 | } else { |
| 160 | #if ADB_HOST |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 161 | fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error)); |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 162 | #else |
| 163 | // Disallow arbitrary connections in adbd. |
| 164 | *error = "adbd does not support arbitrary tcp connections"; |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 165 | return false; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 166 | #endif |
| 167 | } |
| 168 | |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 169 | if (fd->get() > 0) { |
| 170 | disable_tcp_nagle(fd->get()); |
| 171 | if (port) { |
| 172 | *port = port_value; |
| 173 | } |
| 174 | return true; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 175 | } |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 176 | return false; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | for (const auto& it : kLocalSocketTypes) { |
| 180 | std::string prefix = it.first + ":"; |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 181 | if (address.starts_with(prefix)) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 182 | if (!it.second.available) { |
| 183 | *error = StringPrintf("socket type %s is unavailable on this platform", |
| 184 | it.first.c_str()); |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 185 | return false; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 188 | fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace, |
| 189 | SOCK_STREAM, error)); |
| 190 | return true; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 194 | *error = "unknown socket specification: "; |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 195 | *error += address; |
| 196 | return false; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 199 | int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_tcp_port) { |
| 200 | if (spec.starts_with("tcp:")) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 201 | std::string hostname; |
| 202 | int port; |
Cody Schuffelen | af0e220 | 2019-01-02 14:17:29 -0800 | [diff] [blame^] | 203 | if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | int result; |
| 208 | if (hostname.empty() && gListenAll) { |
| 209 | result = network_inaddr_any_server(port, SOCK_STREAM, error); |
| 210 | } else if (tcp_host_is_local(hostname)) { |
| 211 | result = network_loopback_server(port, SOCK_STREAM, error); |
| 212 | } else { |
| 213 | // TODO: Implement me. |
| 214 | *error = "listening on specified hostname currently unsupported"; |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | if (result >= 0 && port == 0 && resolved_tcp_port) { |
| 219 | *resolved_tcp_port = adb_socket_get_local_port(result); |
| 220 | } |
| 221 | return result; |
| 222 | } |
| 223 | |
| 224 | for (const auto& it : kLocalSocketTypes) { |
| 225 | std::string prefix = it.first + ":"; |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 226 | if (spec.starts_with(prefix)) { |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 227 | if (!it.second.available) { |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 228 | *error = "attempted to listen on unavailable socket type: "; |
| 229 | *error += spec; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | return network_local_server(&spec[prefix.length()], it.second.socket_namespace, |
| 234 | SOCK_STREAM, error); |
| 235 | } |
| 236 | } |
| 237 | |
Josh Gao | 9dd1f5c | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 238 | *error = "unknown socket specification:"; |
| 239 | *error += spec; |
Josh Gao | cfb2141 | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 240 | return -1; |
| 241 | } |