1 /*
2 * Copyright (C) 2022 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 "common/libs/utils/socket2socket_proxy.h"
18
19 #include <poll.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22
23 #include <cstring>
24 #include <functional>
25 #include <list>
26 #include <memory>
27 #include <string>
28 #include <thread>
29 #include <utility>
30 #include <vector>
31
32 #include <android-base/logging.h>
33
34 namespace cuttlefish {
35 namespace {
36
37 class ProxyPair {
38 public:
ProxyPair()39 ProxyPair()
40 : stop_fd_(SharedFD::Event()) {
41 if (!stop_fd_->IsOpen()) {
42 LOG(FATAL) << "Failed to open eventfd: " << stop_fd_->StrError();
43 return;
44 }
45 }
46
ProxyPair(ProxyPair && other)47 ProxyPair(ProxyPair&& other)
48 : started_(other.started_),
49 stop_fd_(std::move(other.stop_fd_)),
50 c2t_running_(other.c2t_running_.load()),
51 t2c_running_(other.t2c_running_.load()) {
52 if (other.started_) {
53 LOG(FATAL) << "ProxyPair cannot be moved after Start() being executed";
54 }
55 }
56
~ProxyPair()57 ~ProxyPair() {
58 if (stop_fd_->IsOpen() && stop_fd_->EventfdWrite(1) != 0) {
59 LOG(ERROR) << "Failed to stop proxy thread: " << stop_fd_->StrError();
60 }
61 if (c2t_.joinable()) {
62 c2t_.join();
63 }
64 if (t2c_.joinable()) {
65 t2c_.join();
66 }
67 }
68
Start(SharedFD from,SharedFD to)69 void Start(SharedFD from, SharedFD to) {
70 if (started_) {
71 LOG(FATAL) << "ProxyPair cannot be started second time";
72 }
73 started_ = true;
74 c2t_running_ = true;
75 t2c_running_ = true;
76 c2t_ = std::thread(&ProxyPair::Forward, this, "c2t", from, to, stop_fd_,
77 std::ref(c2t_running_));
78 t2c_ = std::thread(&ProxyPair::Forward, this, "t2c", to, from, stop_fd_,
79 std::ref(t2c_running_));
80 }
81
Running()82 bool Running() {
83 return c2t_running_ || t2c_running_;
84 }
85
86 private:
Forward(const std::string & label,SharedFD from,SharedFD to,SharedFD stop,std::atomic<bool> & running)87 void Forward(const std::string& label, SharedFD from, SharedFD to,
88 SharedFD stop, std::atomic<bool>& running) {
89 LOG(DEBUG) << label << ": Proxy thread started. Starting copying data";
90 auto success = to->CopyAllFrom(*from, &(*stop));
91 if (!success) {
92 if (from->GetErrno()) {
93 LOG(ERROR) << label << ": Error reading: " << from->StrError();
94 }
95 if (to->GetErrno()) {
96 LOG(ERROR) << label << ": Error writing: " << to->StrError();
97 }
98 }
99 to->Shutdown(SHUT_WR);
100 running = false;
101 LOG(DEBUG) << label << ": Proxy thread completed";
102 }
103
104 bool started_;
105 SharedFD stop_fd_;
106 std::atomic<bool> c2t_running_;
107 std::atomic<bool> t2c_running_;
108 std::thread c2t_;
109 std::thread t2c_;
110 };
111
112 } // namespace
113
ProxyServer(SharedFD server,std::function<SharedFD ()> clients_factory)114 ProxyServer::ProxyServer(SharedFD server, std::function<SharedFD()> clients_factory)
115 : stop_fd_(SharedFD::Event()) {
116 if (!stop_fd_->IsOpen()) {
117 LOG(FATAL) << "Failed to open eventfd: " << stop_fd_->StrError();
118 return;
119 }
120 server_ = std::thread([&, server_fd = std::move(server),
121 clients_factory = std::move(clients_factory)]() {
122 constexpr ssize_t SERVER = 0;
123 constexpr ssize_t STOP = 1;
124 std::list<ProxyPair> watched;
125
126 std::vector<PollSharedFd> server_poll = {
127 {.fd = server_fd, .events = POLLIN},
128 {.fd = stop_fd_, .events = POLLIN}
129 };
130
131 while (server_fd->IsOpen()) {
132 server_poll[SERVER].revents = 0;
133 server_poll[STOP].revents = 0;
134
135 const int poll_result = SharedFD::Poll(server_poll, -1);
136 if (poll_result < 0) {
137 LOG(ERROR) << "Failed to poll to wait for incoming connection";
138 continue;
139 }
140 if (server_poll[STOP].revents & POLLIN) {
141 // Stop fd is available to read, so we received a stop event
142 // and must stop the thread
143 break;
144 }
145 if (!(server_poll[SERVER].revents & POLLIN)) {
146 continue;
147 }
148
149 // Server fd is available to read, so we can accept the
150 // connection without blocking on that
151 auto client = SharedFD::Accept(*server_fd);
152 if (!client->IsOpen()) {
153 LOG(ERROR) << "Failed to accept incoming connection: " << client->StrError();
154 continue;
155 }
156 auto target = clients_factory();
157 if (target->IsOpen()) {
158 LOG(DEBUG) << "Launching proxy threads";
159 watched.push_back(ProxyPair());
160 watched.back().Start(client, target);
161 LOG(DEBUG) << "Proxy is launched. Amount of currently tracked proxy pairs: "
162 << watched.size();
163 } else {
164 LOG(ERROR) << "Cannot connect to the target to setup proxying: " << target->StrError();
165 }
166 // Unwatch completed proxy pairs
167 watched.remove_if([](ProxyPair& proxy) { return !proxy.Running(); });
168 }
169
170 // Making sure all launched proxy pairs are finished by triggering their destructor
171 LOG(DEBUG) << "Waiting for proxy threads to turn down";
172 watched.clear();
173 LOG(DEBUG) << "Proxy threads are successfully turned down";
174 });
175 }
176
Join()177 void ProxyServer::Join() {
178 if (server_.joinable()) {
179 server_.join();
180 }
181 }
182
~ProxyServer()183 ProxyServer::~ProxyServer() {
184 if (stop_fd_->EventfdWrite(1) != 0) {
185 LOG(ERROR) << "Failed to stop proxy thread: " << stop_fd_->StrError();
186 }
187 Join();
188 }
189
Proxy(SharedFD server,std::function<SharedFD ()> conn_factory)190 void Proxy(SharedFD server, std::function<SharedFD()> conn_factory) {
191 ProxyServer proxy(std::move(server), std::move(conn_factory));
192 proxy.Join();
193 }
194
ProxyAsync(SharedFD server,std::function<SharedFD ()> conn_factory)195 std::unique_ptr<ProxyServer> ProxyAsync(SharedFD server, std::function<SharedFD()> conn_factory) {
196 return std::make_unique<ProxyServer>(std::move(server), std::move(conn_factory));
197 }
198
199 } // namespace cuttlefish
200