net_address v0.2.1 IP.Subnet View Source
Convenience type which encapsulates the idea of an IP subnet. See: https://en.wikipedia.org/wiki/Subnetwork
NB
The distinction between an IP.Range and an IP.Subnet is that a Subnet
must have its bounds at certain powers-of-two and multiple thereof that
are governed by the subnet bit-length. A range is not constrained and
is a simple "dumb list of ip addresses". Typically ranges will be proper
subsets of Subnets.
Enumerable
Implements the Enumerable protocol, so the following sorts of things are possible:
iex> import IP
iex> Enum.map(~i"10.0.0.4/30", &IP.to_string/1)
["10.0.0.4", "10.0.0.5", "10.0.0.6", "10.0.0.7"]
Link to this section Summary
Functions
retrieves the bitlength from a subnet.
finds the broadcast address for a subnet
Finds an ip subnet in a string, returning an ok or error tuple on failure.
converts a string to an ip subnet.
true if the term is a subnet struct, and it's valid.
computes the netmask for a subnet.
creates a new IP Subnet struct from a routing prefix and bit length.
creates a corresponding IP subnet associated with a given IP address and bit length.
retrieves the routing prefix from a subnet.
converts an ip subnet to standard CIDR-form, with a slash delimiter.
Link to this section Types
generic ip subnet
t(ip_type)
View Sourcet(ip_type) :: %IP.Subnet{
__enum__: term(),
bit_length: 0..128,
routing_prefix: ip_type
}
ip subnet typed to ipv4 or ipv6
Link to this section Functions
retrieves the bitlength from a subnet.
iex> import IP
iex> IP.Subnet.bitlength(~i"10.0.0.0/24")
24
finds the broadcast address for a subnet
iex> import IP
iex> IP.Subnet.broadcast(~i"10.0.0.0/23")
{10, 0, 1, 255}
Finds an ip subnet in a string, returning an ok or error tuple on failure.
converts a string to an ip subnet.
The delimiter must be "..", as this is compatible with both ipv4 and ipv6 addresses
checks if the values are sensible.
iex> import IP
iex> IP.Subnet.from_string!("10.0.0.0/24")
%IP.Subnet{
routing_prefix: {10, 0, 0, 0},
bit_length: 24
}
true if the term is a subnet struct, and it's valid.
usable in guards.
iex> import IP
iex> IP.Subnet.is_subnet(~i"10.0.0.0/32")
true
iex> IP.Subnet.is_subnet(:foo)
false
iex> IP.Subnet.is_subnet(%IP.Subnet{routing_prefix: {10, 0, 0, 0}, bit_length: 33})
false
computes the netmask for a subnet.
iex> import IP
iex> IP.Subnet.netmask(~i"10.0.0.0/24")
{255, 255, 255, 0}
creates a new IP Subnet struct from a routing prefix and bit length.
The routing prefix must be an actual routing prefix for the bit length,
otherwise it will raise ArgumentError. If you are attempting to find the
subnet for a given ip address, use of/2
creates a corresponding IP subnet associated with a given IP address and bit length.
retrieves the routing prefix from a subnet.
iex> import IP
iex> IP.Subnet.prefix(~i"10.0.0.0/24")
{10, 0, 0, 0}
converts an ip subnet to standard CIDR-form, with a slash delimiter.
iex> IP.Subnet.to_string(%IP.Subnet{routing_prefix: {10, 0, 0, 0}, bit_length: 24})
"10.0.0.0/24"