IP.Subnet (net_address v0.3.1) 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"]
Membership
In the IP.Subnet
implementation of Enumerable, the member?/2
callback
is implemented to provide a fastlane membership function. You can thus
check IP address membership without having to enumerate all members of the
list first.
see also is_in/2
.
iex> import IP
iex> ~i"10.0.0.1" in ~i"10.0.0.0/24"
true
iex> ~i"10.0.0.1..10.0.0.33" in ~i"10.0.0.0/24"
true
iex> ~i"10.0.0.0/26" in ~i"10.0.0.0/24"
true
iex> ~i"10.0.0.1..10.0.1.1" in ~i"10.0.0.0/24"
false
iex> ~i"10.0.0.0/22" in ~i"10.0.0.0/24"
false
Link to this section Summary
Functions
retrieves the bitlength from a subnet.
finds the broadcast address for a subnet
finds an ip address and subnet together from a config representation
(this is an ip/cidr string where the ip is not necessarily the routing
prefix for the cidr block).
finds an ip address and subnet together from a config representation
(this is an ip/cidr string where the ip is not necessarily the routing
prefix for the cidr block).
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 ip
parameter is inside the subnet. ip
must be a
single ip address; if you need a membership function
that accepts ranges or subnets, use Kernel.in/2
.
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
Specs
generic ip subnet
Specs
t(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
Specs
retrieves the bitlength from a subnet.
iex> import IP
iex> IP.Subnet.bitlength(~i"10.0.0.0/24")
24
Specs
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 address and subnet together from a config representation
(this is an ip/cidr string where the ip is not necessarily the routing
prefix for the cidr block).
returns {:ok, ip, subnet}
if the config string is valid;
{:error, reason}
otherwise.
finds an ip address and subnet together from a config representation
(this is an ip/cidr string where the ip is not necessarily the routing
prefix for the cidr block).
This function is useful if you have configuration files that specify
IP address/subnet identities in this fashion (for example ifupdown
or netplan
configuration files)
returns {ip, subnet}
if the config string is valid; raises otherwise.
iex> IP.Subnet.config_from_string!("10.0.0.4/24")
{{10, 0, 0, 4}, %IP.Subnet{routing_prefix: {10, 0, 0, 0}, bit_length: 24}}
Finds an ip subnet in a string, returning an ok or error tuple on failure.
Specs
converts a string to an ip subnet.
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 ip
parameter is inside the subnet. ip
must be a
single ip address; if you need a membership function
that accepts ranges or subnets, use Kernel.in/2
.
Be aware of the parameter order, if you are using this after import IP.Range
.
Currently only works for ipv4 addresses.
usable in guards.
iex> import IP
iex> IP.Subnet.is_in(~i"10.0.0.0/24", ~i"10.0.0.2")
true
iex> IP.Subnet.is_in(~i"10.0.0.0/24", ~i"10.0.1.5")
false
Specs
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
Specs
computes the netmask for a subnet.
iex> import IP
iex> IP.Subnet.netmask(~i"10.0.0.0/24")
{255, 255, 255, 0}
Specs
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
Specs
creates a corresponding IP subnet associated with a given IP address and bit length.
Specs
retrieves the routing prefix from a subnet.
iex> import IP
iex> IP.Subnet.prefix(~i"10.0.0.0/24")
{10, 0, 0, 0}
Specs
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"