network_interfaces_fuchsia.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright 2017 The Chromium 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. #include "net/base/network_interfaces_fuchsia.h"
  5. #include <lib/sys/cpp/component_context.h>
  6. #include <string>
  7. #include <utility>
  8. #include "base/format_macros.h"
  9. #include "base/fuchsia/fuchsia_logging.h"
  10. #include "base/fuchsia/process_context.h"
  11. #include "net/base/network_interfaces.h"
  12. namespace net {
  13. namespace internal {
  14. namespace {
  15. IPAddress FuchsiaIpAddressToIPAddress(const fuchsia::net::IpAddress& address) {
  16. switch (address.Which()) {
  17. case fuchsia::net::IpAddress::kIpv4:
  18. return IPAddress(address.ipv4().addr.data(), address.ipv4().addr.size());
  19. case fuchsia::net::IpAddress::kIpv6:
  20. return IPAddress(address.ipv6().addr.data(), address.ipv6().addr.size());
  21. default:
  22. return IPAddress();
  23. }
  24. }
  25. } // namespace
  26. // static
  27. absl::optional<InterfaceProperties> InterfaceProperties::VerifyAndCreate(
  28. fuchsia::net::interfaces::Properties properties) {
  29. if (!internal::VerifyCompleteInterfaceProperties(properties))
  30. return absl::nullopt;
  31. return absl::make_optional(InterfaceProperties(std::move(properties)));
  32. }
  33. InterfaceProperties::InterfaceProperties(
  34. fuchsia::net::interfaces::Properties properties)
  35. : properties_(std::move(properties)) {}
  36. InterfaceProperties::InterfaceProperties(InterfaceProperties&& interface) =
  37. default;
  38. InterfaceProperties& InterfaceProperties::operator=(
  39. InterfaceProperties&& interface) = default;
  40. InterfaceProperties::~InterfaceProperties() = default;
  41. bool InterfaceProperties::Update(
  42. fuchsia::net::interfaces::Properties properties) {
  43. if (!properties.has_id() || properties_.id() != properties.id()) {
  44. LOG(WARNING) << "Update failed: invalid properties.";
  45. return false;
  46. }
  47. if (properties.has_addresses()) {
  48. for (const auto& fidl_address : properties.addresses()) {
  49. if (!fidl_address.has_addr()) {
  50. LOG(WARNING) << "Update failed: invalid properties.";
  51. return false;
  52. }
  53. }
  54. properties_.set_addresses(std::move(*properties.mutable_addresses()));
  55. }
  56. if (properties.has_online())
  57. properties_.set_online(properties.online());
  58. if (properties.has_has_default_ipv4_route())
  59. properties_.set_has_default_ipv4_route(properties.has_default_ipv4_route());
  60. if (properties.has_has_default_ipv6_route())
  61. properties_.set_has_default_ipv6_route(properties.has_default_ipv6_route());
  62. return true;
  63. }
  64. void InterfaceProperties::AppendNetworkInterfaces(
  65. NetworkInterfaceList* interfaces) const {
  66. for (const auto& fidl_address : properties_.addresses()) {
  67. IPAddress address = FuchsiaIpAddressToIPAddress(fidl_address.addr().addr);
  68. if (address.empty()) {
  69. LOG(WARNING) << "Unknown fuchsia.net/IpAddress variant "
  70. << fidl_address.addr().addr.Which();
  71. continue;
  72. }
  73. // TODO(crbug.com/1131220): Set correct attributes once available in
  74. // fuchsia::net::interfaces::Properties.
  75. const int kAttributes = 0;
  76. interfaces->emplace_back(
  77. properties_.name(), properties_.name(), properties_.id(),
  78. internal::ConvertConnectionType(properties_.device_class()),
  79. std::move(address), fidl_address.addr().prefix_len, kAttributes);
  80. }
  81. }
  82. bool InterfaceProperties::IsPubliclyRoutable() const {
  83. if (!properties_.online())
  84. return false;
  85. for (const auto& fidl_address : properties_.addresses()) {
  86. const IPAddress address =
  87. FuchsiaIpAddressToIPAddress(fidl_address.addr().addr);
  88. if ((address.IsIPv4() && properties_.has_default_ipv4_route() &&
  89. !address.IsLinkLocal()) ||
  90. (address.IsIPv6() && properties_.has_default_ipv6_route() &&
  91. address.IsPubliclyRoutable())) {
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. NetworkChangeNotifier::ConnectionType ConvertConnectionType(
  98. const fuchsia::net::interfaces::DeviceClass& device_class) {
  99. switch (device_class.Which()) {
  100. case fuchsia::net::interfaces::DeviceClass::kLoopback:
  101. return NetworkChangeNotifier::CONNECTION_NONE;
  102. case fuchsia::net::interfaces::DeviceClass::kDevice:
  103. switch (device_class.device()) {
  104. case fuchsia::hardware::network::DeviceClass::WLAN:
  105. return NetworkChangeNotifier::CONNECTION_WIFI;
  106. case fuchsia::hardware::network::DeviceClass::ETHERNET:
  107. return NetworkChangeNotifier::CONNECTION_ETHERNET;
  108. default:
  109. return NetworkChangeNotifier::CONNECTION_UNKNOWN;
  110. }
  111. default:
  112. LOG(WARNING) << "Received unknown fuchsia.net.interfaces/DeviceClass "
  113. << device_class.Which();
  114. return NetworkChangeNotifier::CONNECTION_UNKNOWN;
  115. }
  116. }
  117. fuchsia::net::interfaces::WatcherHandle ConnectInterfacesWatcher() {
  118. fuchsia::net::interfaces::StateSyncPtr state;
  119. zx_status_t status =
  120. base::ComponentContextForProcess()->svc()->Connect(state.NewRequest());
  121. ZX_CHECK(status == ZX_OK, status) << "Connect()";
  122. fuchsia::net::interfaces::WatcherHandle watcher;
  123. state->GetWatcher({} /*options*/, watcher.NewRequest());
  124. return watcher;
  125. }
  126. bool VerifyCompleteInterfaceProperties(
  127. const fuchsia::net::interfaces::Properties& properties) {
  128. if (!properties.has_id())
  129. return false;
  130. if (!properties.has_addresses())
  131. return false;
  132. for (const auto& fidl_address : properties.addresses()) {
  133. if (!fidl_address.has_addr())
  134. return false;
  135. }
  136. if (!properties.has_online())
  137. return false;
  138. if (!properties.has_device_class())
  139. return false;
  140. if (!properties.has_has_default_ipv4_route())
  141. return false;
  142. if (!properties.has_has_default_ipv6_route())
  143. return false;
  144. return true;
  145. }
  146. absl::optional<ExistingInterfaceProperties> GetExistingInterfaces(
  147. const fuchsia::net::interfaces::WatcherSyncPtr& watcher) {
  148. ExistingInterfaceProperties existing_interfaces;
  149. for (;;) {
  150. fuchsia::net::interfaces::Event event;
  151. zx_status_t status = watcher->Watch(&event);
  152. if (status != ZX_OK) {
  153. ZX_LOG(ERROR, status) << "GetExistingInterfaces: Watch() failed";
  154. return absl::nullopt;
  155. }
  156. switch (event.Which()) {
  157. case fuchsia::net::interfaces::Event::Tag::kExisting: {
  158. absl::optional<InterfaceProperties> interface =
  159. InterfaceProperties::VerifyAndCreate(std::move(event.existing()));
  160. if (!interface) {
  161. LOG(ERROR) << "GetExistingInterfaces: Invalid kExisting event.";
  162. return absl::nullopt;
  163. }
  164. uint64_t id = interface->id();
  165. existing_interfaces.emplace_back(id, std::move(*interface));
  166. break;
  167. }
  168. case fuchsia::net::interfaces::Event::Tag::kIdle:
  169. // Idle means we've listed all the existing interfaces. We can stop
  170. // fetching events.
  171. return existing_interfaces;
  172. default:
  173. LOG(ERROR) << "GetExistingInterfaces: Unexpected event received.";
  174. return absl::nullopt;
  175. }
  176. }
  177. }
  178. } // namespace internal
  179. bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
  180. DCHECK(networks);
  181. fuchsia::net::interfaces::WatcherHandle handle =
  182. internal::ConnectInterfacesWatcher();
  183. fuchsia::net::interfaces::WatcherSyncPtr watcher = handle.BindSync();
  184. // TODO(crbug.com/1131238): Use NetworkChangeNotifier's cached interface
  185. // list.
  186. absl::optional<internal::ExistingInterfaceProperties> existing_interfaces =
  187. internal::GetExistingInterfaces(watcher);
  188. if (!existing_interfaces)
  189. return false;
  190. handle = watcher.Unbind();
  191. for (const auto& interface_entry : *existing_interfaces) {
  192. if (!interface_entry.second.online()) {
  193. // GetNetworkList() only returns online interfaces.
  194. continue;
  195. }
  196. if (interface_entry.second.device_class().is_loopback()) {
  197. // GetNetworkList() returns all interfaces except loopback.
  198. continue;
  199. }
  200. interface_entry.second.AppendNetworkInterfaces(networks);
  201. }
  202. return true;
  203. }
  204. std::string GetWifiSSID() {
  205. NOTIMPLEMENTED();
  206. return std::string();
  207. }
  208. } // namespace net