IP.Range (net_address v0.3.1) View Source

Convenience type which encapsulates the idea of a contiguous range of IP addresses.

NB

The distinction between an IP.Range and an IP.Subnet is that a Subnet must have its bounds at certain powers-of-two and multiple thereof that are governed by the subnet bit-length. A range is not constrained and is a simple "dumb list of ip addresses". Typically ranges will be proper subsets of Subnets.

Enumerable

Implements the Enumerable protocol, so the following sorts of things are possible:

iex> import IP
iex> Enum.map(~i"10.0.0.3..10.0.0.5", &IP.to_string/1)
["10.0.0.3", "10.0.0.4", "10.0.0.5"]

Membership

In the IP.Range implementation of Enumerable, the member?/2 callback is implemented to provide a fastlane membership function. You can thus check IP address membership without having to enumerate all members of the list first.

see also is_in/2.

iex> import IP
iex> ~i"10.0.0.1" in ~i"10.0.0.0..10.0.0.255"
true
iex> ~i"10.0.0.1..10.0.0.33" in ~i"10.0.0.0..10.0.0.255"
true
iex> ~i"10.0.0.0/26" in ~i"10.0.0.0..10.0.0.255"
true
iex> ~i"10.0.0.1..10.0.1.1" in ~i"10.0.0.0..10.0.0.255"
false
iex> ~i"10.0.0.0/22" in ~i"10.0.0.0..10.0.0.255"
false

Link to this section Summary

Types

t()

generic ip range

ip ranges typed to either ipv4 or ipv6

Functions

Finds an ip range in a string, returning an ok or error tuple on failure.

converts a string to an ip range.

true if the ip parameter is inside the range. ip must be a single ip address; if you need a membership function that accepts ranges or subnets, use Kernel.in/2.

true if the argument is an proper ip range

Creates a new IP range, with validation.

converts a ip range to a string with delimiter ".."

Link to this section Types

Specs

t() :: t(IP.v4()) | t(IP.v6())

generic ip range

Specs

t(ip_type) :: %IP.Range{first: ip_type, last: ip_type}

ip ranges typed to either ipv4 or ipv6

Link to this section Functions

Specs

from_string(binary()) :: {:error, :einval} | {:ok, t()}

Finds an ip range in a string, returning an ok or error tuple on failure.

Specs

from_string!(String.t()) :: t()

converts a string to an ip range.

The delimiter must be "..", as this is compatible with both ipv4 and ipv6 addresses

checks if the range is well-ordered.

iex> import IP
iex> IP.Range.from_string!("10.0.0.3..10.0.0.5")
%IP.Range{
  first: {10, 0, 0, 3},
  last: {10, 0, 0, 5}
}
Link to this macro

is_in(range, ip)

View Source (macro)

true if the ip parameter is inside the range. ip must be a single ip address; if you need a membership function that accepts ranges or subnets, use Kernel.in/2.

Be aware of the parameter order, if you are using this after import IP.Range.

usable in guards.

iex> import IP
iex> IP.Range.is_in(~i"10.0.0.1..10.0.0.3", ~i"10.0.0.2")
true
iex> IP.Range.is_in(~i"10.0.0.1..10.0.0.3", ~i"10.0.0.5")
false
Link to this macro

is_range(range)

View Source (macro)

Specs

is_range(any()) :: Macro.t()

true if the argument is an proper ip range

checks if the range is well-ordered.

usable in guards.

iex> import IP
iex> IP.Range.is_range(~i"10.0.0.1..10.0.0.3")
true
iex> IP.Range.is_range(:foo)
false
iex> IP.Range.is_range(%IP.Range{first: ~i"10.0.0.3", last: ~i"10.0.0.1"})
false

Specs

new(IP.addr(), IP.addr()) :: t()

Creates a new IP range, with validation.

If your provide an out-of-order range, it will raise ArgumentError.

iex> IP.Range.new({10, 0, 0, 1}, {10, 0, 0, 5})
%IP.Range{
  first: {10, 0, 0, 1},
  last: {10, 0, 0, 5}
}

Specs

to_string(t()) :: String.t()

converts a ip range to a string with delimiter ".."

checks if the range is well-ordered.

iex> IP.Range.to_string(%IP.Range{first: {10, 0, 0, 3},last: {10, 0, 0, 5}})
"10.0.0.3..10.0.0.5"