blob: 1bdeb022e7eeb5b921b94a244a7951ede2fb3608 [file] [log] [blame]
Garrick Evans3cbac7c2019-04-18 15:31:31 +09001// 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
Garrick Evans96e03042019-05-28 14:30:52 +090011#include <base/files/scoped_file.h>
Garrick Evans3cbac7c2019-04-18 15:31:31 +090012#include <base/memory/weak_ptr.h>
13#include <brillo/daemons/daemon.h>
14#include <base/message_loop/message_loop.h>
15
Garrick Evans96e03042019-05-28 14:30:52 +090016#include "arc/network/message_dispatcher.h"
Garrick Evans3cbac7c2019-04-18 15:31:31 +090017#include "arc/network/socket.h"
18#include "arc/network/socket_forwarder.h"
19
20namespace arc_networkd {
21
22// Subprocess for proxying ADB traffic.
23class AdbProxy : public brillo::Daemon, public base::MessageLoopForIO::Watcher {
24 public:
Garrick Evans96e03042019-05-28 14:30:52 +090025 explicit AdbProxy(base::ScopedFD control_fd);
Garrick Evans3cbac7c2019-04-18 15:31:31 +090026 virtual ~AdbProxy();
27
28 protected:
Garrick Evans3cbac7c2019-04-18 15:31:31 +090029 int OnInit() override;
30
31 // Watcher callback for accepting new connections.
32 void OnFileCanReadWithoutBlocking(int fd) override;
33 void OnFileCanWriteWithoutBlocking(int fd) override {}
34
Garrick Evans96e03042019-05-28 14:30:52 +090035 void OnParentProcessExit();
36 void OnGuestMessage(const GuestMessage& msg);
Garrick Evans3cbac7c2019-04-18 15:31:31 +090037
Garrick Evans96e03042019-05-28 14:30:52 +090038 private:
Garrick Evans6f258d02019-06-28 16:32:07 +090039 // Detects when the guest OS is turned up and torn down and cleans up any
40 // existing forwarders and connections.
41 bool OnSignal(const struct signalfd_siginfo& info);
42
Garrick Evansbdf1f982019-06-07 09:46:49 +090043 void Reset();
44
Garrick Evans3cbac7c2019-04-18 15:31:31 +090045 // Attempts to establish a connection to ADB at well-known destinations.
46 std::unique_ptr<Socket> Connect() const;
47
Garrick Evans96e03042019-05-28 14:30:52 +090048 MessageDispatcher msg_dispatcher_;
Garrick Evans3cbac7c2019-04-18 15:31:31 +090049 std::unique_ptr<Socket> src_;
50 std::deque<std::unique_ptr<SocketForwarder>> fwd_;
51 base::MessageLoopForIO::FileDescriptorWatcher src_watcher_;
52
Garrick Evansbdf1f982019-06-07 09:46:49 +090053 GuestMessage::GuestType arc_type_;
54
Garrick Evans3cbac7c2019-04-18 15:31:31 +090055 base::WeakPtrFactory<AdbProxy> weak_factory_{this};
56 DISALLOW_COPY_AND_ASSIGN(AdbProxy);
57};
58
59} // namespace arc_networkd
60
61#endif // ARC_NETWORK_ADB_PROXY_H_