xref: /aosp_15_r20/external/cronet/net/base/network_interfaces_fuchsia.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 <optional>
12 
13 #include "net/base/network_change_notifier.h"
14 #include "net/base/network_interfaces.h"
15 
16 namespace net::internal {
17 
18 // Move-only wrapper for fuchsia::net::interface::Properties that guarantees
19 // that its inner |properties_| are valid and complete properties as reported by
20 // |VerifyCompleteInterfaceProperties|.
21 class InterfaceProperties final {
22  public:
23   using InterfaceId = uint64_t;
24 
25   // Creates an |InterfaceProperties| if |properties| are valid complete
26   // properties as reported by |VerifyCompleteInterfaceProperties|.
27   static std::optional<InterfaceProperties> VerifyAndCreate(
28       fuchsia::net::interfaces::Properties properties);
29   InterfaceProperties(InterfaceProperties&& interface);
30   InterfaceProperties& operator=(InterfaceProperties&& interface);
31   ~InterfaceProperties();
32 
33   // Updates this instance with the values set in |properties|.
34   // Fields not set in |properties| retain their previous values.
35   // Returns false if the |properties| has a missing or mismatched |id| field,
36   // or if any field set in |properties| has an invalid value (e.g. addresses of
37   // unknown types).
38   bool Update(fuchsia::net::interfaces::Properties properties);
39 
40   // Appends the NetworkInterfaces for this interface to |interfaces|.
41   void AppendNetworkInterfaces(NetworkInterfaceList* interfaces) const;
42 
43   // Returns true if the interface is online and it has either an IPv4 default
44   // route and a non-link-local address, or an IPv6 default route and a global
45   // address.
46   bool IsPubliclyRoutable() const;
47 
HasAddresses()48   bool HasAddresses() const { return !properties_.addresses().empty(); }
id()49   InterfaceId id() const { return properties_.id(); }
online()50   bool online() const { return properties_.online(); }
device_class()51   const fuchsia::net::interfaces::DeviceClass& device_class() const {
52     return properties_.device_class();
53   }
54 
55  private:
56   explicit InterfaceProperties(fuchsia::net::interfaces::Properties properties);
57 
58   fuchsia::net::interfaces::Properties properties_;
59 };
60 
61 // Returns the //net ConnectionType for the supplied netstack interface
62 // description. Returns CONNECTION_NONE for loopback interfaces.
63 NetworkChangeNotifier::ConnectionType ConvertConnectionType(
64     const fuchsia::net::interfaces::DeviceClass& device_class);
65 
66 // Validates that |properties| contains all the required fields, returning
67 // |true| if so.
68 bool VerifyCompleteInterfaceProperties(
69     const fuchsia::net::interfaces::Properties& properties);
70 
71 }  // namespace net::internal
72 
73 #endif  // NET_BASE_NETWORK_INTERFACES_FUCHSIA_H_
74