liaison v0.1.0 Liaison.IP View Source

Allow the conversion of common IP formats to and from

  • string: "1.2.3.4"
  • component: {1, 2, 3, 4}
  • integer: 16909060

Input formats can be equal to output formats

Example

iex> Liaison.IP.as_string("1.2.3.4")
"1.2.3.4"

Link to this section Summary

Functions

Converts any valid address into a tuple {a, b, c, d}

Converts any valid address into an integer

Converts any valid address into a string a.b.c.d

Returns the localhost ip address in component form. If there is more than one ip address for the machine, the nth can be returned. Else nil

Returns true if the given address is valid. This checks if the format is correct, not if the address is functionally valid or accessible

Creates a range of integer ip addresses between the given addresses. Inclusivity: [from, to]

Link to this section Types

Link to this type

format()

View Source
format() :: :integer | :components | :string
Link to this type

ip_addr()

View Source
ip_addr() :: ip() | ip_comp() | String.t()

Link to this section Functions

Link to this function

as_components(ip_addr)

View Source
as_components(ip_addr()) :: ip_comp()

Converts any valid address into a tuple {a, b, c, d}

Example

iex> Liaison.IP.as_components("1.2.3.4")
{1, 2, 3, 4}

iex> Liaison.IP.as_components(16909060)
{1, 2, 3, 4}

iex> Liaison.IP.as_components({1, 2, 3, 4})
{1, 2, 3, 4}
Link to this function

as_integer(ip_addr)

View Source
as_integer(ip_addr()) :: ip()

Converts any valid address into an integer

Example

iex> Liaison.IP.as_integer("1.2.3.4")
16909060

iex> Liaison.IP.as_integer(16909060)
16909060

iex> Liaison.IP.as_integer({1, 2, 3, 4})
16909060
Link to this function

as_string(ip_addr)

View Source
as_string(ip_addr()) :: String.t()

Converts any valid address into a string a.b.c.d

Example

iex> Liaison.IP.as_string("1.2.3.4")
"1.2.3.4"

iex> Liaison.IP.as_string(16909060)
"1.2.3.4"

iex> Liaison.IP.as_string({1, 2, 3, 4})
"1.2.3.4"
Link to this function

get_ipv4_addr(pos \\ 0)

View Source
get_ipv4_addr(integer()) :: ip_comp() | nil

Returns the localhost ip address in component form. If there is more than one ip address for the machine, the nth can be returned. Else nil

Link to this function

is_valid?(ip_addr)

View Source
is_valid?(any()) :: boolean()

Returns true if the given address is valid. This checks if the format is correct, not if the address is functionally valid or accessible

Example

iex> Liaison.IP.is_valid?("1.2.3.4")
true

iex> Liaison.IP.is_valid?({1, 2, 3, 400})
false
Link to this function

to_ip_range(from, to, format \\ :integer)

View Source
to_ip_range(ip_addr(), ip_addr(), format()) :: [ip()] | {:error, String.t()}

Creates a range of integer ip addresses between the given addresses. Inclusivity: [from, to]

Example

iex> Liaison.IP.to_ip_range("1.2.3.4", "1.2.3.6", :string)
["1.2.3.4", "1.2.3.5", "1.2.3.6"]

iex> Liaison.IP.to_ip_range("1.2.3.6", "1.2.3.4")
{:error, "ip1 > ip2"}