|
29 | 29 | #include <string> |
30 | 30 | #include <vector> |
31 | 31 | #include <cstdint> |
32 | | -#include <utility> |
33 | | -#include <boost/predef.h> |
| 32 | +#include "Platform/PlatformDefines.h" |
| 33 | +#if defined(IS_CPP17) |
| 34 | +#include <string_view> |
| 35 | +#endif |
34 | 36 | #include "CoreLibraryDllGlobal.h" |
35 | 37 |
|
36 | 38 | namespace core_lib |
37 | 39 | { |
38 | 40 |
|
39 | | -using ip_octets_t = std::vector<unsigned char>; |
| 41 | +using ip_octets_t = std::vector<uint8_t>; |
40 | 42 |
|
41 | 43 | // Give an IP address, e.g. 192.168.1.1, get a vector of the octets, |
42 | 44 | // e.g. {192, 168, 1, 1}. |
| 45 | +#if defined(IS_CPP17) |
| 46 | +CORE_LIBRARY_DLL_SHARED_API ip_octets_t OctetsFromIpAddress(std::string_view ipAddress); |
| 47 | +#else |
43 | 48 | CORE_LIBRARY_DLL_SHARED_API ip_octets_t OctetsFromIpAddress(std::string const& ipAddress); |
| 49 | +#endif |
| 50 | + |
| 51 | +// Give an IP address, e.g. 192.168.1.1, get a uint32_t |
| 52 | +// representation of the address, u32 from the addresses octets o[4] |
| 53 | +// as follows: |
| 54 | +// u32 = (o[0] << 24) + (o[1] << 16) + (o[2] << 9) + o[3] |
| 55 | +#if defined(IS_CPP17) |
| 56 | +CORE_LIBRARY_DLL_SHARED_API uint32_t Uint32FromIpAddress(std::string_view ipAddress); |
| 57 | +#else |
| 58 | +CORE_LIBRARY_DLL_SHARED_API uint32_t Uint32FromIpAddress(std::string const& ipAddress); |
| 59 | +#endif |
44 | 60 |
|
45 | 61 | // Give a vector of octets, e.g. {192, 168, 1, 1}, get an IP address, |
46 | 62 | // e.g. 192.168.1.1. |
47 | 63 | CORE_LIBRARY_DLL_SHARED_API std::string IpAddressFromOctets(ip_octets_t const& octets); |
48 | 64 |
|
| 65 | +// Given a uint32_t representing an IP address convert it |
| 66 | +// to the corresponding octets then convert to a string. |
| 67 | +CORE_LIBRARY_DLL_SHARED_API std::string IpAddressFromUint32(uint32_t ipValue); |
| 68 | + |
49 | 69 | // Check if an IP address is valid. Disallows multicast addresses. |
| 70 | +// 1.0.0.0 to 255.255.255.254 but excluding multicast |
| 71 | +// range, 224.0.0.0 to 239.255.255.255, an 0.0.0.0 and |
| 72 | +// 255.255.255.255. |
| 73 | +#if defined(IS_CPP17) |
| 74 | +CORE_LIBRARY_DLL_SHARED_API bool IsValidIpAddress(std::string_view address); |
| 75 | +#else |
50 | 76 | CORE_LIBRARY_DLL_SHARED_API bool IsValidIpAddress(std::string const& address); |
| 77 | +#endif |
| 78 | + |
| 79 | +// Check to see if the address is a valid subnet mask. |
| 80 | +#if defined(IS_CPP17) |
| 81 | +CORE_LIBRARY_DLL_SHARED_API bool IsValidSubnetMask(std::string_view subnetMask); |
| 82 | +#else |
| 83 | +bCORE_LIBRARY_DLL_SHARED_API ool IsValidSubnetMask(std::string const& subnetMask); |
| 84 | +#endif |
51 | 85 |
|
52 | 86 | // Check if a broadcast address is valid. Disallows multicast addresses. |
53 | 87 | // Can also be used to check if a subnet mask is valid. |
| 88 | +#if defined(IS_CPP17) |
| 89 | +CORE_LIBRARY_DLL_SHARED_API bool IsValidBroadcastAddress(std::string_view address); |
| 90 | +#else |
54 | 91 | CORE_LIBRARY_DLL_SHARED_API bool IsValidBroadcastAddress(std::string const& address); |
| 92 | +#endif |
55 | 93 |
|
56 | | -// Check if address is valid muticast group address. |
| 94 | +// Check if address is valid multicast group address. |
57 | 95 | // 224.0.0.0 to 239.255.255.255. |
| 96 | +#if defined(IS_CPP17) |
| 97 | +CORE_LIBRARY_DLL_SHARED_API bool IsValidMulticastGroupAddress(std::string_view address); |
| 98 | +#else |
58 | 99 | CORE_LIBRARY_DLL_SHARED_API bool IsValidMulticastGroupAddress(std::string const& address); |
| 100 | +#endif |
59 | 101 |
|
60 | | -// Given an IP address and subnet mask create the appropriat ebroadcast address, e.g. 160.50.100.76 |
| 102 | +// Convert a CIDR prefix value e.g. 16, 24 etc. |
| 103 | +// to the subnet mask, e.g. 255.255.0.0, 255.255.255.0 etc. |
| 104 | +// |
| 105 | +// Returns empty string is there is no valid conversion. |
| 106 | +CORE_LIBRARY_DLL_SHARED_API std::string CidrPrefixToSubnetMask(int32_t prefix); |
| 107 | + |
| 108 | +// Convert a subnet mask, e.g. 255.255.0.0 to |
| 109 | +// a CIDR prefix e.g. 16. |
| 110 | +// |
| 111 | +// Returns -1 if there is no valid conversion. |
| 112 | +#if defined(IS_CPP17) |
| 113 | +CORE_LIBRARY_DLL_SHARED_API int32_t SubnetMaskToCidrPrefix(std::string_view subnetMask); |
| 114 | +#else |
| 115 | +CORE_LIBRARY_DLL_SHARED_API int32_t SubnetMaskToCidrPrefix(std::string const& subnetMask); |
| 116 | +#endif |
| 117 | + |
| 118 | +// Create a CIDR address from an IP and subnet mask. |
| 119 | +// e.g. 192.168.10.1/255.255.0.0 becomes |
| 120 | +// 192.168.10.1/16 |
| 121 | +// |
| 122 | +// Throws std::invalid_argument upon error. |
| 123 | +#if defined(IS_CPP17) |
| 124 | +CORE_LIBRARY_DLL_SHARED_API std::string MakeCidrAddress(std::string_view address, std::string_view subnetMask); |
| 125 | +#else |
| 126 | +CORE_LIBRARY_DLL_SHARED_API std::string MakeCidrAddress(std::string const& address, std::string const& subnetMask); |
| 127 | +#endif |
| 128 | + |
| 129 | +// Given an IP address and subnet mask create the appropriate broadcast address, e.g. 160.50.100.76 |
61 | 130 | // and 255.255.0.0 becomes 160.50.255.255. |
| 131 | +// |
| 132 | +// Throws if an error occurs, std::invalid_argument |
| 133 | +#if defined(IS_CPP17) |
| 134 | +CORE_LIBRARY_DLL_SHARED_API std::string BuildBroadcastAddress(std::string_view address, std::string_view subnetMask); |
| 135 | +#else |
62 | 136 | CORE_LIBRARY_DLL_SHARED_API std::string BuildBroadcastAddress(std::string const& address, std::string const& subnetMask); |
| 137 | +#endif |
63 | 138 |
|
| 139 | +#if defined(IS_CPP17) |
| 140 | +CORE_LIBRARY_DLL_SHARED_API bool IsAddressAndNetmaskOnSameSubnetAsAdapter(std::string_view ipAddress, std::string_view netmask, |
| 141 | + std::string_view adapterAddress, |
| 142 | + std::string_view adapterNetmask); |
| 143 | +#else |
64 | 144 | CORE_LIBRARY_DLL_SHARED_API bool IsAddressAndNetmaskOnSameSubnetAsAdapter(std::string const& ipAddress, |
65 | 145 | std::string const& netmask, |
66 | 146 | std::string const& adapterAddress, |
67 | 147 | std::string const& adapterNetmask); |
| 148 | +#endif |
68 | 149 |
|
69 | | -CORE_LIBRARY_DLL_SHARED_API std::string ConvertToCIDRAddress(std::string const& ipAddress, std::string const& netmask); |
70 | | - |
71 | | -// This functions require root access in Linux and Administartor access in Windows. |
72 | | -CORE_LIBRARY_DLL_SHARED_API std::pair<std::string, std::string> GetIpAddressAndNetmask(std::string const& adapterName); |
73 | 150 |
|
74 | 151 | } // namespace core_lib |
75 | 152 |
|
|
0 commit comments