LibclusterMesh.CIDR (libcluster_mesh v0.1.0)

Copy Markdown View Source

Minimal IPv4 CIDR matching, for selecting cluster members on meshes that have no hostnames (e.g. plain WireGuard).

Summary

Functions

Returns true when the IPv4 address string falls inside the CIDR.

Parses an IPv4 CIDR string. Raises ArgumentError on invalid input.

Types

t()

@type t() :: {prefix :: 0..4_294_967_295, bits :: 0..32}

Functions

contains?(arg, ip)

@spec contains?(t(), String.t()) :: boolean()

Returns true when the IPv4 address string falls inside the CIDR.

iex> cidr = LibclusterMesh.CIDR.parse!("10.77.0.0/24")
iex> LibclusterMesh.CIDR.contains?(cidr, "10.77.0.13")
true
iex> LibclusterMesh.CIDR.contains?(cidr, "10.78.0.13")
false
iex> LibclusterMesh.CIDR.contains?(cidr, "not-an-ip")
false

parse!(cidr)

@spec parse!(String.t()) :: t()

Parses an IPv4 CIDR string. Raises ArgumentError on invalid input.

iex> LibclusterMesh.CIDR.parse!("10.77.0.0/24")
{172818432, 24}

iex> LibclusterMesh.CIDR.parse!("10.77.0.0/-0")
** (ArgumentError) invalid IPv4 CIDR: "10.77.0.0/-0"