Linx.IP.Subnet (Linx v0.1.0)

Copy Markdown View Source

An IPv4 or IPv6 subnet — a network address and a prefix length, parsed from CIDR notation.

iex> Linx.IP.Subnet.parse("10.0.0.0/24")
{:ok, ~IP"10.0.0.0/24"}

iex> import Linx.IP
iex> ~IP"fc00::/16"
~IP"fc00::/16"

Subnet operations

iex> import Linx.IP
iex> Linx.IP.Subnet.contains?(~IP"10.0.0.0/8", ~IP"10.99.0.5")
true
iex> Linx.IP.Subnet.network(~IP"10.0.42.5/24")
~IP"10.0.42.0"
iex> Linx.IP.Subnet.broadcast(~IP"10.0.42.5/24")
~IP"10.0.42.255"

Summary

Functions

Returns the IPv4 broadcast address of subnet, or nil for IPv6 (which has no broadcast).

Returns whether subnet contains ip.

Returns subnet's network address — the input address with host bits zeroed.

Parses a CIDR string ("10.0.0.0/24", "fc00::/16") into a subnet.

Types

t()

@type t() :: %Linx.IP.Subnet{address: Linx.IP.t(), prefix: 0..128}

Functions

broadcast(subnet)

@spec broadcast(t()) :: Linx.IP.t() | nil

Returns the IPv4 broadcast address of subnet, or nil for IPv6 (which has no broadcast).

contains?(arg1, arg2)

@spec contains?(t(), Linx.IP.t()) :: boolean()

Returns whether subnet contains ip.

False when the address families differ.

network(subnet)

@spec network(t()) :: Linx.IP.t()

Returns subnet's network address — the input address with host bits zeroed.

parse(string)

@spec parse(binary()) :: {:ok, t()} | {:error, term()}

Parses a CIDR string ("10.0.0.0/24", "fc00::/16") into a subnet.