blob: a381e5f96e4ebfe9d88f8d56242e69f73c9f665e [file] [log] [blame]
Garrick Evans066dc2c2020-12-10 10:43:55 +09001// Copyright 2021 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 DNS_PROXY_PROXY_H_
6#define DNS_PROXY_PROXY_H_
7
8#include <iostream>
9#include <memory>
10#include <optional>
11#include <string>
12
13#include <base/memory/weak_ptr.h>
14#include <base/files/scoped_file.h>
15#include <brillo/daemons/dbus_daemon.h>
16#include <chromeos/patchpanel/dbus/client.h>
17#include <shill/dbus/client/client.h>
18
19namespace dns_proxy {
20
21// The process that runs the actual proxying code.
22class Proxy : public brillo::DBusDaemon {
23 public:
24 enum class Type { kSystem, kDefault, kARC };
25
26 struct Options {
27 Type type;
28 // Required for ARC proxies as it specifies which physical interface
29 // should (always) be tracked. This field is ignored (but should be empty)
30 // for the system and default network proxies.
31 std::string ifname;
32 };
33
34 explicit Proxy(const Options& opts);
35 Proxy(const Proxy&) = delete;
36 Proxy& operator=(const Proxy&) = delete;
37 ~Proxy() = default;
38
39 static const char* TypeToString(Type t);
40 static std::optional<Type> StringToType(const std::string& s);
41
42 protected:
43 int OnInit() override;
44 void OnShutdown(int*) override;
45
46 private:
47 void Setup();
48 void OnPatchpanelReady(bool success);
49
50 // The system proxy needs to pay attention to the default service type
51 // switching to VPN since it wants to always keep the DNS configuration for
52 // the underlying physical network.
53 void OnDefaultServiceChanged(const std::string& type);
54
55 // Used to detect changes to the DNS configuration of interface(s) of interest
56 // to the proxy.
57 void OnDeviceChanged(bool is_default,
58 const shill::Client::Device* const device);
59
60 const Options opts_;
61 std::unique_ptr<patchpanel::Client> patchpanel_;
62 std::unique_ptr<shill::Client> shill_;
63
64 base::ScopedFD ns_fd_;
65
66 base::WeakPtrFactory<Proxy> weak_factory_{this};
67};
68
69std::ostream& operator<<(std::ostream& stream, Proxy::Type type);
70std::ostream& operator<<(std::ostream& stream, Proxy::Options opt);
71
72} // namespace dns_proxy
73
74#endif // DNS_PROXY_PROXY_H_