krisprice / ipnet

IpNet, Ipv4Net, and Ipv6Net types and methods for Rust
Apache License 2.0
122 stars 26 forks source link

Provide conversion from std::net::IpAddr to ipnet::IpNet #15

Closed glts closed 4 years ago

glts commented 4 years ago

I have recently needed conversion of exact IpAddrs to CIDR network addresses, and found that ipnet does not support conversion from an IpAddr to an IpNet.

Since that conversion is as far as I understand trivial and would always succeed (call Ipv{4,6}Net::new with address and max prefix length [?]), I was wondering if you would consider providing that conversion in IpNet?

This would of course be purely a convenience feature, but perhaps a useful one. Thank you!

krisprice commented 4 years ago

Hi @glts, so you'd find the following From implementations useful? Would you like to fork and impl these yourself then give me a pull request when ready?

From<IpAddr> for IpNet
From<Ipv4Addr> for Ipv4Net
From<Ipv6Addr> for Ipv6Net
glts commented 4 years ago

@krisprice Exactly. This is in a configuration context where administrators may declare addresses from certain networks as trusted. The inputs I would like to support are both CIDR networks (127.0.0.1/8) and IP addresses (127.0.0.1; probably more familiar to most administrators).

I’m not yet familiar with the ipnet codebase, but will try to send a pull request later, thank you.