Pfx.sigil_p
sigil_p
, go back to Pfx module for more information.
Specs
Returns either a Pfx struct, a binary or a tuple for the given binary prefix
.
When Pfx is imported, use the sigil ~p
.
This normally returns the same result as Pfx.new/1
, unless modifiers are
used. The following modifiers are supported:
a
returns the address without applying the mask, if anym
returns the mask for givenprefix
f
returns the first full address forprefix
l
returns the last full address forprefix
n
returns the neighbor forprefix
p
returns the direct parent forprefix
(i.e. drops 1 lsb bit)t
returns the trimmed result forprefix
(i.e. drops all trailing zero's)
The modifiers are checked in the order listed and the first one seen gets
applied. The modifiers above can be used in conjunction with the i
modifier, which inverts the result after the regular modifier (if any) has
been applied.
The result's representation can also be modified by:
S
returns the result as a stringT
returns the result as an address,length- or just an address-tuple.
For modifiers that produce a full length prefix (like address, mask, first and last)
the T
modifier returns an address-tuple, otherwise it will be an
address,length-tuple.
When used as a sigil ~p(pfx)
, pfx is given as a string to Pfx.sigil_p/2
and can be:
- an IPv4 prefix string in CIDR notation
- an IPv6 prefix string
- an EUI48 prefix string
- an EUI64 prefix string
Examples
Most examples use the S
modifier for readability.
iex> ~p"1.1.1.1"
%Pfx{bits: <<1, 1, 1, 1>>, maxlen: 32}
# address,length-tuple format
iex> ~p"1.1.1.1"T
{{1, 1, 1, 1}, 32}
iex> ~p"aa-bb-cc-dd-ee-ff"
%Pfx{bits: <<170, 187, 204, 221, 238, 255>>, maxlen: 48}
# address
iex> ~p"1.1.1.1/24"aS
"1.1.1.1"
# address-tuple format
iex> ~p"1.1.1.1/24"aT
{1, 1, 1, 1}
# first address
iex> ~p"1.1.1.1/24"fS
"1.1.1.0"
# last address
iex> ~p"1.1.1.1/24"lS
"1.1.1.255"
# mask
iex> ~p"1.1.1.1/24"mS
"255.255.255.0"
# inverse mask
iex> ~p"1.1.1.1/24"imS
"0.0.0.255"
# just the inverse
iex> ~p"255.255.255.0"iS
"0.0.0.255"
# neighbor (such that it can be combined in a supernet)
iex> ~p"1.1.1.1"nS
"1.1.1.0"
# parent (the supernet containing the prefix given and its neighbor)
iex> ~p"1.1.1.1"pS
"1.1.1.0/31"
# trim an address to get a fitting prefix
iex> ~p"1.1.0.0"tS
"1.1.0.0/16"
# tuple format
iex> ~p"acdc:1976::b1ba/64"T
{{44252, 6518, 0, 0, 0, 0, 0, 0}, 64}
# address-tuple format
iex> ~p"acdc:1976::b1ba/64"aT
{44252, 6518, 0, 0, 0, 0, 0, 45498}
# enumerate a Pfx.t struct
iex> for x <- ~p"1.1.1.0/30", do: "#{x}"
["1.1.1.0", "1.1.1.1", "1.1.1.2", "1.1.1.3"]
iex> ~p"1.1.1.128" in ~p"1.1.1.0/24"
true
Specs
Returns either a Pfx struct, a binary or a tuple for given binary prefixes
.
If mask
is an empty string, both prefix
and modifiers
are simply handed off
to Pfx.sigil_p/2
.
However, if mask
is not an empty string, then it is used to mask the
address portion of prefix1
before handing the result to Pfx.sigil_p/2
along with the modifiers
. Note that, if given, mask
must be the same
type of prefix as prefix
.
This basically allows for piping any prefix representation into ~p()
with an
optional masking twist.
Examples
# address mimics its input format
iex> {{1, 1, 1, 1}, 30}
...> |> address()
{1, 1, 1, 1}
# so, to get a string representation
iex> {{1, 1, 1, 1}, 30}
...> |> address()
...> |> format()
"1.1.1.1"
# or do both in one
iex> {{1, 1, 1, 1}, 30}
...> |> ~p()aS
"1.1.1.1"
# same when modifying the mask
iex> {{1, 1, 1, 1}, 30}
...> |> mask("255.255.255.0")
...> |> format()
"1.1.1.0/24"
# same thing
iex> {{1, 1, 1, 1}, 30}
...> |> ~p(255.255.255.0)S
"1.1.1.0/24"
# count IP's at /24 level
iex> l = ["1.1.1.1", {1, 1, 1, 2}, {{1, 1, 1, 3}, 32}, new("1.1.1.4")]
iex> slash24 = fn x -> x |> ~p(255.255.255.0)S end
iex> incr = fn v -> v + 1 end
iex> Enum.reduce(l, %{}, fn x, acc -> Map.update(acc, slash24.(x), 1, incr) end)
%{"1.1.1.0/24" => 4}
# apply mask, get Pfx struct
iex> "acdc::1" |> ~p(ffff::)
%Pfx{bits: <<172, 220>>, maxlen: 128}
# apply mask, get string representation
iex> "acdc::1" |> ~p(ffff::)S
"acdc::/16"
# apply mask to an EUI48 address
iex> "aa:bb:cc:dd:ee:ff" |> ~p(ff:ff:ff:ff:ff:00)S
"AA-BB-CC-DD-EE-00/40"