2019-06-15 14:20:32 +00:00
|
|
|
#include "IPv6ToBinary.h"
|
|
|
|
#include <Poco/Net/IPAddress.h>
|
2020-11-08 16:01:12 +00:00
|
|
|
#include <Poco/ByteOrder.h>
|
|
|
|
|
2020-11-29 17:54:46 +00:00
|
|
|
#include <Common/formatIPv6.h>
|
|
|
|
|
2019-06-19 17:31:46 +00:00
|
|
|
#include <cstring>
|
2022-07-31 14:34:05 +00:00
|
|
|
#include <bit>
|
2019-06-15 14:20:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-12-01 19:12:11 +00:00
|
|
|
/// Result array could be indexed with all possible uint8 values without extra check.
|
|
|
|
/// For values greater than 128 we will store same value as for 128 (all bits set).
|
2020-11-29 17:54:46 +00:00
|
|
|
constexpr size_t IPV6_MASKS_COUNT = 256;
|
2021-03-19 07:24:38 +00:00
|
|
|
using RawMaskArrayV6 = std::array<uint8_t, IPV6_BINARY_LENGTH>;
|
2020-11-29 17:54:46 +00:00
|
|
|
|
2020-11-21 14:56:58 +00:00
|
|
|
void IPv6ToRawBinary(const Poco::Net::IPAddress & address, char * res)
|
2019-06-15 14:20:32 +00:00
|
|
|
{
|
|
|
|
if (Poco::Net::IPAddress::IPv6 == address.family())
|
|
|
|
{
|
2020-11-21 14:56:58 +00:00
|
|
|
memcpy(res, address.addr(), 16);
|
2019-06-15 14:20:32 +00:00
|
|
|
}
|
|
|
|
else if (Poco::Net::IPAddress::IPv4 == address.family())
|
|
|
|
{
|
|
|
|
/// Convert to IPv6-mapped address.
|
2020-11-21 14:56:58 +00:00
|
|
|
memset(res, 0, 10);
|
2019-06-15 14:20:32 +00:00
|
|
|
res[10] = '\xFF';
|
|
|
|
res[11] = '\xFF';
|
|
|
|
memcpy(&res[12], address.addr(), 4);
|
|
|
|
}
|
|
|
|
else
|
2020-11-21 14:56:58 +00:00
|
|
|
memset(res, 0, 16);
|
|
|
|
}
|
2019-06-15 14:20:32 +00:00
|
|
|
|
2020-11-21 14:56:58 +00:00
|
|
|
std::array<char, 16> IPv6ToBinary(const Poco::Net::IPAddress & address)
|
|
|
|
{
|
|
|
|
std::array<char, 16> res;
|
|
|
|
IPv6ToRawBinary(address, res.data());
|
2019-06-15 14:20:32 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-03-19 07:24:38 +00:00
|
|
|
template <typename RawMaskArrayT>
|
|
|
|
static constexpr RawMaskArrayT generateBitMask(size_t prefix)
|
2020-12-01 17:38:49 +00:00
|
|
|
{
|
2021-03-19 07:24:38 +00:00
|
|
|
RawMaskArrayT arr{0};
|
|
|
|
if (prefix >= arr.size() * 8)
|
|
|
|
prefix = arr.size() * 8;
|
2020-11-29 17:54:46 +00:00
|
|
|
size_t i = 0;
|
|
|
|
for (; prefix >= 8; ++i, prefix -= 8)
|
|
|
|
arr[i] = 0xff;
|
|
|
|
if (prefix > 0)
|
|
|
|
arr[i++] = ~(0xff >> prefix);
|
2021-03-19 07:24:38 +00:00
|
|
|
while (i < arr.size())
|
2020-11-29 17:54:46 +00:00
|
|
|
arr[i++] = 0x00;
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2021-03-19 07:24:38 +00:00
|
|
|
template <typename RawMaskArrayT, size_t masksCount>
|
|
|
|
static constexpr std::array<RawMaskArrayT, masksCount> generateBitMasks()
|
2020-12-01 17:38:49 +00:00
|
|
|
{
|
2021-03-19 07:24:38 +00:00
|
|
|
std::array<RawMaskArrayT, masksCount> arr{};
|
|
|
|
for (size_t i = 0; i < masksCount; ++i)
|
|
|
|
arr[i] = generateBitMask<RawMaskArrayT>(i);
|
2020-11-29 17:54:46 +00:00
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2021-03-19 07:24:38 +00:00
|
|
|
const std::array<uint8_t, 16> & getCIDRMaskIPv6(UInt8 prefix_len)
|
2020-11-29 17:54:46 +00:00
|
|
|
{
|
2021-03-19 07:24:38 +00:00
|
|
|
static constexpr auto IPV6_RAW_MASK_ARRAY = generateBitMasks<RawMaskArrayV6, IPV6_MASKS_COUNT>();
|
|
|
|
return IPV6_RAW_MASK_ARRAY[prefix_len];
|
|
|
|
}
|
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
bool matchIPv4Subnet(UInt32 addr, UInt32 cidr_addr, UInt8 prefix)
|
2021-03-19 07:24:38 +00:00
|
|
|
{
|
2021-03-29 09:04:05 +00:00
|
|
|
UInt32 mask = (prefix >= 32) ? 0xffffffffu : ~(0xffffffffu >> prefix);
|
|
|
|
return (addr & mask) == (cidr_addr & mask);
|
2020-11-29 17:54:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
#if defined(__SSE2__)
|
|
|
|
#include <emmintrin.h>
|
|
|
|
|
|
|
|
bool matchIPv6Subnet(const uint8_t * addr, const uint8_t * cidr_addr, UInt8 prefix)
|
|
|
|
{
|
|
|
|
uint16_t mask = _mm_movemask_epi8(_mm_cmpeq_epi8(
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(addr)),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(cidr_addr))));
|
|
|
|
mask = ~mask;
|
|
|
|
|
|
|
|
if (mask)
|
|
|
|
{
|
2022-07-31 14:34:05 +00:00
|
|
|
auto offset = std::countr_zero(mask);
|
2021-03-29 09:04:05 +00:00
|
|
|
|
|
|
|
if (prefix / 8 != offset)
|
|
|
|
return prefix / 8 < offset;
|
|
|
|
|
|
|
|
auto cmpmask = ~(0xff >> (prefix % 8));
|
|
|
|
return (addr[offset] & cmpmask) == (cidr_addr[offset] & cmpmask);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
# else
|
|
|
|
|
|
|
|
bool matchIPv6Subnet(const uint8_t * addr, const uint8_t * cidr_addr, UInt8 prefix)
|
|
|
|
{
|
|
|
|
if (prefix > IPV6_BINARY_LENGTH * 8U)
|
|
|
|
prefix = IPV6_BINARY_LENGTH * 8U;
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
for (; prefix >= 8; ++i, prefix -= 8)
|
|
|
|
{
|
2021-03-29 12:50:34 +00:00
|
|
|
if (addr[i] != cidr_addr[i])
|
2021-03-29 09:04:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (prefix == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto mask = ~(0xff >> prefix);
|
|
|
|
return (addr[i] & mask) == (cidr_addr[i] & mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // __SSE2__
|
|
|
|
|
2019-06-15 14:20:32 +00:00
|
|
|
}
|