Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "adb_listeners.h" |
| 18 | |
Dan Albert | 357bd49 | 2015-02-20 17:20:09 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
Elliott Hughes | 1679e92 | 2015-02-24 12:37:13 -0800 | [diff] [blame] | 20 | #include <stdlib.h> |
Dan Albert | 357bd49 | 2015-02-20 17:20:09 -0800 | [diff] [blame] | 21 | |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame^] | 22 | #include <algorithm> |
| 23 | #include <list> |
| 24 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 26 | #include <android-base/strings.h> |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame^] | 27 | #include <android-base/thread_annotations.h> |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 28 | #include <cutils/sockets.h> |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 29 | |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 30 | #include "socket_spec.h" |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 31 | #include "sysdeps.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 32 | #include "transport.h" |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 33 | |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 34 | // A listener is an entity which binds to a local port and, upon receiving a connection on that |
| 35 | // port, creates an asocket to connect the new local connection to a specific remote service. |
| 36 | // |
| 37 | // TODO: some listeners read from the new connection to determine what exact service to connect to |
| 38 | // on the far side. |
| 39 | class alistener { |
| 40 | public: |
| 41 | alistener(const std::string& _local_name, const std::string& _connect_to); |
| 42 | ~alistener(); |
| 43 | |
| 44 | fdevent fde; |
| 45 | int fd = -1; |
| 46 | |
| 47 | std::string local_name; |
| 48 | std::string connect_to; |
| 49 | atransport* transport = nullptr; |
| 50 | adisconnect disconnect; |
| 51 | |
| 52 | private: |
| 53 | DISALLOW_COPY_AND_ASSIGN(alistener); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 56 | alistener::alistener(const std::string& _local_name, const std::string& _connect_to) |
| 57 | : local_name(_local_name), connect_to(_connect_to) { |
| 58 | } |
| 59 | |
| 60 | alistener::~alistener() { |
| 61 | // Closes the corresponding fd. |
| 62 | fdevent_remove(&fde); |
| 63 | |
| 64 | if (transport) { |
| 65 | transport->RemoveDisconnect(&disconnect); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // listener_list retains ownership of all created alistener objects. Removing an alistener from |
| 70 | // this list will cause it to be deleted. |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame^] | 71 | static auto& listener_list_mutex = *new std::mutex(); |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 72 | typedef std::list<std::unique_ptr<alistener>> ListenerList; |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame^] | 73 | static ListenerList& listener_list GUARDED_BY(listener_list_mutex) = *new ListenerList(); |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 74 | |
Elliott Hughes | d98ca8a | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 75 | static void ss_listener_event_func(int _fd, unsigned ev, void *_l) { |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 76 | if (ev & FDE_READ) { |
Josh Gao | 782d8d4 | 2016-08-23 15:41:56 -0700 | [diff] [blame] | 77 | int fd = adb_socket_accept(_fd, nullptr, nullptr); |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 78 | if (fd < 0) return; |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 79 | |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 80 | int rcv_buf_size = CHUNK_SIZE; |
| 81 | adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcv_buf_size, sizeof(rcv_buf_size)); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 82 | |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 83 | asocket* s = create_local_socket(fd); |
| 84 | if (s) { |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 85 | connect_to_smartsocket(s); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | adb_close(fd); |
| 90 | } |
| 91 | } |
| 92 | |
Elliott Hughes | d98ca8a | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 93 | static void listener_event_func(int _fd, unsigned ev, void* _l) |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 94 | { |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 95 | alistener* listener = reinterpret_cast<alistener*>(_l); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 96 | asocket *s; |
| 97 | |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 98 | if (ev & FDE_READ) { |
Josh Gao | 782d8d4 | 2016-08-23 15:41:56 -0700 | [diff] [blame] | 99 | int fd = adb_socket_accept(_fd, nullptr, nullptr); |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 100 | if (fd < 0) { |
| 101 | return; |
| 102 | } |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 103 | |
| 104 | s = create_local_socket(fd); |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 105 | if (s) { |
| 106 | s->transport = listener->transport; |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 107 | connect_to_remote(s, listener->connect_to.c_str()); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
| 111 | adb_close(fd); |
| 112 | } |
| 113 | } |
| 114 | |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 115 | // Called as a transport disconnect function. |arg| is the raw alistener*. |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame^] | 116 | static void listener_disconnect(void* arg, atransport*) EXCLUDES(listener_list_mutex) { |
| 117 | std::lock_guard<std::mutex> lock(listener_list_mutex); |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 118 | for (auto iter = listener_list.begin(); iter != listener_list.end(); ++iter) { |
| 119 | if (iter->get() == arg) { |
| 120 | (*iter)->transport = nullptr; |
| 121 | listener_list.erase(iter); |
| 122 | return; |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 123 | } |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 124 | } |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 127 | // Write the list of current listeners (network redirections) into a string. |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame^] | 128 | std::string format_listeners() EXCLUDES(listener_list_mutex) { |
| 129 | std::lock_guard<std::mutex> lock(listener_list_mutex); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 130 | std::string result; |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 131 | for (auto& l : listener_list) { |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 132 | // Ignore special listeners like those for *smartsocket* |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 133 | if (l->connect_to[0] == '*') { |
| 134 | continue; |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 135 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 136 | // <device-serial> " " <local-name> " " <remote-name> "\n" |
Elliott Hughes | 5259178 | 2015-07-21 17:09:06 -0700 | [diff] [blame] | 137 | // Entries from "adb reverse" have no serial. |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 138 | android::base::StringAppendF(&result, "%s %s %s\n", |
Elliott Hughes | 5259178 | 2015-07-21 17:09:06 -0700 | [diff] [blame] | 139 | l->transport->serial ? l->transport->serial : "(reverse)", |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 140 | l->local_name.c_str(), l->connect_to.c_str()); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 141 | } |
| 142 | return result; |
| 143 | } |
| 144 | |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame^] | 145 | InstallStatus remove_listener(const char* local_name, atransport* transport) |
| 146 | EXCLUDES(listener_list_mutex) { |
| 147 | std::lock_guard<std::mutex> lock(listener_list_mutex); |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 148 | for (auto iter = listener_list.begin(); iter != listener_list.end(); ++iter) { |
| 149 | if (local_name == (*iter)->local_name) { |
| 150 | listener_list.erase(iter); |
Elliott Hughes | a585cbd | 2015-04-20 08:09:20 -0700 | [diff] [blame] | 151 | return INSTALL_STATUS_OK; |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
Elliott Hughes | a585cbd | 2015-04-20 08:09:20 -0700 | [diff] [blame] | 154 | return INSTALL_STATUS_LISTENER_NOT_FOUND; |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame^] | 157 | void remove_all_listeners() EXCLUDES(listener_list_mutex) { |
| 158 | std::lock_guard<std::mutex> lock(listener_list_mutex); |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 159 | auto iter = listener_list.begin(); |
| 160 | while (iter != listener_list.end()) { |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 161 | // Never remove smart sockets. |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 162 | if ((*iter)->connect_to[0] == '*') { |
| 163 | ++iter; |
| 164 | } else { |
| 165 | iter = listener_list.erase(iter); |
| 166 | } |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame^] | 170 | void close_smartsockets() EXCLUDES(listener_list_mutex) { |
| 171 | std::lock_guard<std::mutex> lock(listener_list_mutex); |
| 172 | auto pred = [](const std::unique_ptr<alistener>& listener) { |
| 173 | return listener->local_name == "*smartsocket*"; |
| 174 | }; |
| 175 | listener_list.erase(std::remove_if(listener_list.begin(), listener_list.end(), pred)); |
| 176 | } |
| 177 | |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 178 | InstallStatus install_listener(const std::string& local_name, const char* connect_to, |
| 179 | atransport* transport, int no_rebind, int* resolved_tcp_port, |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame^] | 180 | std::string* error) EXCLUDES(listener_list_mutex) { |
| 181 | std::lock_guard<std::mutex> lock(listener_list_mutex); |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 182 | for (auto& l : listener_list) { |
Elliott Hughes | fb59684 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 183 | if (local_name == l->local_name) { |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 184 | // Can't repurpose a smartsocket. |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 185 | if(l->connect_to[0] == '*') { |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 186 | *error = "cannot repurpose smartsocket"; |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 187 | return INSTALL_STATUS_INTERNAL_ERROR; |
| 188 | } |
| 189 | |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 190 | // Can't repurpose a listener if 'no_rebind' is true. |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 191 | if (no_rebind) { |
Spencer Low | 3262585 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 192 | *error = "cannot rebind"; |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 193 | return INSTALL_STATUS_CANNOT_REBIND; |
| 194 | } |
| 195 | |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 196 | l->connect_to = connect_to; |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 197 | if (l->transport != transport) { |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 198 | l->transport->RemoveDisconnect(&l->disconnect); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 199 | l->transport = transport; |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 200 | l->transport->AddDisconnect(&l->disconnect); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 201 | } |
| 202 | return INSTALL_STATUS_OK; |
| 203 | } |
| 204 | } |
| 205 | |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 206 | std::unique_ptr<alistener> listener(new alistener(local_name, connect_to)); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 207 | |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 208 | int resolved = 0; |
| 209 | listener->fd = socket_spec_listen(listener->local_name, error, &resolved); |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 210 | if (listener->fd < 0) { |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 211 | return INSTALL_STATUS_CANNOT_BIND; |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 214 | // If the caller requested port 0, update the listener name with the resolved port. |
| 215 | if (resolved != 0) { |
| 216 | listener->local_name = android::base::StringPrintf("tcp:%d", resolved); |
| 217 | if (resolved_tcp_port) { |
| 218 | *resolved_tcp_port = resolved; |
| 219 | } |
| 220 | } |
| 221 | |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 222 | close_on_exec(listener->fd); |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 223 | if (listener->connect_to == "*smartsocket*") { |
| 224 | fdevent_install(&listener->fde, listener->fd, ss_listener_event_func, listener.get()); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 225 | } else { |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 226 | fdevent_install(&listener->fde, listener->fd, listener_event_func, listener.get()); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 227 | } |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 228 | fdevent_set(&listener->fde, FDE_READ); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 229 | |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 230 | listener->transport = transport; |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 231 | |
| 232 | if (transport) { |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 233 | listener->disconnect.opaque = listener.get(); |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame^] | 234 | listener->disconnect.func = listener_disconnect; |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 235 | transport->AddDisconnect(&listener->disconnect); |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 236 | } |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 237 | |
David Pursell | 19d0c23 | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 238 | listener_list.push_back(std::move(listener)); |
| 239 | return INSTALL_STATUS_OK; |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 240 | } |