Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2025 Accenture
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : #include "ip/NetworkInterfaceConfig.h"
12 :
13 : namespace ip
14 : {
15 : uint8_t const IPADDRESS_INDEX = 0U;
16 : uint8_t const NETWORKMASK_INDEX = 1U;
17 : uint8_t const DEFAULTGATEWAY_INDEX = 2U;
18 :
19 224 : NetworkInterfaceConfig::NetworkInterfaceConfig() : _config(), _family(IPAddress::FAMILY_UNKNOWN) {}
20 :
21 36 : NetworkInterfaceConfig::NetworkInterfaceConfig(
22 36 : uint32_t const ip4Address, uint32_t const networkMask, uint32_t const defaultGateway)
23 36 : : _family(IPAddress::IPV4)
24 : {
25 36 : uint8_t const freeIndex = 3U;
26 :
27 36 : _config[IPADDRESS_INDEX] = ip4Address;
28 36 : _config[NETWORKMASK_INDEX] = networkMask;
29 36 : _config[DEFAULTGATEWAY_INDEX] = defaultGateway;
30 36 : _config[freeIndex] = 0U;
31 36 : }
32 :
33 22 : NetworkInterfaceConfig::NetworkInterfaceConfig(Ip6AddressType const& ip6Address)
34 22 : : _family(IPAddress::IPV6)
35 : {
36 22 : _config[0U] = ip6Address[0];
37 22 : _config[1U] = ip6Address[1];
38 22 : _config[2U] = ip6Address[2];
39 22 : _config[3U] = ip6Address[3];
40 22 : }
41 :
42 109 : IPAddress NetworkInterfaceConfig::ipAddress() const
43 : {
44 109 : if (_family == IPAddress::IPV4)
45 : {
46 77 : return make_ip4(_config[IPADDRESS_INDEX]);
47 : }
48 : #ifndef OPENBSW_NO_IPV6
49 32 : if (_family == IPAddress::IPV6)
50 : {
51 11 : return make_ip6(&_config[0U]);
52 : }
53 : #endif
54 :
55 21 : return IPAddress();
56 : }
57 :
58 10 : IPAddress NetworkInterfaceConfig::networkMask() const
59 : {
60 10 : if (_family == IPAddress::IPV4)
61 : {
62 3 : return make_ip4(_config[NETWORKMASK_INDEX]);
63 : }
64 :
65 7 : return IPAddress();
66 : }
67 :
68 10 : IPAddress NetworkInterfaceConfig::defaultGateway() const
69 : {
70 10 : if (_family == IPAddress::IPV4)
71 : {
72 3 : return make_ip4(_config[DEFAULTGATEWAY_INDEX]);
73 : }
74 :
75 7 : return IPAddress();
76 : }
77 :
78 57 : IPAddress NetworkInterfaceConfig::broadcastAddress() const
79 : {
80 57 : if (_family == IPAddress::IPV4)
81 : {
82 62 : return make_ip4(
83 31 : _config[IPADDRESS_INDEX] | (static_cast<uint32_t>(~_config[NETWORKMASK_INDEX])));
84 : }
85 :
86 26 : return IPAddress();
87 : }
88 :
89 22 : bool operator==(NetworkInterfaceConfig const& lhs, NetworkInterfaceConfig const& rhs)
90 : {
91 22 : return (lhs._family == rhs._family) && (lhs._config == rhs._config);
92 : }
93 :
94 : } // namespace ip
|