Cooper.IPv4 (Cooper v0.1.0)

Copy Markdown

A parsed IPv4 address, with an optional CIDR prefix (CASC.md §6.7 -- 127.0.0.1 or 127.0.0.1/32). new/2 is the only way to build one, and it's the same validation Cooper.Actions runs on a bare literal at parse time -- an out-of-range octet or CIDR prefix is a load-time Ichor.Error, never a crash or a silently-accepted garbage value.

Summary

Functions

The block's broadcast address (its own prefix carried through unchanged).

Whether other's address falls within cidr's block.

The first usable host address in the block. For a /31 or /32, where every address is usable (no address is reserved as a pure network id), this is the network address itself.

The last usable host address in the block. For a /31 or /32, where every address is usable (no address is reserved as a pure broadcast id), this is the broadcast address itself.

The netmask implied by the block's prefix, as a bare address (no prefix of its own).

The block's network address (its own prefix carried through unchanged).

Builds a validated address. address must be a 4-tuple of 0..255 integers; prefix, if given, must be 0..32.

Types

t()

@type t() :: %Cooper.IPv4{address: :inet.ip4_address(), prefix: 0..32 | nil}

Functions

broadcast(cidr)

@spec broadcast(t()) :: t()

The block's broadcast address (its own prefix carried through unchanged).

contains?(cidr, other)

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

Whether other's address falls within cidr's block.

first_host(cidr)

@spec first_host(t()) :: t()

The first usable host address in the block. For a /31 or /32, where every address is usable (no address is reserved as a pure network id), this is the network address itself.

last_host(cidr)

@spec last_host(t()) :: t()

The last usable host address in the block. For a /31 or /32, where every address is usable (no address is reserved as a pure broadcast id), this is the broadcast address itself.

netmask(cidr)

@spec netmask(t()) :: t()

The netmask implied by the block's prefix, as a bare address (no prefix of its own).

network(cidr)

@spec network(t()) :: t()

The block's network address (its own prefix carried through unchanged).

new(address, prefix \\ nil)

@spec new(:inet.ip4_address(), 0..32 | nil) :: {:ok, t()} | {:error, String.t()}

Builds a validated address. address must be a 4-tuple of 0..255 integers; prefix, if given, must be 0..32.