IP.Prefix (ip v1.2.1)

Defines an IP prefix, otherwise known as a subnet.

Link to this section 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.

Link to this section Types

Link to this type

ipv4_prefix_length()

Specs

ipv4_prefix_length() :: 0..32

Valid IPv4 prefix lengths from 0 to 32.

Link to this type

ipv6_prefix_length()

Specs

ipv6_prefix_length() :: 0..128

Valid IPv6 prefix lengths from 0 to 128.

Link to this type

prefix_length()

Specs

prefix_length() :: ipv4_prefix_length() | ipv6_prefix_length()

Valid IP prefix length.

Specs

t() :: %IP.Prefix{address: IP.Address.t(), mask: IP.Address.ip()}

The main prefix type, contains an address and a mask value.

Link to this section Functions

Link to this function

contains_address?(prefix, address)

Returns true or false depending on whether the supplied address is contained within prefix.

examples

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)

Specs

contains_prefix?(t(), IP.Address.t()) :: boolean()
contains_prefix?(t(), t()) :: boolean()

Returns true or false depending on whether the supplied inside is completely contained by outside.

examples

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)

Specs

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

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)

Specs

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

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>

Specs

first(t()) :: IP.Address.t()

Returns the first address in the prefix.

examples

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)

Specs

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

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)

Specs

from_string(binary(), IP.Address.version()) :: {:ok, t()} | {:error, term()}

Create a prefix by attempting to parse a string of specified IP version.

examples

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)

Specs

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

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)

Specs

from_string!(binary(), IP.Address.version()) :: t()

Create a prefix by attempting to parse a string of specified IP version.

examples

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>

Specs

last(t()) :: IP.Address.t()

Returns the last address in the prefix.

examples

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>

Specs

length(t()) :: prefix_length()

Returns the bit-length of the prefix.

example

Example

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

length(prefix, length)

Specs

length(t(), prefix_length()) :: t()

Alter the bit-length of the prefix.

example

Example

iex> ~i(192.0.2.0/24)
...> |> IP.Prefix.length(25)
#IP.Prefix<192.0.2.0/25 DOCUMENTATION>

Specs

mask(t()) :: IP.Address.ip()

Returns the calculated mask of the prefix.

example

Example

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

new(address, length)

Specs

new(IP.Address.t(), prefix_length()) :: t()

Create an IP prefix from an IP.Address and length.

examples

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>

Specs

space(t()) :: non_neg_integer()

Return the address space within this address.

examples

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)

Specs

subnet_mask(t()) :: IP.Address.t()

Returns an old-fashioned subnet mask for IPv4 prefixes.

example

Example

iex> ~i(192.0.2.0/24)
...> |> IP.Prefix.subnet_mask()
#IP.Address<255.255.255.0 RESERVED>

Specs

usable(t()) :: non_neg_integer()

Return the usable IP address space within this address.

examples

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)

Specs

wildcard_mask(t()) :: IP.Address.t()

Returns an "cisco style" wildcard mask for IPv4 prefixes.

example

Example

iex> ~i(192.0.2.0/24)
...> |> IP.Prefix.wildcard_mask()
#IP.Address<0.0.0.255 CURRENT NETWORK>