Zot.Ip (zot v0.12.0)

View Source

Standalone 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

cidr_set()

@type cidr_set() :: :private | :loopback | :link_local

ip()

@type ip() :: String.t()

ip_tuple()

@type ip_tuple() :: ipv4_tuple() | ipv6_tuple()

ipv4_tuple()

@type ipv4_tuple() :: {0..255, 0..255, 0..255, 0..255}

ipv6_tuple()

@type ipv6_tuple() ::
  {0..65535, 0..65535, 0..65535, 0..65535, 0..65535, 0..65535, 0..65535,
   0..65535}

prefix()

@type prefix() :: non_neg_integer()

Functions

broadcast_address(ip_tuple, prefix_len)

@spec broadcast_address(ip_tuple(), prefix()) :: ip_tuple()

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}

cidrs(atom)

@spec cidrs(cidr_set()) :: [String.t()]

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"]

in_cidr?(ip, set)

@spec in_cidr?(ip() | ip_tuple(), String.t() | [String.t()] | cidr_set()) :: boolean()

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

is_cidr?(string)

@spec is_cidr?(String.t()) :: boolean()

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

is_ip?(string)

@spec is_ip?(String.t()) :: boolean()

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

is_ip?(string, atom)

@spec is_ip?(String.t(), :v4 | :v6) :: boolean()

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

loopback?(ip)

@spec loopback?(ip() | ip_tuple()) :: boolean()

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

network_address(ip_tuple, prefix_len)

@spec network_address(ip_tuple(), prefix()) :: ip_tuple()

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}

parse_cidr(string)

@spec parse_cidr(String.t()) :: {:ok, {ip_tuple(), prefix()}} | :error

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

parse_cidr!(string)

@spec parse_cidr!(String.t()) :: {ip_tuple(), prefix()}

Parses a CIDR string, raising on failure.

Examples

iex> Zot.Ip.parse_cidr!("10.0.0.0/8")
{{10, 0, 0, 0}, 8}

parse_ip(string)

@spec parse_ip(String.t()) :: {:ok, ip_tuple()} | :error

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

parse_ip!(string)

@spec parse_ip!(String.t()) :: ip_tuple()

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}

private?(ip)

@spec private?(ip() | ip_tuple()) :: boolean()

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

to_string(string)

@spec to_string(ip() | ip_tuple()) :: String.t()

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"