-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: add ipnet support #3710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add ipnet support #3710
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::net::IpAddr; | ||
|
||
use ipnet::IpNet; | ||
|
||
use crate::decode::Decode; | ||
use crate::encode::{Encode, IsNull}; | ||
use crate::error::BoxDynError; | ||
use crate::types::Type; | ||
use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef, Postgres}; | ||
|
||
impl Type<Postgres> for IpAddr | ||
where | ||
IpNet: Type<Postgres>, | ||
{ | ||
fn type_info() -> PgTypeInfo { | ||
IpNet::type_info() | ||
} | ||
|
||
fn compatible(ty: &PgTypeInfo) -> bool { | ||
IpNet::compatible(ty) | ||
} | ||
} | ||
|
||
impl PgHasArrayType for IpAddr { | ||
fn array_type_info() -> PgTypeInfo { | ||
<IpNet as PgHasArrayType>::array_type_info() | ||
} | ||
|
||
fn array_compatible(ty: &PgTypeInfo) -> bool { | ||
<IpNet as PgHasArrayType>::array_compatible(ty) | ||
} | ||
} | ||
|
||
impl<'db> Encode<'db, Postgres> for IpAddr | ||
where | ||
IpNet: Encode<'db, Postgres>, | ||
{ | ||
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> { | ||
IpNet::from(*self).encode_by_ref(buf) | ||
} | ||
|
||
fn size_hint(&self) -> usize { | ||
IpNet::from(*self).size_hint() | ||
} | ||
} | ||
|
||
impl<'db> Decode<'db, Postgres> for IpAddr | ||
where | ||
IpNet: Decode<'db, Postgres>, | ||
{ | ||
fn decode(value: PgValueRef<'db>) -> Result<Self, BoxDynError> { | ||
let ipnet = IpNet::decode(value)?; | ||
|
||
if matches!(ipnet, IpNet::V4(net) if net.prefix_len() != 32) | ||
|| matches!(ipnet, IpNet::V6(net) if net.prefix_len() != 128) | ||
{ | ||
Err("lossy decode from inet/cidr")? | ||
} | ||
|
||
Ok(ipnet.addr()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; | ||
|
||
#[cfg(feature = "ipnet")] | ||
use ipnet::{IpNet, Ipv4Net, Ipv6Net}; | ||
|
||
use crate::decode::Decode; | ||
use crate::encode::{Encode, IsNull}; | ||
use crate::error::BoxDynError; | ||
use crate::types::Type; | ||
use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres}; | ||
|
||
// https://github.com/postgres/postgres/blob/574925bfd0a8175f6e161936ea11d9695677ba09/src/include/utils/inet.h#L39 | ||
|
||
// Technically this is a magic number here but it doesn't make sense to drag in the whole of `libc` | ||
// just for one constant. | ||
const PGSQL_AF_INET: u8 = 2; // AF_INET | ||
const PGSQL_AF_INET6: u8 = PGSQL_AF_INET + 1; | ||
|
||
impl Type<Postgres> for IpNet { | ||
fn type_info() -> PgTypeInfo { | ||
PgTypeInfo::INET | ||
} | ||
|
||
fn compatible(ty: &PgTypeInfo) -> bool { | ||
*ty == PgTypeInfo::CIDR || *ty == PgTypeInfo::INET | ||
} | ||
} | ||
|
||
impl PgHasArrayType for IpNet { | ||
fn array_type_info() -> PgTypeInfo { | ||
PgTypeInfo::INET_ARRAY | ||
} | ||
|
||
fn array_compatible(ty: &PgTypeInfo) -> bool { | ||
*ty == PgTypeInfo::CIDR_ARRAY || *ty == PgTypeInfo::INET_ARRAY | ||
} | ||
} | ||
|
||
impl Encode<'_, Postgres> for IpNet { | ||
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> { | ||
// https://github.com/postgres/postgres/blob/574925bfd0a8175f6e161936ea11d9695677ba09/src/backend/utils/adt/network.c#L293 | ||
// https://github.com/postgres/postgres/blob/574925bfd0a8175f6e161936ea11d9695677ba09/src/backend/utils/adt/network.c#L271 | ||
|
||
match self { | ||
IpNet::V4(net) => { | ||
buf.push(PGSQL_AF_INET); // ip_family | ||
buf.push(net.prefix_len()); // ip_bits | ||
buf.push(0); // is_cidr | ||
buf.push(4); // nb (number of bytes) | ||
buf.extend_from_slice(&net.addr().octets()) // address | ||
} | ||
|
||
IpNet::V6(net) => { | ||
buf.push(PGSQL_AF_INET6); // ip_family | ||
buf.push(net.prefix_len()); // ip_bits | ||
buf.push(0); // is_cidr | ||
buf.push(16); // nb (number of bytes) | ||
buf.extend_from_slice(&net.addr().octets()); // address | ||
} | ||
} | ||
|
||
Ok(IsNull::No) | ||
} | ||
|
||
fn size_hint(&self) -> usize { | ||
match self { | ||
IpNet::V4(_) => 8, | ||
IpNet::V6(_) => 20, | ||
} | ||
} | ||
} | ||
|
||
impl Decode<'_, Postgres> for IpNet { | ||
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> { | ||
let bytes = match value.format() { | ||
PgValueFormat::Binary => value.as_bytes()?, | ||
PgValueFormat::Text => { | ||
let s = value.as_str()?; | ||
println!("{s}"); | ||
if s.contains('/') { | ||
return Ok(s.parse()?); | ||
} | ||
// IpNet::from_str doesn't handle conversion from IpAddr to IpNet | ||
let addr: IpAddr = s.parse()?; | ||
return Ok(addr.into()); | ||
} | ||
}; | ||
|
||
if bytes.len() >= 8 { | ||
let family = bytes[0]; | ||
let prefix = bytes[1]; | ||
let _is_cidr = bytes[2] != 0; | ||
let len = bytes[3]; | ||
|
||
match family { | ||
PGSQL_AF_INET => { | ||
if bytes.len() == 8 && len == 4 { | ||
let inet = Ipv4Net::new( | ||
Ipv4Addr::new(bytes[4], bytes[5], bytes[6], bytes[7]), | ||
prefix, | ||
)?; | ||
|
||
return Ok(IpNet::V4(inet)); | ||
} | ||
} | ||
|
||
PGSQL_AF_INET6 => { | ||
if bytes.len() == 20 && len == 16 { | ||
let inet = Ipv6Net::new( | ||
Ipv6Addr::from([ | ||
bytes[4], bytes[5], bytes[6], bytes[7], bytes[8], bytes[9], | ||
bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15], | ||
bytes[16], bytes[17], bytes[18], bytes[19], | ||
]), | ||
prefix, | ||
)?; | ||
|
||
return Ok(IpNet::V6(inet)); | ||
} | ||
} | ||
|
||
_ => { | ||
return Err(format!("unknown ip family {family}").into()); | ||
} | ||
} | ||
} | ||
|
||
Err("invalid data received when expecting an INET".into()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Prefer `ipnetwork` over `ipnet` because it was implemented first (want to avoid breaking change). | ||
#[cfg(not(feature = "ipnetwork"))] | ||
mod ipaddr; | ||
|
||
// Parent module is named after the `ipnet` crate, this is named after the `IpNet` type. | ||
#[allow(clippy::module_inception)] | ||
mod ipnet; |
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod ipaddr; | ||
|
||
// Parent module is named after the `ipnetwork` crate, this is named after the `IpNetwork` type. | ||
#[allow(clippy::module_inception)] | ||
mod ipnetwork; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.