2021-02-17 08:57:48 +00:00
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Common/IPv6ToBinary.h>
|
|
|
|
#include <Common/formatIPv6.h>
|
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2021-02-17 08:57:48 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <variant>
|
2021-03-29 09:04:05 +00:00
|
|
|
#include <charconv>
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
|
2022-04-27 15:05:45 +00:00
|
|
|
#include <Common/logger_useful.h>
|
2021-03-29 09:04:05 +00:00
|
|
|
namespace DB::ErrorCodes
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
2021-03-29 09:04:05 +00:00
|
|
|
extern const int CANNOT_PARSE_TEXT;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2021-03-29 09:48:12 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
namespace
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
class IPAddressVariant
|
|
|
|
{
|
|
|
|
public:
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2022-07-18 17:31:34 +00:00
|
|
|
explicit IPAddressVariant(std::string_view address_str)
|
2021-03-29 09:04:05 +00:00
|
|
|
{
|
2021-03-29 09:48:12 +00:00
|
|
|
/// IP address parser functions require that the input is
|
|
|
|
/// NULL-terminated so we need to copy it.
|
2021-03-29 09:04:05 +00:00
|
|
|
const auto address_str_copy = std::string(address_str);
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
UInt32 v4;
|
|
|
|
if (DB::parseIPv4(address_str_copy.c_str(), reinterpret_cast<unsigned char *>(&v4)))
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
2021-03-29 09:04:05 +00:00
|
|
|
addr = v4;
|
|
|
|
}
|
|
|
|
else
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
2021-03-29 09:04:05 +00:00
|
|
|
addr = IPv6AddrType();
|
|
|
|
bool success = DB::parseIPv6(address_str_copy.c_str(), std::get<IPv6AddrType>(addr).data());
|
|
|
|
if (!success)
|
|
|
|
throw DB::Exception("Neither IPv4 nor IPv6 address: '" + address_str_copy + "'",
|
|
|
|
DB::ErrorCodes::CANNOT_PARSE_TEXT);
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
2021-03-29 09:04:05 +00:00
|
|
|
}
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
UInt32 asV4() const
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
2021-03-29 09:04:05 +00:00
|
|
|
if (const auto * val = std::get_if<IPv4AddrType>(&addr))
|
|
|
|
return *val;
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
const uint8_t * asV6() const
|
|
|
|
{
|
|
|
|
if (const auto * val = std::get_if<IPv6AddrType>(&addr))
|
|
|
|
return val->data();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
private:
|
|
|
|
using IPv4AddrType = UInt32;
|
|
|
|
using IPv6AddrType = std::array<uint8_t, IPV6_BINARY_LENGTH>;
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
std::variant<IPv4AddrType, IPv6AddrType> addr;
|
|
|
|
};
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
struct IPAddressCIDR
|
|
|
|
{
|
|
|
|
IPAddressVariant address;
|
|
|
|
UInt8 prefix;
|
|
|
|
};
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2022-07-17 15:22:12 +00:00
|
|
|
IPAddressCIDR parseIPWithCIDR(std::string_view cidr_str)
|
2021-03-29 09:04:05 +00:00
|
|
|
{
|
2022-07-17 15:22:12 +00:00
|
|
|
size_t pos_slash = cidr_str.find('/');
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
if (pos_slash == 0)
|
|
|
|
throw DB::Exception("Error parsing IP address with prefix: " + std::string(cidr_str), DB::ErrorCodes::CANNOT_PARSE_TEXT);
|
|
|
|
if (pos_slash == std::string_view::npos)
|
|
|
|
throw DB::Exception("The text does not contain '/': " + std::string(cidr_str), DB::ErrorCodes::CANNOT_PARSE_TEXT);
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2022-07-17 15:22:12 +00:00
|
|
|
std::string_view addr_str = cidr_str.substr(0, pos_slash);
|
2022-07-18 17:31:34 +00:00
|
|
|
IPAddressVariant addr(addr_str);
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 12:50:34 +00:00
|
|
|
uint8_t prefix = 0;
|
2022-07-17 15:22:12 +00:00
|
|
|
auto prefix_str = cidr_str.substr(pos_slash+1);
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
const auto * prefix_str_end = prefix_str.data() + prefix_str.size();
|
|
|
|
auto [parse_end, parse_error] = std::from_chars(prefix_str.data(), prefix_str_end, prefix);
|
2021-03-29 12:50:34 +00:00
|
|
|
uint8_t max_prefix = (addr.asV6() ? IPV6_BINARY_LENGTH : IPV4_BINARY_LENGTH) * 8;
|
2021-03-29 09:04:05 +00:00
|
|
|
bool has_error = parse_error != std::errc() || parse_end != prefix_str_end || prefix > max_prefix;
|
|
|
|
if (has_error)
|
|
|
|
throw DB::Exception("The CIDR has a malformed prefix bits: " + std::string(cidr_str), DB::ErrorCodes::CANNOT_PARSE_TEXT);
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 12:50:34 +00:00
|
|
|
return {addr, static_cast<UInt8>(prefix)};
|
2021-03-29 09:04:05 +00:00
|
|
|
}
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:04:05 +00:00
|
|
|
inline bool isAddressInRange(const IPAddressVariant & address, const IPAddressCIDR & cidr)
|
|
|
|
{
|
|
|
|
if (const auto * cidr_v6 = cidr.address.asV6())
|
|
|
|
{
|
|
|
|
if (const auto * addr_v6 = address.asV6())
|
|
|
|
return DB::matchIPv6Subnet(addr_v6, cidr_v6, cidr.prefix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!address.asV6())
|
|
|
|
return DB::matchIPv4Subnet(address.asV4(), cidr.address.asV4(), cidr.prefix);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-17 08:57:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-03-29 09:48:12 +00:00
|
|
|
class FunctionIsIPAddressContainedIn : public IFunction
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-03-29 09:56:11 +00:00
|
|
|
static constexpr auto name = "isIPAddressInRange";
|
2021-03-29 09:48:12 +00:00
|
|
|
String getName() const override { return name; }
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionIsIPAddressContainedIn>(); }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:48:12 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & /* return_type */, size_t input_rows_count) const override
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
|
|
|
const IColumn * col_addr = arguments[0].column.get();
|
|
|
|
const IColumn * col_cidr = arguments[1].column.get();
|
|
|
|
|
|
|
|
if (const auto * col_addr_const = checkAndGetAnyColumnConst(col_addr))
|
|
|
|
{
|
|
|
|
if (const auto * col_cidr_const = checkAndGetAnyColumnConst(col_cidr))
|
2021-03-29 09:48:12 +00:00
|
|
|
return executeImpl(*col_addr_const, *col_cidr_const, input_rows_count);
|
2021-02-17 08:57:48 +00:00
|
|
|
else
|
2021-03-29 09:48:12 +00:00
|
|
|
return executeImpl(*col_addr_const, *col_cidr, input_rows_count);
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (const auto * col_cidr_const = checkAndGetAnyColumnConst(col_cidr))
|
2021-03-29 09:48:12 +00:00
|
|
|
return executeImpl(*col_addr, *col_cidr_const, input_rows_count);
|
2021-02-17 08:57:48 +00:00
|
|
|
else
|
|
|
|
return executeImpl(*col_addr, *col_cidr, input_rows_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 15:44:19 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
2021-03-29 09:48:12 +00:00
|
|
|
if (arguments.size() != 2)
|
|
|
|
throw Exception(
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed " + toString(arguments.size()) + ", should be 2",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
const DataTypePtr & addr_type = arguments[0];
|
|
|
|
const DataTypePtr & prefix_type = arguments[1];
|
|
|
|
|
|
|
|
if (!isString(addr_type) || !isString(prefix_type))
|
|
|
|
throw Exception("The arguments of function " + getName() + " must be String",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 15:44:19 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
2021-03-29 09:48:12 +00:00
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
|
2021-02-17 08:57:48 +00:00
|
|
|
private:
|
2021-03-29 09:48:12 +00:00
|
|
|
/// Like checkAndGetColumnConst() but this function doesn't
|
|
|
|
/// care about the type of data column.
|
2021-02-17 08:57:48 +00:00
|
|
|
static const ColumnConst * checkAndGetAnyColumnConst(const IColumn * column)
|
|
|
|
{
|
|
|
|
if (!column || !isColumnConst(*column))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return assert_cast<const ColumnConst *>(column);
|
|
|
|
}
|
|
|
|
|
2021-03-29 09:48:12 +00:00
|
|
|
/// Both columns are constant.
|
2021-03-29 12:50:34 +00:00
|
|
|
static ColumnPtr executeImpl(
|
2021-03-29 09:48:12 +00:00
|
|
|
const ColumnConst & col_addr_const,
|
|
|
|
const ColumnConst & col_cidr_const,
|
2021-03-29 12:50:34 +00:00
|
|
|
size_t input_rows_count)
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
|
|
|
const auto & col_addr = col_addr_const.getDataColumn();
|
|
|
|
const auto & col_cidr = col_cidr_const.getDataColumn();
|
|
|
|
|
2022-07-18 17:31:34 +00:00
|
|
|
const auto addr = IPAddressVariant(col_addr.getDataAt(0).toView());
|
2022-07-17 15:22:12 +00:00
|
|
|
const auto cidr = parseIPWithCIDR(col_cidr.getDataAt(0).toView());
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:48:12 +00:00
|
|
|
ColumnUInt8::MutablePtr col_res = ColumnUInt8::create(1);
|
|
|
|
ColumnUInt8::Container & vec_res = col_res->getData();
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:48:12 +00:00
|
|
|
vec_res[0] = isAddressInRange(addr, cidr) ? 1 : 0;
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:48:12 +00:00
|
|
|
return ColumnConst::create(std::move(col_res), input_rows_count);
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 09:48:12 +00:00
|
|
|
/// Address is constant.
|
2021-03-29 12:50:34 +00:00
|
|
|
static ColumnPtr executeImpl(const ColumnConst & col_addr_const, const IColumn & col_cidr, size_t input_rows_count)
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
|
|
|
const auto & col_addr = col_addr_const.getDataColumn();
|
|
|
|
|
2022-07-18 17:31:34 +00:00
|
|
|
const auto addr = IPAddressVariant(col_addr.getDataAt(0).toView());
|
2021-03-29 09:48:12 +00:00
|
|
|
|
|
|
|
ColumnUInt8::MutablePtr col_res = ColumnUInt8::create(input_rows_count);
|
|
|
|
ColumnUInt8::Container & vec_res = col_res->getData();
|
|
|
|
|
2021-12-20 12:55:07 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
2022-07-17 15:22:12 +00:00
|
|
|
const auto cidr = parseIPWithCIDR(col_cidr.getDataAt(i).toView());
|
2021-03-29 09:48:12 +00:00
|
|
|
vec_res[i] = isAddressInRange(addr, cidr) ? 1 : 0;
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
2021-03-29 09:48:12 +00:00
|
|
|
return col_res;
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 09:48:12 +00:00
|
|
|
/// CIDR is constant.
|
2021-03-29 12:50:34 +00:00
|
|
|
static ColumnPtr executeImpl(const IColumn & col_addr, const ColumnConst & col_cidr_const, size_t input_rows_count)
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
|
|
|
const auto & col_cidr = col_cidr_const.getDataColumn();
|
|
|
|
|
2022-07-17 15:22:12 +00:00
|
|
|
const auto cidr = parseIPWithCIDR(col_cidr.getDataAt(0).toView());
|
2021-03-29 09:48:12 +00:00
|
|
|
|
|
|
|
ColumnUInt8::MutablePtr col_res = ColumnUInt8::create(input_rows_count);
|
|
|
|
ColumnUInt8::Container & vec_res = col_res->getData();
|
2021-12-20 12:55:07 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
2022-07-18 17:31:34 +00:00
|
|
|
const auto addr = IPAddressVariant(col_addr.getDataAt(i).toView());
|
2021-03-29 09:48:12 +00:00
|
|
|
vec_res[i] = isAddressInRange(addr, cidr) ? 1 : 0;
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
2021-03-29 09:48:12 +00:00
|
|
|
return col_res;
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 09:48:12 +00:00
|
|
|
/// Neither are constant.
|
2021-03-29 12:50:34 +00:00
|
|
|
static ColumnPtr executeImpl(const IColumn & col_addr, const IColumn & col_cidr, size_t input_rows_count)
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
|
|
|
ColumnUInt8::MutablePtr col_res = ColumnUInt8::create(input_rows_count);
|
|
|
|
ColumnUInt8::Container & vec_res = col_res->getData();
|
|
|
|
|
2021-12-20 12:55:07 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
2022-07-18 17:31:34 +00:00
|
|
|
const auto addr = IPAddressVariant(col_addr.getDataAt(i).toView());
|
2022-07-17 15:22:12 +00:00
|
|
|
const auto cidr = parseIPWithCIDR(col_cidr.getDataAt(i).toView());
|
2021-02-17 08:57:48 +00:00
|
|
|
|
2021-03-29 09:48:12 +00:00
|
|
|
vec_res[i] = isAddressInRange(addr, cidr) ? 1 : 0;
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 09:48:12 +00:00
|
|
|
return col_res;
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(IsIPAddressContainedIn)
|
2021-02-17 08:57:48 +00:00
|
|
|
{
|
2021-03-29 09:48:12 +00:00
|
|
|
factory.registerFunction<FunctionIsIPAddressContainedIn>();
|
2021-02-17 08:57:48 +00:00
|
|
|
}
|
|
|
|
}
|