|
| 1 | +use std::net::{Ipv4Addr, Ipv6Addr}; |
| 2 | + |
| 3 | +#[cfg(feature = "ipnet")] |
| 4 | +use ipnet::{IpNet, Ipv4Net, Ipv6Net}; |
| 5 | + |
| 6 | +use crate::decode::Decode; |
| 7 | +use crate::encode::{Encode, IsNull}; |
| 8 | +use crate::error::BoxDynError; |
| 9 | +use crate::types::Type; |
| 10 | +use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres}; |
| 11 | + |
| 12 | +// https://github.com/postgres/postgres/blob/574925bfd0a8175f6e161936ea11d9695677ba09/src/include/utils/inet.h#L39 |
| 13 | + |
| 14 | +// Technically this is a magic number here but it doesn't make sense to drag in the whole of `libc` |
| 15 | +// just for one constant. |
| 16 | +const PGSQL_AF_INET: u8 = 2; // AF_INET |
| 17 | +const PGSQL_AF_INET6: u8 = PGSQL_AF_INET + 1; |
| 18 | + |
| 19 | +impl Type<Postgres> for IpNet { |
| 20 | + fn type_info() -> PgTypeInfo { |
| 21 | + PgTypeInfo::INET |
| 22 | + } |
| 23 | + |
| 24 | + fn compatible(ty: &PgTypeInfo) -> bool { |
| 25 | + *ty == PgTypeInfo::CIDR || *ty == PgTypeInfo::INET |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl PgHasArrayType for IpNet { |
| 30 | + fn array_type_info() -> PgTypeInfo { |
| 31 | + PgTypeInfo::INET_ARRAY |
| 32 | + } |
| 33 | + |
| 34 | + fn array_compatible(ty: &PgTypeInfo) -> bool { |
| 35 | + *ty == PgTypeInfo::CIDR_ARRAY || *ty == PgTypeInfo::INET_ARRAY |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl Encode<'_, Postgres> for IpNet { |
| 40 | + fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> { |
| 41 | + // https://github.com/postgres/postgres/blob/574925bfd0a8175f6e161936ea11d9695677ba09/src/backend/utils/adt/network.c#L293 |
| 42 | + // https://github.com/postgres/postgres/blob/574925bfd0a8175f6e161936ea11d9695677ba09/src/backend/utils/adt/network.c#L271 |
| 43 | + |
| 44 | + match self { |
| 45 | + IpNet::V4(net) => { |
| 46 | + buf.push(PGSQL_AF_INET); // ip_family |
| 47 | + buf.push(net.prefix_len()); // ip_bits |
| 48 | + buf.push(0); // is_cidr |
| 49 | + buf.push(4); // nb (number of bytes) |
| 50 | + buf.extend_from_slice(&net.addr().octets()) // address |
| 51 | + } |
| 52 | + |
| 53 | + IpNet::V6(net) => { |
| 54 | + buf.push(PGSQL_AF_INET6); // ip_family |
| 55 | + buf.push(net.prefix_len()); // ip_bits |
| 56 | + buf.push(0); // is_cidr |
| 57 | + buf.push(16); // nb (number of bytes) |
| 58 | + buf.extend_from_slice(&net.addr().octets()); // address |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + Ok(IsNull::No) |
| 63 | + } |
| 64 | + |
| 65 | + fn size_hint(&self) -> usize { |
| 66 | + match self { |
| 67 | + IpNet::V4(_) => 8, |
| 68 | + IpNet::V6(_) => 20, |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl Decode<'_, Postgres> for IpNet { |
| 74 | + fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> { |
| 75 | + let bytes = match value.format() { |
| 76 | + PgValueFormat::Binary => value.as_bytes()?, |
| 77 | + PgValueFormat::Text => { |
| 78 | + return Ok(value.as_str()?.parse()?); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + if bytes.len() >= 8 { |
| 83 | + let family = bytes[0]; |
| 84 | + let prefix = bytes[1]; |
| 85 | + let _is_cidr = bytes[2] != 0; |
| 86 | + let len = bytes[3]; |
| 87 | + |
| 88 | + match family { |
| 89 | + PGSQL_AF_INET => { |
| 90 | + if bytes.len() == 8 && len == 4 { |
| 91 | + let inet = Ipv4Net::new( |
| 92 | + Ipv4Addr::new(bytes[4], bytes[5], bytes[6], bytes[7]), |
| 93 | + prefix, |
| 94 | + )?; |
| 95 | + |
| 96 | + return Ok(IpNet::V4(inet)); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + PGSQL_AF_INET6 => { |
| 101 | + if bytes.len() == 20 && len == 16 { |
| 102 | + let inet = Ipv6Net::new( |
| 103 | + Ipv6Addr::from([ |
| 104 | + bytes[4], bytes[5], bytes[6], bytes[7], bytes[8], bytes[9], |
| 105 | + bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15], |
| 106 | + bytes[16], bytes[17], bytes[18], bytes[19], |
| 107 | + ]), |
| 108 | + prefix, |
| 109 | + )?; |
| 110 | + |
| 111 | + return Ok(IpNet::V6(inet)); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + _ => { |
| 116 | + return Err(format!("unknown ip family {family}").into()); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + Err("invalid data received when expecting an INET".into()) |
| 122 | + } |
| 123 | +} |
0 commit comments