IP.Prefix (ip v2.1.3)
Defines an IP prefix, otherwise known as a subnet.
Summary
Types
Valid IPv4 prefix lengths from 0 to 32.
Valid IPv6 prefix lengths from 0 to 128.
Valid IP prefix length.
The main prefix type, contains an address and a mask value.
Functions
Returns true or false depending on whether the supplied address is
contained within prefix.
Returns true or false depending on whether the supplied inside is
completely contained by outside.
Generate an EUI-64 host address within the specifed IPv6 prefix.
Generate an EUI-64 host address within the specifed IPv6 prefix.
Returns the first address in the prefix.
Create a prefix by attempting to parse a string of unknown version.
Create a prefix by attempting to parse a string of specified IP version.
Create a prefix by attempting to parse a string of unknown version.
Create a prefix by attempting to parse a string of specified IP version.
Returns the last address in the prefix.
Returns the bit-length of the prefix.
Alter the bit-length of the prefix.
Returns the calculated mask of the prefix.
Create an IP prefix from an IP.Address and length.
Return the address space within this address.
Returns an old-fashioned subnet mask for IPv4 prefixes.
Return the usable IP address space within this address.
Returns an "cisco style" wildcard mask for IPv4 prefixes.
Types
@type ipv4_prefix_length() :: 0..32
Valid IPv4 prefix lengths from 0 to 32.
@type ipv6_prefix_length() :: 0..128
Valid IPv6 prefix lengths from 0 to 128.
@type prefix_length() :: ipv4_prefix_length() | ipv6_prefix_length()
Valid IP prefix length.
@type t() :: %IP.Prefix{address: IP.Address.t(), mask: IP.Address.ip()}
The main prefix type, contains an address and a mask value.
Functions
Returns true or false depending on whether the supplied address is
contained within prefix.
Examples
iex> ~i(192.0.2.0/24)
...> |> IP.Prefix.contains_address?(~i(192.0.2.127))
true
iex> ~i(192.0.2.0/24)
...> |> IP.Prefix.contains_address?(~i(198.51.100.1))
false
iex> ~i(2001:db8::/64)
...> |> IP.Prefix.contains_address?(~i(2001:db8::1))
true
iex> ~i(2001:db8::/64)
...> |> IP.Prefix.contains_address?(~i(2001:db8:1::1))
false
iex> outside = ~i(2001:db8::/64)
...> inside = IP.Prefix.eui_64!(outside, "60:f8:1d:ad:d8:90")
...> IP.Prefix.contains_address?(outside, inside)
true
@spec contains_prefix?(t(), IP.Address.t()) :: boolean()
@spec contains_prefix?(t(), t()) :: boolean()
Returns true or false depending on whether the supplied inside is
completely contained by outside.
Examples
iex> outside = ~i(192.0.2.0/24)
...> inside = ~i(192.0.2.128/25)
...> IP.Prefix.contains_prefix?(outside, inside)
true
iex> outside = ~i(192.0.2.128/25)
...> inside = ~i(192.0.2.0/24)
...> IP.Prefix.contains_prefix?(outside, inside)
false
iex> outside = ~i(2001:db8::/64)
...> inside = ~i(2001:db8::/128)
...> IP.Prefix.contains_prefix?(outside, inside)
true
iex> outside = ~i(2001:db8::/128)
...> inside = ~i(2001:db8::/64)
...> IP.Prefix.contains_prefix?(outside, inside)
false
@spec eui_64(t(), binary()) :: {:ok, IP.Address.t()} | {:error, term()}
Generate an EUI-64 host address within the specifed IPv6 prefix.
EUI-64 addresses can only be generated for 64 bit long IPv6 prefixes.
Examples
iex> ~i(2001:db8::/64)
...> |> IP.Prefix.eui_64("60:f8:1d:ad:d8:90")
...> |> inspect()
"{:ok, #IP.Address<2001:db8::62f8:1dff:fead:d890 Documentation, GLOBAL, RESERVED>}"
@spec eui_64!(t(), binary()) :: IP.Address.t()
Generate an EUI-64 host address within the specifed IPv6 prefix.
EUI-64 addresses can only be generated for 64 bit long IPv6 prefixes.
Examples
iex> ~i(2001:db8::/64)
...> |> IP.Prefix.eui_64!("60:f8:1d:ad:d8:90")
#IP.Address<2001:db8::62f8:1dff:fead:d890 Documentation, GLOBAL, RESERVED>
@spec first(t()) :: IP.Address.t()
Returns the first address in the prefix.
Examples
iex> ~i(192.0.2.128/24)
...> |> IP.Prefix.first()
#IP.Address<192.0.2.0 Documentation (TEST-NET-1), GLOBAL, RESERVED>
iex> ~i(2001:db8::128/64)
...> |> IP.Prefix.first()
#IP.Address<2001:db8:: Documentation, GLOBAL, RESERVED>
Create a prefix by attempting to parse a string of unknown version.
Calling from_string/2 is faster if you know the IP version of the prefix.
Examples
iex> "192.0.2.1/24"
...> |> IP.Prefix.from_string()
...> |> inspect()
"{:ok, #IP.Prefix<192.0.2.0/24 Documentation (TEST-NET-1), GLOBAL, RESERVED>}"
iex> "192.0.2.1/255.255.255.0"
...> |> IP.Prefix.from_string()
...> |> inspect()
"{:ok, #IP.Prefix<192.0.2.0/24 Documentation (TEST-NET-1), GLOBAL, RESERVED>}"
iex> "2001:db8::/64"
...> |> IP.Prefix.from_string()
...> |> inspect()
"{:ok, #IP.Prefix<2001:db8::/64 Documentation, GLOBAL, RESERVED>}"
@spec from_string(any(), IP.Address.version()) :: {:ok, t()} | {:error, term()}
Create a prefix by attempting to parse a string of specified IP version.
Examples
iex> "192.0.2.1/24"
...> |> IP.Prefix.from_string(4)
...> |> inspect()
"{:ok, #IP.Prefix<192.0.2.0/24 Documentation (TEST-NET-1), GLOBAL, RESERVED>}"
iex> "192.0.2.1/255.255.255.0"
...> |> IP.Prefix.from_string(4)
...> |> inspect()
"{:ok, #IP.Prefix<192.0.2.0/24 Documentation (TEST-NET-1), GLOBAL, RESERVED>}"
iex> "2001:db8::/64"
...> |> IP.Prefix.from_string(4)
{:error, "Error parsing IPv4 prefix"}
Create a prefix by attempting to parse a string of unknown version.
Calling from_string!/2 is faster if you know the IP version of the prefix.
Examples
iex> "192.0.2.1/24"
...> |> IP.Prefix.from_string!()
#IP.Prefix<192.0.2.0/24 Documentation (TEST-NET-1), GLOBAL, RESERVED>
iex> "192.0.2.1/255.255.255.0"
...> |> IP.Prefix.from_string!()
#IP.Prefix<192.0.2.0/24 Documentation (TEST-NET-1), GLOBAL, RESERVED>
iex> "2001:db8::/64"
...> |> IP.Prefix.from_string!()
#IP.Prefix<2001:db8::/64 Documentation, GLOBAL, RESERVED>
@spec from_string!(any(), IP.Address.version()) :: t()
Create a prefix by attempting to parse a string of specified IP version.
Examples
iex> "192.0.2.1/24"
...> |> IP.Prefix.from_string!(4)
#IP.Prefix<192.0.2.0/24 Documentation (TEST-NET-1), GLOBAL, RESERVED>
iex> "192.0.2.1/255.255.255.0"
...> |> IP.Prefix.from_string!(4)
#IP.Prefix<192.0.2.0/24 Documentation (TEST-NET-1), GLOBAL, RESERVED>
@spec last(t()) :: IP.Address.t()
Returns the last address in the prefix.
Examples
iex> ~i(192.0.2.128/24)
...> |> IP.Prefix.last()
#IP.Address<192.0.2.255 Documentation (TEST-NET-1), GLOBAL, RESERVED>
iex> ~i(2001:db8::128/64)
...> |> IP.Prefix.last()
#IP.Address<2001:db8::ffff:ffff:ffff:ffff Documentation, GLOBAL, RESERVED>
@spec length(t()) :: prefix_length()
Returns the bit-length of the prefix.
Example
iex> ~i(192.0.2.1/24)
...> |> IP.Prefix.length()
24
@spec length(t(), prefix_length()) :: t()
Alter the bit-length of the prefix.
Example
iex> ~i(192.0.2.0/24)
...> |> IP.Prefix.length(25)
#IP.Prefix<192.0.2.0/25 Documentation (TEST-NET-1), GLOBAL, RESERVED>
@spec mask(t()) :: IP.Address.ip()
Returns the calculated mask of the prefix.
Example
iex> ~i(192.0.2.1/24)
...> |> IP.Prefix.mask()
0b11111111111111111111111100000000
@spec new(IP.Address.t(), prefix_length()) :: t()
Create an IP prefix from an IP.Address and length.
Examples
iex> IP.Prefix.new(~i(192.0.2.1), 24)
#IP.Prefix<192.0.2.0/24 Documentation (TEST-NET-1), GLOBAL, RESERVED>
iex> IP.Prefix.new(~i(2001:db8::1), 64)
#IP.Prefix<2001:db8::/64 Documentation, GLOBAL, RESERVED>
@spec space(t()) :: non_neg_integer()
Return the address space within this address.
Examples
iex> ~i(192.0.2.0/24)
...> |> IP.Prefix.space()
256
iex> ~i(2001:db8::/64)
...> |> IP.Prefix.space()
18446744073709551616
@spec subnet_mask(t()) :: IP.Address.t()
Returns an old-fashioned subnet mask for IPv4 prefixes.
Example
iex> ~i(192.0.2.0/24)
...> |> IP.Prefix.subnet_mask()
#IP.Address<255.255.255.0 Reserved, GLOBAL, RESERVED>
@spec usable(t()) :: non_neg_integer()
Return the usable IP address space within this address.
Examples
iex> ~i(192.0.2.0/24)
...> |> IP.Prefix.usable()
254
iex> ~i(2001:db8::/64)
...> |> IP.Prefix.usable()
18446744073709551616
@spec wildcard_mask(t()) :: IP.Address.t()
Returns an "cisco style" wildcard mask for IPv4 prefixes.
Example
iex> ~i(192.0.2.0/24)
...> |> IP.Prefix.wildcard_mask()
#IP.Address<0.0.0.255 This network, GLOBAL, RESERVED>