Pfx.new

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

Specs

new(ip_address() | ip_prefix() | String.t()) :: t()

Creates a new prefix from address tuples or binaries.

Use:

  • an ipv4 or ipv6 ip_address/0 tuple directly for a full address, or
  • a {ip_address/0, length}-tuple to truncate the bits to length.
  • a binary in CIDR-notation, like "acdc:1976::/32"

Binaries are processed by :inet.parse_address/1, so be aware of IPv4 shorthand notations that may yield surprising results, since digits are taken to be:

  • d1.d2.d3.d4 -> d1.d2.d3.d4 (full address)
  • d1.d2.d3 -> d1.d2.0.d3
  • d1.d2 -> d1.0.0.d2
  • d1 -> 0.0.0.d1

Examples

iex> new({{0xacdc, 0x1976, 0, 0, 0, 0, 0, 0}, 32})
%Pfx{bits: <<0xacdc::16, 0x1976::16>>, maxlen: 128}

iex> new({10, 10, 0, 0})
%Pfx{bits: <<10, 10, 0, 0>>, maxlen: 32}

iex> new({{10, 10, 0, 0}, 16})
%Pfx{bits: <<10, 10>>, maxlen: 32}

iex> new("10.10.0.0")
%Pfx{bits: <<10, 10, 0, 0>>, maxlen: 32}

iex> new("10.10.10.10/16")
%Pfx{bits: <<10, 10>>, maxlen: 32}

# 10.10/16 is interpreted as 10.0.0.10/16 (!)
iex> new("10.10/16")
%Pfx{bits: <<10, 0>>, maxlen: 32}

Specs

new(t() | bitstring(), non_neg_integer()) :: t()

Creates a new Pfx.t/0-prefix.

A prefix can be created from:

  • from a bitstring and a maximum length, truncating the bits as needed,
  • from a Pfx.t/0 prefix and a new maxlen, again truncating as needed,
  • from an ipv4 or ipv6 ip_address/0 tuple
  • from an {ip_address/0, length} tuple

The last form sets the maxlen according to the IP protocol version used, while the length parameter is used to truncate the bits for the prefix.

Examples

iex> new(<<10, 10>>, 32)
%Pfx{bits: <<10, 10>>, maxlen: 32}

iex> new(<<10, 10>>, 8)
%Pfx{bits: <<10>>, maxlen: 8}

# Create a new `Pfx` from an existing one, note:
# this changes the `Pfx`'s meaning
iex> new(<<10, 10>>, 32) |> new(128)
%Pfx{bits: <<10, 10>>, maxlen: 128}