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> |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 17 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 18 | #include <shill/dbus/client/client.h> |
| 19 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 20 | #include "dns-proxy/resolver.h" |
| 21 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 22 | namespace dns_proxy { |
| 23 | |
| 24 | // The process that runs the actual proxying code. |
| 25 | class Proxy : public brillo::DBusDaemon { |
| 26 | public: |
| 27 | enum class Type { kSystem, kDefault, kARC }; |
| 28 | |
| 29 | struct Options { |
| 30 | Type type; |
| 31 | // Required for ARC proxies as it specifies which physical interface |
| 32 | // should (always) be tracked. This field is ignored (but should be empty) |
| 33 | // for the system and default network proxies. |
| 34 | std::string ifname; |
| 35 | }; |
| 36 | |
| 37 | explicit Proxy(const Options& opts); |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 38 | // For testing. |
| 39 | Proxy(const Options& opts, |
| 40 | std::unique_ptr<patchpanel::Client> patchpanel, |
| 41 | std::unique_ptr<shill::Client> shill); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 42 | Proxy(const Proxy&) = delete; |
| 43 | Proxy& operator=(const Proxy&) = delete; |
| 44 | ~Proxy() = default; |
| 45 | |
| 46 | static const char* TypeToString(Type t); |
| 47 | static std::optional<Type> StringToType(const std::string& s); |
| 48 | |
| 49 | protected: |
| 50 | int OnInit() override; |
| 51 | void OnShutdown(int*) override; |
| 52 | |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame^] | 53 | // Added for testing. |
| 54 | virtual std::unique_ptr<Resolver> NewResolver(); |
| 55 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 56 | private: |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 57 | static const uint8_t kMaxShillPropertyRetries = 10; |
| 58 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 59 | void Setup(); |
| 60 | void OnPatchpanelReady(bool success); |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 61 | void OnShillReset(bool reset); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 62 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 63 | // Triggered whenever the device attached to the default network changes. |
| 64 | // |device| can be null and indicates the default service is disconnected. |
| 65 | void OnDefaultDeviceChanged(const shill::Client::Device* const device); |
| 66 | void OnDeviceChanged(const shill::Client::Device* const device); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 67 | |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 68 | // Helper func for setting the dns-proxy address in shill. |
| 69 | // Only valid for the system proxy. |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 70 | // Will retry on failure up to |num_retries| before possibly crashing the |
| 71 | // proxy. |
| 72 | void SetShillProperty(const std::string& addr, |
| 73 | bool die_on_failure = false, |
| 74 | uint8_t num_retries = kMaxShillPropertyRetries); |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 75 | |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 76 | FRIEND_TEST(ProxyTest, SystemProxy_OnShutdownClearsAddressPropertyOnShill); |
| 77 | FRIEND_TEST(ProxyTest, NonSystemProxy_OnShutdownDoesNotCallShill); |
| 78 | FRIEND_TEST(ProxyTest, SystemProxy_SetShillPropertyWithNoRetriesCrashes); |
| 79 | FRIEND_TEST(ProxyTest, SystemProxy_SetShillPropertyDoesntCrashIfDieFalse); |
| 80 | FRIEND_TEST(ProxyTest, SetupInitializesShill); |
| 81 | FRIEND_TEST(ProxyTest, SystemProxy_ConnectedNamedspace); |
| 82 | FRIEND_TEST(ProxyTest, DefaultProxy_ConnectedNamedspace); |
| 83 | FRIEND_TEST(ProxyTest, ArcProxy_ConnectedNamedspace); |
| 84 | FRIEND_TEST(ProxyTest, CrashOnConnectNamespaceFailure); |
| 85 | FRIEND_TEST(ProxyTest, CrashOnPatchpanelNotReady); |
| 86 | FRIEND_TEST(ProxyTest, ShillResetRestoresAddressProperty); |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame^] | 87 | FRIEND_TEST(ProxyTest, StateClearedIfDefaultServiceDrops); |
| 88 | FRIEND_TEST(ProxyTest, ArcProxy_IgnoredIfDefaultServiceDrops); |
| 89 | FRIEND_TEST(ProxyTest, StateClearedIfDefaultServiceIsNotOnline); |
| 90 | FRIEND_TEST(ProxyTest, NewResolverStartsListeningOnDefaultServiceComesOnline); |
| 91 | FRIEND_TEST(ProxyTest, CrashOnListenFailure); |
| 92 | FRIEND_TEST(ProxyTest, NameServersUpdatedOnDefaultServiceComesOnline); |
| 93 | FRIEND_TEST(ProxyTest, |
| 94 | SystemProxy_ShillPropertyUpdatedOnDefaultServiceComesOnline); |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 95 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 96 | const Options opts_; |
| 97 | std::unique_ptr<patchpanel::Client> patchpanel_; |
| 98 | std::unique_ptr<shill::Client> shill_; |
| 99 | |
| 100 | base::ScopedFD ns_fd_; |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 101 | patchpanel::ConnectNamespaceResponse ns_; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 102 | std::unique_ptr<Resolver> resolver_; |
| 103 | std::unique_ptr<shill::Client::Device> device_; |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 104 | |
| 105 | base::WeakPtrFactory<Proxy> weak_factory_{this}; |
| 106 | }; |
| 107 | |
| 108 | std::ostream& operator<<(std::ostream& stream, Proxy::Type type); |
| 109 | std::ostream& operator<<(std::ostream& stream, Proxy::Options opt); |
| 110 | |
| 111 | } // namespace dns_proxy |
| 112 | |
| 113 | #endif // DNS_PROXY_PROXY_H_ |