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> |
Jason Jeremy Iman | 378930a | 2020-12-11 05:40:08 +0900 | [diff] [blame^] | 14 | #include <brillo/daemons/dbus_daemon.h> |
| 15 | #include <dbus/bus.h> |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 16 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 17 | #include "patchpanel/message_dispatcher.h" |
| 18 | #include "patchpanel/socket.h" |
| 19 | #include "patchpanel/socket_forwarder.h" |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 20 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 21 | namespace patchpanel { |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 22 | |
Jason Jeremy Iman | 3081d0e | 2020-03-04 15:52:06 +0900 | [diff] [blame] | 23 | // Running the proxy on port 5555 will cause ADBD to see it as an Android |
| 24 | // emulator rather than an attached device. This means, whenever host ADBD |
| 25 | // server runs a device named "emulator-5554" will show up. |
| 26 | // Connections to ARC via ADB (including by Tast) should now be done by |
| 27 | // starting ADB server (e.g. 'adb devices') instead of |
| 28 | // 'adb connect 127.0.0.1:5555' to avoid seeing multiple devices. |
| 29 | constexpr uint16_t kAdbProxyTcpListenPort = 5555; |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 30 | |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 31 | // Subprocess for proxying ADB traffic. |
Jason Jeremy Iman | 378930a | 2020-12-11 05:40:08 +0900 | [diff] [blame^] | 32 | class AdbProxy : public brillo::DBusDaemon { |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 33 | public: |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 34 | explicit AdbProxy(base::ScopedFD control_fd); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 35 | AdbProxy(const AdbProxy&) = delete; |
| 36 | AdbProxy& operator=(const AdbProxy&) = delete; |
| 37 | |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 38 | virtual ~AdbProxy(); |
| 39 | |
| 40 | protected: |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 41 | int OnInit() override; |
| 42 | |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 43 | void OnParentProcessExit(); |
| 44 | void OnGuestMessage(const GuestMessage& msg); |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 45 | |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 46 | private: |
Jason Jeremy Iman | 378930a | 2020-12-11 05:40:08 +0900 | [diff] [blame^] | 47 | void InitialSetup(); |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 48 | void Reset(); |
Hidehiko Abe | de12922 | 2019-08-16 00:55:04 +0900 | [diff] [blame] | 49 | void OnFileCanReadWithoutBlocking(); |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 50 | |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 51 | // Attempts to establish a connection to ADB at well-known destinations. |
| 52 | std::unique_ptr<Socket> Connect() const; |
| 53 | |
Jason Jeremy Iman | 378930a | 2020-12-11 05:40:08 +0900 | [diff] [blame^] | 54 | // Start listening for ADB connection. Only listen when ARC guest is started |
| 55 | // and either Chrome OS is in developer mode or ADB sideloading is turned on. |
| 56 | void Listen(); |
| 57 | |
| 58 | // Checks ADB sideloading status and set it to |adb_sideloading_enabled_|. |
| 59 | // This function will call itself again if ADB sideloading status is not |
| 60 | // known yet. If ADB sideloading status is enabled and guest is started, |
| 61 | // start listening for connections. |
| 62 | void CheckAdbSideloadingStatus(int num_try); |
| 63 | |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 64 | MessageDispatcher msg_dispatcher_; |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 65 | std::unique_ptr<Socket> src_; |
| 66 | std::deque<std::unique_ptr<SocketForwarder>> fwd_; |
Hidehiko Abe | de12922 | 2019-08-16 00:55:04 +0900 | [diff] [blame] | 67 | std::unique_ptr<base::FileDescriptorWatcher::Controller> src_watcher_; |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 68 | |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 69 | GuestMessage::GuestType arc_type_; |
Garrick Evans | 1cce71a | 2019-06-21 10:43:14 +0900 | [diff] [blame] | 70 | uint32_t arcvm_vsock_cid_; |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 71 | |
Jason Jeremy Iman | 378930a | 2020-12-11 05:40:08 +0900 | [diff] [blame^] | 72 | bool dev_mode_enabled_; |
| 73 | bool adb_sideloading_enabled_; |
| 74 | |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 75 | base::WeakPtrFactory<AdbProxy> weak_factory_{this}; |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 76 | }; |
| 77 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 78 | } // namespace patchpanel |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 79 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 80 | #endif // PATCHPANEL_ADB_PROXY_H_ |