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 : 9 : namespace ip 10 : { 11 : struct Ip4Config 12 : { 13 : bool useDhcp = false; 14 : }; 15 : 16 : /** 17 : * Represents a IP configuration for a network interface. It can represent configurations 18 : * for both IPv4 and IPv6 addressing. In case of IPv4 it holds also the corresponding 19 : * values for network mask, default gateway address and can return the valid subnet broadcast 20 : * address. 21 : */ 22 : 23 : class NetworkInterfaceConfig 24 : { 25 : public: 26 : using Ip6AddressType = uint32_t[4]; 27 : 28 : /** 29 : * Constructor. Represents an invalid interface configuration (not configured). 30 : */ 31 : NetworkInterfaceConfig(); 32 : /** 33 : * Constructor for a IPv4 interface configuration. The corresponding values are expected as raw 34 : * 32 bit values (compare the IPAddress IPv4 constructor with a single uint32_t value). 35 : * \param ip4Address raw value for the IPv4 address 36 : * \param networkMask raw value for the corresponding network mask 37 : * \param defaultGateway raw value for the IPv4 address of the default gateway 38 : */ 39 : NetworkInterfaceConfig(uint32_t ip4Address, uint32_t networkMask, uint32_t defaultGateway); 40 : 41 : /** 42 : * Constructor for a IPv6 interface configuration. The corresponding address is expected as 43 : * a raw array of four 32 bit values (compare to the IPAddress IPv6 constructor with an array). 44 : * \param ip6Address raw array holding the IPv6 address 45 : */ 46 : explicit NetworkInterfaceConfig(Ip6AddressType const& ip6Address); 47 : 48 : /** 49 : * Checks whether this object represents a physically linked, valid IPv4 or IPv6 configuration. 50 : * \return true if physically linked and address valid 51 : */ 52 : bool isValid() const; 53 : 54 : /** 55 : * Get the family of the represented IP configuration 56 : * \return 57 : * - IPV4 in case of a valid IPv4 configuration 58 : * - IPV6 in case of a valid IPv6 configuration 59 : * - FAMILY_UNKNOWN in case of an invalid configurations 60 : */ 61 : ::ip::IPAddress::Family ipFamily() const; 62 : 63 : /** 64 : * Get the IP address for the configuration. 65 : * \return a valid IPv4 or IPv6 address in case of a valid configuration, an undefined IP 66 : * address otherwise 67 : */ 68 : ::ip::IPAddress ipAddress() const; 69 : 70 : /** 71 : * Get the corresponding IPv4 network mask for the configuration. 72 : * \return a valid IPv4 address holding the network mask for a IPv4 configuration, an undefined 73 : * IP address otherwise 74 : */ 75 : ::ip::IPAddress networkMask() const; 76 : 77 : /** 78 : * Get the corresponding IPv4 default gateway address for the configuration. 79 : * \return a valid IPv4 default gateway address for a IPv4 configuration, an undefined IP 80 : * address otherwise 81 : */ 82 : ::ip::IPAddress defaultGateway() const; 83 : 84 : /** 85 : * Calculates the subnet broadcast address for a IPv4 configuration. 86 : * \return a valid IPv4 subnet broadcat address for a IPv4 configuration, an undefined IP 87 : * address otherwise 88 : */ 89 : ::ip::IPAddress broadcastAddress() const; 90 : 91 : /** 92 : * Compare two network addresses for equality. 93 : * \param lhs config on the left-hand side of the operator 94 : * \param rhs config on the right-hand side of the operator 95 : * - true if config family and all address fields are equal. 96 : * - false otherwise 97 : */ 98 : friend bool operator==(NetworkInterfaceConfig const& lhs, NetworkInterfaceConfig const& rhs); 99 : 100 : /** 101 : * Compare two network addresses for inequality. 102 : * \param lhs config on the left-hand side of the operator 103 : * \param rhs config on the right-hand side of the operator 104 : * - true if config family or any address field is not equal. 105 : * - false otherwise 106 : */ 107 : friend bool operator!=(NetworkInterfaceConfig const& lhs, NetworkInterfaceConfig const& rhs); 108 : 109 : private: 110 : ::etl::array<uint32_t, 4> _config{}; 111 : ::ip::IPAddress::Family _family; 112 : }; 113 : 114 : /** 115 : * Inline implementation. 116 : */ 117 10 : inline bool NetworkInterfaceConfig::isValid() const 118 : { 119 10 : return _family != ::ip::IPAddress::FAMILY_UNKNOWN; 120 : } 121 : 122 10 : inline ::ip::IPAddress::Family NetworkInterfaceConfig::ipFamily() const { return _family; } 123 : 124 11 : inline bool operator!=(NetworkInterfaceConfig const& lhs, NetworkInterfaceConfig const& rhs) 125 : { 126 11 : return !operator==(lhs, rhs); 127 : } 128 : 129 : } // namespace ip