NetAddr (NetAddr v1.3.1) 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
.
Compare two NetAddrs.
Tests whether netaddr
contains netaddr2
, up to
equality.
Test whether two NetAddrs are strictly contiguous (i.e. can be more succinctly represented as a single, shorter prefix that is not equal to either of the given prefixes).
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.
Calculate the join, or least upper bound, of two netaddrs. This effectively returns the smallest summary that contains both netaddrs.
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.
Calculate the meet, or greatest lower bound, of two netaddrs. If the two provided netaddrs overlap, this function returns the lowest, smallest netaddr. Otherwise, it returns nil.
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.
Explicitly constructs a NetAddr.Generic.t/0
struct.
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.
Converts a NetAddr.t/0
to a
t:Range.t/0
.
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
Specs
erl_ip() :: ipv4_tuple() | ipv6_tuple()
Specs
Specs
Specs
sexdectet() :: 0..65535
Specs
t() :: NetAddr.Generic.t() | NetAddr.IPv4.t() | NetAddr.IPv6.t() | NetAddr.MAC_48.t()
Link to this section Functions
Specs
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"
Specs
address_length(t()) :: non_neg_integer()
Return the address length of netaddr
.
Examples
iex> NetAddr.address_length NetAddr.ip("192.0.2.1/24")
24
Specs
address_length(t(), pos_integer()) :: 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}
Specs
address_size(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
Specs
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>>
Specs
aton(binary()) :: non_neg_integer()
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
Specs
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"
Specs
Compare two NetAddrs.
Prefixes with lower addresses are less than prefixes with higher addresses. Shorter prefixes are less than longer prefixes having the same address.
Examples
iex> use NetAddr
iex> NetAddr.compare(~p"192.0.2.0/24", ~p"198.51.100.0/24")
:lt
iex> NetAddr.compare(~p"192.0.2.0/24", ~p"192.0.2.0/25")
:lt
iex> NetAddr.compare(~p"192.0.2.0/24", ~p"192.0.2.0/24")
:eq
iex> NetAddr.compare(~p"192.0.2.0/25", ~p"192.0.2.0/24")
:gt
iex> NetAddr.compare(~p"198.51.100.0/24", ~p"192.0.2.0/24")
:gt
Specs
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
Specs
Test whether two NetAddrs are strictly contiguous (i.e. can be more succinctly represented as a single, shorter prefix that is not equal to either of the given prefixes).
Examples
iex> use NetAddr
iex> NetAddr.contiguous?(~p"192.0.2.0/24", ~p"192.0.2.0/24")
false
iex> NetAddr.contiguous?(~p"192.0.2.0/24", ~p"192.0.2.0/25")
false
iex> NetAddr.contiguous?(~p"192.0.2.0/24", ~p"198.51.100.0/24")
false
iex> NetAddr.contiguous?(~p"192.0.2.64/26", ~p"192.0.2.128/25")
false
iex> NetAddr.contiguous?(~p"192.0.2.0/25", ~p"192.0.2.128/25")
true
Specs
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}}
Specs
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}
Specs
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}
Specs
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}
Specs
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}
Specs
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}
Specs
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
Specs
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
Specs
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
Specs
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
Specs
Calculate the join, or least upper bound, of two netaddrs. This effectively returns the smallest summary that contains both netaddrs.
Examples
iex> use NetAddr
iex> NetAddr.join(~p(192.0.2.0/26), ~p(192.0.2.192/26))
%NetAddr.IPv4{address: <<192,0,2,0>>, length: 24}
iex> use NetAddr
iex> NetAddr.join(~p(192.0.2.0/26), ~p(192.0.2.128/25))
%NetAddr.IPv4{address: <<192,0,2,0>>, length: 24}
iex> use NetAddr
iex> NetAddr.join(~p(192.0.2.0/26), ~p(192.0.2.64/26))
%NetAddr.IPv4{address: <<192,0,2,0>>, length: 25}
iex> use NetAddr
iex> NetAddr.join(~p(192.0.2.7/26), ~p(192.0.2.78/26))
%NetAddr.IPv4{address: <<192,0,2,0>>, length: 25}
iex> use NetAddr
iex> NetAddr.join(~p(192.0.2.64/26), ~p(192.0.2.128/26))
%NetAddr.IPv4{address: <<192,0,2,0>>, length: 24}
iex> use NetAddr
iex> NetAddr.join(~p(192.0.2.0/24), ~p(192.0.2.128/25))
%NetAddr.IPv4{address: <<192,0,2,0>>, length: 24}
iex> use NetAddr
iex> NetAddr.join(~p(192.0.2.0/24), ~p(192.0.2.0/25))
%NetAddr.IPv4{address: <<192,0,2,0>>, length: 24}
iex> use NetAddr
iex> NetAddr.join(~p(192.0.2.0/24), ~p(192.0.2.0/24))
%NetAddr.IPv4{address: <<192,0,2,0>>, length: 24}
Specs
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}
Specs
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>>
Specs
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}
Specs
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}
Specs
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
Specs
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}
Specs
Calculate the meet, or greatest lower bound, of two netaddrs. If the two provided netaddrs overlap, this function returns the lowest, smallest netaddr. Otherwise, it returns nil.
Examples
iex> use NetAddr
iex> NetAddr.meet(~p(192.0.2.0/26), ~p(192.0.2.192/26))
nil
iex> use NetAddr
iex> NetAddr.meet(~p(192.0.2.0/26), ~p(192.0.2.128/25))
nil
iex> use NetAddr
iex> NetAddr.meet(~p(192.0.2.0/26), ~p(192.0.2.64/26))
nil
iex> use NetAddr
iex> NetAddr.meet(~p(192.0.2.7/26), ~p(192.0.2.78/26))
nil
iex> use NetAddr
iex> NetAddr.meet(~p(192.0.2.64/26), ~p(192.0.2.128/26))
nil
iex> use NetAddr
iex> NetAddr.meet(~p(192.0.2.0/24), ~p(192.0.2.128/25))
nil
iex> use NetAddr
iex> NetAddr.meet(~p(192.0.2.0/24), ~p(192.0.2.0/25))
%NetAddr.IPv4{address: <<192,0,2,0>>, length: 25}
iex> use NetAddr
iex> NetAddr.meet(~p(192.0.2.0/24), ~p(192.0.2.0/24))
%NetAddr.IPv4{address: <<192,0,2,0>>, length: 24}
Specs
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}
Specs
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.
Specs
netaddr(binary(), non_neg_integer(), pos_integer()) :: NetAddr.Generic.t()
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}
Specs
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}}
Specs
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}
Specs
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}}
Specs
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}
Specs
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]
Specs
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"}
Specs
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
Specs
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/
Specs
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"
Specs
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"
Specs
ntoa(non_neg_integer(), pos_integer()) :: binary()
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>>
Specs
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"
Specs
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}}
Specs
range_to_netaddr(Range.t(), pos_integer()) :: 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}
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,
},
]
Specs
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"