blob: 1271124a557015c43c8d656fb5e4f04ac7780d67 [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
Garrick Evans34650b32021-02-03 09:24:35 +090019#include "dns-proxy/resolver.h"
20
Garrick Evans066dc2c2020-12-10 10:43:55 +090021namespace dns_proxy {
22
23// The process that runs the actual proxying code.
24class 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 Evans9c7afb82021-01-29 22:38:03 +090049 static const uint8_t kMaxShillPropertyRetries = 10;
50
Garrick Evans066dc2c2020-12-10 10:43:55 +090051 void Setup();
52 void OnPatchpanelReady(bool success);
Garrick Evans9c7afb82021-01-29 22:38:03 +090053 void OnShillReset(bool reset);
Garrick Evans066dc2c2020-12-10 10:43:55 +090054
Garrick Evans34650b32021-02-03 09:24:35 +090055 // 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 Evans066dc2c2020-12-10 10:43:55 +090059
Garrick Evans48c84ef2021-01-28 11:29:42 +090060 // Helper func for setting the dns-proxy address in shill.
61 // Only valid for the system proxy.
Garrick Evans9c7afb82021-01-29 22:38:03 +090062 // 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 Evans48c84ef2021-01-28 11:29:42 +090067
Garrick Evans066dc2c2020-12-10 10:43:55 +090068 const Options opts_;
69 std::unique_ptr<patchpanel::Client> patchpanel_;
70 std::unique_ptr<shill::Client> shill_;
71
72 base::ScopedFD ns_fd_;
Garrick Evans9c7afb82021-01-29 22:38:03 +090073 patchpanel::ConnectNamespaceResponse ns_;
Garrick Evans34650b32021-02-03 09:24:35 +090074 std::unique_ptr<Resolver> resolver_;
75 std::unique_ptr<shill::Client::Device> device_;
Garrick Evans066dc2c2020-12-10 10:43:55 +090076
77 base::WeakPtrFactory<Proxy> weak_factory_{this};
78};
79
80std::ostream& operator<<(std::ostream& stream, Proxy::Type type);
81std::ostream& operator<<(std::ostream& stream, Proxy::Options opt);
82
83} // namespace dns_proxy
84
85#endif // DNS_PROXY_PROXY_H_