IP.Prefix (ip v2.0.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.

t()

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

Link to this type

ipv4_prefix_length()

@type ipv4_prefix_length() :: 0..32

Valid IPv4 prefix lengths from 0 to 32.

Link to this type

ipv6_prefix_length()

@type ipv6_prefix_length() :: 0..128

Valid IPv6 prefix lengths from 0 to 128.

Link to this type

prefix_length()

@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

Link to this function

contains_address?(prefix, address)

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

contains_prefix?(outside, inside)

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

eui_64(prefix, mac)

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

eui_64!(prefix, mac)

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

iex> ~i(2001:db8::128/64)
...> |> IP.Prefix.first()
#IP.Address<2001:db8:: DOCUMENTATION>
Link to this function

from_string(prefix)

@spec from_string(binary()) :: {:ok, t()} | {:error, term()}

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

iex> "192.0.2.1/255.255.255.0"
...> |> IP.Prefix.from_string()
...> |> inspect()
"{:ok, #IP.Prefix<192.0.2.0/24 DOCUMENTATION>}"

iex> "2001:db8::/64"
...> |> IP.Prefix.from_string()
...> |> inspect()
"{:ok, #IP.Prefix<2001:db8::/64 DOCUMENTATION>}"
Link to this function

from_string(prefix, version)

@spec from_string(binary(), 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>}"

iex> "192.0.2.1/255.255.255.0"
...> |> IP.Prefix.from_string(4)
...> |> inspect()
"{:ok, #IP.Prefix<192.0.2.0/24 DOCUMENTATION>}"

iex> "2001:db8::/64"
...> |> IP.Prefix.from_string(4)
{:error, "Error parsing IPv4 prefix"}
Link to this function

from_string!(prefix)

@spec from_string!(binary()) :: t()

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>

iex> "192.0.2.1/255.255.255.0"
...> |> IP.Prefix.from_string!()
#IP.Prefix<192.0.2.0/24 DOCUMENTATION>

iex> "2001:db8::/64"
...> |> IP.Prefix.from_string!()
#IP.Prefix<2001:db8::/64 DOCUMENTATION>
Link to this function

from_string!(prefix, version)

@spec from_string!(binary(), 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>

iex> "192.0.2.1/255.255.255.0"
...> |> IP.Prefix.from_string!(4)
#IP.Prefix<192.0.2.0/24 DOCUMENTATION>
@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>

iex> ~i(2001:db8::128/64)
...> |> IP.Prefix.last()
#IP.Address<2001:db8::ffff:ffff:ffff:ffff DOCUMENTATION>
@spec length(t()) :: prefix_length()

Returns the bit-length of the prefix.

Example

iex> ~i(192.0.2.1/24)
...> |> IP.Prefix.length()
24
Link to this function

length(prefix, length)

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

new(address, length)

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

iex> IP.Prefix.new(~i(2001:db8::1), 64)
#IP.Prefix<2001:db8::/64 DOCUMENTATION>
@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
Link to this function

subnet_mask(prefix)

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

wildcard_mask(prefix)

@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 CURRENT NETWORK>