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 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 19 | #include "dns-proxy/resolver.h" |
| 20 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 21 | namespace dns_proxy { |
| 22 | |
| 23 | // The process that runs the actual proxying code. |
| 24 | class Proxy : public brillo::DBusDaemon { |
| 25 | public: |
| 26 | enum class Type { kSystem, kDefault, kARC }; |
| 27 | |
| 28 | struct Options { |
| 29 | Type type; |
| 30 | // Required for ARC proxies as it specifies which physical interface |
| 31 | // should (always) be tracked. This field is ignored (but should be empty) |
| 32 | // for the system and default network proxies. |
| 33 | std::string ifname; |
| 34 | }; |
| 35 | |
| 36 | explicit Proxy(const Options& opts); |
| 37 | Proxy(const Proxy&) = delete; |
| 38 | Proxy& operator=(const Proxy&) = delete; |
| 39 | ~Proxy() = default; |
| 40 | |
| 41 | static const char* TypeToString(Type t); |
| 42 | static std::optional<Type> StringToType(const std::string& s); |
| 43 | |
| 44 | protected: |
| 45 | int OnInit() override; |
| 46 | void OnShutdown(int*) override; |
| 47 | |
| 48 | private: |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 49 | static const uint8_t kMaxShillPropertyRetries = 10; |
| 50 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 51 | void Setup(); |
| 52 | void OnPatchpanelReady(bool success); |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 53 | void OnShillReset(bool reset); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 54 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 55 | // Triggered whenever the device attached to the default network changes. |
| 56 | // |device| can be null and indicates the default service is disconnected. |
| 57 | void OnDefaultDeviceChanged(const shill::Client::Device* const device); |
| 58 | void OnDeviceChanged(const shill::Client::Device* const device); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 59 | |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 60 | // Helper func for setting the dns-proxy address in shill. |
| 61 | // Only valid for the system proxy. |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 62 | // Will retry on failure up to |num_retries| before possibly crashing the |
| 63 | // proxy. |
| 64 | void SetShillProperty(const std::string& addr, |
| 65 | bool die_on_failure = false, |
| 66 | uint8_t num_retries = kMaxShillPropertyRetries); |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 67 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 68 | const Options opts_; |
| 69 | std::unique_ptr<patchpanel::Client> patchpanel_; |
| 70 | std::unique_ptr<shill::Client> shill_; |
| 71 | |
| 72 | base::ScopedFD ns_fd_; |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 73 | patchpanel::ConnectNamespaceResponse ns_; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 74 | std::unique_ptr<Resolver> resolver_; |
| 75 | std::unique_ptr<shill::Client::Device> device_; |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 76 | |
| 77 | base::WeakPtrFactory<Proxy> weak_factory_{this}; |
| 78 | }; |
| 79 | |
| 80 | std::ostream& operator<<(std::ostream& stream, Proxy::Type type); |
| 81 | std::ostream& operator<<(std::ostream& stream, Proxy::Options opt); |
| 82 | |
| 83 | } // namespace dns_proxy |
| 84 | |
| 85 | #endif // DNS_PROXY_PROXY_H_ |