|
38 | 38 |
|
39 | 39 | namespace core_lib |
40 | 40 | { |
41 | | -namespace net_utils |
42 | | -{ |
43 | | -using ip_octets_t = std::vector<uint8_t>; |
| 41 | + namespace net_utils |
| 42 | + { |
| 43 | + using ip_octets_t = std::vector<uint8_t>; |
44 | 44 |
|
45 | 45 | // Give an IP address, e.g. 192.168.1.1, get a vector of the octets, |
46 | 46 | // e.g. {192, 168, 1, 1}. |
47 | 47 | #if defined(IS_CPP17) |
48 | | -CORE_LIBRARY_DLL_SHARED_API ip_octets_t OctetsFromIpAddress(std::string_view ipAddress); |
| 48 | + CORE_LIBRARY_DLL_SHARED_API ip_octets_t OctetsFromIpAddress(std::string_view ipAddress); |
49 | 49 | #else |
50 | | -CORE_LIBRARY_DLL_SHARED_API ip_octets_t OctetsFromIpAddress(std::string const& ipAddress); |
| 50 | + CORE_LIBRARY_DLL_SHARED_API ip_octets_t OctetsFromIpAddress(std::string const &ipAddress); |
51 | 51 | #endif |
52 | 52 |
|
53 | 53 | // Give an IP address, e.g. 192.168.1.1, get a uint32_t |
54 | 54 | // representation of the address, u32 from the addresses octets o[4] |
55 | 55 | // as follows: |
56 | 56 | // u32 = (o[0] << 24) + (o[1] << 16) + (o[2] << 9) + o[3] |
57 | 57 | #if defined(IS_CPP17) |
58 | | -CORE_LIBRARY_DLL_SHARED_API uint32_t Uint32FromIpAddress(std::string_view ipAddress); |
| 58 | + CORE_LIBRARY_DLL_SHARED_API uint32_t Uint32FromIpAddress(std::string_view ipAddress); |
59 | 59 | #else |
60 | | -CORE_LIBRARY_DLL_SHARED_API uint32_t Uint32FromIpAddress(std::string const& ipAddress); |
| 60 | + CORE_LIBRARY_DLL_SHARED_API uint32_t Uint32FromIpAddress(std::string const &ipAddress); |
61 | 61 | #endif |
62 | 62 |
|
63 | | -// Give a vector of octets, e.g. {192, 168, 1, 1}, get an IP address, |
64 | | -// e.g. 192.168.1.1. |
65 | | -CORE_LIBRARY_DLL_SHARED_API std::string IpAddressFromOctets(ip_octets_t const& octets); |
| 63 | + // Give a vector of octets, e.g. {192, 168, 1, 1}, get an IP address, |
| 64 | + // e.g. 192.168.1.1. |
| 65 | + CORE_LIBRARY_DLL_SHARED_API std::string IpAddressFromOctets(ip_octets_t const &octets); |
66 | 66 |
|
67 | | -// Given a uint32_t representing an IP address convert it |
68 | | -// to the corresponding octets then convert to a string. |
69 | | -CORE_LIBRARY_DLL_SHARED_API std::string IpAddressFromUint32(uint32_t ipValue); |
| 67 | + // Given a uint32_t representing an IP address convert it |
| 68 | + // to the corresponding octets then convert to a string. |
| 69 | + CORE_LIBRARY_DLL_SHARED_API std::string IpAddressFromUint32(uint32_t ipValue); |
70 | 70 |
|
71 | 71 | // Check if an IP address is valid. Disallows multicast addresses. |
72 | 72 | // 1.0.0.0 to 255.255.255.254 but excluding multicast |
73 | 73 | // range, 224.0.0.0 to 239.255.255.255, an 0.0.0.0 and |
74 | 74 | // 255.255.255.255. |
75 | 75 | #if defined(IS_CPP17) |
76 | | -CORE_LIBRARY_DLL_SHARED_API bool IsValidIpAddress(std::string_view address); |
| 76 | + CORE_LIBRARY_DLL_SHARED_API bool IsValidIpAddress(std::string_view address); |
77 | 77 | #else |
78 | | -CORE_LIBRARY_DLL_SHARED_API bool IsValidIpAddress(std::string const& address); |
| 78 | + CORE_LIBRARY_DLL_SHARED_API bool IsValidIpAddress(std::string const &address); |
79 | 79 | #endif |
80 | 80 |
|
81 | 81 | // Check to see if the address is a valid subnet mask. |
82 | 82 | #if defined(IS_CPP17) |
83 | | -CORE_LIBRARY_DLL_SHARED_API bool IsValidSubnetMask(std::string_view subnetMask); |
| 83 | + CORE_LIBRARY_DLL_SHARED_API bool IsValidSubnetMask(std::string_view subnetMask); |
84 | 84 | #else |
85 | | -bCORE_LIBRARY_DLL_SHARED_API ool IsValidSubnetMask(std::string const& subnetMask); |
| 85 | + bCORE_LIBRARY_DLL_SHARED_API ool IsValidSubnetMask(std::string const &subnetMask); |
86 | 86 | #endif |
87 | 87 |
|
88 | 88 | // Check if a broadcast address is valid. Disallows multicast addresses. |
89 | 89 | // Can also be used to check if a subnet mask is valid. |
90 | 90 | #if defined(IS_CPP17) |
91 | | -CORE_LIBRARY_DLL_SHARED_API bool IsValidBroadcastAddress(std::string_view address); |
| 91 | + CORE_LIBRARY_DLL_SHARED_API bool IsValidBroadcastAddress(std::string_view address); |
92 | 92 | #else |
93 | | -CORE_LIBRARY_DLL_SHARED_API bool IsValidBroadcastAddress(std::string const& address); |
| 93 | + CORE_LIBRARY_DLL_SHARED_API bool IsValidBroadcastAddress(std::string const &address); |
94 | 94 | #endif |
95 | 95 |
|
96 | 96 | // Check if address is valid multicast group address. |
97 | 97 | // 224.0.0.0 to 239.255.255.255. |
98 | 98 | #if defined(IS_CPP17) |
99 | | -CORE_LIBRARY_DLL_SHARED_API bool IsValidMulticastGroupAddress(std::string_view address); |
| 99 | + CORE_LIBRARY_DLL_SHARED_API bool IsValidMulticastGroupAddress(std::string_view address); |
100 | 100 | #else |
101 | | -CORE_LIBRARY_DLL_SHARED_API bool IsValidMulticastGroupAddress(std::string const& address); |
| 101 | + CORE_LIBRARY_DLL_SHARED_API bool IsValidMulticastGroupAddress(std::string const &address); |
102 | 102 | #endif |
103 | 103 |
|
104 | | -// Convert a CIDR prefix value e.g. 16, 24 etc. |
105 | | -// to the subnet mask, e.g. 255.255.0.0, 255.255.255.0 etc. |
106 | | -// |
107 | | -// Returns empty string is there is no valid conversion. |
108 | | -CORE_LIBRARY_DLL_SHARED_API std::string CidrPrefixToSubnetMask(int32_t prefix); |
| 104 | + // Convert a CIDR prefix value e.g. 16, 24 etc. |
| 105 | + // to the subnet mask, e.g. 255.255.0.0, 255.255.255.0 etc. |
| 106 | + // |
| 107 | + // Returns empty string is there is no valid conversion. |
| 108 | + CORE_LIBRARY_DLL_SHARED_API std::string CidrPrefixToSubnetMask(int32_t prefix); |
109 | 109 |
|
110 | 110 | // Convert a subnet mask, e.g. 255.255.0.0 to |
111 | 111 | // a CIDR prefix e.g. 16. |
112 | 112 | // |
113 | 113 | // Returns -1 if there is no valid conversion. |
114 | 114 | #if defined(IS_CPP17) |
115 | | -CORE_LIBRARY_DLL_SHARED_API int32_t SubnetMaskToCidrPrefix(std::string_view subnetMask); |
| 115 | + CORE_LIBRARY_DLL_SHARED_API int32_t SubnetMaskToCidrPrefix(std::string_view subnetMask); |
116 | 116 | #else |
117 | | -CORE_LIBRARY_DLL_SHARED_API int32_t SubnetMaskToCidrPrefix(std::string const& subnetMask); |
| 117 | + CORE_LIBRARY_DLL_SHARED_API int32_t SubnetMaskToCidrPrefix(std::string const &subnetMask); |
118 | 118 | #endif |
119 | 119 |
|
120 | | -// Convert a CIDR prefix length to a uint32_t subnet mask in network byte order. |
121 | | -CORE_LIBRARY_DLL_SHARED_API uint32_t PrefixToMaskNetworkOrder(uint8_t prefixLength); |
| 120 | + // Convert a CIDR prefix length to a uint32_t subnet mask in network byte order. |
| 121 | + CORE_LIBRARY_DLL_SHARED_API uint32_t PrefixToMaskNetworkOrder(uint8_t prefixLength); |
122 | 122 |
|
123 | 123 | // Create a CIDR address from an IP and subnet mask. |
124 | 124 | // e.g. 192.168.10.1/255.255.0.0 becomes |
125 | 125 | // 192.168.10.1/16 |
126 | 126 | // |
127 | 127 | // Throws std::invalid_argument upon error. |
128 | 128 | #if defined(IS_CPP17) |
129 | | -CORE_LIBRARY_DLL_SHARED_API std::string MakeCidrAddress(std::string_view address, std::string_view subnetMask); |
| 129 | + CORE_LIBRARY_DLL_SHARED_API std::string MakeCidrAddress(std::string_view address, std::string_view subnetMask); |
130 | 130 | #else |
131 | | -CORE_LIBRARY_DLL_SHARED_API std::string MakeCidrAddress(std::string const& address, std::string const& subnetMask); |
| 131 | + CORE_LIBRARY_DLL_SHARED_API std::string MakeCidrAddress(std::string const &address, std::string const &subnetMask); |
132 | 132 | #endif |
133 | 133 |
|
134 | 134 | // Given an IP address and subnet mask create the appropriate broadcast address, e.g. 160.50.100.76 |
135 | 135 | // and 255.255.0.0 becomes 160.50.255.255. |
136 | 136 | // |
137 | 137 | // Throws if an error occurs, std::invalid_argument |
138 | 138 | #if defined(IS_CPP17) |
139 | | -CORE_LIBRARY_DLL_SHARED_API std::string BuildBroadcastAddress(std::string_view address, std::string_view subnetMask); |
| 139 | + CORE_LIBRARY_DLL_SHARED_API std::string BuildBroadcastAddress(std::string_view address, std::string_view subnetMask); |
140 | 140 | #else |
141 | | -CORE_LIBRARY_DLL_SHARED_API std::string BuildBroadcastAddress(std::string const& address, std::string const& subnetMask); |
| 141 | + CORE_LIBRARY_DLL_SHARED_API std::string BuildBroadcastAddress(std::string const &address, std::string const &subnetMask); |
142 | 142 | #endif |
143 | 143 |
|
144 | 144 | // Check if 2 addresses (with their subnet masks) are on the same subnet. This is done by checking if the broadcast address |
145 | 145 | // for the address and subnet mask is the same as the broadcast address for the adapter address and adapter subnet mask. |
146 | 146 | #if defined(IS_CPP17) |
147 | | -CORE_LIBRARY_DLL_SHARED_API bool IsAddressAndNetmaskOnSameSubnetAsAdapter(std::string_view ipAddress, std::string_view netmask, |
148 | | - std::string_view adapterAddress, |
149 | | - std::string_view adapterNetmask); |
| 147 | + CORE_LIBRARY_DLL_SHARED_API bool IsAddressAndNetmaskOnSameSubnetAsAdapter(std::string_view ipAddress, std::string_view netmask, |
| 148 | + std::string_view adapterAddress, |
| 149 | + std::string_view adapterNetmask); |
150 | 150 | #else |
151 | | -CORE_LIBRARY_DLL_SHARED_API bool IsAddressAndNetmaskOnSameSubnetAsAdapter(std::string const& ipAddress, |
152 | | - std::string const& netmask, |
153 | | - std::string const& adapterAddress, |
154 | | - std::string const& adapterNetmask); |
| 151 | + CORE_LIBRARY_DLL_SHARED_API bool IsAddressAndNetmaskOnSameSubnetAsAdapter(std::string const &ipAddress, |
| 152 | + std::string const &netmask, |
| 153 | + std::string const &adapterAddress, |
| 154 | + std::string const &adapterNetmask); |
155 | 155 | #endif |
156 | 156 |
|
157 | 157 | // Function to look up the IP address and subnet mask for a given adapter name. The adapter name is the friendly name |
158 | 158 | // of the adapter, e.g. "Ethernet", "Wi-Fi" etc. |
159 | 159 | #if defined(IS_CPP17) |
160 | | -CORE_LIBRARY_DLL_SHARED_API std::pair<std::string, std::string> GetIpAddressAndNetmask(std::string_view adapterName); |
| 160 | + CORE_LIBRARY_DLL_SHARED_API std::pair<std::string, std::string> GetIpAddressAndNetmask(std::string_view adapterName); |
161 | 161 | #else |
162 | | -CORE_LIBRARY_DLL_SHARED_API std::pair<std::string, std::string> GetIpAddressAndNetmask(std::string const& adapterName); |
| 162 | + CORE_LIBRARY_DLL_SHARED_API std::pair<std::string, std::string> GetIpAddressAndNetmask(std::string const &adapterName); |
163 | 163 | #endif |
164 | 164 |
|
165 | | -CORE_LIBRARY_DLL_SHARED_API struct IPv4Address |
166 | | -{ |
167 | | - std::string address; |
168 | | - std::string netmask; |
169 | | -}; |
170 | | - |
171 | | -CORE_LIBRARY_DLL_SHARED_API struct IPv4Adapter |
172 | | -{ |
173 | | - std::string name; |
174 | | - bool supportsBroadcast{false}; |
175 | | - bool supportsMulticast{false}; |
176 | | - std::vector<IPv4Address> addresses; |
177 | | -}; |
178 | | - |
179 | | -enum class eInterfaceFilter |
180 | | -{ |
181 | | - All, |
182 | | - RealOnly |
183 | | -}; |
184 | | - |
185 | | -// Get a list of all the IPv4 adapters on the system. If filter is eInterfaceFilter::RealOnly then |
186 | | -// only return real adapters and not loopback, tunnel etc. interfaces. |
187 | | -CORE_LIBRARY_DLL_SHARED_API std::vector<IPv4Adapter> GetIPv4Adapters(eInterfaceFilter filter = eInterfaceFilter::All); |
188 | | - |
189 | | -} // namespace net_utils |
| 165 | + struct CORE_LIBRARY_DLL_SHARED_API IPv4Address |
| 166 | + { |
| 167 | + std::string address; |
| 168 | + std::string netmask; |
| 169 | + }; |
| 170 | + |
| 171 | + struct CORE_LIBRARY_DLL_SHARED_API IPv4Adapter |
| 172 | + { |
| 173 | + std::string name; |
| 174 | + bool supportsBroadcast{false}; |
| 175 | + bool supportsMulticast{false}; |
| 176 | + std::vector<IPv4Address> addresses; |
| 177 | + }; |
| 178 | + |
| 179 | + enum class eInterfaceFilter |
| 180 | + { |
| 181 | + All, |
| 182 | + RealOnly |
| 183 | + }; |
| 184 | + |
| 185 | + // Get a list of all the IPv4 adapters on the system. If filter is eInterfaceFilter::RealOnly then |
| 186 | + // only return real adapters and not loopback, tunnel etc. interfaces. |
| 187 | + CORE_LIBRARY_DLL_SHARED_API std::vector<IPv4Adapter> GetIPv4Adapters(eInterfaceFilter filter = eInterfaceFilter::All); |
| 188 | + |
| 189 | + } // namespace net_utils |
190 | 190 | } // namespace core_lib |
191 | 191 |
|
192 | 192 | #endif // HGLQTNETWORKUTILS_H |
0 commit comments