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 | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 22 | // ADB gets confused if we listen on 5555 and thinks there is an emulator |
| 23 | // running, which in turn ends up confusing our integration test libraries |
| 24 | // because multiple devices show up. |
| 25 | constexpr uint16_t kAdbProxyTcpListenPort = 5550; |
| 26 | |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 27 | // Subprocess for proxying ADB traffic. |
Hidehiko Abe | de12922 | 2019-08-16 00:55:04 +0900 | [diff] [blame] | 28 | class AdbProxy : public brillo::Daemon { |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 29 | public: |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 30 | explicit AdbProxy(base::ScopedFD control_fd); |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 31 | virtual ~AdbProxy(); |
| 32 | |
| 33 | protected: |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 34 | int OnInit() override; |
| 35 | |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 36 | void OnParentProcessExit(); |
| 37 | void OnGuestMessage(const GuestMessage& msg); |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 38 | |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 39 | private: |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 40 | void Reset(); |
Hidehiko Abe | de12922 | 2019-08-16 00:55:04 +0900 | [diff] [blame] | 41 | void OnFileCanReadWithoutBlocking(); |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 42 | |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 43 | // Attempts to establish a connection to ADB at well-known destinations. |
| 44 | std::unique_ptr<Socket> Connect() const; |
| 45 | |
Garrick Evans | 96e0304 | 2019-05-28 14:30:52 +0900 | [diff] [blame] | 46 | MessageDispatcher msg_dispatcher_; |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 47 | std::unique_ptr<Socket> src_; |
| 48 | std::deque<std::unique_ptr<SocketForwarder>> fwd_; |
Hidehiko Abe | de12922 | 2019-08-16 00:55:04 +0900 | [diff] [blame] | 49 | std::unique_ptr<base::FileDescriptorWatcher::Controller> src_watcher_; |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 50 | |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 51 | GuestMessage::GuestType arc_type_; |
Garrick Evans | 1cce71a | 2019-06-21 10:43:14 +0900 | [diff] [blame] | 52 | uint32_t arcvm_vsock_cid_; |
Garrick Evans | bdf1f98 | 2019-06-07 09:46:49 +0900 | [diff] [blame] | 53 | |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 54 | base::WeakPtrFactory<AdbProxy> weak_factory_{this}; |
| 55 | DISALLOW_COPY_AND_ASSIGN(AdbProxy); |
| 56 | }; |
| 57 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 58 | } // namespace patchpanel |
Garrick Evans | 3cbac7c | 2019-04-18 15:31:31 +0900 | [diff] [blame] | 59 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 60 | #endif // PATCHPANEL_ADB_PROXY_H_ |