• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_BASE_NETWORK_INTERFACES_FUCHSIA_H_
6 #define NET_BASE_NETWORK_INTERFACES_FUCHSIA_H_
7 
8 #include <fuchsia/net/interfaces/cpp/fidl.h>
9 #include <stdint.h>
10 
11 #include "net/base/network_change_notifier.h"
12 #include "net/base/network_interfaces.h"
13 #include "third_party/abseil-cpp/absl/types/optional.h"
14 
15 namespace net::internal {
16 
17 // Move-only wrapper for fuchsia::net::interface::Properties that guarantees
18 // that its inner |properties_| are valid and complete properties as reported by
19 // |VerifyCompleteInterfaceProperties|.
20 class InterfaceProperties final {
21  public:
22   using InterfaceId = uint64_t;
23 
24   // Creates an |InterfaceProperties| if |properties| are valid complete
25   // properties as reported by |VerifyCompleteInterfaceProperties|.
26   static absl::optional<InterfaceProperties> VerifyAndCreate(
27       fuchsia::net::interfaces::Properties properties);
28   InterfaceProperties(InterfaceProperties&& interface);
29   InterfaceProperties& operator=(InterfaceProperties&& interface);
30   ~InterfaceProperties();
31 
32   // Updates this instance with the values set in |properties|.
33   // Fields not set in |properties| retain their previous values.
34   // Returns false if the |properties| has a missing or mismatched |id| field,
35   // or if any field set in |properties| has an invalid value (e.g. addresses of
36   // unknown types).
37   bool Update(fuchsia::net::interfaces::Properties properties);
38 
39   // Appends the NetworkInterfaces for this interface to |interfaces|.
40   void AppendNetworkInterfaces(NetworkInterfaceList* interfaces) const;
41 
42   // Returns true if the interface is online and it has either an IPv4 default
43   // route and a non-link-local address, or an IPv6 default route and a global
44   // address.
45   bool IsPubliclyRoutable() const;
46 
HasAddresses()47   bool HasAddresses() const { return !properties_.addresses().empty(); }
id()48   InterfaceId id() const { return properties_.id(); }
online()49   bool online() const { return properties_.online(); }
device_class()50   const fuchsia::net::interfaces::DeviceClass& device_class() const {
51     return properties_.device_class();
52   }
53 
54  private:
55   explicit InterfaceProperties(fuchsia::net::interfaces::Properties properties);
56 
57   fuchsia::net::interfaces::Properties properties_;
58 };
59 
60 // Returns the //net ConnectionType for the supplied netstack interface
61 // description. Returns CONNECTION_NONE for loopback interfaces.
62 NetworkChangeNotifier::ConnectionType ConvertConnectionType(
63     const fuchsia::net::interfaces::DeviceClass& device_class);
64 
65 // Validates that |properties| contains all the required fields, returning
66 // |true| if so.
67 bool VerifyCompleteInterfaceProperties(
68     const fuchsia::net::interfaces::Properties& properties);
69 
70 }  // namespace net::internal
71 
72 #endif  // NET_BASE_NETWORK_INTERFACES_FUCHSIA_H_
73