blob: 30cb29b8806492e873005248addc7497ff8fa8b2 [file] [log] [blame]
Dan Alberte9fca142015-02-18 18:03:26 -08001/*
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 Albertbf106472015-02-20 17:20:09 -080019#include <stdio.h>
Elliott Hughes3b967f52015-02-24 12:37:13 -080020#include <stdlib.h>
Dan Albertbf106472015-02-20 17:20:09 -080021
Josh Gao01b7bc42017-05-09 13:43:35 -070022#include <algorithm>
23#include <list>
24
Elliott Hughes4f713192015-12-04 22:00:26 -080025#include <android-base/stringprintf.h>
David Purselleaae97e2016-04-07 11:25:48 -070026#include <android-base/strings.h>
Josh Gao01b7bc42017-05-09 13:43:35 -070027#include <android-base/thread_annotations.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070028#include <cutils/sockets.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070029
Josh Gaocfb21412016-08-24 18:38:44 -070030#include "socket_spec.h"
Dan Alberte9fca142015-02-18 18:03:26 -080031#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080032#include "transport.h"
Dan Alberte9fca142015-02-18 18:03:26 -080033
David Purselleaae97e2016-04-07 11:25:48 -070034// 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.
39class 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 Alberte9fca142015-02-18 18:03:26 -080054};
55
David Purselleaae97e2016-04-07 11:25:48 -070056alistener::alistener(const std::string& _local_name, const std::string& _connect_to)
57 : local_name(_local_name), connect_to(_connect_to) {
58}
59
60alistener::~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 Gao01b7bc42017-05-09 13:43:35 -070071static auto& listener_list_mutex = *new std::mutex();
David Purselleaae97e2016-04-07 11:25:48 -070072typedef std::list<std::unique_ptr<alistener>> ListenerList;
Josh Gao01b7bc42017-05-09 13:43:35 -070073static ListenerList& listener_list GUARDED_BY(listener_list_mutex) = *new ListenerList();
David Purselleaae97e2016-04-07 11:25:48 -070074
Elliott Hughes424af022015-05-29 17:55:19 -070075static void ss_listener_event_func(int _fd, unsigned ev, void *_l) {
Elliott Hughescc65c3b2015-11-20 22:01:06 -080076 if (ev & FDE_READ) {
Josh Gao78e1eb12016-08-23 15:41:56 -070077 int fd = adb_socket_accept(_fd, nullptr, nullptr);
Elliott Hughescc65c3b2015-11-20 22:01:06 -080078 if (fd < 0) return;
Dan Alberte9fca142015-02-18 18:03:26 -080079
Elliott Hughescc65c3b2015-11-20 22:01:06 -080080 int rcv_buf_size = CHUNK_SIZE;
81 adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcv_buf_size, sizeof(rcv_buf_size));
Dan Alberte9fca142015-02-18 18:03:26 -080082
Elliott Hughescc65c3b2015-11-20 22:01:06 -080083 asocket* s = create_local_socket(fd);
84 if (s) {
Dan Alberte9fca142015-02-18 18:03:26 -080085 connect_to_smartsocket(s);
86 return;
87 }
88
89 adb_close(fd);
90 }
91}
92
Elliott Hughes424af022015-05-29 17:55:19 -070093static void listener_event_func(int _fd, unsigned ev, void* _l)
Dan Alberte9fca142015-02-18 18:03:26 -080094{
Dan Albertbac34742015-02-25 17:51:28 -080095 alistener* listener = reinterpret_cast<alistener*>(_l);
Dan Alberte9fca142015-02-18 18:03:26 -080096 asocket *s;
97
Dan Albertbac34742015-02-25 17:51:28 -080098 if (ev & FDE_READ) {
Josh Gao78e1eb12016-08-23 15:41:56 -070099 int fd = adb_socket_accept(_fd, nullptr, nullptr);
Dan Albertbac34742015-02-25 17:51:28 -0800100 if (fd < 0) {
101 return;
102 }
Dan Alberte9fca142015-02-18 18:03:26 -0800103
104 s = create_local_socket(fd);
Dan Albertbac34742015-02-25 17:51:28 -0800105 if (s) {
106 s->transport = listener->transport;
David Purselleaae97e2016-04-07 11:25:48 -0700107 connect_to_remote(s, listener->connect_to.c_str());
Dan Alberte9fca142015-02-18 18:03:26 -0800108 return;
109 }
110
111 adb_close(fd);
112 }
113}
114
David Purselleaae97e2016-04-07 11:25:48 -0700115// Called as a transport disconnect function. |arg| is the raw alistener*.
Josh Gao01b7bc42017-05-09 13:43:35 -0700116static void listener_disconnect(void* arg, atransport*) EXCLUDES(listener_list_mutex) {
117 std::lock_guard<std::mutex> lock(listener_list_mutex);
David Purselleaae97e2016-04-07 11:25:48 -0700118 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 Alberte9fca142015-02-18 18:03:26 -0800123 }
Dan Alberte9fca142015-02-18 18:03:26 -0800124 }
David Purselleaae97e2016-04-07 11:25:48 -0700125}
126
Elliott Hughese67f1f82015-04-30 17:32:03 -0700127// Write the list of current listeners (network redirections) into a string.
Josh Gao01b7bc42017-05-09 13:43:35 -0700128std::string format_listeners() EXCLUDES(listener_list_mutex) {
129 std::lock_guard<std::mutex> lock(listener_list_mutex);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700130 std::string result;
David Purselleaae97e2016-04-07 11:25:48 -0700131 for (auto& l : listener_list) {
Dan Alberte9fca142015-02-18 18:03:26 -0800132 // Ignore special listeners like those for *smartsocket*
Elliott Hughese67f1f82015-04-30 17:32:03 -0700133 if (l->connect_to[0] == '*') {
134 continue;
Dan Alberte9fca142015-02-18 18:03:26 -0800135 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700136 // <device-serial> " " <local-name> " " <remote-name> "\n"
Elliott Hughes34c20bb2015-07-21 17:09:06 -0700137 // Entries from "adb reverse" have no serial.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700138 android::base::StringAppendF(&result, "%s %s %s\n",
Elliott Hughes34c20bb2015-07-21 17:09:06 -0700139 l->transport->serial ? l->transport->serial : "(reverse)",
David Purselleaae97e2016-04-07 11:25:48 -0700140 l->local_name.c_str(), l->connect_to.c_str());
Dan Alberte9fca142015-02-18 18:03:26 -0800141 }
142 return result;
143}
144
Josh Gao01b7bc42017-05-09 13:43:35 -0700145InstallStatus remove_listener(const char* local_name, atransport* transport)
146 EXCLUDES(listener_list_mutex) {
147 std::lock_guard<std::mutex> lock(listener_list_mutex);
David Purselleaae97e2016-04-07 11:25:48 -0700148 for (auto iter = listener_list.begin(); iter != listener_list.end(); ++iter) {
149 if (local_name == (*iter)->local_name) {
150 listener_list.erase(iter);
Elliott Hughes7b506092015-04-20 08:09:20 -0700151 return INSTALL_STATUS_OK;
Dan Alberte9fca142015-02-18 18:03:26 -0800152 }
153 }
Elliott Hughes7b506092015-04-20 08:09:20 -0700154 return INSTALL_STATUS_LISTENER_NOT_FOUND;
Dan Alberte9fca142015-02-18 18:03:26 -0800155}
156
Josh Gao01b7bc42017-05-09 13:43:35 -0700157void remove_all_listeners() EXCLUDES(listener_list_mutex) {
158 std::lock_guard<std::mutex> lock(listener_list_mutex);
David Purselleaae97e2016-04-07 11:25:48 -0700159 auto iter = listener_list.begin();
160 while (iter != listener_list.end()) {
Dan Alberte9fca142015-02-18 18:03:26 -0800161 // Never remove smart sockets.
David Purselleaae97e2016-04-07 11:25:48 -0700162 if ((*iter)->connect_to[0] == '*') {
163 ++iter;
164 } else {
165 iter = listener_list.erase(iter);
166 }
Dan Alberte9fca142015-02-18 18:03:26 -0800167 }
168}
169
Josh Gao01b7bc42017-05-09 13:43:35 -0700170void 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 Purselleaae97e2016-04-07 11:25:48 -0700178InstallStatus install_listener(const std::string& local_name, const char* connect_to,
179 atransport* transport, int no_rebind, int* resolved_tcp_port,
Josh Gao01b7bc42017-05-09 13:43:35 -0700180 std::string* error) EXCLUDES(listener_list_mutex) {
181 std::lock_guard<std::mutex> lock(listener_list_mutex);
David Purselleaae97e2016-04-07 11:25:48 -0700182 for (auto& l : listener_list) {
Elliott Hughesab52c182015-05-01 17:04:38 -0700183 if (local_name == l->local_name) {
David Purselleaae97e2016-04-07 11:25:48 -0700184 // Can't repurpose a smartsocket.
Dan Alberte9fca142015-02-18 18:03:26 -0800185 if(l->connect_to[0] == '*') {
Spencer Lowbf7c6052015-08-11 16:45:32 -0700186 *error = "cannot repurpose smartsocket";
Dan Alberte9fca142015-02-18 18:03:26 -0800187 return INSTALL_STATUS_INTERNAL_ERROR;
188 }
189
David Purselleaae97e2016-04-07 11:25:48 -0700190 // Can't repurpose a listener if 'no_rebind' is true.
Dan Alberte9fca142015-02-18 18:03:26 -0800191 if (no_rebind) {
Spencer Lowbf7c6052015-08-11 16:45:32 -0700192 *error = "cannot rebind";
Dan Alberte9fca142015-02-18 18:03:26 -0800193 return INSTALL_STATUS_CANNOT_REBIND;
194 }
195
David Purselleaae97e2016-04-07 11:25:48 -0700196 l->connect_to = connect_to;
Dan Alberte9fca142015-02-18 18:03:26 -0800197 if (l->transport != transport) {
Yabin Cuib3298242015-08-28 15:09:44 -0700198 l->transport->RemoveDisconnect(&l->disconnect);
Dan Alberte9fca142015-02-18 18:03:26 -0800199 l->transport = transport;
Yabin Cuib3298242015-08-28 15:09:44 -0700200 l->transport->AddDisconnect(&l->disconnect);
Dan Alberte9fca142015-02-18 18:03:26 -0800201 }
202 return INSTALL_STATUS_OK;
203 }
204 }
205
David Purselleaae97e2016-04-07 11:25:48 -0700206 std::unique_ptr<alistener> listener(new alistener(local_name, connect_to));
Dan Alberte9fca142015-02-18 18:03:26 -0800207
Josh Gaocfb21412016-08-24 18:38:44 -0700208 int resolved = 0;
209 listener->fd = socket_spec_listen(listener->local_name, error, &resolved);
Dan Albertbac34742015-02-25 17:51:28 -0800210 if (listener->fd < 0) {
Dan Albertbac34742015-02-25 17:51:28 -0800211 return INSTALL_STATUS_CANNOT_BIND;
Dan Alberte9fca142015-02-18 18:03:26 -0800212 }
213
Josh Gaocfb21412016-08-24 18:38:44 -0700214 // 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 Albertbac34742015-02-25 17:51:28 -0800222 close_on_exec(listener->fd);
David Purselleaae97e2016-04-07 11:25:48 -0700223 if (listener->connect_to == "*smartsocket*") {
224 fdevent_install(&listener->fde, listener->fd, ss_listener_event_func, listener.get());
Dan Alberte9fca142015-02-18 18:03:26 -0800225 } else {
David Purselleaae97e2016-04-07 11:25:48 -0700226 fdevent_install(&listener->fde, listener->fd, listener_event_func, listener.get());
Dan Alberte9fca142015-02-18 18:03:26 -0800227 }
Dan Albertbac34742015-02-25 17:51:28 -0800228 fdevent_set(&listener->fde, FDE_READ);
Dan Alberte9fca142015-02-18 18:03:26 -0800229
Dan Albertbac34742015-02-25 17:51:28 -0800230 listener->transport = transport;
Dan Alberte9fca142015-02-18 18:03:26 -0800231
232 if (transport) {
David Purselleaae97e2016-04-07 11:25:48 -0700233 listener->disconnect.opaque = listener.get();
Josh Gao01b7bc42017-05-09 13:43:35 -0700234 listener->disconnect.func = listener_disconnect;
Yabin Cuib3298242015-08-28 15:09:44 -0700235 transport->AddDisconnect(&listener->disconnect);
Dan Alberte9fca142015-02-18 18:03:26 -0800236 }
Dan Alberte9fca142015-02-18 18:03:26 -0800237
David Purselleaae97e2016-04-07 11:25:48 -0700238 listener_list.push_back(std::move(listener));
239 return INSTALL_STATUS_OK;
Dan Alberte9fca142015-02-18 18:03:26 -0800240}