blob: 0299be3d3bd6796a8ac32e344db4a2fe1f8596ca [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
Dan Alberte9fca142015-02-18 18:03:26 -080026#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080027#include "transport.h"
Dan Alberte9fca142015-02-18 18:03:26 -080028
David Purselleaae97e2016-04-07 11:25:48 -070029// Not static because it is used in commandline.c.
30int gListenAll = 0;
Dan Alberte9fca142015-02-18 18:03:26 -080031
David Purselleaae97e2016-04-07 11:25:48 -070032// A listener is an entity which binds to a local port and, upon receiving a connection on that
33// port, creates an asocket to connect the new local connection to a specific remote service.
34//
35// TODO: some listeners read from the new connection to determine what exact service to connect to
36// on the far side.
37class alistener {
38 public:
39 alistener(const std::string& _local_name, const std::string& _connect_to);
40 ~alistener();
41
42 fdevent fde;
43 int fd = -1;
44
45 std::string local_name;
46 std::string connect_to;
47 atransport* transport = nullptr;
48 adisconnect disconnect;
49
50 private:
51 DISALLOW_COPY_AND_ASSIGN(alistener);
Dan Alberte9fca142015-02-18 18:03:26 -080052};
53
David Purselleaae97e2016-04-07 11:25:48 -070054alistener::alistener(const std::string& _local_name, const std::string& _connect_to)
55 : local_name(_local_name), connect_to(_connect_to) {
56}
57
58alistener::~alistener() {
59 // Closes the corresponding fd.
60 fdevent_remove(&fde);
61
62 if (transport) {
63 transport->RemoveDisconnect(&disconnect);
64 }
65}
66
67// listener_list retains ownership of all created alistener objects. Removing an alistener from
68// this list will cause it to be deleted.
69typedef std::list<std::unique_ptr<alistener>> ListenerList;
70static ListenerList& listener_list = *new ListenerList();
71
Elliott Hughes424af022015-05-29 17:55:19 -070072static void ss_listener_event_func(int _fd, unsigned ev, void *_l) {
Elliott Hughescc65c3b2015-11-20 22:01:06 -080073 if (ev & FDE_READ) {
Josh Gao78e1eb12016-08-23 15:41:56 -070074 int fd = adb_socket_accept(_fd, nullptr, nullptr);
Elliott Hughescc65c3b2015-11-20 22:01:06 -080075 if (fd < 0) return;
Dan Alberte9fca142015-02-18 18:03:26 -080076
Elliott Hughescc65c3b2015-11-20 22:01:06 -080077 int rcv_buf_size = CHUNK_SIZE;
78 adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcv_buf_size, sizeof(rcv_buf_size));
Dan Alberte9fca142015-02-18 18:03:26 -080079
Elliott Hughescc65c3b2015-11-20 22:01:06 -080080 asocket* s = create_local_socket(fd);
81 if (s) {
Dan Alberte9fca142015-02-18 18:03:26 -080082 connect_to_smartsocket(s);
83 return;
84 }
85
86 adb_close(fd);
87 }
88}
89
Elliott Hughes424af022015-05-29 17:55:19 -070090static void listener_event_func(int _fd, unsigned ev, void* _l)
Dan Alberte9fca142015-02-18 18:03:26 -080091{
Dan Albertbac34742015-02-25 17:51:28 -080092 alistener* listener = reinterpret_cast<alistener*>(_l);
Dan Alberte9fca142015-02-18 18:03:26 -080093 asocket *s;
94
Dan Albertbac34742015-02-25 17:51:28 -080095 if (ev & FDE_READ) {
Josh Gao78e1eb12016-08-23 15:41:56 -070096 int fd = adb_socket_accept(_fd, nullptr, nullptr);
Dan Albertbac34742015-02-25 17:51:28 -080097 if (fd < 0) {
98 return;
99 }
Dan Alberte9fca142015-02-18 18:03:26 -0800100
101 s = create_local_socket(fd);
Dan Albertbac34742015-02-25 17:51:28 -0800102 if (s) {
103 s->transport = listener->transport;
David Purselleaae97e2016-04-07 11:25:48 -0700104 connect_to_remote(s, listener->connect_to.c_str());
Dan Alberte9fca142015-02-18 18:03:26 -0800105 return;
106 }
107
108 adb_close(fd);
109 }
110}
111
David Purselleaae97e2016-04-07 11:25:48 -0700112// Called as a transport disconnect function. |arg| is the raw alistener*.
Yabin Cuib3298242015-08-28 15:09:44 -0700113static void listener_disconnect(void* arg, atransport*) {
David Purselleaae97e2016-04-07 11:25:48 -0700114 for (auto iter = listener_list.begin(); iter != listener_list.end(); ++iter) {
115 if (iter->get() == arg) {
116 (*iter)->transport = nullptr;
117 listener_list.erase(iter);
118 return;
Dan Alberte9fca142015-02-18 18:03:26 -0800119 }
Dan Alberte9fca142015-02-18 18:03:26 -0800120 }
David Purselleaae97e2016-04-07 11:25:48 -0700121}
122
123int local_name_to_fd(alistener* listener, int* resolved_tcp_port, std::string* error) {
124 if (android::base::StartsWith(listener->local_name, "tcp:")) {
125 int requested_port = atoi(&listener->local_name[4]);
126 int sock = -1;
127 if (gListenAll > 0) {
128 sock = network_inaddr_any_server(requested_port, SOCK_STREAM, error);
129 } else {
130 sock = network_loopback_server(requested_port, SOCK_STREAM, error);
131 }
132
133 // If the caller requested port 0, update the listener name with the resolved port.
134 if (sock >= 0 && requested_port == 0) {
135 int local_port = adb_socket_get_local_port(sock);
136 if (local_port > 0) {
137 listener->local_name = android::base::StringPrintf("tcp:%d", local_port);
138 if (resolved_tcp_port != nullptr) {
139 *resolved_tcp_port = local_port;
140 }
141 }
142 }
143
144 return sock;
145 }
Elliott Hughes095307e2015-07-09 10:03:18 -0700146#if !defined(_WIN32) // No Unix-domain sockets on Windows.
David Purselleaae97e2016-04-07 11:25:48 -0700147 // It's nonsensical to support the "reserved" space on the adb host side.
148 if (android::base::StartsWith(listener->local_name, "local:")) {
149 return network_local_server(&listener->local_name[6], ANDROID_SOCKET_NAMESPACE_ABSTRACT,
150 SOCK_STREAM, error);
151 } else if (android::base::StartsWith(listener->local_name, "localabstract:")) {
152 return network_local_server(&listener->local_name[14], ANDROID_SOCKET_NAMESPACE_ABSTRACT,
153 SOCK_STREAM, error);
154 } else if (android::base::StartsWith(listener->local_name, "localfilesystem:")) {
155 return network_local_server(&listener->local_name[16], ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
156 SOCK_STREAM, error);
Dan Alberte9fca142015-02-18 18:03:26 -0800157 }
158
159#endif
David Purselleaae97e2016-04-07 11:25:48 -0700160 *error = android::base::StringPrintf("unknown local portname '%s'",
161 listener->local_name.c_str());
Dan Alberte9fca142015-02-18 18:03:26 -0800162 return -1;
163}
164
Elliott Hughese67f1f82015-04-30 17:32:03 -0700165// Write the list of current listeners (network redirections) into a string.
166std::string format_listeners() {
167 std::string result;
David Purselleaae97e2016-04-07 11:25:48 -0700168 for (auto& l : listener_list) {
Dan Alberte9fca142015-02-18 18:03:26 -0800169 // Ignore special listeners like those for *smartsocket*
Elliott Hughese67f1f82015-04-30 17:32:03 -0700170 if (l->connect_to[0] == '*') {
171 continue;
Dan Alberte9fca142015-02-18 18:03:26 -0800172 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700173 // <device-serial> " " <local-name> " " <remote-name> "\n"
Elliott Hughes34c20bb2015-07-21 17:09:06 -0700174 // Entries from "adb reverse" have no serial.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700175 android::base::StringAppendF(&result, "%s %s %s\n",
Elliott Hughes34c20bb2015-07-21 17:09:06 -0700176 l->transport->serial ? l->transport->serial : "(reverse)",
David Purselleaae97e2016-04-07 11:25:48 -0700177 l->local_name.c_str(), l->connect_to.c_str());
Dan Alberte9fca142015-02-18 18:03:26 -0800178 }
179 return result;
180}
181
David Purselleaae97e2016-04-07 11:25:48 -0700182InstallStatus remove_listener(const char* local_name, atransport* transport) {
183 for (auto iter = listener_list.begin(); iter != listener_list.end(); ++iter) {
184 if (local_name == (*iter)->local_name) {
185 listener_list.erase(iter);
Elliott Hughes7b506092015-04-20 08:09:20 -0700186 return INSTALL_STATUS_OK;
Dan Alberte9fca142015-02-18 18:03:26 -0800187 }
188 }
Elliott Hughes7b506092015-04-20 08:09:20 -0700189 return INSTALL_STATUS_LISTENER_NOT_FOUND;
Dan Alberte9fca142015-02-18 18:03:26 -0800190}
191
David Purselleaae97e2016-04-07 11:25:48 -0700192void remove_all_listeners() {
193 auto iter = listener_list.begin();
194 while (iter != listener_list.end()) {
Dan Alberte9fca142015-02-18 18:03:26 -0800195 // Never remove smart sockets.
David Purselleaae97e2016-04-07 11:25:48 -0700196 if ((*iter)->connect_to[0] == '*') {
197 ++iter;
198 } else {
199 iter = listener_list.erase(iter);
200 }
Dan Alberte9fca142015-02-18 18:03:26 -0800201 }
202}
203
David Purselleaae97e2016-04-07 11:25:48 -0700204InstallStatus install_listener(const std::string& local_name, const char* connect_to,
205 atransport* transport, int no_rebind, int* resolved_tcp_port,
206 std::string* error) {
207 for (auto& l : listener_list) {
Elliott Hughesab52c182015-05-01 17:04:38 -0700208 if (local_name == l->local_name) {
David Purselleaae97e2016-04-07 11:25:48 -0700209 // Can't repurpose a smartsocket.
Dan Alberte9fca142015-02-18 18:03:26 -0800210 if(l->connect_to[0] == '*') {
Spencer Lowbf7c6052015-08-11 16:45:32 -0700211 *error = "cannot repurpose smartsocket";
Dan Alberte9fca142015-02-18 18:03:26 -0800212 return INSTALL_STATUS_INTERNAL_ERROR;
213 }
214
David Purselleaae97e2016-04-07 11:25:48 -0700215 // Can't repurpose a listener if 'no_rebind' is true.
Dan Alberte9fca142015-02-18 18:03:26 -0800216 if (no_rebind) {
Spencer Lowbf7c6052015-08-11 16:45:32 -0700217 *error = "cannot rebind";
Dan Alberte9fca142015-02-18 18:03:26 -0800218 return INSTALL_STATUS_CANNOT_REBIND;
219 }
220
David Purselleaae97e2016-04-07 11:25:48 -0700221 l->connect_to = connect_to;
Dan Alberte9fca142015-02-18 18:03:26 -0800222 if (l->transport != transport) {
Yabin Cuib3298242015-08-28 15:09:44 -0700223 l->transport->RemoveDisconnect(&l->disconnect);
Dan Alberte9fca142015-02-18 18:03:26 -0800224 l->transport = transport;
Yabin Cuib3298242015-08-28 15:09:44 -0700225 l->transport->AddDisconnect(&l->disconnect);
Dan Alberte9fca142015-02-18 18:03:26 -0800226 }
227 return INSTALL_STATUS_OK;
228 }
229 }
230
David Purselleaae97e2016-04-07 11:25:48 -0700231 std::unique_ptr<alistener> listener(new alistener(local_name, connect_to));
Dan Alberte9fca142015-02-18 18:03:26 -0800232
David Purselleaae97e2016-04-07 11:25:48 -0700233 listener->fd = local_name_to_fd(listener.get(), resolved_tcp_port, error);
Dan Albertbac34742015-02-25 17:51:28 -0800234 if (listener->fd < 0) {
Dan Albertbac34742015-02-25 17:51:28 -0800235 return INSTALL_STATUS_CANNOT_BIND;
Dan Alberte9fca142015-02-18 18:03:26 -0800236 }
237
Dan Albertbac34742015-02-25 17:51:28 -0800238 close_on_exec(listener->fd);
David Purselleaae97e2016-04-07 11:25:48 -0700239 if (listener->connect_to == "*smartsocket*") {
240 fdevent_install(&listener->fde, listener->fd, ss_listener_event_func, listener.get());
Dan Alberte9fca142015-02-18 18:03:26 -0800241 } else {
David Purselleaae97e2016-04-07 11:25:48 -0700242 fdevent_install(&listener->fde, listener->fd, listener_event_func, listener.get());
Dan Alberte9fca142015-02-18 18:03:26 -0800243 }
Dan Albertbac34742015-02-25 17:51:28 -0800244 fdevent_set(&listener->fde, FDE_READ);
Dan Alberte9fca142015-02-18 18:03:26 -0800245
Dan Albertbac34742015-02-25 17:51:28 -0800246 listener->transport = transport;
Dan Alberte9fca142015-02-18 18:03:26 -0800247
248 if (transport) {
David Purselleaae97e2016-04-07 11:25:48 -0700249 listener->disconnect.opaque = listener.get();
Dan Albertbac34742015-02-25 17:51:28 -0800250 listener->disconnect.func = listener_disconnect;
Yabin Cuib3298242015-08-28 15:09:44 -0700251 transport->AddDisconnect(&listener->disconnect);
Dan Alberte9fca142015-02-18 18:03:26 -0800252 }
Dan Alberte9fca142015-02-18 18:03:26 -0800253
David Purselleaae97e2016-04-07 11:25:48 -0700254 listener_list.push_back(std::move(listener));
255 return INSTALL_STATUS_OK;
Dan Alberte9fca142015-02-18 18:03:26 -0800256}