|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Richard Plny <[email protected]> |
| 4 | + * @brief IP prefix interface |
| 5 | + * Based on: |
| 6 | + * https://github.com/CESNET/nemea-modules-ng/blob/main/modules/whitelist/src/ipAddressPrefix.hpp |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: BSD-3-Clause |
| 9 | + */ |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include "wif/storage/ipAddress.hpp" |
| 14 | + |
| 15 | +namespace WIF { |
| 16 | + |
| 17 | +/** |
| 18 | + * @brief Class representing an IP prefix (or an IP subnet) |
| 19 | + */ |
| 20 | +class IpPrefix { |
| 21 | +public: |
| 22 | + /** |
| 23 | + * @brief Max bit lenth of IPv4 prefix |
| 24 | + */ |
| 25 | + static constexpr size_t IPV4_MAX_PREFIX_LENGTH = 32; |
| 26 | + |
| 27 | + /** |
| 28 | + * @brief Max bit lenth of IPv6 prefix |
| 29 | + */ |
| 30 | + static constexpr size_t IPV6_MAX_PREFIX_LENGTH = 128; |
| 31 | + |
| 32 | + /** |
| 33 | + * @brief Construct a new IP Prefix object representing just 1 IP address |
| 34 | + * |
| 35 | + * @param address single IP address |
| 36 | + */ |
| 37 | + IpPrefix(const IpAddress& address); |
| 38 | + |
| 39 | + /** |
| 40 | + * @brief Construct a new IP Prefix object |
| 41 | + * @param addressStr string representation of IP prefix |
| 42 | + * @param prefixLength number of bits of the prefix |
| 43 | + */ |
| 44 | + IpPrefix(const std::string& addressStr, size_t prefixLength); |
| 45 | + |
| 46 | + /** |
| 47 | + * @brief Construct a new IP Prefix object |
| 48 | + * @param address IP prefix |
| 49 | + * @param prefixLength number of bits of the prefix |
| 50 | + */ |
| 51 | + IpPrefix(const IpAddress& address, size_t prefixLength); |
| 52 | + |
| 53 | + /** |
| 54 | + * @brief Getter for the number of bits used by prefix |
| 55 | + * @return size_t number of bits of the prefix |
| 56 | + */ |
| 57 | + size_t prefixLength() const noexcept { return m_prefixLength; } |
| 58 | + |
| 59 | + /** |
| 60 | + * @brief Getter for number of IP addresses in the prefix |
| 61 | + * @return size_t number of IP addresses in the prefix |
| 62 | + */ |
| 63 | + size_t size() const noexcept; |
| 64 | + |
| 65 | + /** |
| 66 | + * @brief Getter for the prefix address |
| 67 | + * @return const IpAddress& prefix address |
| 68 | + */ |
| 69 | + const IpAddress& getPrefix() const noexcept { return m_prefixAddress; } |
| 70 | + |
| 71 | + /** |
| 72 | + * @brief Getter for the prefix mask |
| 73 | + * @return const IpAddress& prefix mask |
| 74 | + */ |
| 75 | + const IpAddress& getMask() const noexcept { return m_prefixMask; } |
| 76 | + |
| 77 | + /** |
| 78 | + * @brief Match IP prefix and check if IP address in part of the prefix |
| 79 | + * @param ipAddress to be matched |
| 80 | + * @return true if ipAddress is in this prefix |
| 81 | + * @return false otherwise |
| 82 | + */ |
| 83 | + bool match(const IpAddress& ipAddress) const noexcept; |
| 84 | + |
| 85 | + /** |
| 86 | + * @brief Comparison operator < |
| 87 | + * @param l left operand |
| 88 | + * @param r right operand |
| 89 | + * @return bool true if l is less than r |
| 90 | + */ |
| 91 | + friend bool operator<(const IpPrefix& l, const IpPrefix& r); |
| 92 | + |
| 93 | + /** |
| 94 | + * @brief Comparison operator < |
| 95 | + * Used for binary search of IP address in a vector of IP prefixes |
| 96 | + * @param l left operand |
| 97 | + * @param r right operand |
| 98 | + * @return bool true if l is less than r |
| 99 | + */ |
| 100 | + friend bool operator<(const IpAddress& l, const IpPrefix& r); |
| 101 | + |
| 102 | + /** |
| 103 | + * @brief Comparison operator < |
| 104 | + * Used for binary search of IP address in a vector of IP prefixes |
| 105 | + * @param l left operand |
| 106 | + * @param r right operand |
| 107 | + * @return bool true if l is less than r |
| 108 | + */ |
| 109 | + friend bool operator<(const IpPrefix& l, const IpAddress& r); |
| 110 | + |
| 111 | +private: |
| 112 | + IpAddress m_prefixAddress; |
| 113 | + IpAddress m_prefixMask; |
| 114 | + size_t m_prefixLength; |
| 115 | +}; |
| 116 | + |
| 117 | +} // namespace WIF |
0 commit comments