blob: 95ea04d37137513511346da61a51fc2815848887 [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>
Garrick Evans5fe2a4f2021-02-03 17:04:48 +090017#include <gtest/gtest_prod.h> // for FRIEND_TEST
Garrick Evans066dc2c2020-12-10 10:43:55 +090018#include <shill/dbus/client/client.h>
19
Garrick Evans34650b32021-02-03 09:24:35 +090020#include "dns-proxy/resolver.h"
21
Garrick Evans066dc2c2020-12-10 10:43:55 +090022namespace dns_proxy {
23
24// The process that runs the actual proxying code.
25class 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 Evans5fe2a4f2021-02-03 17:04:48 +090038 // For testing.
39 Proxy(const Options& opts,
40 std::unique_ptr<patchpanel::Client> patchpanel,
41 std::unique_ptr<shill::Client> shill);
Garrick Evans066dc2c2020-12-10 10:43:55 +090042 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
53 private:
Garrick Evans9c7afb82021-01-29 22:38:03 +090054 static const uint8_t kMaxShillPropertyRetries = 10;
55
Garrick Evans066dc2c2020-12-10 10:43:55 +090056 void Setup();
57 void OnPatchpanelReady(bool success);
Garrick Evans9c7afb82021-01-29 22:38:03 +090058 void OnShillReset(bool reset);
Garrick Evans066dc2c2020-12-10 10:43:55 +090059
Garrick Evans34650b32021-02-03 09:24:35 +090060 // Triggered whenever the device attached to the default network changes.
61 // |device| can be null and indicates the default service is disconnected.
62 void OnDefaultDeviceChanged(const shill::Client::Device* const device);
63 void OnDeviceChanged(const shill::Client::Device* const device);
Garrick Evans066dc2c2020-12-10 10:43:55 +090064
Garrick Evans48c84ef2021-01-28 11:29:42 +090065 // Helper func for setting the dns-proxy address in shill.
66 // Only valid for the system proxy.
Garrick Evans9c7afb82021-01-29 22:38:03 +090067 // Will retry on failure up to |num_retries| before possibly crashing the
68 // proxy.
69 void SetShillProperty(const std::string& addr,
70 bool die_on_failure = false,
71 uint8_t num_retries = kMaxShillPropertyRetries);
Garrick Evans48c84ef2021-01-28 11:29:42 +090072
Garrick Evans5fe2a4f2021-02-03 17:04:48 +090073 FRIEND_TEST(ProxyTest, SystemProxy_OnShutdownClearsAddressPropertyOnShill);
74 FRIEND_TEST(ProxyTest, NonSystemProxy_OnShutdownDoesNotCallShill);
75 FRIEND_TEST(ProxyTest, SystemProxy_SetShillPropertyWithNoRetriesCrashes);
76 FRIEND_TEST(ProxyTest, SystemProxy_SetShillPropertyDoesntCrashIfDieFalse);
77 FRIEND_TEST(ProxyTest, SetupInitializesShill);
78 FRIEND_TEST(ProxyTest, SystemProxy_ConnectedNamedspace);
79 FRIEND_TEST(ProxyTest, DefaultProxy_ConnectedNamedspace);
80 FRIEND_TEST(ProxyTest, ArcProxy_ConnectedNamedspace);
81 FRIEND_TEST(ProxyTest, CrashOnConnectNamespaceFailure);
82 FRIEND_TEST(ProxyTest, CrashOnPatchpanelNotReady);
83 FRIEND_TEST(ProxyTest, ShillResetRestoresAddressProperty);
84
Garrick Evans066dc2c2020-12-10 10:43:55 +090085 const Options opts_;
86 std::unique_ptr<patchpanel::Client> patchpanel_;
87 std::unique_ptr<shill::Client> shill_;
88
89 base::ScopedFD ns_fd_;
Garrick Evans9c7afb82021-01-29 22:38:03 +090090 patchpanel::ConnectNamespaceResponse ns_;
Garrick Evans34650b32021-02-03 09:24:35 +090091 std::unique_ptr<Resolver> resolver_;
92 std::unique_ptr<shill::Client::Device> device_;
Garrick Evans066dc2c2020-12-10 10:43:55 +090093
94 base::WeakPtrFactory<Proxy> weak_factory_{this};
95};
96
97std::ostream& operator<<(std::ostream& stream, Proxy::Type type);
98std::ostream& operator<<(std::ostream& stream, Proxy::Options opt);
99
100} // namespace dns_proxy
101
102#endif // DNS_PROXY_PROXY_H_