Macaw.Plug.Ip (macaw_plug v0.1.4)

Copy Markdown View Source

IP addresses and CIDR blocks: parsing, membership and normalization.

An IPv4 address that arrives in IPv4-mapped IPv6 form (::ffff:203.0.113.5, as a dual-stack listener can report its peer) is normalized to its IPv4 form, so a trusted IPv4 proxy matches however the socket reported it.

Summary

Functions

Whether ip is in any of the CIDRs.

Whether ip is in the CIDR {network, prefix}. Families never match each other.

An IPv4-mapped IPv6 address as its IPv4 form; any other address unchanged.

Parses an address string to a normalized tuple, or returns nil.

Parses a CIDR ("10.0.0.0/8", "2001:db8::/32") or a single address ("192.0.2.1", a /32 or /128) to {network, prefix}, or returns an error naming what is wrong.

Formats an address.

Types

cidr()

@type cidr() :: {ip(), 0..128}

ip()

@type ip() :: :inet.ip_address()

Functions

in_any?(ip, cidrs)

@spec in_any?(ip(), [cidr()]) :: boolean()

Whether ip is in any of the CIDRs.

in_cidr?(ip, arg2)

@spec in_cidr?(ip(), cidr()) :: boolean()

Whether ip is in the CIDR {network, prefix}. Families never match each other.

normalize(ip)

@spec normalize(ip()) :: ip()

An IPv4-mapped IPv6 address as its IPv4 form; any other address unchanged.

parse(text)

@spec parse(String.t()) :: ip() | nil

Parses an address string to a normalized tuple, or returns nil.

parse_cidr(text)

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

Parses a CIDR ("10.0.0.0/8", "2001:db8::/32") or a single address ("192.0.2.1", a /32 or /128) to {network, prefix}, or returns an error naming what is wrong.

to_string(ip)

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

Formats an address.