2017-06-13 05:14:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2022-06-28 11:29:07 +00:00
|
|
|
#include <cstring>
|
2018-12-13 13:41:47 +00:00
|
|
|
#include <algorithm>
|
2022-11-29 01:07:25 +00:00
|
|
|
#include <type_traits>
|
2018-12-13 13:41:47 +00:00
|
|
|
#include <utility>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/range.h>
|
2022-10-17 19:02:28 +00:00
|
|
|
#include <base/unaligned.h>
|
2023-03-06 14:53:58 +00:00
|
|
|
#include <base/hex.h>
|
2018-12-13 13:41:47 +00:00
|
|
|
#include <Common/StringUtils/StringUtils.h>
|
2017-06-13 05:14:24 +00:00
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
constexpr size_t IPV4_BINARY_LENGTH = 4;
|
|
|
|
constexpr size_t IPV6_BINARY_LENGTH = 16;
|
|
|
|
constexpr size_t IPV4_MAX_TEXT_LENGTH = 15; /// Does not count tail zero byte.
|
2022-03-23 15:11:46 +00:00
|
|
|
constexpr size_t IPV6_MAX_TEXT_LENGTH = 45; /// Does not count tail zero byte.
|
2017-06-13 05:14:24 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-12-07 21:29:17 +00:00
|
|
|
extern const std::array<std::pair<const char *, size_t>, 256> one_byte_to_string_lookup_table;
|
2017-06-13 05:14:24 +00:00
|
|
|
|
|
|
|
/** Rewritten inet_ntop6 from http://svn.apache.org/repos/asf/apr/apr/trunk/network_io/unix/inet_pton.c
|
|
|
|
* performs significantly faster than the reference implementation due to the absence of sprintf calls,
|
|
|
|
* bounds checking, unnecessary string copying and length calculation.
|
|
|
|
*/
|
2020-02-22 05:46:35 +00:00
|
|
|
void formatIPv6(const unsigned char * src, char *& dst, uint8_t zeroed_tail_bytes_count = 0);
|
2017-06-13 05:14:24 +00:00
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
/** Unsafe (no bounds-checking for src nor dst), optimized version of parsing IPv4 string.
|
|
|
|
*
|
2021-03-19 02:41:34 +00:00
|
|
|
* Parses the input string `src` and stores binary host-endian value into buffer pointed by `dst`,
|
2018-12-13 13:41:47 +00:00
|
|
|
* which should be long enough.
|
|
|
|
* That is "127.0.0.1" becomes 0x7f000001.
|
|
|
|
*
|
2022-12-09 21:20:52 +00:00
|
|
|
* In case of failure doesn't modify buffer pointed by `dst`.
|
2018-12-13 13:41:47 +00:00
|
|
|
*
|
2022-12-09 21:20:52 +00:00
|
|
|
* WARNING - this function is adapted to work with ReadBuffer, where src is the position reference (ReadBuffer::position())
|
2022-12-09 21:35:05 +00:00
|
|
|
* and eof is the ReadBuffer::eof() - therefore algorithm below does not rely on buffer's continuity.
|
2022-12-09 21:20:52 +00:00
|
|
|
* To parse strings use overloads below.
|
|
|
|
*
|
|
|
|
* @param src - iterator (reference to pointer) over input string - warning - continuity is not guaranteed.
|
|
|
|
* @param eof - function returning true if iterator riched the end - warning - can break iterator's continuity.
|
|
|
|
* @param dst - where to put output bytes, expected to be non-null and at IPV4_BINARY_LENGTH-long.
|
|
|
|
* @param first_octet - preparsed first octet
|
2022-12-09 21:35:05 +00:00
|
|
|
* @return - true if parsed successfully, false otherwise.
|
2018-12-13 13:41:47 +00:00
|
|
|
*/
|
2022-12-09 21:20:52 +00:00
|
|
|
template <typename T, typename EOFfunction>
|
2023-11-13 08:06:18 +00:00
|
|
|
requires (std::is_same_v<std::remove_cv_t<T>, char>)
|
2022-12-10 04:39:43 +00:00
|
|
|
inline bool parseIPv4(T * &src, EOFfunction eof, unsigned char * dst, int32_t first_octet = -1)
|
2018-12-13 13:41:47 +00:00
|
|
|
{
|
2022-12-09 21:20:52 +00:00
|
|
|
if (src == nullptr || first_octet > 255)
|
|
|
|
return false;
|
2022-11-15 21:49:35 +00:00
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
UInt32 result = 0;
|
2022-12-09 21:20:52 +00:00
|
|
|
int offset = 24;
|
|
|
|
if (first_octet >= 0)
|
|
|
|
{
|
2022-12-09 21:35:05 +00:00
|
|
|
result |= first_octet << offset;
|
2022-12-09 21:20:52 +00:00
|
|
|
offset -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; true; offset -= 8, ++src)
|
2018-12-13 13:41:47 +00:00
|
|
|
{
|
2022-12-09 21:20:52 +00:00
|
|
|
if (eof())
|
|
|
|
return false;
|
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
UInt32 value = 0;
|
|
|
|
size_t len = 0;
|
|
|
|
while (isNumericASCII(*src) && len <= 3)
|
|
|
|
{
|
|
|
|
value = value * 10 + (*src - '0');
|
|
|
|
++len;
|
|
|
|
++src;
|
2022-12-09 21:20:52 +00:00
|
|
|
if (eof())
|
2022-11-15 21:49:35 +00:00
|
|
|
break;
|
2018-12-13 13:41:47 +00:00
|
|
|
}
|
2022-12-09 21:20:52 +00:00
|
|
|
if (len == 0 || value > 255 || (offset > 0 && (eof() || *src != '.')))
|
|
|
|
return false;
|
2018-12-13 13:41:47 +00:00
|
|
|
result |= value << offset;
|
2022-12-09 21:20:52 +00:00
|
|
|
|
|
|
|
if (offset == 0)
|
|
|
|
break;
|
2018-12-13 13:41:47 +00:00
|
|
|
}
|
2022-11-15 21:49:35 +00:00
|
|
|
|
2023-03-03 14:20:18 +00:00
|
|
|
memcpy(dst, &result, sizeof(result));
|
2022-12-09 21:20:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-10 00:48:44 +00:00
|
|
|
/// returns pointer to the right after parsed sequence or null on failed parsing
|
|
|
|
inline const char * parseIPv4(const char * src, const char * end, unsigned char * dst)
|
2022-12-09 21:20:52 +00:00
|
|
|
{
|
2022-12-10 00:48:44 +00:00
|
|
|
if (parseIPv4(src, [&src, end](){ return src == end; }, dst))
|
|
|
|
return src;
|
|
|
|
return nullptr;
|
2022-11-15 21:49:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 00:48:44 +00:00
|
|
|
/// returns true if whole buffer was parsed successfully
|
|
|
|
inline bool parseIPv4whole(const char * src, const char * end, unsigned char * dst)
|
2022-11-15 21:49:35 +00:00
|
|
|
{
|
2022-12-10 00:48:44 +00:00
|
|
|
return parseIPv4(src, end, dst) == end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// returns pointer to the right after parsed sequence or null on failed parsing
|
|
|
|
inline const char * parseIPv4(const char * src, unsigned char * dst)
|
|
|
|
{
|
|
|
|
if (parseIPv4(src, [](){ return false; }, dst))
|
|
|
|
return src;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// returns true if whole null-terminated string was parsed successfully
|
|
|
|
inline bool parseIPv4whole(const char * src, unsigned char * dst)
|
|
|
|
{
|
|
|
|
const char * end = parseIPv4(src, dst);
|
|
|
|
return end != nullptr && *end == '\0';
|
2018-12-13 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Unsafe (no bounds-checking for src nor dst), optimized version of parsing IPv6 string.
|
|
|
|
*
|
2021-03-19 02:41:34 +00:00
|
|
|
* Parses the input string `src` and stores binary big-endian value into buffer pointed by `dst`,
|
2022-12-09 21:20:52 +00:00
|
|
|
* which should be long enough. In case of failure zeroes IPV6_BINARY_LENGTH bytes of buffer pointed by `dst`.
|
|
|
|
*
|
|
|
|
* WARNING - this function is adapted to work with ReadBuffer, where src is the position reference (ReadBuffer::position())
|
2022-12-09 21:35:05 +00:00
|
|
|
* and eof is the ReadBuffer::eof() - therefore algorithm below does not rely on buffer's continuity.
|
2022-12-09 21:20:52 +00:00
|
|
|
* To parse strings use overloads below.
|
2018-12-13 13:41:47 +00:00
|
|
|
*
|
2022-12-10 04:39:43 +00:00
|
|
|
* @param src - iterator (reference to pointer) over input string - warning - continuity is not guaranteed.
|
|
|
|
* @param eof - function returning true if iterator riched the end - warning - can break iterator's continuity.
|
|
|
|
* @param dst - where to put output bytes, expected to be non-null and at IPV6_BINARY_LENGTH-long.
|
|
|
|
* @param first_block - preparsed first block
|
|
|
|
* @return - true if parsed successfully, false otherwise.
|
2018-12-13 13:41:47 +00:00
|
|
|
*/
|
2022-12-09 21:20:52 +00:00
|
|
|
template <typename T, typename EOFfunction>
|
|
|
|
requires (std::is_same<typename std::remove_cv<T>::type, char>::value)
|
2022-12-10 04:39:43 +00:00
|
|
|
inline bool parseIPv6(T * &src, EOFfunction eof, unsigned char * dst, int32_t first_block = -1)
|
2018-12-13 13:41:47 +00:00
|
|
|
{
|
|
|
|
const auto clear_dst = [dst]()
|
|
|
|
{
|
2022-12-09 21:20:52 +00:00
|
|
|
std::memset(dst, '\0', IPV6_BINARY_LENGTH);
|
|
|
|
return false;
|
2018-12-13 13:41:47 +00:00
|
|
|
};
|
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
if (src == nullptr || eof())
|
2022-11-15 21:49:35 +00:00
|
|
|
return clear_dst();
|
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
int groups = 0; /// number of parsed groups
|
|
|
|
unsigned char * iter = dst; /// iterator over dst buffer
|
|
|
|
unsigned char * zptr = nullptr; /// pointer into dst buffer array where all-zeroes block ("::") is started
|
2018-12-13 13:41:47 +00:00
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
std::memset(dst, '\0', IPV6_BINARY_LENGTH);
|
2018-12-13 13:41:47 +00:00
|
|
|
|
2022-12-10 04:39:43 +00:00
|
|
|
if (first_block >= 0)
|
|
|
|
{
|
|
|
|
*iter++ = static_cast<unsigned char>((first_block >> 8) & 0xffu);
|
|
|
|
*iter++ = static_cast<unsigned char>(first_block & 0xffu);
|
2022-12-10 06:22:49 +00:00
|
|
|
if (*src == ':')
|
|
|
|
{
|
|
|
|
zptr = iter;
|
|
|
|
++src;
|
|
|
|
}
|
2022-12-10 04:39:43 +00:00
|
|
|
++groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool group_start = true;
|
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
while (!eof() && groups < 8)
|
2018-12-13 13:41:47 +00:00
|
|
|
{
|
2022-11-15 21:49:35 +00:00
|
|
|
if (*src == ':')
|
2018-12-13 13:41:47 +00:00
|
|
|
{
|
2022-12-09 21:20:52 +00:00
|
|
|
++src;
|
2022-12-09 21:35:05 +00:00
|
|
|
if (eof()) /// trailing colon is not allowed
|
2022-12-09 21:20:52 +00:00
|
|
|
return clear_dst();
|
2022-12-10 00:48:44 +00:00
|
|
|
|
|
|
|
group_start = true;
|
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
if (*src == ':')
|
2018-12-13 13:41:47 +00:00
|
|
|
{
|
2022-12-09 21:20:52 +00:00
|
|
|
if (zptr != nullptr) /// multiple all-zeroes blocks are not allowed
|
2018-12-13 13:41:47 +00:00
|
|
|
return clear_dst();
|
2022-12-09 21:20:52 +00:00
|
|
|
zptr = iter;
|
|
|
|
++src;
|
2018-12-13 13:41:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-12-09 21:20:52 +00:00
|
|
|
if (groups == 0) /// leading colon is not allowed
|
2018-12-13 13:41:47 +00:00
|
|
|
return clear_dst();
|
|
|
|
}
|
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
if (*src == '.') /// mixed IPv4 parsing
|
2018-12-13 13:41:47 +00:00
|
|
|
{
|
2022-12-09 21:20:52 +00:00
|
|
|
if (groups <= 1 && zptr == nullptr) /// IPv4 block can't be the first
|
|
|
|
return clear_dst();
|
|
|
|
|
2023-02-01 01:02:58 +00:00
|
|
|
if (group_start) /// first octet of IPv4 should be already parsed as an IPv6 group
|
|
|
|
return clear_dst();
|
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
++src;
|
|
|
|
if (eof())
|
|
|
|
return clear_dst();
|
|
|
|
|
|
|
|
/// last parsed group should be reinterpreted as a decimal value - it's the first octet of IPv4
|
|
|
|
--groups;
|
|
|
|
iter -= 2;
|
|
|
|
|
|
|
|
UInt16 num = 0;
|
|
|
|
for (int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
unsigned char first = (iter[i] >> 4) & 0x0fu;
|
|
|
|
unsigned char second = iter[i] & 0x0fu;
|
|
|
|
if (first > 9 || second > 9)
|
|
|
|
return clear_dst();
|
|
|
|
(num *= 100) += first * 10 + second;
|
|
|
|
}
|
|
|
|
if (num > 255)
|
|
|
|
return clear_dst();
|
|
|
|
|
|
|
|
/// parse IPv4 with known first octet
|
|
|
|
if (!parseIPv4(src, eof, iter, num))
|
2018-12-13 13:41:47 +00:00
|
|
|
return clear_dst();
|
2022-11-09 21:54:32 +00:00
|
|
|
|
2022-11-15 16:53:39 +00:00
|
|
|
if constexpr (std::endian::native == std::endian::little)
|
2022-12-09 21:20:52 +00:00
|
|
|
std::reverse(iter, iter + IPV4_BINARY_LENGTH);
|
2018-12-13 13:41:47 +00:00
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
iter += 4;
|
|
|
|
groups += 2;
|
|
|
|
break; /// IPv4 block is the last - end of parsing
|
2018-12-13 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 00:48:44 +00:00
|
|
|
if (!group_start) /// end of parsing
|
|
|
|
break;
|
|
|
|
group_start = false;
|
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
UInt16 val = 0; /// current decoded group
|
|
|
|
int xdigits = 0; /// number of decoded hex digits in current group
|
2018-12-13 13:41:47 +00:00
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
for (; !eof() && xdigits < 4; ++src, ++xdigits)
|
|
|
|
{
|
|
|
|
UInt8 num = unhex(*src);
|
|
|
|
if (num == 0xFF)
|
|
|
|
break;
|
|
|
|
(val <<= 4) |= num;
|
|
|
|
}
|
|
|
|
|
2022-12-10 00:48:44 +00:00
|
|
|
if (xdigits == 0) /// end of parsing
|
|
|
|
break;
|
2018-12-13 13:41:47 +00:00
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
*iter++ = static_cast<unsigned char>((val >> 8) & 0xffu);
|
|
|
|
*iter++ = static_cast<unsigned char>(val & 0xffu);
|
|
|
|
++groups;
|
2018-12-13 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
/// either all 8 groups or all-zeroes block should be present
|
|
|
|
if (groups < 8 && zptr == nullptr)
|
|
|
|
return clear_dst();
|
2018-12-13 13:41:47 +00:00
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
if (zptr != nullptr) /// process all-zeroes block
|
|
|
|
{
|
|
|
|
size_t msize = iter - zptr;
|
|
|
|
std::memmove(dst + IPV6_BINARY_LENGTH - msize, zptr, msize);
|
|
|
|
std::memset(zptr, '\0', IPV6_BINARY_LENGTH - (iter - dst));
|
2018-12-13 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 21:20:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-13 13:41:47 +00:00
|
|
|
|
2022-12-10 00:48:44 +00:00
|
|
|
/// returns pointer to the right after parsed sequence or null on failed parsing
|
|
|
|
inline const char * parseIPv6(const char * src, const char * end, unsigned char * dst)
|
|
|
|
{
|
|
|
|
if (parseIPv6(src, [&src, end](){ return src == end; }, dst))
|
|
|
|
return src;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// returns true if whole buffer was parsed successfully
|
|
|
|
inline bool parseIPv6whole(const char * src, const char * end, unsigned char * dst)
|
|
|
|
{
|
|
|
|
return parseIPv6(src, end, dst) == end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// returns pointer to the right after parsed sequence or null on failed parsing
|
|
|
|
inline const char * parseIPv6(const char * src, unsigned char * dst)
|
2022-12-09 21:20:52 +00:00
|
|
|
{
|
2022-12-10 00:48:44 +00:00
|
|
|
if (parseIPv6(src, [](){ return false; }, dst))
|
|
|
|
return src;
|
|
|
|
return nullptr;
|
2022-11-15 21:49:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 00:48:44 +00:00
|
|
|
/// returns true if whole null-terminated string was parsed successfully
|
|
|
|
inline bool parseIPv6whole(const char * src, unsigned char * dst)
|
2022-11-15 21:49:35 +00:00
|
|
|
{
|
2022-12-10 00:48:44 +00:00
|
|
|
const char * end = parseIPv6(src, dst);
|
|
|
|
return end != nullptr && *end == '\0';
|
2018-12-13 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 04:39:43 +00:00
|
|
|
/** Unsafe (no bounds-checking for src nor dst), optimized version of parsing IPv6 string.
|
|
|
|
*
|
|
|
|
* Parses the input string `src` IPv6 or possible IPv4 into IPv6 and stores binary big-endian value into buffer pointed by `dst`,
|
|
|
|
* which should be long enough. In case of failure zeroes IPV6_BINARY_LENGTH bytes of buffer pointed by `dst`.
|
|
|
|
*
|
|
|
|
* WARNING - this function is adapted to work with ReadBuffer, where src is the position reference (ReadBuffer::position())
|
|
|
|
* and eof is the ReadBuffer::eof() - therefore algorithm below does not rely on buffer's continuity.
|
|
|
|
*
|
|
|
|
* @param src - iterator (reference to pointer) over input string - warning - continuity is not guaranteed.
|
|
|
|
* @param eof - function returning true if iterator riched the end - warning - can break iterator's continuity.
|
|
|
|
* @param dst - where to put output bytes, expected to be non-null and at IPV6_BINARY_LENGTH-long.
|
|
|
|
* @return - true if parsed successfully, false otherwise.
|
|
|
|
*/
|
|
|
|
template <typename T, typename EOFfunction>
|
|
|
|
requires (std::is_same<typename std::remove_cv<T>::type, char>::value)
|
|
|
|
inline bool parseIPv6orIPv4(T * &src, EOFfunction eof, unsigned char * dst)
|
|
|
|
{
|
|
|
|
const auto clear_dst = [dst]()
|
|
|
|
{
|
|
|
|
std::memset(dst, '\0', IPV6_BINARY_LENGTH);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (src == nullptr)
|
|
|
|
return clear_dst();
|
|
|
|
|
|
|
|
bool leading_zero = false;
|
|
|
|
uint16_t val = 0;
|
|
|
|
int digits = 0;
|
|
|
|
/// parse up to 4 first digits as hexadecimal
|
|
|
|
for (; !eof() && digits < 4; ++src, ++digits)
|
|
|
|
{
|
|
|
|
if (*src == ':' || *src == '.')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (digits == 0 && *src == '0')
|
|
|
|
leading_zero = true;
|
|
|
|
|
|
|
|
UInt8 num = unhex(*src);
|
|
|
|
if (num == 0xFF)
|
|
|
|
return clear_dst();
|
|
|
|
(val <<= 4) |= num;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eof())
|
|
|
|
return clear_dst();
|
|
|
|
|
|
|
|
if (*src == ':') /// IPv6
|
|
|
|
{
|
|
|
|
if (digits == 0) /// leading colon - no preparsed group
|
|
|
|
return parseIPv6(src, eof, dst);
|
|
|
|
++src;
|
|
|
|
return parseIPv6(src, eof, dst, val); /// parse with first preparsed group
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*src == '.') /// IPv4
|
|
|
|
{
|
2022-12-10 06:22:49 +00:00
|
|
|
/// should has some digits
|
|
|
|
if (digits == 0)
|
|
|
|
return clear_dst();
|
|
|
|
/// should not has leading zeroes, should has no more than 3 digits
|
|
|
|
if ((leading_zero && digits > 1) || digits > 3)
|
2022-12-10 04:39:43 +00:00
|
|
|
return clear_dst();
|
|
|
|
|
|
|
|
/// recode first group as decimal
|
|
|
|
UInt16 num = 0;
|
|
|
|
for (int exp = 1; exp < 1000; exp *= 10)
|
|
|
|
{
|
|
|
|
int n = val & 0x0fu;
|
|
|
|
if (n > 9)
|
|
|
|
return clear_dst();
|
|
|
|
num += n * exp;
|
|
|
|
val >>= 4;
|
|
|
|
}
|
|
|
|
if (num > 255)
|
|
|
|
return clear_dst();
|
|
|
|
|
|
|
|
++src;
|
|
|
|
if (!parseIPv4(src, eof, dst, num)) /// try to parse as IPv4 with preparsed first octet
|
|
|
|
return clear_dst();
|
|
|
|
|
|
|
|
/// convert into IPv6
|
|
|
|
if constexpr (std::endian::native == std::endian::little)
|
|
|
|
{
|
|
|
|
dst[15] = dst[0]; dst[0] = 0;
|
|
|
|
dst[14] = dst[1]; dst[1] = 0;
|
|
|
|
dst[13] = dst[2]; dst[2] = 0;
|
|
|
|
dst[12] = dst[3]; dst[3] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dst[15] = dst[3]; dst[3] = 0;
|
|
|
|
dst[14] = dst[2]; dst[2] = 0;
|
|
|
|
dst[13] = dst[1]; dst[1] = 0;
|
|
|
|
dst[12] = dst[0]; dst[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst[11] = 0xff;
|
|
|
|
dst[10] = 0xff;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clear_dst();
|
|
|
|
}
|
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
/** Format 4-byte binary sequesnce as IPv4 text: 'aaa.bbb.ccc.ddd',
|
2020-08-08 01:01:47 +00:00
|
|
|
* expects in out to be in BE-format, that is 0x7f000001 => "127.0.0.1".
|
2018-12-13 13:41:47 +00:00
|
|
|
*
|
|
|
|
* Any number of the tail bytes can be masked with given mask string.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* src is IPV4_BINARY_LENGTH long,
|
|
|
|
* dst is IPV4_MAX_TEXT_LENGTH long,
|
|
|
|
* mask_tail_octets <= IPV4_BINARY_LENGTH
|
|
|
|
* mask_string is NON-NULL, if mask_tail_octets > 0.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* formatIPv4(&0x7f000001, dst, mask_tail_octets = 0, nullptr);
|
|
|
|
* > dst == "127.0.0.1"
|
|
|
|
* formatIPv4(&0x7f000001, dst, mask_tail_octets = 1, "xxx");
|
|
|
|
* > dst == "127.0.0.xxx"
|
|
|
|
* formatIPv4(&0x7f000001, dst, mask_tail_octets = 1, "0");
|
|
|
|
* > dst == "127.0.0.0"
|
|
|
|
*/
|
2022-11-21 07:45:42 +00:00
|
|
|
inline void formatIPv4(const unsigned char * src, size_t src_size, char *& dst, uint8_t mask_tail_octets = 0, const char * mask_string = "xxx")
|
2018-12-13 13:41:47 +00:00
|
|
|
{
|
|
|
|
const size_t mask_length = mask_string ? strlen(mask_string) : 0;
|
|
|
|
const size_t limit = std::min(IPV4_BINARY_LENGTH, IPV4_BINARY_LENGTH - mask_tail_octets);
|
2022-11-21 07:45:42 +00:00
|
|
|
const size_t padding = std::min(4 - src_size, limit);
|
|
|
|
|
|
|
|
for (size_t octet = 0; octet < padding; ++octet)
|
|
|
|
{
|
|
|
|
*dst++ = '0';
|
|
|
|
*dst++ = '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t octet = 4 - src_size; octet < limit; ++octet)
|
2018-12-13 13:41:47 +00:00
|
|
|
{
|
2022-11-09 05:12:47 +00:00
|
|
|
uint8_t value = 0;
|
2022-11-15 16:53:39 +00:00
|
|
|
if constexpr (std::endian::native == std::endian::little)
|
2022-11-09 05:12:47 +00:00
|
|
|
value = static_cast<uint8_t>(src[IPV4_BINARY_LENGTH - octet - 1]);
|
|
|
|
else
|
|
|
|
value = static_cast<uint8_t>(src[octet]);
|
2022-12-07 21:29:17 +00:00
|
|
|
const uint8_t len = one_byte_to_string_lookup_table[value].second;
|
|
|
|
const char* str = one_byte_to_string_lookup_table[value].first;
|
2018-12-13 13:41:47 +00:00
|
|
|
|
|
|
|
memcpy(dst, str, len);
|
|
|
|
dst += len;
|
|
|
|
*dst++ = '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t mask = 0; mask < mask_tail_octets; ++mask)
|
|
|
|
{
|
|
|
|
memcpy(dst, mask_string, mask_length);
|
|
|
|
dst += mask_length;
|
|
|
|
|
|
|
|
*dst++ = '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
dst[-1] = '\0';
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:45:42 +00:00
|
|
|
inline void formatIPv4(const unsigned char * src, char *& dst, uint8_t mask_tail_octets = 0, const char * mask_string = "xxx")
|
|
|
|
{
|
|
|
|
formatIPv4(src, 4, dst, mask_tail_octets, mask_string);
|
|
|
|
}
|
|
|
|
|
2017-06-13 05:14:24 +00:00
|
|
|
}
|