Expand description
Types for IPv4 and IPv6 network addresses.
This module provides types and useful methods for working with IPv4
and IPv6 network addresses, commonly called IP prefixes. The new
IpNet, Ipv4Net, and Ipv6Net types build on the existing
IpAddr, Ipv4Addr, and Ipv6Addr types already provided in
Rust’s standard library and align to their design to stay
consistent.
The module also provides the IpSubnets, Ipv4Subnets, and
Ipv6Subnets types for iterating over the subnets contained in
an IP address range. The IpAddrRange, Ipv4AddrRange, and
Ipv6AddrRange types for iterating over IP addresses in a range.
And traits that extend Ipv4Addr and Ipv6Addr with methods for
addition, subtraction, bitwise-and, and bitwise-or operations that
are missing in Rust’s standard library.
The module only uses stable features so it is guaranteed to compile using the stable toolchain.
§Organization
IpNetrepresents an IP network address, either IPv4 or IPv6.Ipv4NetandIpv6Netare respectively IPv4 and IPv6 network addresses.IpSubnets,Ipv4Subnets, andIpv6Subnetsare iterators that generate the smallest set of IP network addresses bound by an IP address range and minimum prefix length. These can be created using their constructors. They are also returned by thesubnets()methods and used within theaggregate()methods.IpAddrRange,Ipv4AddrRange, andIpv6AddrRangeare iterators that generate IP addresses. These can be created using their constructors. They are also returned by thehosts()methods.- The
IpAdd,IpSub,IpBitAnd,IpBitOrtraits extend theIpv4AddrandIpv6Addrtypes with methods to perform these operations.
§Serde support
This library comes with support for serde but
it’s not enabled by default. Use the serde feature to enable.
[dependencies]
ipnet = { version = "2", features = ["serde"] }For human readable formats (e.g. JSON) the IpNet, Ipv4Net, and
Ipv6Net types will serialize to their Display strings.
For compact binary formats (e.g. Bincode) the Ipv4Net and
Ipv6Net types will serialize to a string of 5 and 17 bytes that
consist of the network address octects followed by the prefix
length. The IpNet type will serialize to an Enum with the V4 or V6
variant index prepending the above string of 5 or 17 bytes.
Structs§
- Addr
Parse Error - An error which can be returned when parsing an IP network address.
- Ipv4
Addr Range - An
Iteratorover a range of IPv4 addresses. - Ipv4Net
- An IPv4 network address.
- Ipv4
Subnets - An
Iteratorthat generates IPv4 network addresses. - Ipv6
Addr Range - An
Iteratorover a range of IPv6 addresses. - Ipv6Net
- An IPv6 network address.
- Ipv6
Subnets - An
Iteratorthat generates IPv6 network addresses. - Prefix
LenError - An error which can be returned when the prefix length is invalid.
Enums§
- IpAddr
Range - An
Iteratorover a range of IP addresses, either IPv4 or IPv6. - IpNet
- An IP network address, either IPv4 or IPv6.
- IpSubnets
- An
Iteratorthat generates IP network addresses, either IPv4 or IPv6.
Traits§
- IpAdd
- Provides a
saturating_add()method forIpv4AddrandIpv6Addr. - IpBit
And - Provides a
bitand()method forIpv4AddrandIpv6Addr. - IpBitOr
- Provides a
bitor()method forIpv4AddrandIpv6Addr. - IpSub
- Provides a
saturating_sub()method forIpv4AddrandIpv6Addr.
Functions§
- ip_
mask_ to_ prefix - Converts a
IpAddrnetwork mask into a prefix. - ipv4_
mask_ to_ prefix - Converts a
Ipv4Addrnetwork mask into a prefix. - ipv6_
mask_ to_ prefix - Converts a
Ipv6Addrnetwork mask into a prefix.