blob: b9d2a38cfa212d91c545c7f3b4b1bc995f8c64c7 [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
Garrick Evans48c84ef2021-01-28 11:29:42 +090060 // Helper func for setting the dns-proxy address in shill.
61 // Only valid for the system proxy.
62 bool SetShillProperty(const std::string& addr);
63
Garrick Evans066dc2c2020-12-10 10:43:55 +090064 const Options opts_;
65 std::unique_ptr<patchpanel::Client> patchpanel_;
66 std::unique_ptr<shill::Client> shill_;
67
68 base::ScopedFD ns_fd_;
69
70 base::WeakPtrFactory<Proxy> weak_factory_{this};
71};
72
73std::ostream& operator<<(std::ostream& stream, Proxy::Type type);
74std::ostream& operator<<(std::ostream& stream, Proxy::Options opt);
75
76} // namespace dns_proxy
77
78#endif // DNS_PROXY_PROXY_H_