ip v0.1.0 IP.Prefix

Defines an IP prefix, otherwise known as a subnet.

Link to this section Summary

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()
ipv4_prefix_length() :: 0..32
Link to this type ipv6_prefix_length()
ipv6_prefix_length() :: 0..128
Link to this type t()
t() :: %IP.Prefix{address: term, mask: term}

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

iex> IP.Prefix.from_string!("192.0.2.0/24")
...> |> IP.Prefix.contains_address?(IP.Address.from_string!("192.0.2.127"))
true

iex> IP.Prefix.from_string!("192.0.2.0/24")
...> |> IP.Prefix.contains_address?(IP.Address.from_string!("198.51.100.1"))
false

iex> IP.Prefix.from_string!("2001:db8::/64")
...> |> IP.Prefix.contains_address?(IP.Address.from_string!("2001:db8::1"))
true

iex> IP.Prefix.from_string!("2001:db8::/64")
...> |> IP.Prefix.contains_address?(IP.Address.from_string!("2001:db8:1::1"))
false

iex> outside = IP.Prefix.from_string!("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)
contains_prefix?(t, t) :: true | false

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

Examples

iex> outside = IP.Prefix.from_string!("192.0.2.0/24")
...> inside  = IP.Prefix.from_string!("192.0.2.128/25")
...> IP.Prefix.contains_prefix?(outside, inside)
true

iex> outside = IP.Prefix.from_string!("192.0.2.128/25")
...> inside  = IP.Prefix.from_string!("192.0.2.0/24")
...> IP.Prefix.contains_prefix?(outside, inside)
false

iex> outside = IP.Prefix.from_string!("2001:db8::/64")
...> inside  = IP.Prefix.from_string!("2001:db8::/128")
...> IP.Prefix.contains_prefix?(outside, inside)
true

iex> outside = IP.Prefix.from_string!("2001:db8::/128")
...> inside  = IP.Prefix.from_string!("2001:db8::/64")
...> IP.Prefix.contains_prefix?(outside, inside)
false
Link to this function eui_64(prefix, mac)
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> "2001:db8::/64"
...> |> IP.Prefix.from_string!
...> |> 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)
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> "2001:db8::/64"
...> |> IP.Prefix.from_string!
...> |> IP.Prefix.eui_64!("60:f8:1d:ad:d8:90")
#IP.Address<2001:db8::62f8:1dff:fead:d890 DOCUMENTATION>
Link to this function first(prefix)
first(t) :: IP.Address.t

Returns the first address in the prefix.

Examples

iex> IP.Prefix.from_string!("192.0.2.128/24")
...> |> IP.Prefix.first()
#IP.Address<192.0.2.0 DOCUMENTATION>

iex> IP.Prefix.from_string!("2001:db8::128/64")
...> |> IP.Prefix.first()
#IP.Address<2001:db8:: DOCUMENTATION>
Link to this function from_string(prefix)
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>}"

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

iex> "2001:db8::/64"
...> |> IP.Prefix.from_string()
...> |> inspect()
"{:ok, #IP.Prefix<2001:db8::/64>}"
Link to this function from_string(prefix, version)
from_string(binary, 4 | 6) :: {: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>}"

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

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

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

iex> "2001:db8::/64"
...> |> IP.Prefix.from_string!()
#IP.Prefix<2001:db8::/64>
Link to this function from_string!(prefix, version)
from_string!(binary, 4 | 6) :: 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>

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

Returns the last address in the prefix.

Examples

iex> IP.Prefix.from_string!("192.0.2.128/24")
...> |> IP.Prefix.last()
#IP.Address<192.0.2.255 DOCUMENTATION>

iex> IP.Prefix.from_string!("2001:db8::128/64")
...> |> IP.Prefix.last()
#IP.Address<2001:db8::ffff:ffff:ffff:ffff DOCUMENTATION>

Returns the bit-length of the prefix.

Example

iex> "192.0.2.1/24"
...> |> IP.Prefix.from_string!()
...> |> IP.Prefix.length()
24
Link to this function length(prefix, length)

Alter the bit-length of the prefix.

Example

iex> "192.0.2.0/24"
...> |> IP.Prefix.from_string!()
...> |> IP.Prefix.length(25)
#IP.Prefix<192.0.2.0/25>
Link to this function mask(prefix)
mask(t) :: non_neg_integer

Returns the calculated mask of the prefix.

Example

iex> IP.Prefix.from_string!("192.0.2.1/24")
...> |> IP.Prefix.mask()
0b11111111111111111111111100000000

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

Examples

iex> IP.Prefix.new(IP.Address.from_string!("192.0.2.1"), 24)
#IP.Prefix<192.0.2.0/24>

iex> IP.Prefix.new(IP.Address.from_string!("2001:db8::1"), 64)
#IP.Prefix<2001:db8::/64>
Link to this function space(prefix)
space(t) :: non_neg_integer

Return the address space within this address.

Examples

iex> "192.0.2.0/24"
...> |> IP.Prefix.from_string!()
...> |> IP.Prefix.space()
256

iex> "2001:db8::/64"
...> |> IP.Prefix.from_string!()
...> |> IP.Prefix.space()
18446744073709551616
Link to this function subnet_mask(prefix)
subnet_mask(t) :: binary

Returns an old-fashioned subnet mask for IPv4 prefixes.

Example

iex> IP.Prefix.from_string!("192.0.2.0/24")
...> |> IP.Prefix.subnet_mask()
#IP.Address<255.255.255.0 RESERVED>
Link to this function usable(prefix)
usable(t) :: non_neg_integer

Return the usable IP address space within this address.

Examples

iex> "192.0.2.0/24"
...> |> IP.Prefix.from_string!()
...> |> IP.Prefix.usable()
254

iex> "2001:db8::/64"
...> |> IP.Prefix.from_string!()
...> |> IP.Prefix.usable()
18446744073709551616
Link to this function wildcard_mask(prefix)
wildcard_mask(t) :: binary

Returns an “cisco style” wildcard mask for IPv4 prefixes.

Example

iex> IP.Prefix.from_string!("192.0.2.0/24")
...> |> IP.Prefix.wildcard_mask()
#IP.Address<0.0.0.255 CURRENT NETWORK>