Line data Source code
1 : // Copyright 2025 Accenture.
2 :
3 : #pragma once
4 :
5 : #include "ip/IPAddress.h"
6 :
7 : #include <etl/array.h>
8 : #include <util/estd/signal.h>
9 :
10 : namespace ip
11 : {
12 : struct Ip4Config
13 : {
14 : bool useDhcp = false;
15 : };
16 :
17 : /**
18 : * Represents a IP configuration for a network interface. It can represent configurations
19 : * for both IPv4 and IPv6 addressing. In case of IPv4 it holds also the corresponding
20 : * values for network mask, default gateway address and can return the valid subnet broadcast
21 : * address.
22 : */
23 :
24 : class NetworkInterfaceConfig
25 : {
26 : public:
27 : using Ip6AddressType = uint32_t[4];
28 :
29 : /**
30 : * Constructor. Represents an invalid interface configuration (not configured).
31 : */
32 : NetworkInterfaceConfig();
33 : /**
34 : * Constructor for a IPv4 interface configuration. The corresponding values are expected as raw
35 : * 32 bit values (compare the IPAddress IPv4 constructor with a single uint32_t value).
36 : * \param ip4Address raw value for the IPv4 address
37 : * \param networkMask raw value for the corresponding network mask
38 : * \param defaultGateway raw value for the IPv4 address of the default gateway
39 : */
40 : NetworkInterfaceConfig(uint32_t ip4Address, uint32_t networkMask, uint32_t defaultGateway);
41 :
42 : /**
43 : * Constructor for a IPv6 interface configuration. The corresponding address is expected as
44 : * a raw array of four 32 bit values (compare to the IPAddress IPv6 constructor with an array).
45 : * \param ip6Address raw array holding the IPv6 address
46 : */
47 : explicit NetworkInterfaceConfig(Ip6AddressType const& ip6Address);
48 :
49 : /**
50 : * Checks whether this object represents a physically linked, valid IPv4 or IPv6 configuration.
51 : * \return true if physically linked and address valid
52 : */
53 : bool isValid() const;
54 :
55 : /**
56 : * Get the family of the represented IP configuration
57 : * \return
58 : * - IPV4 in case of a valid IPv4 configuration
59 : * - IPV6 in case of a valid IPv6 configuration
60 : * - FAMILY_UNKNOWN in case of an invalid configurations
61 : */
62 : ::ip::IPAddress::Family ipFamily() const;
63 :
64 : /**
65 : * Get the IP address for the configuration.
66 : * \return a valid IPv4 or IPv6 address in case of a valid configuration, an undefined IP
67 : * address otherwise
68 : */
69 : ::ip::IPAddress ipAddress() const;
70 :
71 : /**
72 : * Get the corresponding IPv4 network mask for the configuration.
73 : * \return a valid IPv4 address holding the network mask for a IPv4 configuration, an undefined
74 : * IP address otherwise
75 : */
76 : ::ip::IPAddress networkMask() const;
77 :
78 : /**
79 : * Get the corresponding IPv4 default gateway address for the configuration.
80 : * \return a valid IPv4 default gateway address for a IPv4 configuration, an undefined IP
81 : * address otherwise
82 : */
83 : ::ip::IPAddress defaultGateway() const;
84 :
85 : /**
86 : * Calculates the subnet broadcast address for a IPv4 configuration.
87 : * \return a valid IPv4 subnet broadcat address for a IPv4 configuration, an undefined IP
88 : * address otherwise
89 : */
90 : ::ip::IPAddress broadcastAddress() const;
91 :
92 : /**
93 : * Compare two network addresses for equality.
94 : * \param lhs config on the left-hand side of the operator
95 : * \param rhs config on the right-hand side of the operator
96 : * - true if config family and all address fields are equal.
97 : * - false otherwise
98 : */
99 : friend bool operator==(NetworkInterfaceConfig const& lhs, NetworkInterfaceConfig const& rhs);
100 :
101 : /**
102 : * Compare two network addresses for inequality.
103 : * \param lhs config on the left-hand side of the operator
104 : * \param rhs config on the right-hand side of the operator
105 : * - true if config family or any address field is not equal.
106 : * - false otherwise
107 : */
108 : friend bool operator!=(NetworkInterfaceConfig const& lhs, NetworkInterfaceConfig const& rhs);
109 :
110 : private:
111 : ::etl::array<uint32_t, 4> _config{};
112 : ::ip::IPAddress::Family _family;
113 : };
114 :
115 : /**
116 : * Inline implementation.
117 : */
118 51 : inline bool NetworkInterfaceConfig::isValid() const
119 : {
120 51 : return _family != ::ip::IPAddress::FAMILY_UNKNOWN;
121 : }
122 :
123 25 : inline ::ip::IPAddress::Family NetworkInterfaceConfig::ipFamily() const { return _family; }
124 :
125 11 : inline bool operator!=(NetworkInterfaceConfig const& lhs, NetworkInterfaceConfig const& rhs)
126 : {
127 11 : return !operator==(lhs, rhs);
128 : }
129 :
130 : using NetworkInterfaceConfigKey = uint8_t;
131 :
132 : struct NetworkInterfaceConfigRegistry
133 : {
134 : using ConfigChangedSignal
135 : = ::util::estd::signal<::estd::function<void(uint8_t, NetworkInterfaceConfig const&)>>;
136 :
137 21 : NetworkInterfaceConfigRegistry(
138 : ::etl::span<uint8_t const> busIds, ::etl::span<NetworkInterfaceConfig const> configs)
139 21 : : busIds(busIds), configs(configs)
140 21 : {}
141 :
142 : ::etl::span<uint8_t const> busIds;
143 : ::etl::span<NetworkInterfaceConfig const> configs;
144 :
145 : ConfigChangedSignal configChangedSignal;
146 :
147 0 : virtual NetworkInterfaceConfig getConfig(uint8_t const busId) const
148 : {
149 0 : for (size_t i = 0; i < busIds.size(); ++i)
150 : {
151 0 : if (busIds[i] == busId)
152 : {
153 0 : return configs[i];
154 : }
155 : }
156 0 : return {};
157 : }
158 : };
159 : } // namespace ip
|