forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionsCodingIP.h
230 lines (183 loc) · 7.96 KB
/
FunctionsCodingIP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#pragma once
#include <Common/formatIPv6.h>
#include <Columns/ColumnFixedString.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnsNumber.h>
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_PARSE_DOMAIN_VALUE_FROM_STRING;
extern const int ILLEGAL_COLUMN;
}
enum class IPStringToNumExceptionMode : uint8_t
{
Throw,
Default,
Null
};
static inline bool tryParseIPv4(const char * pos, UInt32 & result_value)
{
return parseIPv4(pos, reinterpret_cast<unsigned char *>(&result_value));
}
namespace detail
{
template <IPStringToNumExceptionMode exception_mode, typename StringColumnType>
ColumnPtr convertToIPv6(const StringColumnType & string_column, const PaddedPODArray<UInt8> * null_map = nullptr)
{
size_t column_size = string_column.size();
ColumnUInt8::MutablePtr col_null_map_to;
ColumnUInt8::Container * vec_null_map_to = nullptr;
if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
{
col_null_map_to = ColumnUInt8::create(column_size, false);
vec_null_map_to = &col_null_map_to->getData();
}
auto col_res = ColumnFixedString::create(IPV6_BINARY_LENGTH);
auto & vec_res = col_res->getChars();
vec_res.resize(column_size * IPV6_BINARY_LENGTH);
using Chars = typename StringColumnType::Chars;
const Chars & vec_src = string_column.getChars();
size_t src_offset = 0;
char src_ipv4_buf[sizeof("::ffff:") + IPV4_MAX_TEXT_LENGTH + 1] = "::ffff:";
/// ColumnFixedString contains not null terminated strings. But functions parseIPv6, parseIPv4 expect null terminated string.
std::string fixed_string_buffer;
if constexpr (std::is_same_v<StringColumnType, ColumnFixedString>)
{
fixed_string_buffer.resize(string_column.getN());
}
for (size_t out_offset = 0, i = 0; out_offset < vec_res.size(); out_offset += IPV6_BINARY_LENGTH, ++i)
{
size_t src_next_offset = src_offset;
const char * src_value = nullptr;
unsigned char * res_value = reinterpret_cast<unsigned char *>(&vec_res[out_offset]);
if constexpr (std::is_same_v<StringColumnType, ColumnString>)
{
src_value = reinterpret_cast<const char *>(&vec_src[src_offset]);
src_next_offset = string_column.getOffsets()[i];
}
else if constexpr (std::is_same_v<StringColumnType, ColumnFixedString>)
{
size_t fixed_string_size = string_column.getN();
std::memcpy(fixed_string_buffer.data(), reinterpret_cast<const char *>(&vec_src[src_offset]), fixed_string_size);
src_value = fixed_string_buffer.data();
src_next_offset += fixed_string_size;
}
if (null_map && (*null_map)[i])
{
std::fill_n(&vec_res[i], IPV6_BINARY_LENGTH, 0);
src_offset = src_next_offset;
if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
(*vec_null_map_to)[i] = true;
continue;
}
bool parse_result = false;
UInt32 dummy_result = 0;
/// For both cases below: In case of failure, the function parseIPv6 fills vec_res with zero bytes.
/// If the source IP address is parsable as an IPv4 address, then transform it into a valid IPv6 address.
/// Keeping it simple by just prefixing `::ffff:` to the IPv4 address to represent it as a valid IPv6 address.
if (tryParseIPv4(src_value, dummy_result))
{
std::memcpy(
src_ipv4_buf + std::strlen("::ffff:"),
src_value,
std::min<UInt64>(src_next_offset - src_offset, IPV4_MAX_TEXT_LENGTH + 1));
parse_result = parseIPv6(src_ipv4_buf, res_value);
}
else
{
parse_result = parseIPv6(src_value, res_value);
}
if (!parse_result)
{
if constexpr (exception_mode == IPStringToNumExceptionMode::Throw)
throw Exception("Invalid IPv6 value", ErrorCodes::CANNOT_PARSE_DOMAIN_VALUE_FROM_STRING);
else if constexpr (exception_mode == IPStringToNumExceptionMode::Default)
vec_res[i] = 0;
else if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
(*vec_null_map_to)[i] = true;
}
src_offset = src_next_offset;
}
if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
return ColumnNullable::create(std::move(col_res), std::move(col_null_map_to));
return col_res;
}
}
template <IPStringToNumExceptionMode exception_mode>
ColumnPtr convertToIPv6(ColumnPtr column, const PaddedPODArray<UInt8> * null_map = nullptr)
{
size_t column_size = column->size();
auto col_res = ColumnFixedString::create(IPV6_BINARY_LENGTH);
auto & vec_res = col_res->getChars();
vec_res.resize(column_size * IPV6_BINARY_LENGTH);
if (const auto * column_input_string = checkAndGetColumn<ColumnString>(column.get()))
{
return detail::convertToIPv6<exception_mode>(*column_input_string, null_map);
}
else if (const auto * column_input_fixed_string = checkAndGetColumn<ColumnFixedString>(column.get()))
{
return detail::convertToIPv6<exception_mode>(*column_input_fixed_string, null_map);
}
else
{
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column type {}. Expected String or FixedString", column->getName());
}
}
template <IPStringToNumExceptionMode exception_mode>
ColumnPtr convertToIPv4(ColumnPtr column, const PaddedPODArray<UInt8> * null_map = nullptr)
{
const ColumnString * column_string = checkAndGetColumn<ColumnString>(column.get());
if (!column_string)
{
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column type {}. Expected String.", column->getName());
}
size_t column_size = column_string->size();
ColumnUInt8::MutablePtr col_null_map_to;
ColumnUInt8::Container * vec_null_map_to = nullptr;
if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
{
col_null_map_to = ColumnUInt8::create(column_size, false);
vec_null_map_to = &col_null_map_to->getData();
}
auto col_res = ColumnUInt32::create();
ColumnUInt32::Container & vec_res = col_res->getData();
vec_res.resize(column_size);
const ColumnString::Chars & vec_src = column_string->getChars();
const ColumnString::Offsets & offsets_src = column_string->getOffsets();
size_t prev_offset = 0;
for (size_t i = 0; i < vec_res.size(); ++i)
{
if (null_map && (*null_map)[i])
{
vec_res[i] = 0;
prev_offset = offsets_src[i];
if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
(*vec_null_map_to)[i] = true;
continue;
}
bool parse_result = tryParseIPv4(reinterpret_cast<const char *>(&vec_src[prev_offset]), vec_res[i]);
if (!parse_result)
{
if constexpr (exception_mode == IPStringToNumExceptionMode::Throw)
{
throw Exception("Invalid IPv4 value", ErrorCodes::CANNOT_PARSE_DOMAIN_VALUE_FROM_STRING);
}
else if constexpr (exception_mode == IPStringToNumExceptionMode::Default)
{
vec_res[i] = 0;
}
else if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
{
(*vec_null_map_to)[i] = true;
vec_res[i] = 0;
}
}
prev_offset = offsets_src[i];
}
if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
return ColumnNullable::create(std::move(col_res), std::move(col_null_map_to));
return col_res;
}
}