Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/base/network_change_notifier_fuchsia.h" |
| 6 | |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 7 | #include <algorithm> |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 8 | #include <iterator> |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
Jeremy Manson | 18f6ea3 | 2018-11-13 21:12:27 | [diff] [blame] | 13 | #include "base/fuchsia/fuchsia_logging.h" |
Avi Drissman | 41c4a41 | 2023-01-11 22:45:37 | [diff] [blame^] | 14 | #include "base/functional/bind.h" |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 15 | #include "base/strings/stringprintf.h" |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 16 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 17 | |
| 18 | namespace net { |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 19 | |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 20 | NetworkChangeNotifierFuchsia::NetworkChangeNotifierFuchsia(bool require_wlan) |
| 21 | : NetworkChangeNotifierFuchsia(internal::ConnectInterfacesWatcher(), |
| 22 | require_wlan) {} |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 23 | |
| 24 | NetworkChangeNotifierFuchsia::NetworkChangeNotifierFuchsia( |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 25 | fidl::InterfaceHandle<fuchsia::net::interfaces::Watcher> handle, |
| 26 | bool require_wlan, |
Eric Orth | c398f1e | 2019-07-09 21:54:55 | [diff] [blame] | 27 | SystemDnsConfigChangeNotifier* system_dns_config_notifier) |
| 28 | : NetworkChangeNotifier(NetworkChangeCalculatorParams(), |
| 29 | system_dns_config_notifier), |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 30 | require_wlan_(require_wlan) { |
| 31 | DCHECK(handle); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 32 | |
Wez | f1e023e | 2021-10-26 08:42:58 | [diff] [blame] | 33 | watcher_.set_error_handler(base::LogFidlErrorAndExitProcess( |
| 34 | FROM_HERE, "fuchsia.net.interfaces.Watcher")); |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame] | 35 | |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 36 | fuchsia::net::interfaces::WatcherSyncPtr watcher = handle.BindSync(); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 37 | absl::optional<internal::ExistingInterfaceProperties> interfaces = |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 38 | internal::GetExistingInterfaces(watcher); |
Wez | f9992b7e | 2021-01-13 17:53:18 | [diff] [blame] | 39 | if (!interfaces) |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 40 | return; |
Wez | f9992b7e | 2021-01-13 17:53:18 | [diff] [blame] | 41 | |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 42 | handle = watcher.Unbind(); |
| 43 | bool notify_ip_address_changed = false; |
| 44 | for (const auto& interface_entry : *interfaces) { |
| 45 | notify_ip_address_changed |= |
| 46 | CanReachExternalNetwork(interface_entry.second); |
| 47 | } |
| 48 | interface_cache_ = InterfacePropertiesMap(std::move(*interfaces)); |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame] | 49 | |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 50 | UpdateConnectionType(); |
| 51 | if (notify_ip_address_changed) { |
| 52 | NotifyObserversOfIPAddressChange(); |
| 53 | } |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame] | 54 | |
| 55 | // Bind to the dispatcher for the thread's MessagePump. |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 56 | zx_status_t status = watcher_.Bind(std::move(handle)); |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame] | 57 | ZX_CHECK(status == ZX_OK, status) << "Bind()"; |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 58 | watcher_->Watch( |
| 59 | fit::bind_member(this, &NetworkChangeNotifierFuchsia::OnInterfacesEvent)); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | NetworkChangeNotifierFuchsia::~NetworkChangeNotifierFuchsia() { |
| 63 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
Paul Jensen | 61a51a1 | 2019-05-23 23:12:49 | [diff] [blame] | 64 | ClearGlobalPointer(); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | NetworkChangeNotifier::ConnectionType |
| 68 | NetworkChangeNotifierFuchsia::GetCurrentConnectionType() const { |
| 69 | ConnectionType type = static_cast<ConnectionType>( |
| 70 | base::subtle::Acquire_Load(&cached_connection_type_)); |
| 71 | return type; |
| 72 | } |
| 73 | |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 74 | void NetworkChangeNotifierFuchsia::OnInterfacesEvent( |
| 75 | fuchsia::net::interfaces::Event event) { |
| 76 | // Immediately trigger the next watch, which will happen asynchronously. If |
| 77 | // event processing encounters an error it'll close the watcher channel which |
| 78 | // will cancel any pending callbacks. |
| 79 | watcher_->Watch( |
| 80 | fit::bind_member(this, &NetworkChangeNotifierFuchsia::OnInterfacesEvent)); |
| 81 | |
| 82 | switch (event.Which()) { |
| 83 | case fuchsia::net::interfaces::Event::kAdded: |
| 84 | OnInterfaceAdded(std::move(event.added())); |
| 85 | break; |
| 86 | case fuchsia::net::interfaces::Event::kRemoved: |
| 87 | OnInterfaceRemoved(event.removed()); |
| 88 | break; |
| 89 | case fuchsia::net::interfaces::Event::kChanged: |
| 90 | OnInterfaceChanged(std::move(event.changed())); |
| 91 | break; |
| 92 | case fuchsia::net::interfaces::Event::kExisting: |
| 93 | case fuchsia::net::interfaces::Event::kIdle: |
| 94 | OnWatcherError(base::StringPrintf( |
| 95 | "OnInterfaceEvent: unexpected event %lu.", event.Which())); |
| 96 | break; |
| 97 | case fuchsia::net::interfaces::Event::Invalid: |
| 98 | LOG(WARNING) |
| 99 | << "Invalid event received from fuchsia.net.interfaces/Watcher"; |
| 100 | break; |
| 101 | } |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 102 | } |
| 103 | |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 104 | void NetworkChangeNotifierFuchsia::OnInterfaceAdded( |
| 105 | fuchsia::net::interfaces::Properties properties) { |
Maksim Ivanov | f478c33 | 2020-12-11 12:01:14 | [diff] [blame] | 106 | uint64_t id = properties.id(); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 107 | absl::optional<internal::InterfaceProperties> cache_entry = |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 108 | internal::InterfaceProperties::VerifyAndCreate(std::move(properties)); |
| 109 | if (!cache_entry) { |
| 110 | OnWatcherError("OnInterfaceAdded: incomplete interface properties."); |
| 111 | return; |
Marina Ciocea | 022c5b0e | 2020-11-17 16:58:20 | [diff] [blame] | 112 | } |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 113 | if (interface_cache_.find(id) != interface_cache_.end()) { |
| 114 | OnWatcherError(base::StringPrintf( |
| 115 | "OnInterfaceAdded: duplicate interface ID %lu.", id)); |
| 116 | return; |
Stephen Roe | b9b9702 | 2020-11-20 03:57:23 | [diff] [blame] | 117 | } |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 118 | const bool can_reach = CanReachExternalNetwork(*cache_entry); |
| 119 | interface_cache_.emplace(id, std::move(*cache_entry)); |
| 120 | UpdateConnectionType(); |
| 121 | if (can_reach) { |
Stephen Roe | b9b9702 | 2020-11-20 03:57:23 | [diff] [blame] | 122 | NotifyObserversOfIPAddressChange(); |
| 123 | } |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 124 | } |
Stephen Roe | b9b9702 | 2020-11-20 03:57:23 | [diff] [blame] | 125 | |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 126 | void NetworkChangeNotifierFuchsia::OnInterfaceRemoved(uint64_t interface_id) { |
| 127 | InterfacePropertiesMap::iterator cache_entry = |
| 128 | interface_cache_.find(interface_id); |
| 129 | if (cache_entry == interface_cache_.end()) { |
| 130 | OnWatcherError(base::StringPrintf( |
| 131 | "OnInterfaceRemoved: unknown interface ID %lu.", interface_id)); |
| 132 | return; |
| 133 | } |
| 134 | const bool can_reach = CanReachExternalNetwork(cache_entry->second); |
| 135 | interface_cache_.erase(cache_entry); |
| 136 | UpdateConnectionType(); |
| 137 | if (can_reach) { |
| 138 | NotifyObserversOfIPAddressChange(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void NetworkChangeNotifierFuchsia::OnInterfaceChanged( |
| 143 | fuchsia::net::interfaces::Properties properties) { |
| 144 | if (!properties.has_id()) { |
| 145 | OnWatcherError("OnInterfaceChanged: no interface ID."); |
| 146 | return; |
| 147 | } |
| 148 | const uint64_t id = properties.id(); |
| 149 | InterfacePropertiesMap::iterator cache_entry = interface_cache_.find(id); |
| 150 | if (cache_entry == interface_cache_.end()) { |
| 151 | OnWatcherError(base::StringPrintf( |
| 152 | "OnInterfaceChanged: unknown interface ID %lu.", id)); |
| 153 | return; |
| 154 | } |
| 155 | const bool old_can_reach = CanReachExternalNetwork(cache_entry->second); |
| 156 | const bool has_addresses = properties.has_addresses(); |
| 157 | if (!cache_entry->second.Update(std::move(properties))) { |
| 158 | OnWatcherError("OnInterfaceChanged: update failed."); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | UpdateConnectionType(); |
| 163 | const bool can_reach = CanReachExternalNetwork(cache_entry->second); |
| 164 | if (has_addresses || old_can_reach != can_reach) { |
| 165 | NotifyObserversOfIPAddressChange(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void NetworkChangeNotifierFuchsia::OnWatcherError( |
| 170 | base::StringPiece error_message) { |
| 171 | LOG(ERROR) << error_message; |
| 172 | watcher_.Unbind(); |
| 173 | ResetConnectionType(); |
| 174 | } |
| 175 | |
| 176 | void NetworkChangeNotifierFuchsia::UpdateConnectionType() { |
| 177 | ConnectionType connection_type = ConnectionType::CONNECTION_NONE; |
| 178 | for (const auto& interface : interface_cache_) { |
| 179 | if (CanReachExternalNetwork(interface.second)) { |
| 180 | connection_type = GetEffectiveConnectionType(interface.second); |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | if (connection_type != GetCurrentConnectionType()) { |
Wez | ec6e7f1 | 2019-05-17 23:02:09 | [diff] [blame] | 185 | base::subtle::Release_Store(&cached_connection_type_, connection_type); |
Haines Sy | 2eb17bcb | 2019-10-26 17:48:18 | [diff] [blame] | 186 | NotifyObserversOfConnectionTypeChange(); |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 187 | } |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 188 | } |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame] | 189 | |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 190 | void NetworkChangeNotifierFuchsia::ResetConnectionType() { |
| 191 | base::subtle::Release_Store(&cached_connection_type_, |
| 192 | ConnectionType::CONNECTION_UNKNOWN); |
| 193 | } |
| 194 | |
| 195 | NetworkChangeNotifier::ConnectionType |
| 196 | NetworkChangeNotifierFuchsia::GetEffectiveConnectionType( |
| 197 | const internal::InterfaceProperties& properties) { |
| 198 | if (!properties.IsPubliclyRoutable()) |
| 199 | return NetworkChangeNotifier::CONNECTION_NONE; |
| 200 | |
| 201 | NetworkChangeNotifier::ConnectionType connection_type = |
| 202 | internal::ConvertConnectionType(properties.device_class()); |
| 203 | if (require_wlan_ && |
| 204 | connection_type != NetworkChangeNotifier::CONNECTION_WIFI) { |
| 205 | return NetworkChangeNotifier::CONNECTION_NONE; |
Tamir Duberstein | bf0a6a1d | 2020-08-03 18:44:11 | [diff] [blame] | 206 | } |
Marina Ciocea | c05c3e3 | 2020-12-03 19:36:58 | [diff] [blame] | 207 | return connection_type; |
| 208 | } |
| 209 | |
| 210 | bool NetworkChangeNotifierFuchsia::CanReachExternalNetwork( |
| 211 | const internal::InterfaceProperties& properties) { |
| 212 | return GetEffectiveConnectionType(properties) != |
| 213 | NetworkChangeNotifier::CONNECTION_NONE; |
Kevin Marshall | 3e89fd7 | 2018-06-05 21:29:10 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | } // namespace net |