Pfx.mask

You're seeing just the function mask, go back to Pfx module for more information.

Specs

mask(prefix()) :: prefix()

Returns the mask for given pfx.

The result is always a full length prefix.

Examples

iex> mask("10.10.10.0/25")
"255.255.255.128"

iex> mask({10, 10, 10, 0})
{255, 255, 255, 255}

iex> mask({{10, 10, 10, 0}, 25})
{{255, 255, 255, 128}, 32}

iex> mask("acdc:1976::/32")
"ffff:ffff::"

# some prefix with some other maxlen
iex> mask(%Pfx{bits: <<10, 10, 0::1>>, maxlen: 20})
%Pfx{bits: <<255, 255, 8::4>>, maxlen: 20}
Link to this function

mask(prefix, mask, opts \\ [])

View Source

Specs

mask(prefix(), prefix(), Keyword.t()) :: prefix()

Applies mask to given prefix.

The mask is applied through a bitwise AND after which the result is trimmed to the size of mask. Both prefix and mask do not need to be full length prefixes.

Options include:

  • :inv_mask, if true the mask is inverted before applying it (default: false)
  • :trim, if false the result is not trimmed to the size of mask (default: true)

Note that both prefix and mask must be of the same type (same maxlen).

Examples

# trims result by default
iex> mask("1.1.1.1", "255.255.255.0")
"1.1.1.0/24"

iex> mask("1.1.1.1", "255.255.255.0") |> first()
"1.1.1.0"

# same as above
iex> mask("1.1.1.1", "255.255.255.0", trim: false)
"1.1.1.0"

# mirror representation
iex> mask({{1, 1, 1, 1}, 32}, {255, 255, 255, 0})
{{1, 1, 1, 0}, 24}

iex> mask("1.1.1.1", "255.0.0.255")
"1.0.0.1"

# mask need not be full length prefix
iex> mask("1.1.1.1", "255.255.0.0/16")
"1.1.0.0/16"

iex> mask("1.1.1.1", "255.255.0.0/16", trim: false)
"1.1.0.0"

# neither does prefix
iex> mask("1.1.1.0/24", "255.255.0.0/16")
"1.1.0.0/16"

# no trim, so prefix length stays 24 bits
iex> mask("1.1.1.0/24", "255.255.0.0/16", trim: false)
"1.1.0.0/24"

# inverted mask
iex> mask("10.16.0.0", "0.3.255.255", inv_mask: true)
...> |> (fn x -> {x, first(x), last(x)} end).()
{"10.16.0.0/14", "10.16.0.0", "10.19.255.255"}