Skip to content

Commit f7f320f

Browse files
committed
Reworked NetworkUtils unit, fixed a compile isue in serialization functions
1 parent 8ed6bbd commit f7f320f

3 files changed

Lines changed: 267 additions & 153 deletions

File tree

Include/Asio/NetworkUtils.h

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,124 @@
2929
#include <string>
3030
#include <vector>
3131
#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
3436
#include "CoreLibraryDllGlobal.h"
3537

3638
namespace core_lib
3739
{
3840

39-
using ip_octets_t = std::vector<unsigned char>;
41+
using ip_octets_t = std::vector<uint8_t>;
4042

4143
// Give an IP address, e.g. 192.168.1.1, get a vector of the octets,
4244
// 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
4348
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
4460

4561
// Give a vector of octets, e.g. {192, 168, 1, 1}, get an IP address,
4662
// e.g. 192.168.1.1.
4763
CORE_LIBRARY_DLL_SHARED_API std::string IpAddressFromOctets(ip_octets_t const& octets);
4864

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+
4969
// 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
5076
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
5185

5286
// Check if a broadcast address is valid. Disallows multicast addresses.
5387
// 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
5491
CORE_LIBRARY_DLL_SHARED_API bool IsValidBroadcastAddress(std::string const& address);
92+
#endif
5593

56-
// Check if address is valid muticast group address.
94+
// Check if address is valid multicast group address.
5795
// 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
5899
CORE_LIBRARY_DLL_SHARED_API bool IsValidMulticastGroupAddress(std::string const& address);
100+
#endif
59101

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
61130
// 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
62136
CORE_LIBRARY_DLL_SHARED_API std::string BuildBroadcastAddress(std::string const& address, std::string const& subnetMask);
137+
#endif
63138

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
64144
CORE_LIBRARY_DLL_SHARED_API bool IsAddressAndNetmaskOnSameSubnetAsAdapter(std::string const& ipAddress,
65145
std::string const& netmask,
66146
std::string const& adapterAddress,
67147
std::string const& adapterNetmask);
148+
#endif
68149

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);
73150

74151
} // namespace core_lib
75152

Include/Serialization/SerializeToVector.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ char_vector_t ToCharVectorFlatBuf(const T& object, PackFunc packFunc)
413413
std::memcpy(out.data(), p, n);
414414
return out;
415415
}
416-
}
417416

418417
/*!
419418
* \brief Function to serialize object via flatbuffers

0 commit comments

Comments
 (0)