Cider v0.3.3 Cider View Source

Parsing and matching of CIDRs with ips.

Link to this section Summary

Types

A tuple with each octet of the ip.

An IP represented as integer

t()

A cidr made out of {ip, subnet_mask}.

Functions

Remove a CIDR or range from a IP whitelist.

Checks whether a given ip falls within a cidr range.

Returns the raw numeric IP.

Optimize a Cider whitelist by merging overlapping CIDR.

Optimize a Cider whitelist by merging overlapping CIDR.

Parses a CIDR in string representation.

Creates a CIDR based on an ip and bit_mask.

Parses a CIDR in list representation. The first 4 items are the octets of the ip. The last item is the bit mask.

Convert a tuple or numeric IP to string.

Generate an IP whitelist.

Add a CIDR or range to a IP whitelist.

Generate an IP whitelist.

Check whether a given IP is whitelisted.

Link to this section Types

A tuple with each octet of the ip.

An IP represented as integer

A cidr made out of {ip, subnet_mask}.

Link to this section Functions

Link to this function

blacklist(whitelist, cidr)

View Source
blacklist(binary() | [t()], binary() | t()) :: [t()]

Remove a CIDR or range from a IP whitelist.

Example

iex> wl = Cider.whitelist("192.168.0.0/24", "8.8.8.4-8")
iex> Cider.to_string(wl)
"192.168.0.0/24, 8.8.8.4-8"
iex> wl = Cider.blacklist(wl, "192.168.0.5-32")
iex> Cider.to_string(wl)
"192.168.0.128/25, 192.168.0.64/26, 192.168.0.32/27, 8.8.8.4-8, 192.168.0.0-4"
Link to this function

contains?(ip, range)

View Source
contains?(ip() | raw_ip(), t()) :: boolean()

Checks whether a given ip falls within a cidr range.

Example

iex> Cider.contains?({192, 168, 0, 1}, Cider.parse({192, 168, 0, 0}, 24))
true
iex> Cider.contains?(3232235520, Cider.parse({192, 168, 0, 0}, 24))
true
iex> Cider.contains?({192, 168, 254, 1}, Cider.parse({192, 168, 0, 0}, 24))
false

Returns the raw numeric IP.

Examples

iex> Cider.ip!("192.168.1.1")
3_232_235_777
iex> Cider.ip!({192, 168, 1, 1})
3_232_235_777
Link to this function

optimize(whitelist)

View Source
optimize(String.t() | list()) ::
  {:ok, [t()]} | {:error, :invalid_whitelist | :invalid_whitelist_cidr}

Optimize a Cider whitelist by merging overlapping CIDR.

Examples

iex> {:ok, optimized} = Cider.optimize("192.168.0.1-5, 192.168.0.10-20, 192.168.1.1/16, 192.168.0.6-9, 192.168.0.1/24")
iex> Cider.to_string(optimized)
"192.168.0.0/16"
iex> {:ok, optimized} = Cider.optimize("192.168.0.1-5, 192.168.0.10-20, 192.168.0.6-9")
iex> Cider.to_string(optimized)
"192.168.0.1-20"
iex> {:ok, optimized} = Cider.optimize("192.168.0.1-5, 192.168.0.10-20, 192.168.0.6-9, 192.168.0.0/31, 192.168.1.0/31")
iex> Cider.to_string(optimized)
"192.168.0.0-20, 192.168.1.0/31"
Link to this function

optimize!(whitelist)

View Source
optimize!(String.t() | list()) :: [t()]

Optimize a Cider whitelist by merging overlapping CIDR.

See: optimize/1.

Example

iex> optimized = Cider.optimize!("192.168.0.1-5, 192.168.0.10-20, 192.168.1.1/16, 192.168.0.6-9, 192.168.0.1/24")
iex> Cider.to_string(optimized)
"192.168.0.0/16"

Parses a CIDR in string representation.

Examples

iex> Cider.parse "192.168.0.0/24"
{3232235520, 4294967040}
iex> Cider.parse "192.168.0.1-24"
3232235521..3232235544
Link to this function

parse(ip, bit_mask)

View Source
parse(ip(), integer() | nil) :: t()

Creates a CIDR based on an ip and bit_mask.

The ip is represented by an octet tuple.

Examples

iex> Cider.parse {192, 168, 0, 0}, 24
{3232235520, 4294967040}
iex> Cider.parse {0, 0, 0, 0, 0, 65535, 49320, 10754}, 128
{281473913989634, 340282366920938463463374607431768211455}
Link to this function

parse(a, b, c, d, bit_mask \\ nil)

View Source
parse(integer(), integer(), integer(), integer(), integer() | nil) :: t()

Parses a CIDR in list representation. The first 4 items are the octets of the ip. The last item is the bit mask.

Examples

iex> Cider.parse 192, 168, 0, 0, 24
{3232235520, 4294967040}
Link to this function

to_string(ip)

View Source
to_string(tuple() | integer()) :: String.t()

Convert a tuple or numeric IP to string.

Examples

iex> Cider.to_string({192, 168, 1, 1})
"192.168.1.1"
iex> Cider.to_string(3_232_235_777)
"192.168.1.1"
iex> Cider.to_string({0, 0, 0, 0, 0, 65535, 49320, 10754})
"0:0:0:0:0:FFFF:C0A8:2A02"
iex> Cider.to_string(281_473_913_989_634)
"0:0:0:0:0:FFFF:C0A8:2A02"
Link to this function

whitelist(whitelist)

View Source
whitelist(String.t() | [String.t() | t()]) :: {:ok, [t()]} | {:error, atom()}

Generate an IP whitelist.

Examples

iex> Cider.whitelist("192.168.0.1-3, 192.168.1.0/32")
{:ok, [{3232235776, 4294967295}, 3232235521..3232235523]}
Link to this function

whitelist(whitelist, cidr)

View Source
whitelist(binary() | [t()], binary() | t()) :: [t()]

Add a CIDR or range to a IP whitelist.

Example

iex> wl = Cider.whitelist([], "192.168.0.0-23")
iex> Cider.to_string(wl)
"192.168.0.0-23"
iex> wl = Cider.whitelist(wl, "192.168.0.24-31")
iex> Cider.to_string(wl)
"192.168.0.0/27"
Link to this function

whitelist!(whitelist)

View Source
whitelist!(String.t() | [String.t() | t()]) :: [t()]

Generate an IP whitelist.

See: whitelist/1.

Example

iex> Cider.whitelist!("192.168.0.1-3, 192.168.1.0/32")
[{3232235776, 4294967295}, 3232235521..3232235523]

iex> Cider.whitelist!("flurp")
** (RuntimeError) Failed to whitelist. (invalid_whitelist_cidr)
Link to this function

whitelisted?(ip, whitelist)

View Source
whitelisted?(String.t() | ip() | raw_ip(), String.t() | [t()]) :: boolean()

Check whether a given IP is whitelisted.

An empty whitelist will always return false.

Examples

iex> Cider.whitelisted?("192.168.0.2", "192.168.0.1-3, 192.168.1.0/24")
true
iex> Cider.whitelisted?("192.168.1.2", "192.168.0.1-3, 192.168.1.0/24")
true
iex> Cider.whitelisted?("192.168.2.2", "192.168.0.1-3, 192.168.1.0/24")
false