1
0
mirror of https://github.com/pavel-odintsov/fastnetmon synced 2024-11-23 09:12:14 +01:00

Polished network structures and removed unused

This commit is contained in:
Pavel Odintsov 2023-07-09 12:57:25 +01:00
parent 1a8ed44b61
commit b53458d53f

@ -18,11 +18,6 @@
// IPv6 subnet with mask in cidr form
class subnet_ipv6_cidr_mask_t {
public:
subnet_ipv6_cidr_mask_t() {
// Fill subnet by zeros
memset(&this->subnet_address, 0, sizeof(in6_addr));
}
void set_cidr_prefix_length(uint32_t cidr_prefix_length) {
this->cidr_prefix_length = cidr_prefix_length;
}
@ -32,13 +27,13 @@ class subnet_ipv6_cidr_mask_t {
memcpy(&subnet_address, ipv6_host_address_param, sizeof(in6_addr));
}
template <class Archive> void serialize(Archive& ar, const unsigned int version) {
// Boost does not know how to serialize in6_addr but nested s6_addr is a just array with 16 elements of char and we do serialzie it instead
template <class Archive> void serialize(Archive& ar, [[maybe_unused]] const unsigned int version) {
// Boost does not know how to serialize in6_addr but nested s6_addr is a just array with 16 elements of char and we do serialize it instead
ar& BOOST_SERIALIZATION_NVP(subnet_address.s6_addr);
ar& BOOST_SERIALIZATION_NVP(cidr_prefix_length);
}
in6_addr subnet_address;
in6_addr subnet_address{};
uint32_t cidr_prefix_length = 128;
};
@ -50,7 +45,7 @@ inline bool operator<(const subnet_ipv6_cidr_mask_t& lhs, const subnet_ipv6_cidr
return true;
} else if (lhs.cidr_prefix_length == rhs.cidr_prefix_length) {
// Compare addresses as memory blocks
// Order may be incorrect (desc vs asc)
// Order may be incorrect (descending vs ascending)
return memcmp(&lhs.subnet_address, &rhs.subnet_address, sizeof(in6_addr)) < 0;
} else {
return false;
@ -62,7 +57,7 @@ inline bool operator<(const subnet_ipv6_cidr_mask_t& lhs, const subnet_ipv6_cidr
namespace std {
template <> struct hash<subnet_ipv6_cidr_mask_t> {
typedef std::size_t result_type;
std::size_t operator()(subnet_ipv6_cidr_mask_t const& s) const {
std::size_t operator()(const subnet_ipv6_cidr_mask_t& s) const {
std::size_t seed = 0;
const uint8_t* b = s.subnet_address.s6_addr;
@ -92,6 +87,15 @@ inline bool operator!=(const subnet_ipv6_cidr_mask_t& lhs, const subnet_ipv6_cid
return !(lhs == rhs);
}
// Compares for standard IPv6 structure
inline bool operator==(const in6_addr& lhs, const in6_addr& rhs) {
return memcmp(&lhs, &rhs, sizeof(in6_addr)) == 0;
}
inline bool operator!=(const in6_addr& lhs, const in6_addr& rhs) {
return !(lhs == rhs);
}
// Subnet with cidr mask
class subnet_cidr_mask_t {
public:
@ -132,7 +136,7 @@ class subnet_cidr_mask_t {
this->cidr_prefix_length = cidr_prefix_length;
}
template <class Archive> void serialize(Archive& ar, const unsigned int version) {
template <class Archive> void serialize(Archive& ar, [[maybe_unused]] const unsigned int version) {
ar& BOOST_SERIALIZATION_NVP(subnet_address);
ar& BOOST_SERIALIZATION_NVP(cidr_prefix_length);
}
@ -144,7 +148,6 @@ class subnet_cidr_mask_t {
uint32_t cidr_prefix_length = 0;
};
// TODO: move to .cpp file!!!
inline bool operator==(const subnet_cidr_mask_t& lhs, const subnet_cidr_mask_t& rhs) {
// Prefixes has different lengths
if (lhs.cidr_prefix_length != rhs.cidr_prefix_length) {
@ -158,8 +161,7 @@ inline bool operator!=(const subnet_cidr_mask_t& lhs, const subnet_cidr_mask_t&
return !(lhs == rhs);
}
// TODO: move to .cpp file!!!
// We need free function for comparision code
// We need free function for comparison code
inline bool operator<(const subnet_cidr_mask_t& lhs, const subnet_cidr_mask_t& rhs) {
if (lhs.cidr_prefix_length < rhs.cidr_prefix_length) {
return true;
@ -170,28 +172,13 @@ inline bool operator<(const subnet_cidr_mask_t& lhs, const subnet_cidr_mask_t& r
}
}
// Subnet with binary mask
class subnet_binary_netmask_t {
public:
subnet_binary_netmask_t() {
this->subnet_address = 0;
this->subnet_binary_mask = 0;
}
subnet_binary_netmask_t(uint32_t subnet_address, uint32_t subnet_binary_mask) {
this->subnet_address = subnet_address;
this->subnet_binary_mask = subnet_binary_mask;
}
uint32_t subnet_address = 0;
uint32_t subnet_binary_mask = 0;
};
namespace std {
// Inject custom specialization of std::hash in namespace std
// We need it for std::unordered_map
template <> struct hash<subnet_cidr_mask_t> {
typedef std::size_t result_type;
std::size_t operator()(subnet_cidr_mask_t const& s) const {
std::size_t operator()(const subnet_cidr_mask_t& s) const {
std::size_t seed = 0;
boost::hash_combine(seed, s.cidr_prefix_length);
@ -202,3 +189,12 @@ template <> struct hash<subnet_cidr_mask_t> {
};
} // namespace std
// This class can keep any network
class subnet_ipv4_ipv6_cidr_t {
public:
subnet_cidr_mask_t ipv4{};
subnet_ipv6_cidr_mask_t ipv6{};
bool is_ipv6 = false;
};