Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame^] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef ARC_NETWORK_ADB_PROXY_H_ |
| 6 | #define ARC_NETWORK_ADB_PROXY_H_ |
| 7 | |
| 8 | #include <deque> |
| 9 | #include <memory> |
| 10 | |
| 11 | #include <base/memory/weak_ptr.h> |
| 12 | #include <brillo/daemons/daemon.h> |
| 13 | #include <base/message_loop/message_loop.h> |
| 14 | |
| 15 | #include "arc/network/socket.h" |
| 16 | #include "arc/network/socket_forwarder.h" |
| 17 | |
| 18 | namespace arc_networkd { |
| 19 | |
| 20 | // Subprocess for proxying ADB traffic. |
| 21 | class AdbProxy : public brillo::Daemon, public base::MessageLoopForIO::Watcher { |
| 22 | public: |
| 23 | AdbProxy(); |
| 24 | virtual ~AdbProxy(); |
| 25 | |
| 26 | protected: |
| 27 | // Starts the server on the |src_port| over TCP. |
| 28 | int OnInit() override; |
| 29 | |
| 30 | // Watcher callback for accepting new connections. |
| 31 | void OnFileCanReadWithoutBlocking(int fd) override; |
| 32 | void OnFileCanWriteWithoutBlocking(int fd) override {} |
| 33 | |
| 34 | private: |
| 35 | // Detects when the guest OS is turned up and torn down and cleans up any |
| 36 | // existing forwarders and connections. |
| 37 | bool OnSignal(const struct signalfd_siginfo& info); |
| 38 | |
| 39 | // Attempts to establish a connection to ADB at well-known destinations. |
| 40 | std::unique_ptr<Socket> Connect() const; |
| 41 | |
| 42 | std::unique_ptr<Socket> src_; |
| 43 | std::deque<std::unique_ptr<SocketForwarder>> fwd_; |
| 44 | base::MessageLoopForIO::FileDescriptorWatcher src_watcher_; |
| 45 | |
| 46 | base::WeakPtrFactory<AdbProxy> weak_factory_{this}; |
| 47 | DISALLOW_COPY_AND_ASSIGN(AdbProxy); |
| 48 | }; |
| 49 | |
| 50 | } // namespace arc_networkd |
| 51 | |
| 52 | #endif // ARC_NETWORK_ADB_PROXY_H_ |