blob: 18b14923c0d9623e724eb7cecb8e62a1c8d44871 [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
Elliott Hughes4f713192015-12-04 22:00:26 -080022#include <android-base/stringprintf.h>
David Purselleaae97e2016-04-07 11:25:48 -070023#include <android-base/strings.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070024#include <cutils/sockets.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070025
Josh Gaocfb21412016-08-24 18:38:44 -070026#include "socket_spec.h"
Dan Alberte9fca142015-02-18 18:03:26 -080027#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080028#include "transport.h"
Dan Alberte9fca142015-02-18 18:03:26 -080029
David Purselleaae97e2016-04-07 11:25:48 -070030// 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.
35class 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 Alberte9fca142015-02-18 18:03:26 -080050};
51
David Purselleaae97e2016-04-07 11:25:48 -070052alistener::alistener(const std::string& _local_name, const std::string& _connect_to)
53 : local_name(_local_name), connect_to(_connect_to) {
54}
55
56alistener::~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.
67typedef std::list<std::unique_ptr<alistener>> ListenerList;
68static ListenerList& listener_list = *new ListenerList();
69
Elliott Hughes424af022015-05-29 17:55:19 -070070static void ss_listener_event_func(int _fd, unsigned ev, void *_l) {
Elliott Hughescc65c3b2015-11-20 22:01:06 -080071 if (ev & FDE_READ) {
Josh Gao78e1eb12016-08-23 15:41:56 -070072 int fd = adb_socket_accept(_fd, nullptr, nullptr);
Elliott Hughescc65c3b2015-11-20 22:01:06 -080073 if (fd < 0) return;
Dan Alberte9fca142015-02-18 18:03:26 -080074
Elliott Hughescc65c3b2015-11-20 22:01:06 -080075 int rcv_buf_size = CHUNK_SIZE;
76 adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcv_buf_size, sizeof(rcv_buf_size));
Dan Alberte9fca142015-02-18 18:03:26 -080077
Elliott Hughescc65c3b2015-11-20 22:01:06 -080078 asocket* s = create_local_socket(fd);
79 if (s) {
Dan Alberte9fca142015-02-18 18:03:26 -080080 connect_to_smartsocket(s);
81 return;
82 }
83
84 adb_close(fd);
85 }
86}
87
Elliott Hughes424af022015-05-29 17:55:19 -070088static void listener_event_func(int _fd, unsigned ev, void* _l)
Dan Alberte9fca142015-02-18 18:03:26 -080089{
Dan Albertbac34742015-02-25 17:51:28 -080090 alistener* listener = reinterpret_cast<alistener*>(_l);
Dan Alberte9fca142015-02-18 18:03:26 -080091 asocket *s;
92
Dan Albertbac34742015-02-25 17:51:28 -080093 if (ev & FDE_READ) {
Josh Gao78e1eb12016-08-23 15:41:56 -070094 int fd = adb_socket_accept(_fd, nullptr, nullptr);
Dan Albertbac34742015-02-25 17:51:28 -080095 if (fd < 0) {
96 return;
97 }
Dan Alberte9fca142015-02-18 18:03:26 -080098
99 s = create_local_socket(fd);
Dan Albertbac34742015-02-25 17:51:28 -0800100 if (s) {
101 s->transport = listener->transport;
David Purselleaae97e2016-04-07 11:25:48 -0700102 connect_to_remote(s, listener->connect_to.c_str());
Dan Alberte9fca142015-02-18 18:03:26 -0800103 return;
104 }
105
106 adb_close(fd);
107 }
108}
109
David Purselleaae97e2016-04-07 11:25:48 -0700110// Called as a transport disconnect function. |arg| is the raw alistener*.
Yabin Cuib3298242015-08-28 15:09:44 -0700111static void listener_disconnect(void* arg, atransport*) {
David Purselleaae97e2016-04-07 11:25:48 -0700112 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 Alberte9fca142015-02-18 18:03:26 -0800117 }
Dan Alberte9fca142015-02-18 18:03:26 -0800118 }
David Purselleaae97e2016-04-07 11:25:48 -0700119}
120
Elliott Hughese67f1f82015-04-30 17:32:03 -0700121// Write the list of current listeners (network redirections) into a string.
122std::string format_listeners() {
123 std::string result;
David Purselleaae97e2016-04-07 11:25:48 -0700124 for (auto& l : listener_list) {
Dan Alberte9fca142015-02-18 18:03:26 -0800125 // Ignore special listeners like those for *smartsocket*
Elliott Hughese67f1f82015-04-30 17:32:03 -0700126 if (l->connect_to[0] == '*') {
127 continue;
Dan Alberte9fca142015-02-18 18:03:26 -0800128 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700129 // <device-serial> " " <local-name> " " <remote-name> "\n"
Elliott Hughes34c20bb2015-07-21 17:09:06 -0700130 // Entries from "adb reverse" have no serial.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700131 android::base::StringAppendF(&result, "%s %s %s\n",
Elliott Hughes34c20bb2015-07-21 17:09:06 -0700132 l->transport->serial ? l->transport->serial : "(reverse)",
David Purselleaae97e2016-04-07 11:25:48 -0700133 l->local_name.c_str(), l->connect_to.c_str());
Dan Alberte9fca142015-02-18 18:03:26 -0800134 }
135 return result;
136}
137
David Purselleaae97e2016-04-07 11:25:48 -0700138InstallStatus 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 Hughes7b506092015-04-20 08:09:20 -0700142 return INSTALL_STATUS_OK;
Dan Alberte9fca142015-02-18 18:03:26 -0800143 }
144 }
Elliott Hughes7b506092015-04-20 08:09:20 -0700145 return INSTALL_STATUS_LISTENER_NOT_FOUND;
Dan Alberte9fca142015-02-18 18:03:26 -0800146}
147
David Purselleaae97e2016-04-07 11:25:48 -0700148void remove_all_listeners() {
149 auto iter = listener_list.begin();
150 while (iter != listener_list.end()) {
Dan Alberte9fca142015-02-18 18:03:26 -0800151 // Never remove smart sockets.
David Purselleaae97e2016-04-07 11:25:48 -0700152 if ((*iter)->connect_to[0] == '*') {
153 ++iter;
154 } else {
155 iter = listener_list.erase(iter);
156 }
Dan Alberte9fca142015-02-18 18:03:26 -0800157 }
158}
159
David Purselleaae97e2016-04-07 11:25:48 -0700160InstallStatus 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 Hughesab52c182015-05-01 17:04:38 -0700164 if (local_name == l->local_name) {
David Purselleaae97e2016-04-07 11:25:48 -0700165 // Can't repurpose a smartsocket.
Dan Alberte9fca142015-02-18 18:03:26 -0800166 if(l->connect_to[0] == '*') {
Spencer Lowbf7c6052015-08-11 16:45:32 -0700167 *error = "cannot repurpose smartsocket";
Dan Alberte9fca142015-02-18 18:03:26 -0800168 return INSTALL_STATUS_INTERNAL_ERROR;
169 }
170
David Purselleaae97e2016-04-07 11:25:48 -0700171 // Can't repurpose a listener if 'no_rebind' is true.
Dan Alberte9fca142015-02-18 18:03:26 -0800172 if (no_rebind) {
Spencer Lowbf7c6052015-08-11 16:45:32 -0700173 *error = "cannot rebind";
Dan Alberte9fca142015-02-18 18:03:26 -0800174 return INSTALL_STATUS_CANNOT_REBIND;
175 }
176
David Purselleaae97e2016-04-07 11:25:48 -0700177 l->connect_to = connect_to;
Dan Alberte9fca142015-02-18 18:03:26 -0800178 if (l->transport != transport) {
Yabin Cuib3298242015-08-28 15:09:44 -0700179 l->transport->RemoveDisconnect(&l->disconnect);
Dan Alberte9fca142015-02-18 18:03:26 -0800180 l->transport = transport;
Yabin Cuib3298242015-08-28 15:09:44 -0700181 l->transport->AddDisconnect(&l->disconnect);
Dan Alberte9fca142015-02-18 18:03:26 -0800182 }
183 return INSTALL_STATUS_OK;
184 }
185 }
186
David Purselleaae97e2016-04-07 11:25:48 -0700187 std::unique_ptr<alistener> listener(new alistener(local_name, connect_to));
Dan Alberte9fca142015-02-18 18:03:26 -0800188
Josh Gaocfb21412016-08-24 18:38:44 -0700189 int resolved = 0;
190 listener->fd = socket_spec_listen(listener->local_name, error, &resolved);
Dan Albertbac34742015-02-25 17:51:28 -0800191 if (listener->fd < 0) {
Dan Albertbac34742015-02-25 17:51:28 -0800192 return INSTALL_STATUS_CANNOT_BIND;
Dan Alberte9fca142015-02-18 18:03:26 -0800193 }
194
Josh Gaocfb21412016-08-24 18:38:44 -0700195 // 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 Albertbac34742015-02-25 17:51:28 -0800203 close_on_exec(listener->fd);
David Purselleaae97e2016-04-07 11:25:48 -0700204 if (listener->connect_to == "*smartsocket*") {
205 fdevent_install(&listener->fde, listener->fd, ss_listener_event_func, listener.get());
Dan Alberte9fca142015-02-18 18:03:26 -0800206 } else {
David Purselleaae97e2016-04-07 11:25:48 -0700207 fdevent_install(&listener->fde, listener->fd, listener_event_func, listener.get());
Dan Alberte9fca142015-02-18 18:03:26 -0800208 }
Dan Albertbac34742015-02-25 17:51:28 -0800209 fdevent_set(&listener->fde, FDE_READ);
Dan Alberte9fca142015-02-18 18:03:26 -0800210
Dan Albertbac34742015-02-25 17:51:28 -0800211 listener->transport = transport;
Dan Alberte9fca142015-02-18 18:03:26 -0800212
213 if (transport) {
David Purselleaae97e2016-04-07 11:25:48 -0700214 listener->disconnect.opaque = listener.get();
Dan Albertbac34742015-02-25 17:51:28 -0800215 listener->disconnect.func = listener_disconnect;
Yabin Cuib3298242015-08-28 15:09:44 -0700216 transport->AddDisconnect(&listener->disconnect);
Dan Alberte9fca142015-02-18 18:03:26 -0800217 }
Dan Alberte9fca142015-02-18 18:03:26 -0800218
David Purselleaae97e2016-04-07 11:25:48 -0700219 listener_list.push_back(std::move(listener));
220 return INSTALL_STATUS_OK;
Dan Alberte9fca142015-02-18 18:03:26 -0800221}