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 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_ADB_PROXY_H_ |
| 6 | #define PATCHPANEL_ADB_PROXY_H_ |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 7 | |
| 8 | #include <deque> |
| 9 | #include <memory> |
| 10 | |
Hidehiko Abe | de12922 | 2019-08-16 00:55:04 +0900 | [diff] [blame] | 11 | #include <base/files/file_descriptor_watcher_posix.h> |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 12 | #include <base/files/scoped_file.h> |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 13 | #include <base/memory/weak_ptr.h> |
| 14 | #include <brillo/daemons/daemon.h> |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 15 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 16 | #include "patchpanel/message_dispatcher.h" |
| 17 | #include "patchpanel/socket.h" |
| 18 | #include "patchpanel/socket_forwarder.h" |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 19 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 20 | namespace patchpanel { |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 21 | |
Jason Jeremy Iman | 3081d0e | 2020-03-04 15:52:06 +0900 | [diff] [blame] | 22 | // Running the proxy on port 5555 will cause ADBD to see it as an Android |
| 23 | // emulator rather than an attached device. This means, whenever host ADBD |
| 24 | // server runs a device named "emulator-5554" will show up. |
| 25 | // Connections to ARC via ADB (including by Tast) should now be done by |
| 26 | // starting ADB server (e.g. 'adb devices') instead of |
| 27 | // 'adb connect 127.0.0.1:5555' to avoid seeing multiple devices. |
| 28 | constexpr uint16_t kAdbProxyTcpListenPort = 5555; |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 29 | |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 30 | // Subprocess for proxying ADB traffic. |
Hidehiko Abe | de12922 | 2019-08-16 00:55:04 +0900 | [diff] [blame] | 31 | class AdbProxy : public brillo::Daemon { |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 32 | public: |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 33 | explicit AdbProxy(base::ScopedFD control_fd); |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 34 | virtual ~AdbProxy(); |
| 35 | |
| 36 | protected: |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 37 | int OnInit() override; |
| 38 | |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 39 | void OnParentProcessExit(); |
| 40 | void OnGuestMessage(const GuestMessage& msg); |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 41 | |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 42 | private: |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 43 | void Reset(); |
Hidehiko Abe | de12922 | 2019-08-16 00:55:04 +0900 | [diff] [blame] | 44 | void OnFileCanReadWithoutBlocking(); |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 45 | |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 46 | // Attempts to establish a connection to ADB at well-known destinations. |
| 47 | std::unique_ptr<Socket> Connect() const; |
| 48 | |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 49 | MessageDispatcher msg_dispatcher_; |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 50 | std::unique_ptr<Socket> src_; |
| 51 | std::deque<std::unique_ptr<SocketForwarder>> fwd_; |
Hidehiko Abe | de12922 | 2019-08-16 00:55:04 +0900 | [diff] [blame] | 52 | std::unique_ptr<base::FileDescriptorWatcher::Controller> src_watcher_; |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 53 | |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 54 | GuestMessage::GuestType arc_type_; |
Garrick Evans | 1cce71a | 2019-06-21 10:43:14 +0900 | [diff] [blame] | 55 | uint32_t arcvm_vsock_cid_; |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 56 | |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 57 | base::WeakPtrFactory<AdbProxy> weak_factory_{this}; |
| 58 | DISALLOW_COPY_AND_ASSIGN(AdbProxy); |
| 59 | }; |
| 60 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 61 | } // namespace patchpanel |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 62 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 63 | #endif // PATCHPANEL_ADB_PROXY_H_ |