Cider v0.3.1 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
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
Check whether a given IP is whitelisted
Link to this section Types
A tuple with each octet of the ip.
raw_ip()
View Source
raw_ip() :: integer()
raw_ip() :: integer()
An IP represented as integer
A cidr made out of {ip
, subnet_mask
}.
Link to this section Functions
contains?(ip, range) View Source
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
ip!(ip) View Source
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(whitelist) View Source
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!(whitelist) View Source
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"
parse(cidr) View Source
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
parse(ip, bit_mask) View Source
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}
parse(a, b, c, d, bit_mask \\ nil) View Source
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}
to_string(ip) View Source
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"
whitelist(whitelist) View Source
Generate an IP whitelist.
Examples
iex> Cider.whitelist("192.168.0.1-3, 192.168.1.0/32")
{:ok, [{3232235776, 4294967295}, 3232235521..3232235523]}
whitelisted?(ip, whitelist) View Source
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