Karta.CIDR (Karta v1.0.0)

IPv4 and IPv6 address ranges in CIDR notation, and address normalisation.

A dual-stack listener on :: sees IPv4 clients as IPv4-mapped IPv6 addresses (::ffff:192.0.2.1, RFC 4291 §2.5.5.2). normalize/1 turns those back into IPv4 tuples, so limits, policies and logs see one form per client.

Summary

Functions

The client an address belongs to, for per-client limits: an IPv4 address itself, an IPv6 address's /64 prefix (an end site usually holds a whole /64, so counting single IPv6 addresses would be trivial to evade). IPv4-mapped addresses count as IPv4.

The canonical text form of a range: the network address (host bits cleared) and the prefix length, e.g. "10.20.0.0/16".

True when ip lies in any of ranges. IPv4-mapped addresses match IPv4 ranges.

The range with its host bits cleared: 10.20.5.9/16 becomes 10.20.0.0/16.

Turns an IPv4-mapped IPv6 address into its IPv4 address; other addresses are unchanged.

Parses "192.0.2.0/24", "2001:db8::/32" or a bare address, which is a host range.

Types

ip()

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

t()

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

Functions

client_key(ip)

@spec client_key(ip()) :: ip()

The client an address belongs to, for per-client limits: an IPv4 address itself, an IPv6 address's /64 prefix (an end site usually holds a whole /64, so counting single IPv6 addresses would be trivial to evade). IPv4-mapped addresses count as IPv4.

format(arg)

@spec format(t()) :: String.t()

The canonical text form of a range: the network address (host bits cleared) and the prefix length, e.g. "10.20.0.0/16".

member?(ranges, ip)

@spec member?([t()], ip()) :: boolean()

True when ip lies in any of ranges. IPv4-mapped addresses match IPv4 ranges.

network(arg)

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

The range with its host bits cleared: 10.20.5.9/16 becomes 10.20.0.0/16.

normalize(ip)

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

Turns an IPv4-mapped IPv6 address into its IPv4 address; other addresses are unchanged.

parse(string)

@spec parse(String.t()) :: {:ok, t()} | {:error, :invalid_cidr}

Parses "192.0.2.0/24", "2001:db8::/32" or a bare address, which is a host range.

parse!(string)

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