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
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
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"
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
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"
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
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}
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}
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"
Generate an IP whitelist.
Examples
iex> Cider.whitelist("192.168.0.1-3, 192.168.1.0/32")
{:ok, [{3232235776, 4294967295}, 3232235521..3232235523]}
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"
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)
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