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