BitwiseIp.Block.parse
parse
, go back to BitwiseIp.Block module for more information.
Specs
Parses a bitwise IP block from a string in CIDR notation.
This function parses strings in CIDR
notation,
where an IP address is followed by a prefix length composed of a slash (/
)
and a decimal number of leading bits in the subnet mask. The prefix length is
optional. If missing, it defaults to the full width of the IP address: 32
bits for IPv4, 128 for IPv6.
The constituent parts are parsed using BitwiseIp.parse/1
and
BitwiseIp.Mask.parse/2
. The address has the mask applied before
constructing the BitwiseIp.Block
struct, thereby discarding any lower bits.
This parsing is done in an error-safe way by returning a tagged tuple. To
raise an error, use parse!/1
instead.
BitwiseIp.Block
implements the String.Chars
protocol, so parsing can be
undone using to_string/1
.
Examples
iex> BitwiseIp.Block.parse("192.168.0.0/16")
{:ok, %BitwiseIp.Block{proto: :v4, addr: 3232235520, mask: 4294901760}}
iex> BitwiseIp.Block.parse("fc00::/8")
{:ok, %BitwiseIp.Block{proto: :v6, addr: 334965454937798799971759379190646833152, mask: 338953138925153547590470800371487866880}}
iex> BitwiseIp.Block.parse("256.0.0.0/8")
{:error, "Invalid IP address \"256.0.0.0\" in CIDR \"256.0.0.0/8\""}
iex> BitwiseIp.Block.parse("dead::beef/129")
{:error, "Invalid IPv6 mask \"129\" in CIDR \"dead::beef/129\""}
iex> BitwiseIp.Block.parse("192.168.0.0/8") |> elem(1) |> to_string()
"192.0.0.0/8"
iex> BitwiseIp.Block.parse("::") |> elem(1) |> to_string()
"::/128"