blob: 921a7a2a86baeac6f881f4d839f27983fb930579 [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:
Garrick Evans9c7afb82021-01-29 22:38:03 +090047 static const uint8_t kMaxShillPropertyRetries = 10;
48
Garrick Evans066dc2c2020-12-10 10:43:55 +090049 void Setup();
50 void OnPatchpanelReady(bool success);
Garrick Evans9c7afb82021-01-29 22:38:03 +090051 void OnShillReset(bool reset);
Garrick Evans066dc2c2020-12-10 10:43:55 +090052
53 // The system proxy needs to pay attention to the default service type
54 // switching to VPN since it wants to always keep the DNS configuration for
55 // the underlying physical network.
56 void OnDefaultServiceChanged(const std::string& type);
57
58 // Used to detect changes to the DNS configuration of interface(s) of interest
59 // to the proxy.
60 void OnDeviceChanged(bool is_default,
61 const shill::Client::Device* const device);
62
Garrick Evans48c84ef2021-01-28 11:29:42 +090063 // Helper func for setting the dns-proxy address in shill.
64 // Only valid for the system proxy.
Garrick Evans9c7afb82021-01-29 22:38:03 +090065 // Will retry on failure up to |num_retries| before possibly crashing the
66 // proxy.
67 void SetShillProperty(const std::string& addr,
68 bool die_on_failure = false,
69 uint8_t num_retries = kMaxShillPropertyRetries);
Garrick Evans48c84ef2021-01-28 11:29:42 +090070
Garrick Evans066dc2c2020-12-10 10:43:55 +090071 const Options opts_;
72 std::unique_ptr<patchpanel::Client> patchpanel_;
73 std::unique_ptr<shill::Client> shill_;
74
75 base::ScopedFD ns_fd_;
Garrick Evans9c7afb82021-01-29 22:38:03 +090076 patchpanel::ConnectNamespaceResponse ns_;
Garrick Evans066dc2c2020-12-10 10:43:55 +090077
78 base::WeakPtrFactory<Proxy> weak_factory_{this};
79};
80
81std::ostream& operator<<(std::ostream& stream, Proxy::Type type);
82std::ostream& operator<<(std::ostream& stream, Proxy::Options opt);
83
84} // namespace dns_proxy
85
86#endif // DNS_PROXY_PROXY_H_