blob: 2f9e9b4d039e323deac955c1d8e0893d9996f5c6 [file] [log] [blame]
Joshua Duong290ccb52019-11-20 14:18:43 -08001/*
2 * Copyright (C) 2019 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#if !ADB_HOST
18
19#define TRACE_TAG ADB_WIRELESS
20
21#include "adb_wifi.h"
22
23#include <unistd.h>
24#include <optional>
25
26#include <adbd_auth.h>
27#include <android-base/properties.h>
28
29#include "adb.h"
30#include "daemon/mdns.h"
31#include "sysdeps.h"
32#include "transport.h"
33
34using namespace android::base;
35
36namespace {
37
38static AdbdAuthContext* auth_ctx;
39
40static void adb_disconnected(void* unused, atransport* t);
41static struct adisconnect adb_disconnect = {adb_disconnected, nullptr};
42
43static void adb_disconnected(void* unused, atransport* t) {
44 LOG(INFO) << "ADB wifi device disconnected";
Josh Gao887ab012020-06-24 16:25:49 -070045 CHECK(t->auth_id.has_value());
46 adbd_auth_tls_device_disconnected(auth_ctx, kAdbTransportTypeWifi, t->auth_id.value());
Joshua Duong290ccb52019-11-20 14:18:43 -080047}
48
49// TODO(b/31559095): need bionic host so that we can use 'prop_info' returned
50// from WaitForProperty
51#if defined(__ANDROID__)
52
53class TlsServer {
54 public:
55 explicit TlsServer(int port);
56 virtual ~TlsServer();
57 bool Start();
58 uint16_t port() { return port_; };
59
60 private:
61 void OnFdEvent(int fd, unsigned ev);
62 static void StaticOnFdEvent(int fd, unsigned ev, void* opaque);
63
64 fdevent* fd_event_ = nullptr;
65 uint16_t port_;
66}; // TlsServer
67
68TlsServer::TlsServer(int port) : port_(port) {}
69
70TlsServer::~TlsServer() {
71 fdevent* fde = fd_event_;
72 fdevent_run_on_main_thread([fde]() {
73 if (fde != nullptr) {
74 fdevent_destroy(fde);
75 }
76 });
77}
78
79bool TlsServer::Start() {
80 std::condition_variable cv;
81 std::mutex mutex;
82 std::optional<bool> success;
83 auto callback = [&](bool result) {
84 {
85 std::lock_guard<std::mutex> lock(mutex);
86 success = result;
87 }
88 cv.notify_one();
89 };
90
91 std::string err;
92 unique_fd fd(network_inaddr_any_server(port_, SOCK_STREAM, &err));
93 if (fd.get() == -1) {
94 LOG(ERROR) << "Failed to start TLS server [" << err << "]";
95 return false;
96 }
97 close_on_exec(fd.get());
98 int port = socket_get_local_port(fd.get());
99 if (port <= 0 || port > 65535) {
100 LOG(ERROR) << "Invalid port for tls server";
101 return false;
102 }
103 port_ = static_cast<uint16_t>(port);
104 LOG(INFO) << "adbwifi started on port " << port_;
105
106 std::unique_lock<std::mutex> lock(mutex);
107 fdevent_run_on_main_thread([&]() {
108 fd_event_ = fdevent_create(fd.release(), &TlsServer::StaticOnFdEvent, this);
109 if (fd_event_ == nullptr) {
110 LOG(ERROR) << "Failed to create fd event for TlsServer.";
111 callback(false);
112 return;
113 }
114 callback(true);
115 });
116
117 cv.wait(lock, [&]() { return success.has_value(); });
118 if (!*success) {
119 LOG(INFO) << "TlsServer fdevent_create failed";
120 return false;
121 }
122 fdevent_set(fd_event_, FDE_READ);
123 LOG(INFO) << "TlsServer running on port " << port_;
124
125 return *success;
126}
127
128// static
129void TlsServer::StaticOnFdEvent(int fd, unsigned ev, void* opaque) {
130 auto server = reinterpret_cast<TlsServer*>(opaque);
131 server->OnFdEvent(fd, ev);
132}
133
134void TlsServer::OnFdEvent(int fd, unsigned ev) {
135 if ((ev & FDE_READ) == 0 || fd != fd_event_->fd.get()) {
136 LOG(INFO) << __func__ << ": No read [ev=" << ev << " fd=" << fd << "]";
137 return;
138 }
139
140 unique_fd new_fd(adb_socket_accept(fd, nullptr, nullptr));
141 if (new_fd >= 0) {
142 LOG(INFO) << "New TLS connection [fd=" << new_fd.get() << "]";
143 close_on_exec(new_fd.get());
144 disable_tcp_nagle(new_fd.get());
145 std::string serial = android::base::StringPrintf("host-%d", new_fd.get());
Joshua Duong64fab752020-01-21 13:19:42 -0800146 register_socket_transport(
147 std::move(new_fd), std::move(serial), port_, 1,
148 [](atransport*) { return ReconnectResult::Abort; }, true);
Joshua Duong290ccb52019-11-20 14:18:43 -0800149 }
150}
151
152TlsServer* sTlsServer = nullptr;
153const char kWifiPortProp[] = "service.adb.tls.port";
154
155const char kWifiEnabledProp[] = "persist.adb.tls_server.enable";
156
157static void enable_wifi_debugging() {
158 start_mdnsd();
159
160 if (sTlsServer != nullptr) {
161 delete sTlsServer;
162 }
163 sTlsServer = new TlsServer(0);
164 if (!sTlsServer->Start()) {
165 LOG(ERROR) << "Failed to start TlsServer";
166 delete sTlsServer;
167 sTlsServer = nullptr;
168 return;
169 }
170
171 // Start mdns connect service for discovery
172 register_adb_secure_connect_service(sTlsServer->port());
173 LOG(INFO) << "adb wifi started on port " << sTlsServer->port();
174 SetProperty(kWifiPortProp, std::to_string(sTlsServer->port()));
175}
176
177static void disable_wifi_debugging() {
178 if (sTlsServer != nullptr) {
179 delete sTlsServer;
180 sTlsServer = nullptr;
181 }
182 if (is_adb_secure_connect_service_registered()) {
183 unregister_adb_secure_connect_service();
184 }
185 kick_all_tcp_tls_transports();
186 LOG(INFO) << "adb wifi stopped";
187 SetProperty(kWifiPortProp, "");
188}
189
190// Watches for the #kWifiEnabledProp property to toggle the TlsServer
191static void start_wifi_enabled_observer() {
192 std::thread([]() {
193 bool wifi_enabled = false;
194 while (true) {
195 std::string toggled_val = wifi_enabled ? "0" : "1";
196 LOG(INFO) << "Waiting for " << kWifiEnabledProp << "=" << toggled_val;
197 if (WaitForProperty(kWifiEnabledProp, toggled_val)) {
198 wifi_enabled = !wifi_enabled;
199 LOG(INFO) << kWifiEnabledProp << " changed to " << toggled_val;
200 if (wifi_enabled) {
201 enable_wifi_debugging();
202 } else {
203 disable_wifi_debugging();
204 }
205 }
206 }
207 }).detach();
208}
209#endif //__ANDROID__
210
211} // namespace
212
213void adbd_wifi_init(AdbdAuthContext* ctx) {
214 auth_ctx = ctx;
215#if defined(__ANDROID__)
216 start_wifi_enabled_observer();
217#endif //__ANDROID__
218}
219
220void adbd_wifi_secure_connect(atransport* t) {
221 t->AddDisconnect(&adb_disconnect);
222 handle_online(t);
223 send_connect(t);
224 LOG(INFO) << __func__ << ": connected " << t->serial;
225 t->auth_id = adbd_auth_tls_device_connected(auth_ctx, kAdbTransportTypeWifi, t->auth_key.data(),
226 t->auth_key.size());
227}
Joshua Duong64fab752020-01-21 13:19:42 -0800228
Joshua Duong290ccb52019-11-20 14:18:43 -0800229#endif /* !HOST */