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