9#include <userver/storages/postgres/io/buffer_io.hpp>
10#include <userver/storages/postgres/io/buffer_io_base.hpp>
11#include <userver/storages/postgres/io/integral_types.hpp>
12#include <userver/storages/postgres/io/string_types.hpp>
13#include <userver/storages/postgres/io/type_mapping.hpp>
14#include <userver/utils/ip.hpp>
16USERVER_NAMESPACE_BEGIN
21using NetworkV4 = USERVER_NAMESPACE::utils::
ip::NetworkV4;
24using AddressV4 = USERVER_NAMESPACE::utils::
ip::AddressV4;
26using NetworkV6 = USERVER_NAMESPACE::utils::
ip::NetworkV6;
28using AddressV6 = USERVER_NAMESPACE::utils::
ip::AddressV6;
30using InetNetwork = USERVER_NAMESPACE::utils::
ip::InetNetwork;
36inline constexpr char kPgsqlAfInet = AF_INET + 0;
37inline constexpr char kPgsqlAfInet6 = AF_INET + 1;
40inline constexpr char kIsCidr = 1;
41inline constexpr char kIsInet = 0;
44inline constexpr bool kIsNetworkType =
45 std::is_same_v<T, USERVER_NAMESPACE::utils::ip::NetworkV4> ||
46 std::is_same_v<T, USERVER_NAMESPACE::utils::ip::NetworkV6>;
49inline constexpr bool kIsAddressType =
50 std::is_same_v<T, USERVER_NAMESPACE::utils::ip::AddressV4> ||
51 std::is_same_v<T, USERVER_NAMESPACE::utils::ip::AddressV6>;
54struct IpBufferFormatterBase : BufferFormatterBase<T> {
56 using BaseType = BufferFormatterBase<T>;
57 using BaseType::BaseType;
58 template <
typename Address>
59 struct IpFormatterInfo {
61 char address_family =
'\0';
62 char prefix_length =
'\0';
66 template <
typename Buffer,
typename Address>
67 void Format(IpFormatterInfo<Address> info,
const UserTypes& types, Buffer& buffer) {
68 buffer.reserve(buffer.size() + info.address.size() + 4);
69 io::WriteBuffer(types, buffer, info.address_family);
70 io::WriteBuffer(types, buffer, info.prefix_length);
71 io::WriteBuffer(types, buffer, info.is_cidr);
72 io::WriteBuffer(types, buffer,
static_cast<
char>(info.address.size()));
73 for (
const auto val : info.address) {
74 io::WriteBuffer(types, buffer,
static_cast<
char>(val));
79template <
typename T,
typename = std::enable_if_t<kIsAddressType<T>>>
80struct AddressNetworkBuffer : IpBufferFormatterBase<T> {
81 using BaseType = IpBufferFormatterBase<T>;
82 using BaseType::BaseType;
84 template <
typename Buffer>
85 void operator()(
const UserTypes& types, Buffer& buffer) {
86 using Address =
typename T::BytesType;
87 constexpr bool kIsAddressV4 = std::is_same_v<T, USERVER_NAMESPACE::utils::
ip::AddressV4>;
88 const typename BaseType::
template IpFormatterInfo<Address> info{
89 this->value.GetBytes(),
90 kIsAddressV4 ? kPgsqlAfInet : kPgsqlAfInet6,
92 static_cast<
char>(kIsAddressV4 ? NetworkV4::kMaximumPrefixLength : NetworkV6::kMaximumPrefixLength),
95 BaseType::Format(info, types, buffer);
99template <
typename T,
typename = std::enable_if_t<kIsNetworkType<T>>>
100struct NetworkBufferFormatter : IpBufferFormatterBase<T> {
101 using BaseType = IpBufferFormatterBase<T>;
102 using BaseType::BaseType;
104 template <
typename Buffer>
105 void operator()(
const UserTypes& types, Buffer& buffer) {
106 using Address =
typename T::AddressType::BytesType;
107 const auto canonical_network = USERVER_NAMESPACE::utils::
ip::TransformToCidrFormat(
this->value);
108 if (canonical_network !=
this->value) {
110 "Network expected CIDR format. Use utils::ip::TransformToCidrFormat "
111 "method to conversation."
114 const typename BaseType::
template IpFormatterInfo<Address> info{
115 canonical_network.GetAddress().GetBytes(),
117 std::is_same_v<T, USERVER_NAMESPACE::utils::
ip::NetworkV4> ? kPgsqlAfInet : kPgsqlAfInet6,
119 static_cast<
char>(canonical_network.GetPrefixLength()),
122 BaseType::Format(info, types, buffer);
127struct IpBufferParserBase : BufferParserBase<T> {
129 using BaseType = BufferParserBase<T>;
130 using BaseType::BaseType;
132 template <
typename Bytes>
133 struct IpParserInfo {
135 unsigned char family =
'\0';
136 unsigned char prefix_length =
'\0';
137 unsigned char is_cidr =
'\0';
138 unsigned char bytes_number =
'\0';
141 template <
typename Bytes>
143 IpParserInfo<Bytes> result;
144 const uint8_t* byte_cptr = buffer.buffer;
145 result.family = *byte_cptr;
147 result.prefix_length = *byte_cptr;
149 result.is_cidr = *byte_cptr;
151 result.bytes_number = *byte_cptr;
153 this->ParseIpBytes(byte_cptr, result.bytes, result.bytes_number, result.family);
160 const uint8_t* byte_cptr,
161 std::array<
unsigned char, N>& bytes,
162 unsigned char bytes_number,
165 if (bytes_number != bytes.size()) {
167 fmt::format(
"Expected address size is {}, actual is {}", bytes_number, bytes.size())
170 std::memcpy(bytes.data(), byte_cptr, bytes.size());
174 const uint8_t* byte_cptr,
175 std::vector<
unsigned char>& bytes,
176 unsigned char bytes_number,
177 unsigned char address_family
179 if (!(bytes_number == 16 && address_family == kPgsqlAfInet6) &&
180 !(bytes_number == 4 && address_family == kPgsqlAfInet))
184 bytes.resize(bytes_number);
185 std::memcpy(bytes.data(), byte_cptr, bytes_number);
189template <
typename T,
typename = std::enable_if_t<kIsNetworkType<T>>>
190struct NetworkBufferParser : IpBufferParserBase<T> {
191 using BaseType = IpBufferParserBase<T>;
192 using BaseType::BaseType;
195 using Address =
typename T::AddressType;
196 using Bytes =
typename Address::BytesType;
197 const auto info = BaseType::
template Parse<Bytes>(buffer);
198 constexpr auto expected_family = std::is_same_v<T, NetworkV4> ? kPgsqlAfInet : kPgsqlAfInet6;
199 if (info.family != expected_family) {
202 if (info.is_cidr != kIsCidr) {
205 this->value = T(Address(info.bytes), info.prefix_length);
209template <
typename T,
typename = std::enable_if_t<kIsAddressType<T>>>
210struct AddressBufferParser : detail::IpBufferParserBase<T> {
211 using BaseType = detail::IpBufferParserBase<T>;
212 using BaseType::BaseType;
215 using Bytes =
typename T::BytesType;
216 const auto info = BaseType::
template Parse<Bytes>(buffer);
217 constexpr auto expected_family = std::is_same_v<T, AddressV4> ? kPgsqlAfInet : kPgsqlAfInet6;
218 if (info.family != expected_family) {
221 constexpr unsigned char expected_prefix_length =
222 std::is_same_v<T, AddressV4> ? NetworkV4::kMaximumPrefixLength : NetworkV6::kMaximumPrefixLength;
223 if (info.prefix_length != expected_prefix_length) {
225 "Expected prefix length is {}, actual prefix is {}",
226 static_cast<
int>(expected_prefix_length),
227 static_cast<
int>(info.prefix_length)
230 if (info.is_cidr != kIsCidr) {
233 this->value = T(info.bytes);
241struct BufferFormatter<NetworkV4> : detail::NetworkBufferFormatter<NetworkV4> {
242 using BaseType = detail::NetworkBufferFormatter<NetworkV4>;
244 using BaseType::BaseType;
249struct BufferFormatter<NetworkV6> : detail::NetworkBufferFormatter<NetworkV6> {
250 using BaseType = detail::NetworkBufferFormatter<NetworkV6>;
252 using BaseType::BaseType;
257struct BufferFormatter<AddressV4> : detail::AddressNetworkBuffer<AddressV4> {
258 using BaseType = detail::AddressNetworkBuffer<AddressV4>;
260 using BaseType::BaseType;
265struct BufferFormatter<AddressV6> : detail::AddressNetworkBuffer<AddressV6> {
266 using BaseType = detail::AddressNetworkBuffer<AddressV6>;
268 using BaseType::BaseType;
273struct BufferFormatter<InetNetwork> : detail::IpBufferFormatterBase<InetNetwork> {
274 using BaseType = detail::IpBufferFormatterBase<InetNetwork>;
275 using BaseType::BaseType;
277 template <
typename Buffer>
278 void operator()(
const UserTypes& types, Buffer& buffer) {
279 using Address = std::vector<
unsigned char>;
280 const typename BaseType::
template IpFormatterInfo<Address> info{
284 ? detail::kPgsqlAfInet
285 : detail::kPgsqlAfInet6,
289 BaseType::Format(info, types, buffer);
295struct BufferParser<NetworkV4> : detail::NetworkBufferParser<NetworkV4> {
296 using BaseType = detail::NetworkBufferParser<NetworkV4>;
298 using BaseType::BaseType;
303struct BufferParser<NetworkV6> : detail::NetworkBufferParser<NetworkV6> {
304 using BaseType = detail::NetworkBufferParser<NetworkV6>;
306 using BaseType::BaseType;
311struct BufferParser<AddressV4> : detail::AddressBufferParser<AddressV4> {
312 using BaseType = detail::AddressBufferParser<AddressV4>;
314 using BaseType::BaseType;
319struct BufferParser<AddressV6> : detail::AddressBufferParser<AddressV6> {
320 using BaseType = detail::AddressBufferParser<AddressV6>;
322 using BaseType::BaseType;
327struct BufferParser<InetNetwork> : detail::IpBufferParserBase<InetNetwork> {
328 using BaseType = detail::IpBufferParserBase<InetNetwork>;
329 using BaseType::BaseType;
332 using Bytes = std::vector<
unsigned char>;
333 auto info = BaseType::
template Parse<Bytes>(buffer);
334 this->value = InetNetwork(
335 std::move(info.bytes),
337 (info.family == detail::kPgsqlAfInet ? InetNetwork::AddressFamily::kIPv4 : InetNetwork::AddressFamily::kIPv6