network_interfaces_fuchsia.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2018 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. #ifndef NET_BASE_NETWORK_INTERFACES_FUCHSIA_H_
  5. #define NET_BASE_NETWORK_INTERFACES_FUCHSIA_H_
  6. #include <fuchsia/net/interfaces/cpp/fidl.h>
  7. #include <vector>
  8. #include "net/base/network_change_notifier.h"
  9. #include "net/base/network_interfaces.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace net {
  12. namespace internal {
  13. // Move-only wrapper for fuchsia::net::interface::Properties that guarantees
  14. // that its inner |properties_| are valid and complete properties as reported by
  15. // |VerifyCompleteInterfaceProperties|.
  16. class InterfaceProperties final {
  17. public:
  18. // Creates an |InterfaceProperties| if |properties| are valid complete
  19. // properties as reported by |VerifyCompleteInterfaceProperties|.
  20. static absl::optional<InterfaceProperties> VerifyAndCreate(
  21. fuchsia::net::interfaces::Properties properties);
  22. InterfaceProperties(InterfaceProperties&& interface);
  23. InterfaceProperties& operator=(InterfaceProperties&& interface);
  24. ~InterfaceProperties();
  25. // Updates this instance with the values set in |properties|.
  26. // Fields not set in |properties| retain their previous values.
  27. // Returns false if the |properties| has a missing or mismatched |id| field,
  28. // or if any field set in |properties| has an invalid value (e.g. addresses of
  29. // unknown types).
  30. bool Update(fuchsia::net::interfaces::Properties properties);
  31. // Appends the NetworkInterfaces for this interface to |interfaces|.
  32. void AppendNetworkInterfaces(NetworkInterfaceList* interfaces) const;
  33. // Returns true if the interface is online and it has either an IPv4 default
  34. // route and a non-link-local address, or an IPv6 default route and a global
  35. // address.
  36. bool IsPubliclyRoutable() const;
  37. bool HasAddresses() const { return !properties_.addresses().empty(); }
  38. uint64_t id() const { return properties_.id(); }
  39. bool online() const { return properties_.online(); }
  40. const fuchsia::net::interfaces::DeviceClass& device_class() const {
  41. return properties_.device_class();
  42. }
  43. private:
  44. explicit InterfaceProperties(fuchsia::net::interfaces::Properties properties);
  45. fuchsia::net::interfaces::Properties properties_;
  46. };
  47. using ExistingInterfaceProperties =
  48. std::vector<std::pair<uint64_t, InterfaceProperties>>;
  49. // Returns the //net ConnectionType for the supplied netstack interface
  50. // description. Returns CONNECTION_NONE for loopback interfaces.
  51. NetworkChangeNotifier::ConnectionType ConvertConnectionType(
  52. const fuchsia::net::interfaces::DeviceClass& device_class);
  53. // Connects to the service via the process' ComponentContext, and connects the
  54. // Watcher to the service.
  55. fuchsia::net::interfaces::WatcherHandle ConnectInterfacesWatcher();
  56. // Validates that |properties| contains all the required fields, returning
  57. // |true| if so.
  58. bool VerifyCompleteInterfaceProperties(
  59. const fuchsia::net::interfaces::Properties& properties);
  60. // Consumes events describing existing interfaces from |watcher| and
  61. // returns a vector of interface Id & properties pairs. |watcher| must
  62. // be a newly-connected Watcher channel.
  63. // Returns absl::nullopt if any protocol error is encountered, e.g.
  64. // |watcher| supplies an invalid event, or disconnects.
  65. absl::optional<internal::ExistingInterfaceProperties> GetExistingInterfaces(
  66. const fuchsia::net::interfaces::WatcherSyncPtr& watcher);
  67. } // namespace internal
  68. } // namespace net
  69. #endif // NET_BASE_NETWORK_INTERFACES_FUCHSIA_H_