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 | #include "patchpanel/shill_client.h" |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 6 | |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 9 | #include <base/bind.h> |
| 10 | #include <base/logging.h> |
Garrick Evans | e87bb4f | 2020-02-18 10:27:37 +0900 | [diff] [blame] | 11 | #include <base/strings/string_util.h> |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
| 13 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 14 | namespace patchpanel { |
Hugo Benichi | d17aa59 | 2019-04-26 15:15:01 +0900 | [diff] [blame] | 15 | |
Garrick Evans | 0884393 | 2019-09-17 14:41:08 +0900 | [diff] [blame] | 16 | ShillClient::ShillClient(const scoped_refptr<dbus::Bus>& bus) : bus_(bus) { |
Hidehiko Abe | 3a7e513 | 2018-02-15 13:07:50 +0900 | [diff] [blame] | 17 | manager_proxy_.reset(new org::chromium::flimflam::ManagerProxy(bus_)); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 18 | manager_proxy_->RegisterPropertyChangedSignalHandler( |
| 19 | base::Bind(&ShillClient::OnManagerPropertyChange, |
| 20 | weak_factory_.GetWeakPtr()), |
| 21 | base::Bind(&ShillClient::OnManagerPropertyChangeRegistration, |
| 22 | weak_factory_.GetWeakPtr())); |
| 23 | } |
| 24 | |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 25 | const std::string& ShillClient::default_interface() const { |
| 26 | return default_interface_; |
| 27 | } |
| 28 | |
Hugo Benichi | cc6850f | 2020-01-17 13:26:06 +0900 | [diff] [blame] | 29 | const std::set<std::string> ShillClient::get_devices() const { |
| 30 | return devices_; |
| 31 | } |
| 32 | |
| 33 | bool ShillClient::has_device(const std::string& ifname) const { |
| 34 | return devices_.find(ifname) != devices_.end(); |
| 35 | } |
| 36 | |
Jie Jiang | 84c76a1 | 2020-04-17 16:45:20 +0900 | [diff] [blame] | 37 | void ShillClient::ScanDevices() { |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 38 | brillo::VariantDictionary props; |
| 39 | if (!manager_proxy_->GetProperties(&props, nullptr)) { |
| 40 | LOG(ERROR) << "Unable to get manager properties"; |
| 41 | return; |
| 42 | } |
| 43 | const auto it = props.find(shill::kDevicesProperty); |
| 44 | if (it == props.end()) { |
| 45 | LOG(WARNING) << "Manager properties is missing devices"; |
| 46 | return; |
| 47 | } |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 48 | UpdateDevices(it->second); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 49 | } |
| 50 | |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 51 | std::string ShillClient::GetDefaultInterface() { |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 52 | brillo::VariantDictionary manager_props; |
| 53 | |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 54 | if (!manager_proxy_->GetProperties(&manager_props, nullptr)) { |
| 55 | LOG(ERROR) << "Unable to get manager properties"; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 56 | return ""; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | auto it = manager_props.find(shill::kDefaultServiceProperty); |
| 60 | if (it == manager_props.end()) { |
| 61 | LOG(WARNING) << "Manager properties is missing default service"; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 62 | return ""; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | dbus::ObjectPath service_path = it->second.TryGet<dbus::ObjectPath>(); |
| 66 | if (!service_path.IsValid() || service_path.value() == "/") { |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 67 | return ""; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | org::chromium::flimflam::ServiceProxy service_proxy(bus_, service_path); |
| 71 | brillo::VariantDictionary service_props; |
| 72 | if (!service_proxy.GetProperties(&service_props, nullptr)) { |
| 73 | LOG(ERROR) << "Can't retrieve properties for service"; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 74 | return ""; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Alex Khouderchah | 05a8b5b | 2019-11-20 12:18:51 -0800 | [diff] [blame] | 77 | it = service_props.find(shill::kIsConnectedProperty); |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 78 | if (it == service_props.end()) { |
Alex Khouderchah | 05a8b5b | 2019-11-20 12:18:51 -0800 | [diff] [blame] | 79 | LOG(WARNING) << "Service properties is missing \"IsConnected\""; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 80 | return ""; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 81 | } |
Alex Khouderchah | 05a8b5b | 2019-11-20 12:18:51 -0800 | [diff] [blame] | 82 | if (!it->second.TryGet<bool>()) { |
| 83 | LOG(INFO) << "Ignoring non-connected service"; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 84 | return ""; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | it = service_props.find(shill::kDeviceProperty); |
| 88 | if (it == service_props.end()) { |
| 89 | LOG(WARNING) << "Service properties is missing device path"; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 90 | return ""; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | dbus::ObjectPath device_path = it->second.TryGet<dbus::ObjectPath>(); |
| 94 | if (!device_path.IsValid()) { |
| 95 | LOG(WARNING) << "Invalid device path"; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 96 | return ""; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | org::chromium::flimflam::DeviceProxy device_proxy(bus_, device_path); |
| 100 | brillo::VariantDictionary device_props; |
| 101 | if (!device_proxy.GetProperties(&device_props, nullptr)) { |
| 102 | LOG(ERROR) << "Can't retrieve properties for device"; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 103 | return ""; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | it = device_props.find(shill::kInterfaceProperty); |
| 107 | if (it == device_props.end()) { |
| 108 | LOG(WARNING) << "Device properties is missing interface name"; |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 109 | return ""; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | std::string interface = it->second.TryGet<std::string>(); |
| 113 | if (interface.empty()) { |
| 114 | LOG(WARNING) << "Device interface name is empty"; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 117 | return interface; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void ShillClient::OnManagerPropertyChangeRegistration( |
| 121 | const std::string& interface, |
| 122 | const std::string& signal_name, |
| 123 | bool success) { |
| 124 | if (!success) |
| 125 | LOG(FATAL) << "Unable to register for interface change events"; |
| 126 | } |
| 127 | |
| 128 | void ShillClient::OnManagerPropertyChange(const std::string& property_name, |
| 129 | const brillo::Any& property_value) { |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 130 | if (property_name == shill::kDevicesProperty) { |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 131 | UpdateDevices(property_value); |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 132 | |
| 133 | // Choose a fallback interface when any network device exist. Update the |
| 134 | // fallback interface if it that device does not exist anymore. |
Garrick Evans | d474283 | 2019-05-20 11:51:40 +0900 | [diff] [blame] | 135 | if (!devices_.empty() && |
| 136 | devices_.find(fallback_default_interface_) == devices_.end()) { |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 137 | fallback_default_interface_ = *devices_.begin(); |
| 138 | // When the system appears to have no default interface, use the fallback |
| 139 | // interface instead. |
Garrick Evans | d474283 | 2019-05-20 11:51:40 +0900 | [diff] [blame] | 140 | if (default_interface_.empty() || |
| 141 | default_interface_ != fallback_default_interface_) |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 142 | SetDefaultInterface(fallback_default_interface_); |
| 143 | } |
| 144 | |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 145 | // Remove the fallback interface when no network device is managed by shill. |
| 146 | if (!fallback_default_interface_.empty() && devices_.empty()) { |
| 147 | fallback_default_interface_ = ""; |
| 148 | SetDefaultInterface(""); |
| 149 | } |
| 150 | |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 154 | if (property_name != shill::kDefaultServiceProperty && |
| 155 | property_name != shill::kConnectionStateProperty) |
| 156 | return; |
| 157 | |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 158 | SetDefaultInterface(GetDefaultInterface()); |
| 159 | } |
| 160 | |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 161 | std::string ShillClient::SetDefaultInterface(std::string new_default) { |
Hugo Benichi | ddee281 | 2019-05-10 16:03:43 +0900 | [diff] [blame] | 162 | // When the system default is lost, use the fallback interface instead. |
| 163 | if (new_default.empty()) |
| 164 | new_default = fallback_default_interface_; |
| 165 | |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 166 | if (default_interface_ == new_default) |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 167 | return default_interface_; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 168 | |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 169 | LOG(INFO) << "Default interface changed from [" << default_interface_ |
| 170 | << "] to [" << new_default << "]"; |
| 171 | |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 172 | const std::string prev_default = default_interface_; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 173 | default_interface_ = new_default; |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 174 | for (const auto& h : default_interface_handlers_) { |
| 175 | if (!h.is_null()) |
| 176 | h.Run(default_interface_, prev_default); |
Garrick Evans | c7fea0a | 2020-02-04 10:46:42 +0900 | [diff] [blame] | 177 | } |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 178 | return prev_default; |
Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void ShillClient::RegisterDefaultInterfaceChangedHandler( |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 182 | const DefaultInterfaceChangeHandler& handler) { |
| 183 | default_interface_handlers_.emplace_back(handler); |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 184 | const auto prev_default = SetDefaultInterface(GetDefaultInterface()); |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 185 | handler.Run(default_interface_, prev_default); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void ShillClient::RegisterDevicesChangedHandler( |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 189 | const DevicesChangeHandler& handler) { |
| 190 | device_handlers_.emplace_back(handler); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 191 | } |
| 192 | |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 193 | void ShillClient::UpdateDevices(const brillo::Any& property_value) { |
| 194 | std::set<std::string> new_devices, added, removed; |
| 195 | for (const auto& path : |
| 196 | property_value.TryGet<std::vector<dbus::ObjectPath>>()) { |
| 197 | std::string device = path.value(); |
| 198 | // Strip "/device/" prefix. |
| 199 | device = device.substr(device.find_last_of('/') + 1); |
Garrick Evans | e87bb4f | 2020-02-18 10:27:37 +0900 | [diff] [blame] | 200 | |
Garrick Evans | 139708f | 2020-02-06 14:38:59 +0900 | [diff] [blame] | 201 | new_devices.emplace(device); |
| 202 | if (devices_.find(device) == devices_.end()) |
| 203 | added.insert(device); |
| 204 | } |
| 205 | |
| 206 | for (const auto& d : devices_) { |
| 207 | if (new_devices.find(d) == new_devices.end()) |
| 208 | removed.insert(d); |
| 209 | } |
| 210 | |
| 211 | devices_ = new_devices; |
| 212 | |
| 213 | for (const auto& h : device_handlers_) |
| 214 | h.Run(added, removed); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 217 | } // namespace patchpanel |