NetAddr v1.0.5 NetAddr View Source

General functions for network address parsing and manipulation, with support for addresses of arbitrary size.

Link to this section Summary

Functions

Returns a human-readable string for the address part of netaddr.

Return the address length of netaddr.

Returns a new NetAddr.t/0 with the address part of netaddr and the given address length.

Returns size of netaddr in bytes.

Bitwise ANDs two address binaries, returning the result.

Converts address to a decimal.

Returns a human-readable string for the last address in ipv4_netaddr.

Tests whether netaddr contains netaddr2, up to equality.

Constructs a NetAddr.t/0 struct given an Erlang/OTP IP address tuple.

Returns a new NetAddr.t/0 with the first address in netaddr.

Parses ip_string as an IPv4/IPv6 address or CIDR, returning a NetAddr.IPv4.t/0 or NetAddr.IPv6.t/0 as appropriate.

Parses ip_address_string with the given address length or ip_mask_string.

Identical to ip/1, but returns {:ok, value} on success instead of just value.

Identical to ip/2, but returns {:ok, value} on success instead of just value.

Tests whether netaddr has length equal to its size in bits.

Tests whether string can be parsed as an IP address.

Tests whether string can be parsed as an IPv4 address.

Tests whether string can be parsed as an IPv6 address.

Returns a new NetAddr.t/0 with the last address in netaddr.

Converts address_length to an address mask binary.

Parses mac_string, returning a NetAddr.MAC_48.t/0.

Identical to mac_48/1, but returns {:ok, value} on success instead of just value.

Converts address_mask to an address length.

Convert address_mask to an address length.

Constructs a NetAddr.t/0 struct given a network address binary.

Constructs a NetAddr.t/0 struct given a network address binary and an address length.

Identical to netaddr/1, but returns {:ok, value} on success instead of just value.

Identical to netaddr/2, but returns {:ok, value} on success instead of just value.

Identical to netaddr/3, but returns {:ok, value} on success instead of just value.

Constructs an Erlang/OTP IP address tuple given a NetAddr.t/0.

Converts a NetAddr.t/0 to a list of bytes.

Converts a NetAddr.t/0 to a format suitable for DNS PTR records.

Convert IPv4 netaddr to a regular expression.

Returns a human-readable CIDR or pseudo-CIDR for netaddr.

Returns a human-readable string for the first address in netaddr.

Converts decimal to an address.

Returns a human-readable CIDR for the first address in netaddr.

Convert a DNS PTR record name to a NetAddr.t/0.

Converts range to a NetAddr.t/0 given an address size hint.

Succinctly describe IP NetAddrs at compile time.

Returns a human-readable address mask for ipv4_netaddr.

Link to this section Types

Link to this section Functions

Returns a human-readable string for the address part of netaddr.

Examples

iex> NetAddr.address NetAddr.ip("192.0.2.1/24")
"192.0.2.1"

iex> NetAddr.address NetAddr.netaddr(<<1, 2, 3, 4, 5>>)
"0x0102030405"
Link to this function

address_length(netaddr) View Source
address_length(NetAddr.t()) :: non_neg_integer()

Return the address length of netaddr.

Examples

iex> NetAddr.address_length NetAddr.ip("192.0.2.1/24")
24
Link to this function

address_length(netaddr, new_length) View Source
address_length(NetAddr.t(), pos_integer()) :: NetAddr.t()

Returns a new NetAddr.t/0 with the address part of netaddr and the given address length.

Examples

iex> NetAddr.ip("192.0.2.1/24")
...>   |> NetAddr.address_length(22)
%NetAddr.IPv4{address: <<192, 0, 2, 1>>, length: 22}
Link to this function

address_size(netaddr) View Source
address_size(NetAddr.t()) :: pos_integer()

Returns size of netaddr in bytes.

Examples

iex> NetAddr.address_size NetAddr.ip("192.0.2.1")
4

iex> NetAddr.address_size NetAddr.ip("::")
16

iex> NetAddr.address_size NetAddr.mac_48("c0:ff:33:c0:ff:33")
6

iex> NetAddr.address_size NetAddr.netaddr(<<1, 2, 3, 4, 5>>)
5
Link to this function

apply_mask(address, mask) View Source
apply_mask(binary(), binary()) :: binary()

Bitwise ANDs two address binaries, returning the result.

Examples

iex> NetAddr.apply_mask <<192,0,2,1>>, <<255,255,255,0>>
<<192, 0, 2, 0>>

iex> NetAddr.apply_mask <<192,0,2,1>>, <<14,249,150,22>>
<<0,0,2,0>>

Converts address to a decimal.

Examples

iex> NetAddr.aton <<192,0,2,1>>
3221225985

iex> NetAddr.aton <<254, 128, 0, 0, 193, 0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 1>>
338288524986991696549538495105230488577

iex> NetAddr.aton(<<1,2,3,4,5>>)
4328719365
Link to this function

broadcast(ipv4_netaddr) View Source
broadcast(NetAddr.IPv4.t()) :: String.t()

Returns a human-readable string for the last address in ipv4_netaddr.

Examples

iex> NetAddr.broadcast NetAddr.ip("192.0.2.1/24")
"192.0.2.255"
Link to this function

contains?(netaddr1, netaddr2) View Source
contains?(NetAddr.t(), NetAddr.t()) :: boolean() | none()

Tests whether netaddr contains netaddr2, up to equality.

Examples

iex> NetAddr.ip("192.0.2.0/24")
...>   |> NetAddr.contains?(NetAddr.ip("192.0.2.0/25"))
true

iex> NetAddr.ip("192.0.2.0/24")
...>   |> NetAddr.contains?(NetAddr.ip("192.0.2.0/24"))
true

iex> NetAddr.ip("192.0.2.0/25")
...>   |> NetAddr.contains?(NetAddr.ip("192.0.2.0/24"))
false

iex> NetAddr.ip("192.0.2.0/25")
...>   |> NetAddr.contains?(NetAddr.ip("192.0.2.128/25"))
false

iex> NetAddr.ip("192.0.2.3/31")
...>   |> NetAddr.contains?(NetAddr.ip("192.0.2.2"))
true
Link to this function

erl_ip_to_netaddr(erl_ip) View Source
erl_ip_to_netaddr(erl_ip()) :: {:ok, NetAddr.t()} | {:error, :einval}

Constructs a NetAddr.t/0 struct given an Erlang/OTP IP address tuple.

Examples

iex> NetAddr.erl_ip_to_netaddr({192, 0, 2, 1})
{:ok, %NetAddr.IPv4{address: <<192, 0, 2, 1>>, length: 32}}

iex> NetAddr.erl_ip_to_netaddr({0x2001, 0xdb8, 0, 0, 0, 0, 0, 1})
{:ok, %NetAddr.IPv6{address: <<0x2001::16, 0xdb8::16, 0::5*16, 1::16>>, length: 128}}
Link to this function

first_address(netaddr) View Source
first_address(NetAddr.t()) :: NetAddr.t()

Returns a new NetAddr.t/0 with the first address in netaddr.

Examples

iex> NetAddr.first_address NetAddr.ip("192.0.2.1/24")
%NetAddr.IPv4{address: <<192, 0, 2, 0>>, length: 24}
Link to this function

ip(ip_string) View Source
ip(String.t()) :: NetAddr.IPv4.t() | NetAddr.IPv6.t() | {:error, :einval}

Parses ip_string as an IPv4/IPv6 address or CIDR, returning a NetAddr.IPv4.t/0 or NetAddr.IPv6.t/0 as appropriate.

Examples

iex> NetAddr.ip "192.0.2.1"
%NetAddr.IPv4{address: <<192,0,2,1>>, length: 32}

iex> NetAddr.ip "192.0.2.1/24"
%NetAddr.IPv4{address: <<192,0,2,1>>, length: 24}

iex> NetAddr.ip "fe80::c101"
%NetAddr.IPv6{address: <<0xfe,0x80,0::12*8,0xc1,0x01>>, length: 128}

iex> NetAddr.ip "fe80::c101/64"
%NetAddr.IPv6{address: <<0xfe,0x80,0::12*8,0xc1,0x01>>, length: 64}

iex> NetAddr.ip "blarg"
{:error, :einval}
Link to this function

ip(ip_address_string, ip_mask_string_or_length) View Source
ip(String.t(), nil) :: NetAddr.IPv4.t() | NetAddr.IPv6.t() | {:error, :einval}
ip(String.t(), String.t()) ::
  NetAddr.IPv4.t() | NetAddr.IPv6.t() | {:error, :einval}
ip(String.t(), non_neg_integer()) ::
  NetAddr.IPv4.t() | NetAddr.IPv6.t() | {:error, :einval}

Parses ip_address_string with the given address length or ip_mask_string.

Examples

iex> NetAddr.ip "0.0.0.0", 0
%NetAddr.IPv4{address: <<0, 0, 0, 0>>, length: 0}

iex> NetAddr.ip "192.0.2.1", 24
%NetAddr.IPv4{address: <<192, 0, 2, 1>>, length: 24}

iex> NetAddr.ip "192.0.2.1", "255.255.255.0"
%NetAddr.IPv4{address: <<192, 0, 2, 1>>, length: 24}

iex> NetAddr.ip "fe80:0:c100::c401", 64
%NetAddr.IPv6{address: <<254, 128, 0, 0, 193, 0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 1>>, length: 64}

iex> NetAddr.ip "blarg", 32
{:error, :einval}

iex> NetAddr.ip "192.0.2.010"
%NetAddr.IPv4{address: <<192, 0, 2, 10>>, length: 32}

iex> NetAddr.ip "192.0.2.1/33"
{:error, :einval}
Link to this function

ip_2(ip_string) View Source
ip_2(String.t()) ::
  {:ok, NetAddr.IPv4.t() | NetAddr.IPv4.t()} | {:error, :einval}

Identical to ip/1, but returns {:ok, value} on success instead of just value.

Examples

iex> NetAddr.ip_2 "192.0.2.1"
{:ok, %NetAddr.IPv4{address: <<192,0,2,1>>, length: 32}}

iex> NetAddr.ip_2 "192.0.2.1/24"
{:ok, %NetAddr.IPv4{address: <<192,0,2,1>>, length: 24}}

iex> NetAddr.ip_2 "fe80::c101"
{:ok, %NetAddr.IPv6{address: <<0xfe,0x80,0::12*8,0xc1,0x01>>, length: 128}}

iex> NetAddr.ip_2 "fe80::c101/64"
{:ok, %NetAddr.IPv6{address: <<0xfe,0x80,0::12*8,0xc1,0x01>>, length: 64}}

iex> NetAddr.ip_2 "blarg"
{:error, :einval}
Link to this function

ip_2(ip_address_string, ip_mask_string) View Source
ip_2(String.t(), nil) ::
  {:ok, NetAddr.IPv4.t() | NetAddr.IPv6.t()} | {:error, :einval}
ip_2(String.t(), String.t()) ::
  {:ok, NetAddr.IPv4.t() | NetAddr.IPv6.t()} | {:error, :einval}
ip_2(String.t(), non_neg_integer()) ::
  {:ok, NetAddr.IPv4.t() | NetAddr.IPv6.t()} | {:error, :einval}

Identical to ip/2, but returns {:ok, value} on success instead of just value.

Examples

iex> NetAddr.ip_2 "0.0.0.0", 0
{:ok, %NetAddr.IPv4{address: <<0, 0, 0, 0>>, length: 0}}

iex> NetAddr.ip_2 "192.0.2.1", 24
{:ok, %NetAddr.IPv4{address: <<192, 0, 2, 1>>, length: 24}}

iex> NetAddr.ip_2 "192.0.2.1", "255.255.255.0"
{:ok, %NetAddr.IPv4{address: <<192, 0, 2, 1>>, length: 24}}

iex> NetAddr.ip_2 "192.0.2.1", "14.249.150.22"
{:error, :einval}

iex> NetAddr.ip_2 "fe80:0:c100::c401", 64
{:ok, %NetAddr.IPv6{address: <<254, 128, 0, 0, 193, 0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 1>>, length: 64}}

iex> NetAddr.ip_2 "blarg", 32
{:error, :einval}
Link to this function

is_host_address(netaddr) View Source
is_host_address(NetAddr.t()) :: boolean()

Tests whether netaddr has length equal to its size in bits.

Examples

iex> NetAddr.is_host_address NetAddr.ip("0.0.0.0/0")
false

iex> NetAddr.is_host_address NetAddr.ip("192.0.2.1")
true

iex> NetAddr.is_host_address NetAddr.ip("fe80:0:c100::c401")
true

iex> NetAddr.is_host_address NetAddr.ip("::/0")
false

Tests whether string can be parsed as an IP address.

Examples

iex> NetAddr.is_ip "not an IP address"
false

iex> NetAddr.is_ip %{}
false

iex> NetAddr.is_ip "0.0.0.0/0"
true

iex> NetAddr.is_ip "192.0.2.1"
true

iex> NetAddr.is_ip "fe80:0:c100::c401"
true

iex> NetAddr.is_ip "::/0"
true

Tests whether string can be parsed as an IPv4 address.

Examples

iex> NetAddr.is_ipv4 "not an IP address"
false

iex> NetAddr.is_ip %{}
false

iex> NetAddr.is_ipv4 "0.0.0.0/0"
true

iex> NetAddr.is_ipv4 "192.0.2.1"
true

iex> NetAddr.is_ipv4 "fe80:0:c100::c401"
false

iex> NetAddr.is_ipv4 "::/0"
false

Tests whether string can be parsed as an IPv6 address.

Examples

iex> NetAddr.is_ipv6 "not an IP address"
false

iex> NetAddr.is_ip %{}
false

iex> NetAddr.is_ipv6 "0.0.0.0/0"
false

iex> NetAddr.is_ipv6 "192.0.2.1"
false

iex> NetAddr.is_ipv6 "fe80:0:c100::c401"
true

iex> NetAddr.is_ipv6 "::/0"
true
Link to this function

last_address(netaddr) View Source
last_address(NetAddr.t()) :: NetAddr.t()

Returns a new NetAddr.t/0 with the last address in netaddr.

Examples

iex> NetAddr.last_address NetAddr.ip("192.0.2.1/24")
%NetAddr.IPv4{address: <<192, 0, 2, 255>>, length: 24}
Link to this function

length_to_mask(address_length, mask_length_in_bytes) View Source
length_to_mask(non_neg_integer(), pos_integer()) :: binary()

Converts address_length to an address mask binary.

Examples

iex> NetAddr.length_to_mask(30, 4)
<<255, 255, 255, 252>>

iex> NetAddr.length_to_mask(64, 16)
<<255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0>>

iex> NetAddr.length_to_mask(37, 6)
<<255, 255, 255, 255, 248, 0>>
Link to this function

mac_48(mac_string) View Source
mac_48(binary()) :: NetAddr.MAC_48.t() | {:error, :einval}

Parses mac_string, returning a NetAddr.MAC_48.t/0.

For manifest reasons, the corresponding parser may be robust to the point of returning incorrect results. Caveat emptor.

Examples

iex> NetAddr.mac_48 "01:23:45:67:89:AB"
%NetAddr.MAC_48{address: <<0x01,0x23,0x45,0x67,0x89,0xab>>, length: 48}

iex> NetAddr.mac_48 "01-23-45-67-89-AB"
%NetAddr.MAC_48{address: <<0x01,0x23,0x45,0x67,0x89,0xab>>, length: 48}

iex> NetAddr.mac_48 "0123456789aB"
%NetAddr.MAC_48{address: <<0x01,0x23,0x45,0x67,0x89,0xab>>, length: 48}

iex> NetAddr.mac_48 "01 23 45 67 89 AB"
%NetAddr.MAC_48{address: <<0x01,0x23,0x45,0x67,0x89,0xab>>, length: 48}

iex> NetAddr.mac_48 "\"0fF:33-C0.Ff   33 \""
%NetAddr.MAC_48{address: <<0x0f, 0xf, 0x33, 0xc0, 0xff, 0x33>>, length: 48}

iex> NetAddr.mac_48 "1:2:3:4:5:6"
%NetAddr.MAC_48{address: <<1,2,3,4,5,6>>, length: 48}

iex> NetAddr.mac_48 "01-23-45-67-89-ag"
%NetAddr.MAC_48{address: <<0x01,0x23,0x45,0x67,0x89,0xa>>, length: 48}

iex> NetAddr.mac_48 "123456789aB"
%NetAddr.MAC_48{address: <<0x12,0x34,0x56,0x78,0x9a,0xb>>, length: 48}

iex> NetAddr.mac_48 "blarg"
{:error, :einval}
Link to this function

mac_48_2(mac_string) View Source
mac_48_2(binary()) :: {:ok, NetAddr.MAC_48.t()} | {:error, :einval}

Identical to mac_48/1, but returns {:ok, value} on success instead of just value.

Examples

iex> NetAddr.mac_48_2 "01:23:45:67:89:AB"
{:ok, %NetAddr.MAC_48{address: <<0x01,0x23,0x45,0x67,0x89,0xab>>, length: 48}}

iex> NetAddr.mac_48_2 "01-23-45-67-89-AB"
{:ok, %NetAddr.MAC_48{address: <<0x01,0x23,0x45,0x67,0x89,0xab>>, length: 48}}

iex> NetAddr.mac_48_2 "0123456789aB"
{:ok, %NetAddr.MAC_48{address: <<0x01,0x23,0x45,0x67,0x89,0xab>>, length: 48}}

iex> NetAddr.mac_48_2 "01 23 45 67 89 AB"
{:ok, %NetAddr.MAC_48{address: <<0x01,0x23,0x45,0x67,0x89,0xab>>, length: 48}}

iex> NetAddr.mac_48_2 "\"0fF:33-C0.Ff   33 \""
{:ok, %NetAddr.MAC_48{address: <<0x0f, 0xf, 0x33, 0xc0, 0xff, 0x33>>, length: 48}}

iex> NetAddr.mac_48_2 "1:2:3:4:5:6"
{:ok, %NetAddr.MAC_48{address: <<1,2,3,4,5,6>>, length: 48}}

iex> NetAddr.mac_48_2 "01-23-45-67-89-ag"
{:ok, %NetAddr.MAC_48{address: <<0x01,0x23,0x45,0x67,0x89,0xa>>, length: 48}}

iex> NetAddr.mac_48_2 "123456789aB"
{:ok, %NetAddr.MAC_48{address: <<0x12,0x34,0x56,0x78,0x9a,0xb>>, length: 48}}

iex> NetAddr.mac_48_2 "blarg"
{:error, :einval}
Link to this function

mask_to_length(address_mask) View Source
mask_to_length(binary()) :: non_neg_integer()

Converts address_mask to an address length.

Examples

iex> NetAddr.mask_to_length(<<255,255,248,0>>)
21
Link to this function

mask_to_length_2(address_mask) View Source
mask_to_length_2(binary()) :: {:ok, non_neg_integer()} | {:error, :einval}

Convert address_mask to an address length.

Unlike mask_to_length/1, this function returns {:ok, length}, on success, and {:error, :einval}, otherwise. In particular, this function rejects a mask that contains non-consecutive ones bits.

Examples

iex> NetAddr.mask_to_length_2(<<255,255,248,0>>)
{:ok, 21}

iex> NetAddr.mask_to_length_2(<<14,249,150,22>>)
{:error, :einval}
Link to this function

netaddr(address) View Source
netaddr(<<_::8, _::_*8>>) ::
  NetAddr.Generic.t()
  | NetAddr.IPv4.t()
  | NetAddr.IPv6.t()
  | NetAddr.MAC_48.t()
  | {:error, :einval}

Constructs a NetAddr.t/0 struct given a network address binary.

Examples

iex> NetAddr.netaddr <<1, 2, 3, 4, 5, 6>>
%NetAddr.MAC_48{address: <<1, 2, 3, 4, 5, 6>>, length: 48}

iex> NetAddr.netaddr <<1, 2, 3, 4, 5>>
%NetAddr.Generic{address: <<1, 2, 3, 4, 5>>, length: 40}
Link to this function

netaddr(address, address_length) View Source
netaddr(<<_::8, _::_*8>>, pos_integer()) ::
  NetAddr.Generic.t()
  | NetAddr.IPv4.t()
  | NetAddr.IPv6.t()
  | NetAddr.MAC_48.t()
  | {:error, :einval}

Constructs a NetAddr.t/0 struct given a network address binary and an address length.

Link to this function

netaddr(address, address_length, size_in_bytes) View Source

Explicitly constructs a NetAddr.Generic.t/0 struct.

Examples

iex> NetAddr.netaddr(<<1, 2, 3, 4, 5, 6>>, 48, 6)
%NetAddr.Generic{address: <<1, 2, 3, 4, 5, 6>>, length: 48}

iex> NetAddr.netaddr(<<1, 2, 3, 4, 5>>, 48, 6)
%NetAddr.Generic{address: <<0, 1, 2, 3, 4, 5>>, length: 48}
Link to this function

netaddr_2(address) View Source
netaddr_2(<<_::8, _::_*8>>) ::
  {:ok,
   NetAddr.Generic.t()
   | NetAddr.IPv4.t()
   | NetAddr.IPv6.t()
   | NetAddr.MAC_48.t()}
  | {:error, :einval}

Identical to netaddr/1, but returns {:ok, value} on success instead of just value.

Examples

iex> NetAddr.netaddr_2(<<1, 2, 3, 4, 5, 6>>)
{:ok, %NetAddr.MAC_48{address: <<1, 2, 3, 4, 5, 6>>, length: 48}}

iex> NetAddr.netaddr_2(<<1, 2, 3, 4, 5>>)
{:ok, %NetAddr.Generic{address: <<1, 2, 3, 4, 5>>, length: 40}}
Link to this function

netaddr_2(address, address_length) View Source
netaddr_2(<<_::8, _::_*8>>, pos_integer()) ::
  {:ok,
   NetAddr.Generic.t()
   | NetAddr.IPv4.t()
   | NetAddr.IPv6.t()
   | NetAddr.MAC_48.t()}
  | {:error, :einval}

Identical to netaddr/2, but returns {:ok, value} on success instead of just value.

Examples

iex> NetAddr.netaddr_2(<<1,2,3,4>>, 16)
{:ok, %NetAddr.IPv4{address: <<1,2,3,4>>, length: 16}}

iex> NetAddr.netaddr_2(<<1,2,3,4>>, 33)
{:error, :einval}
Link to this function

netaddr_2(address, address_length, size_in_bytes) View Source
netaddr_2(binary(), non_neg_integer(), pos_integer()) ::
  {:ok, NetAddr.Generic.t()}

Identical to netaddr/3, but returns {:ok, value} on success instead of just value.

Examples

iex> NetAddr.netaddr_2(<<1, 2, 3, 4, 5, 6>>, 48, 6)
{:ok, %NetAddr.Generic{address: <<1, 2, 3, 4, 5, 6>>, length: 48}}

iex> NetAddr.netaddr_2(<<1, 2, 3, 4, 5>>, 48, 6)
{:ok, %NetAddr.Generic{address: <<0, 1, 2, 3, 4, 5>>, length: 48}}
Link to this function

netaddr_to_erl_ip(netaddr) View Source
netaddr_to_erl_ip(NetAddr.t()) :: {:ok, erl_ip()} | {:error, :einval}

Constructs an Erlang/OTP IP address tuple given a NetAddr.t/0.

Examples

iex> NetAddr.netaddr_to_erl_ip NetAddr.ip("192.0.2.1")
{192, 0, 2, 1}

iex> NetAddr.netaddr_to_erl_ip NetAddr.ip("2001:db8::1")
{0x2001, 0xdb8, 0, 0, 0, 0, 0, 1}
Link to this function

netaddr_to_list(netaddr) View Source
netaddr_to_list(NetAddr.t()) :: [byte()]

Converts a NetAddr.t/0 to a list of bytes.

Examples

iex> NetAddr.ip("192.0.2.3/24")
...>   |> NetAddr.netaddr_to_list
[192, 0, 2, 3]
Link to this function

netaddr_to_ptr(netaddr) View Source
netaddr_to_ptr(NetAddr.t()) :: {:ok, String.t()} | {:error, :einval}

Converts a NetAddr.t/0 to a format suitable for DNS PTR records.

Examples

iex> NetAddr.netaddr_to_ptr NetAddr.ip("192.0.2.1")
{:ok, "1.2.0.192.in-addr.arpa"}

iex> NetAddr.netaddr_to_ptr NetAddr.ip("2001:db8::1")
{:ok, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa"}
Link to this function

netaddr_to_range(netaddr) View Source
netaddr_to_range(NetAddr.t()) :: Range.t()

Converts a NetAddr.t/0 to a t:Range.t/0.

Examples

iex> NetAddr.netaddr_to_range NetAddr.ip("198.51.100.0/24")
3325256704..3325256959
Link to this function

netaddr_to_regex(netaddr) View Source
netaddr_to_regex(NetAddr.IPv4.t()) :: Regex.t()

Convert IPv4 netaddr to a regular expression.

Examples

iex> NetAddr.netaddr_to_regex NetAddr.ip("192.0.2.0/23")
~r/\b192\.0\.[2-3]\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b/

iex> NetAddr.netaddr_to_regex NetAddr.ip("192.0.64.0/17")
~r/\b192\.0\.([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-7])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b/
Link to this function

netaddr_to_string(netaddr) View Source
netaddr_to_string(NetAddr.t()) :: String.t()

Returns a human-readable CIDR or pseudo-CIDR for netaddr.

This is like NetAddr.prefix/1 except host bits are not set to zero. All String.Chars implementations call this function.

Examples

iex> NetAddr.netaddr_to_string %NetAddr.Generic{address: <<1, 2, 3, 4, 5>>, length: 32}
"0x0102030405/32"

Returns a human-readable string for the first address in netaddr.

Examples

iex> NetAddr.network NetAddr.ip("192.0.2.1/24")
"192.0.2.0"
Link to this function

ntoa(decimal, size_in_bytes) View Source

Converts decimal to an address.

Examples

iex> NetAddr.ntoa 3221225985, 4
<<192, 0, 2, 1>>

iex> NetAddr.ntoa 338288524986991696549538495105230488577, 16
<<254, 128, 0, 0, 193, 0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 1>>

iex> NetAddr.ntoa 4328719365, 5
<<1, 2, 3, 4, 5>>

Returns a human-readable CIDR for the first address in netaddr.

Examples

iex> NetAddr.prefix %NetAddr.IPv4{address: <<192, 0, 2, 1>>, length: 24}
"192.0.2.0/24"

iex> NetAddr.prefix %NetAddr.Generic{address: <<1, 2, 3, 4, 5>>, length: 32}
"0x0102030400/32"
Link to this function

ptr_to_netaddr(ptr_name) View Source
ptr_to_netaddr(String.t()) :: {:ok, NetAddr.t()} | {:error, :einval}

Convert a DNS PTR record name to a NetAddr.t/0.

Examples

iex> NetAddr.ptr_to_netaddr "1.2.0.192.in-addr.arpa"
{:ok, %NetAddr.IPv4{address: <<192,0,2,1>>, length: 32}}

iex> NetAddr.ptr_to_netaddr "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa"
{:ok, %NetAddr.IPv6{address: <<0x2001::2*8,0xdb8::2*8,0::11*8,1>>, length: 128}}
Link to this function

range_to_netaddr(range, size_in_bytes) View Source
range_to_netaddr(Range.t(), pos_integer()) :: NetAddr.t()

Converts range to a NetAddr.t/0 given an address size hint.

Examples

iex> NetAddr.range_to_netaddr 3325256704..3325256959, 4
%NetAddr.IPv4{address: <<198, 51, 100, 0>>, length: 24}
Link to this macro

sigil_p(term, modifiers) View Source (macro)

Succinctly describe IP NetAddrs at compile time.

Examples

iex> use NetAddr
iex> ~p"192.0.2.1/24"
%NetAddr.IPv4{address: <<192,0,2,1>>, length: 24}

iex> use NetAddr
iex> ~p"2001:db8::1"
%NetAddr.IPv6{
  address: <<0x2001::16,0xdb8::16,0::5*16,1::16>>,
  length: 128,
}

iex> use NetAddr
iex> ~p(192.0.2.1/24 2001:db8::1)
[ %NetAddr.IPv4{address: <<192,0,2,1>>, length: 24},
  %NetAddr.IPv6{
    address: <<0x2001::16,0xdb8::16,0::5*16,1::16>>,
    length: 128,
  },
]
Link to this function

subnet_mask(ipv4_netaddr) View Source
subnet_mask(NetAddr.IPv4.t()) :: String.t()

Returns a human-readable address mask for ipv4_netaddr.

Examples

iex> NetAddr.subnet_mask %NetAddr.IPv4{address: <<192, 0, 2, 1>>, length: 24}
"255.255.255.0"