net_address v0.2.2 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

Types

t()

generic ip subnet

ip subnet typed to ipv4 or ipv6

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 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

Link to this type

t(ip_type)

View Source
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

Link to this function

bitlength(map)

View Source
bitlength(t(IP.v4())) :: 0..32
bitlength(t(IP.v6())) :: 0..128

retrieves the bitlength from a subnet.

iex> import IP
iex> IP.Subnet.bitlength(~i"10.0.0.0/24")
24
Link to this function

broadcast(subnet)

View Source
broadcast(t(IP.v4())) :: IP.v4()

finds the broadcast address for a subnet

iex> import IP
iex> IP.Subnet.broadcast(~i"10.0.0.0/23")
{10, 0, 1, 255}
Link to this function

config_from_string(config_str)

View Source

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.

Link to this function

config_from_string!(config_str)

View Source

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.

Link to this function

from_string!(subnet_str)

View Source
from_string!(String.t()) :: t() | no_return()

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
}
Link to this macro

is_subnet(subnet)

View Source (macro)
is_subnet(any()) :: Macro.t()

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
Link to this function

netmask(map)

View Source
netmask(t()) :: IP.addr()

computes the netmask for a subnet.

iex> import IP
iex> IP.Subnet.netmask(~i"10.0.0.0/24")
{255, 255, 255, 0}
Link to this function

new(routing_prefix, bit_length)

View Source
new(IP.v4(), 0..32) :: t(IP.v4())
new(IP.v6(), 0..128) :: t(IP.v6())

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

Link to this function

of(ip_addr, bit_length)

View Source
of(IP.v4(), 0..32) :: t(IP.v4())
of(IP.v6(), 0..128) :: t(IP.v6())

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}
Link to this function

to_string(subnet)

View Source
to_string(t()) :: String.t()

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"