Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_SHILL_CLIENT_H_ |
| 6 | #define PATCHPANEL_SHILL_CLIENT_H_ |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 7 | |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 8 | #include <map> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 9 | #include <memory> |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 10 | #include <ostream> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 11 | #include <set> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 12 | #include <string> |
Garrick Evans | c7fea0a | 2020-02-04 10:46:42 +0900 | [diff] [blame] | 13 | #include <vector> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 14 | |
| 15 | #include <base/macros.h> |
| 16 | #include <base/memory/weak_ptr.h> |
| 17 | #include <shill/dbus-proxies.h> |
| 18 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 19 | namespace patchpanel { |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 20 | |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 21 | // Listens for shill signals over dbus in order to: |
| 22 | // - Figure out which network interface (if any) is being used as the default |
| 23 | // service. |
| 24 | // - Invoke callbacks when the IPConfigs of a device has changed. |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 25 | class ShillClient { |
| 26 | public: |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 27 | // IPConfig for a device. If the device does not have a valid ipv4/ipv6 |
| 28 | // config, the corresponding fields will be empty or 0. |
| 29 | // TODO(jiejiang): add the following fields into this struct: |
| 30 | // - IPv4 search domains |
| 31 | // - IPv6 search domains |
| 32 | // - MTU (one only per network) |
| 33 | struct IPConfig { |
| 34 | int ipv4_prefix_length; |
| 35 | std::string ipv4_address; |
| 36 | std::string ipv4_gateway; |
| 37 | std::vector<std::string> ipv4_dns_addresses; |
| 38 | |
| 39 | int ipv6_prefix_length; |
| 40 | // Note due to the limitation of shill, we will only get one IPv6 address |
| 41 | // from it. This address should be the privacy address for device with type |
| 42 | // of ethernet or wifi. |
| 43 | std::string ipv6_address; |
| 44 | std::string ipv6_gateway; |
| 45 | std::vector<std::string> ipv6_dns_addresses; |
| 46 | }; |
| 47 | |
Jie Jiang | 01c1a2e | 2020-04-08 20:58:30 +0900 | [diff] [blame] | 48 | // Represents the properties of an object of org.chromium.flimflam.Device. |
| 49 | // Only contains the properties we care about. |
| 50 | // TODO(jiejiang): add the following fields into this struct: |
| 51 | // - the DBus path of the Service associated to this Device if any |
| 52 | // - the connection state of the Service, if possible by translating back to |
| 53 | // the enum shill::Service::ConnectState |
| 54 | struct Device { |
Jie Jiang | 48c99ce | 2020-06-08 15:18:23 +0900 | [diff] [blame] | 55 | // A subset of shill::Technology::Type. |
| 56 | enum class Type { |
| 57 | kUnknown, |
| 58 | kCellular, |
| 59 | kEthernet, |
| 60 | kEthernetEap, |
| 61 | kGuestInterface, |
| 62 | kLoopback, |
| 63 | kPPP, |
| 64 | kPPPoE, |
| 65 | kTunnel, |
| 66 | kVPN, |
| 67 | kWifi, |
| 68 | }; |
| 69 | |
| 70 | Type type; |
Jie Jiang | 01c1a2e | 2020-04-08 20:58:30 +0900 | [diff] [blame] | 71 | std::string ifname; |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 72 | std::string service_path; |
Jie Jiang | 01c1a2e | 2020-04-08 20:58:30 +0900 | [diff] [blame] | 73 | IPConfig ipconfig; |
| 74 | }; |
| 75 | |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 76 | using DefaultDeviceChangeHandler = |
| 77 | base::Callback<void(const Device& new_device, const Device& prev_device)>; |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 78 | using DevicesChangeHandler = |
| 79 | base::Callback<void(const std::set<std::string>& added, |
| 80 | const std::set<std::string>& removed)>; |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 81 | using IPConfigsChangeHandler = |
| 82 | base::Callback<void(const std::string& device, const IPConfig& ipconfig)>; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 83 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 84 | explicit ShillClient(const scoped_refptr<dbus::Bus>& bus); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 85 | ShillClient(const ShillClient&) = delete; |
| 86 | ShillClient& operator=(const ShillClient&) = delete; |
| 87 | |
Ben Chan | 4f38650 | 2019-09-20 16:17:59 -0700 | [diff] [blame] | 88 | virtual ~ShillClient() = default; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 89 | |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 90 | // Registers the provided handler for changes in shill default network |
| 91 | // services. The handler will be called once immediately at registration |
| 92 | // with the current default network as |new_device| and an empty Device as |
| 93 | // |prev_device|. |
| 94 | void RegisterDefaultDeviceChangedHandler( |
| 95 | const DefaultDeviceChangeHandler& handler); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 96 | |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 97 | void RegisterDevicesChangedHandler(const DevicesChangeHandler& handler); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 98 | |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 99 | void RegisterIPConfigsChangedHandler(const IPConfigsChangeHandler& handler); |
| 100 | |
Jie Jiang | 84c76a1 | 2020-04-17 16:45:20 +0900 | [diff] [blame] | 101 | void ScanDevices(); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 102 | |
Jie Jiang | 01c1a2e | 2020-04-08 20:58:30 +0900 | [diff] [blame] | 103 | // Fetches device properties via dbus. Returns false if an error occurs. Notes |
| 104 | // that this method will block the current thread. |
| 105 | virtual bool GetDeviceProperties(const std::string& device, Device* output); |
| 106 | |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 107 | // Returns the cached interface name; does not initiate a property fetch. |
| 108 | virtual const std::string& default_interface() const; |
Hugo Benichi | cc6850f | 2020-01-17 13:26:06 +0900 | [diff] [blame] | 109 | // Returns interface names of all known shill Devices. |
| 110 | const std::set<std::string> get_devices() const; |
| 111 | // Returns true if |ifname| is a known shill Device. |
| 112 | bool has_device(const std::string& ifname) const; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 113 | |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 114 | protected: |
| 115 | void OnManagerPropertyChangeRegistration(const std::string& interface, |
| 116 | const std::string& signal_name, |
| 117 | bool success); |
| 118 | void OnManagerPropertyChange(const std::string& property_name, |
| 119 | const brillo::Any& property_value); |
| 120 | |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 121 | void OnDevicePropertyChangeRegistration(const std::string& interface, |
| 122 | const std::string& signal_name, |
| 123 | bool success); |
| 124 | void OnDevicePropertyChange(const std::string& device, |
| 125 | const std::string& property_name, |
| 126 | const brillo::Any& property_value); |
| 127 | |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 128 | // Returns the default interface for the system, or an empty Device result |
| 129 | // when the system has no default interface. |
| 130 | virtual Device GetDefaultDevice(); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 131 | |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 132 | private: |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 133 | void UpdateDevices(const brillo::Any& property_value); |
| 134 | |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 135 | // Sets the internal variable tracking the system default interface and calls |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 136 | // the default interface handler if the default interface changed. |
| 137 | void SetDefaultDevice(const Device& new_default); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 138 | |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 139 | // Parses the |property_value| as the IPConfigs property of |device|, which |
| 140 | // should be a list of object paths of IPConfigs. |
| 141 | IPConfig ParseIPConfigsProperty(const std::string& device, |
| 142 | const brillo::Any& property_value); |
| 143 | |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 144 | // Tracks the system default logical network chosen by shill. This corresponds |
| 145 | // to the physical or virtual device associated with the default logical |
| 146 | // network service. |
| 147 | Device default_device_; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 148 | // Tracks all network interfaces managed by shill. |
| 149 | std::set<std::string> devices_; |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 150 | // Stores the map from interface to its object path in shill for all the |
| 151 | // devices we have seen. Unlike |devices_|, entries in this map will never |
| 152 | // be removed during the lifetime of this class. We maintain this map mainly |
| 153 | // for keeping track of the device object proxies we have created, to avoid |
| 154 | // registering the handler on the same object twice. |
| 155 | std::map<std::string, dbus::ObjectPath> known_device_paths_; |
| 156 | |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 157 | // Called when the interface used as the default interface changes. |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 158 | std::vector<DefaultDeviceChangeHandler> default_device_handlers_; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 159 | // Called when the list of network interfaces managed by shill changes. |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 160 | std::vector<DevicesChangeHandler> device_handlers_; |
Jie Jiang | 850a471 | 2020-04-08 21:06:36 +0900 | [diff] [blame] | 161 | // Called when the IPConfigs of any device changes. |
| 162 | std::vector<IPConfigsChangeHandler> ipconfigs_handlers_; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 163 | |
| 164 | scoped_refptr<dbus::Bus> bus_; |
| 165 | std::unique_ptr<org::chromium::flimflam::ManagerProxy> manager_proxy_; |
| 166 | |
| 167 | base::WeakPtrFactory<ShillClient> weak_factory_{this}; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Hugo Benichi | 78148a0 | 2020-10-30 18:37:00 +0900 | [diff] [blame^] | 170 | std::ostream& operator<<(std::ostream& stream, const ShillClient::Device& dev); |
| 171 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 172 | } // namespace patchpanel |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 173 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 174 | #endif // PATCHPANEL_SHILL_CLIENT_H_ |