Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 1 | // 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 | |
| 19 | namespace dns_proxy { |
| 20 | |
| 21 | // The process that runs the actual proxying code. |
| 22 | class 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 Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame^] | 47 | static const uint8_t kMaxShillPropertyRetries = 10; |
| 48 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 49 | void Setup(); |
| 50 | void OnPatchpanelReady(bool success); |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame^] | 51 | void OnShillReset(bool reset); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 52 | |
| 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 Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 63 | // Helper func for setting the dns-proxy address in shill. |
| 64 | // Only valid for the system proxy. |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame^] | 65 | // 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 Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 70 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 71 | const Options opts_; |
| 72 | std::unique_ptr<patchpanel::Client> patchpanel_; |
| 73 | std::unique_ptr<shill::Client> shill_; |
| 74 | |
| 75 | base::ScopedFD ns_fd_; |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame^] | 76 | patchpanel::ConnectNamespaceResponse ns_; |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 77 | |
| 78 | base::WeakPtrFactory<Proxy> weak_factory_{this}; |
| 79 | }; |
| 80 | |
| 81 | std::ostream& operator<<(std::ostream& stream, Proxy::Type type); |
| 82 | std::ostream& operator<<(std::ostream& stream, Proxy::Options opt); |
| 83 | |
| 84 | } // namespace dns_proxy |
| 85 | |
| 86 | #endif // DNS_PROXY_PROXY_H_ |