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