Zot.Ip (zot v0.12.0)
View SourceStandalone utility module for IP address and CIDR operations.
Provides parsing, predicate checks, and network calculations for both
IPv4 and IPv6 addresses. Accepts both string and :inet tuple
representations where applicable.
Summary
Functions
Computes the broadcast address for a given IP and prefix length.
Returns a list of CIDR strings for the given predefined set.
Checks whether an IP address falls within a CIDR range, a list of CIDR ranges, or a predefined CIDR set.
Returns true if the given string is a valid CIDR notation.
Returns true if the given string is a valid IP address.
Returns true if the given string is a valid IP address of the
specified version.
Returns true if the IP address is a link-local address.
Returns true if the IP address is a loopback address.
Computes the network address for a given IP and prefix length.
Parses a CIDR string (e.g. "192.168.0.0/24") into a tuple of
{ip_tuple, prefix_length}.
Parses a CIDR string, raising on failure.
Parses an IP address string into an :inet tuple.
Parses an IP address string into an :inet tuple, raising on failure.
Returns true if the IP address is in a private range.
Converts an :inet IP tuple to its string representation.
Types
@type cidr_set() :: :private | :loopback | :link_local
@type ip() :: String.t()
@type ip_tuple() :: ipv4_tuple() | ipv6_tuple()
@type ipv4_tuple() :: {0..255, 0..255, 0..255, 0..255}
@type ipv6_tuple() ::
{0..65535, 0..65535, 0..65535, 0..65535, 0..65535, 0..65535, 0..65535,
0..65535}
@type prefix() :: non_neg_integer()
Functions
Computes the broadcast address for a given IP and prefix length.
Examples
iex> Zot.Ip.broadcast_address({192, 168, 1, 130}, 24)
{192, 168, 1, 255}
Returns a list of CIDR strings for the given predefined set.
Examples
iex> Zot.Ip.cidrs(:private)
["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "fc00::/7"]
iex> Zot.Ip.cidrs(:loopback)
["127.0.0.0/8", "::1/128"]
iex> Zot.Ip.cidrs(:link_local)
["169.254.0.0/16", "fe80::/10"]
Checks whether an IP address falls within a CIDR range, a list of CIDR ranges, or a predefined CIDR set.
The IP can be given as a string or an :inet tuple.
Examples
iex> Zot.Ip.in_cidr?("10.0.0.1", "10.0.0.0/8")
true
iex> Zot.Ip.in_cidr?("10.0.0.1", ["10.0.0.0/8", "172.16.0.0/12"])
true
iex> Zot.Ip.in_cidr?("10.0.0.1", :private)
true
iex> Zot.Ip.in_cidr?("8.8.8.8", :private)
false
Returns true if the given string is a valid CIDR notation.
Examples
iex> Zot.Ip.is_cidr?("192.168.0.0/24")
true
iex> Zot.Ip.is_cidr?("fc00::/7")
true
iex> Zot.Ip.is_cidr?("not_a_cidr")
false
Returns true if the given string is a valid IP address.
Examples
iex> Zot.Ip.is_ip?("192.168.1.1")
true
iex> Zot.Ip.is_ip?("::1")
true
iex> Zot.Ip.is_ip?("not_an_ip")
false
Returns true if the given string is a valid IP address of the
specified version.
Examples
iex> Zot.Ip.is_ip?("192.168.1.1", :v4)
true
iex> Zot.Ip.is_ip?("::1", :v4)
false
iex> Zot.Ip.is_ip?("::1", :v6)
true
iex> Zot.Ip.is_ip?("192.168.1.1", :v6)
false
Returns true if the IP address is a link-local address.
Examples
iex> Zot.Ip.link_local?("169.254.1.1")
true
iex> Zot.Ip.link_local?("10.0.0.1")
false
Returns true if the IP address is a loopback address.
Examples
iex> Zot.Ip.loopback?("127.0.0.1")
true
iex> Zot.Ip.loopback?({0, 0, 0, 0, 0, 0, 0, 1})
true
Computes the network address for a given IP and prefix length.
Examples
iex> Zot.Ip.network_address({192, 168, 1, 130}, 24)
{192, 168, 1, 0}
Parses a CIDR string (e.g. "192.168.0.0/24") into a tuple of
{ip_tuple, prefix_length}.
Examples
iex> Zot.Ip.parse_cidr("192.168.0.0/24")
{:ok, {{192, 168, 0, 0}, 24}}
iex> Zot.Ip.parse_cidr("fc00::/7")
{:ok, {{64512, 0, 0, 0, 0, 0, 0, 0}, 7}}
iex> Zot.Ip.parse_cidr("garbage")
:error
Parses a CIDR string, raising on failure.
Examples
iex> Zot.Ip.parse_cidr!("10.0.0.0/8")
{{10, 0, 0, 0}, 8}
Parses an IP address string into an :inet tuple.
Wraps :inet.parse_address/1.
Examples
iex> Zot.Ip.parse_ip("192.168.1.1")
{:ok, {192, 168, 1, 1}}
iex> Zot.Ip.parse_ip("::1")
{:ok, {0, 0, 0, 0, 0, 0, 0, 1}}
iex> Zot.Ip.parse_ip("not_an_ip")
:error
Parses an IP address string into an :inet tuple, raising on failure.
Examples
iex> Zot.Ip.parse_ip!("10.0.0.1")
{10, 0, 0, 1}
Returns true if the IP address is in a private range.
Examples
iex> Zot.Ip.private?("192.168.1.1")
true
iex> Zot.Ip.private?("8.8.8.8")
false
Converts an :inet IP tuple to its string representation.
Examples
iex> Zot.Ip.to_string({192, 168, 1, 1})
"192.168.1.1"
iex> Zot.Ip.to_string({0, 0, 0, 0, 0, 0, 0, 1})
"::1"