IP.Address (ip v2.0.3)

Simple representations of IP Addresses.

Summary

Types

Valid IP address

Valid IPv4 address - integer between zero and 32 ones.

Valid IPv6 address - integer between zero and 128 ones.

t()

IP address struct type, contains a valid address and version.

Valid IP version (currently only 4 and 6 are deployed in the wild).

Functions

Returns true if the address is an EUI-64 address.

Return a MAC address coded in an EUI-64 address.

Convert a 6to4 IPv6 address to it's correlated IPv6 address.

Convert from (packed) binary representations (either 32 or 128 bits long) into an address.

Convert from a packed binary presentation to an address or raise an IP.Address.InvalidAddress exception.

Convert an integer into an IP address of specified version.

Convert an integer into an IP address of specified version or raise an IP.Address.InvalidAddress exception.

Convert a string representation into an IP address of unknown version.

Convert a string representation into an IP address of specified version.

Convert a string representation into an IP address or raise an IP.Address.InvalidAddress exception.

Convert a string representation into an IP address of specified version or raise an IP.Address.InvalidAddress exception.

Convert an Erlang-style tuple of bytes to an address.

Convert an Erlang-style tuple of bytes to an address.

Generate an IPv6 Unique Local Address

Determine if the IP address is a 6to4 address.

Determine if an IP address is a teredo connection.

Return information about a teredo connection.

Convert an IPv4 address into a 6to4 address.

Returns the IP Address as an integer

Convert an address to an IP.Prefix.

Convert an address into a string.

Convert an address into an Erlang-style tuple.

Returns true if address is version 4.

Returns true if address is version 6.

Returns the IP version of the address.

Types

@type ip() :: ipv4() | ipv6()

Valid IP address

@type ipv4() :: 0..4_294_967_295

Valid IPv4 address - integer between zero and 32 ones.

@type ipv6() :: 0..340_282_366_920_938_463_463_374_607_431_768_211_455

Valid IPv6 address - integer between zero and 128 ones.

@type t() :: %IP.Address{address: ip(), version: version()}

IP address struct type, contains a valid address and version.

@type version() :: 4 | 6

Valid IP version (currently only 4 and 6 are deployed in the wild).

Functions

Link to this function

eui_64?(address)

@spec eui_64?(t()) :: boolean()

Returns true if the address is an EUI-64 address.

Examples

iex> ~i(2001:db8::62f8:1dff:fead:d890)
...> |> IP.Address.eui_64?()
true
Link to this function

eui_64_mac(address)

@spec eui_64_mac(t()) :: {:ok, binary()} | {:error, term()}

Return a MAC address coded in an EUI-64 address.

Examples

iex> ~i(2001:db8::62f8:1dff:fead:d890)
...> |> IP.Address.eui_64_mac()
{:ok, "60f8.1dad.d890"}
Link to this function

from_6to4(address)

@spec from_6to4(t()) :: {:ok, t()} | {:error, term()}

Convert a 6to4 IPv6 address to it's correlated IPv6 address.

Examples

iex> ~i(2002:c000:201::)
...> |> IP.Address.from_6to4()
...> |> inspect()
"{:ok, #IP.Address<192.0.2.1 DOCUMENTATION>}"

iex> ~i(2001:db8::)
...> |> IP.Address.from_6to4()
{:error, "Not a 6to4 address"}
Link to this function

from_binary(arg1)

@spec from_binary(binary()) :: {:ok, t()} | {:error, term()}

Convert from (packed) binary representations (either 32 or 128 bits long) into an address.

Examples

iex> <<192, 0, 2, 1>>
...> |> IP.Address.from_binary()
{:ok, %IP.Address{address: 3221225985, version: 4}}

iex> <<32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>
...> |> IP.Address.from_binary()
{:ok, %IP.Address{address: 42540766411282592856903984951653826560, version: 6}}

iex> "192.0.2.1"
...> |> IP.Address.from_binary()
{:error, "Unable to convert binary to address"}
Link to this function

from_binary!(address)

@spec from_binary!(binary()) :: t()

Convert from a packed binary presentation to an address or raise an IP.Address.InvalidAddress exception.

Examples

iex> <<192, 0, 2, 1>>
...> |> IP.Address.from_binary!()
%IP.Address{address: 3221225985, version: 4}

iex> <<32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1>>
...> |> IP.Address.from_binary!()
%IP.Address{address: 42540766411282592856903984951653826561, version: 6}
Link to this function

from_integer(address, version)

@spec from_integer(ip(), version()) :: {:ok, t()} | {:error, term()}

Convert an integer into an IP address of specified version.

Examples

iex> 3221225985
...> |> IP.Address.from_integer(4)
{:ok, %IP.Address{address: 3221225985, version: 4}}

iex> 42540766411282592856903984951653826561
...> |> IP.Address.from_integer(6)
{:ok, %IP.Address{address: 42540766411282592856903984951653826561, version: 6}}
Link to this function

from_integer!(address, version)

@spec from_integer!(ip(), version()) :: t()

Convert an integer into an IP address of specified version or raise an IP.Address.InvalidAddress exception.

Examples

iex> 3221225985
...> |> IP.Address.from_integer!(4)
%IP.Address{address: 3221225985, version: 4}

iex> 42540766411282592856903984951653826561
...> |> IP.Address.from_integer!(6)
%IP.Address{address: 42540766411282592856903984951653826561, version: 6}
Link to this function

from_string(address)

@spec from_string(binary()) :: {:ok, t()} | {:error, term()}

Convert a string representation into an IP address of unknown version.

Tries to parse the string as IPv6, then IPv4 before failing. Obviously if you know the version then using from_string/2 is faster.

Examples

iex> "192.0.2.1"
...> |> IP.Address.from_string()
{:ok, %IP.Address{address: 3221225985, version: 4}}

iex> "2001:db8::1"
...> |> IP.Address.from_string()
{:ok, %IP.Address{address: 42540766411282592856903984951653826561, version: 6}}
Link to this function

from_string(address, version)

@spec from_string(binary(), version()) :: {:ok, t()} | {:error, term()}

Convert a string representation into an IP address of specified version.

Examples

iex> "192.0.2.1"
...> |> IP.Address.from_string(4)
{:ok, %IP.Address{address: 3221225985, version: 4}}

iex> "2001:db8::1"
...> |> IP.Address.from_string(6)
{:ok, %IP.Address{address: 42540766411282592856903984951653826561, version: 6}}
Link to this function

from_string!(address)

@spec from_string!(binary()) :: t()

Convert a string representation into an IP address or raise an IP.Address.InvalidAddress exception.

Examples

iex> "192.0.2.1"
...> |> IP.Address.from_string!()
%IP.Address{address: 3221225985, version: 4}

iex> "2001:db8::1"
...> |> IP.Address.from_string!()
%IP.Address{address: 42540766411282592856903984951653826561, version: 6}
Link to this function

from_string!(address, version)

@spec from_string!(binary(), version()) :: t()

Convert a string representation into an IP address of specified version or raise an IP.Address.InvalidAddress exception.

Examples

iex> "192.0.2.1"
...> |> IP.Address.from_string!(4)
%IP.Address{address: 3221225985, version: 4}

iex> "2001:db8::1"
...> |> IP.Address.from_string!(6)
%IP.Address{address: 42540766411282592856903984951653826561, version: 6}
Link to this function

from_tuple(arg1)

@spec from_tuple(:inet.ip_address()) :: {:ok, t()} | {:error, term()}

Convert an Erlang-style tuple of bytes to an address.

Examples

iex> {192, 0, 2, 1}
...> |> IP.Address.from_tuple()
{:ok, %IP.Address{address: 3221225985, version: 4}}

iex> {8193, 3512, 0, 0, 0, 0, 0, 1}
...> |> IP.Address.from_tuple()
{:ok, %IP.Address{address: 42540766411282592856903984951653826561, version: 6}}
Link to this function

from_tuple!(tuple)

@spec from_tuple!(:inet.ip_address()) :: t() | no_return()

Convert an Erlang-style tuple of bytes to an address.

Examples

iex> {192, 0, 2, 1}
...> |> IP.Address.from_tuple!()
%IP.Address{address: 3221225985, version: 4}

iex> {8193, 3512, 0, 0, 0, 0, 0, 1}
...> |> IP.Address.from_tuple!()
%IP.Address{address: 42540766411282592856903984951653826561, version: 6}
Link to this function

generate_ula(mac, subnet_id \\ 0, locally_assigned \\ true)

@spec generate_ula(binary(), non_neg_integer(), boolean()) ::
  {:ok, t()} | {:error, term()}

Generate an IPv6 Unique Local Address

Note that the MAC address is just used as a source of randomness, so where you get it from is not important and doesn't restrict this ULA to just that system. See RFC4193

Examples

iex> IP.Address.generate_ula("60:f8:1d:ad:d8:90")
#IP.Address<fd29:f1ef:86a1::>
Link to this function

is_6to4?(address)

@spec is_6to4?(t()) :: boolean()

Determine if the IP address is a 6to4 address.

Examples

iex> ~i(2002:c000:201::)
...> |> IP.Address.is_6to4?()
true

iex> ~i(2001:db8::)
...> |> IP.Address.is_6to4?()
false
Link to this function

is_teredo?(address)

@spec is_teredo?(t()) :: boolean()

Determine if an IP address is a teredo connection.

Examples

iex> ~i(2001::)
...> |> IP.Address.is_teredo?()
true
Link to this function

teredo(address)

@spec teredo(t()) :: {:ok, map()} | {:error, term()}

Return information about a teredo connection.

Examples

iex> ~i(2001:0:4136:e378:8000:63bf:3fff:fdd2)
...> |> IP.Address.teredo()
...> |> Map.get(:server)
#IP.Address<65.54.227.120 GLOBAL UNICAST>

iex> ~i(2001:0:4136:e378:8000:63bf:3fff:fdd2)
...> |> IP.Address.teredo()
...> |> Map.get(:client)
#IP.Address<63.255.253.210 GLOBAL UNICAST>

iex> ~i(2001:0:4136:e378:8000:63bf:3fff:fdd2)
...> |> IP.Address.teredo()
...> |> Map.get(:port)
25535
Link to this function

to_6to4(address)

@spec to_6to4(t()) :: {:ok, t()} | {:error, term()}

Convert an IPv4 address into a 6to4 address.

Examples

iex> ~i(192.0.2.1)
...> |> IP.Address.to_6to4()
#IP.Address<2002:c000:201:: GLOBAL UNICAST (6to4)>
Link to this function

to_integer(address)

@spec to_integer(t()) :: ip()

Returns the IP Address as an integer

Examples

iex> ~i(192.0.2.1)
...> |> IP.Address.to_integer()
3221225985

iex> ~i(2001:db8::1)
...> |> IP.Address.to_integer()
42540766411282592856903984951653826561
Link to this function

to_prefix(address, length)

@spec to_prefix(t(), IP.Prefix.prefix_length()) :: IP.Prefix.t()

Convert an address to an IP.Prefix.

Examples

iex> ~i(192.0.2.1)
...> |> IP.Address.to_prefix(32)
#IP.Prefix<192.0.2.1/32 DOCUMENTATION>
Link to this function

to_string(address)

@spec to_string(t()) :: binary()

Convert an address into a string.

Examples

iex> ~i(192.0.2.1)
...> |> IP.Address.to_string()
"192.0.2.1"

iex> ~i(2001:db8::1)
...> |> IP.Address.to_string()
"2001:db8::1"
Link to this function

to_tuple(address)

@spec to_tuple(t()) :: :inet.ip_address()

Convert an address into an Erlang-style tuple.

Examples

iex> ~i(192.0.2.1)
...> |> IP.Address.to_tuple()
{192, 0, 2, 1}

iex> ~i(2001:db8::1)
...> |> IP.Address.to_tuple()
{8193, 3512, 0, 0, 0, 0, 0, 1}
@spec v4?(t()) :: boolean()

Returns true if address is version 4.

Examples

iex> ~i(192.0.2.1)
...> |> IP.Address.v4?
true

iex> ~i(2001:db8::)
...> |> IP.Address.v4?
false
@spec v6?(t()) :: boolean()

Returns true if address is version 6.

Examples

iex> ~i(192.0.2.1)
...> |> IP.Address.v6?
false

iex> ~i(2001:db8::)
...> |> IP.Address.v6?
true
Link to this function

version(address)

@spec version(t()) :: version()

Returns the IP version of the address.

Examples

iex> ~i(192.0.2.1)
...> |> IP.Address.version()
4

iex> ~i(2001:db8::1)
...> |> IP.Address.version()
6