ipa v0.0.2 IPA

Functions for working with IP addresses.

Currently only compatible with IPv4 addresses.

Summary

Functions

Returns an atom describing which reserved block the address is a member of if it is a private address, returns :public otherwise

Checks whether a given IP address is reserved

Converts CIDR, binary, hexadecimal, dotted binary and tuple notation IP address/subnet mask to a 0b prefixed binary number

Converts CIDR, binary, hexadecimal, dotted binary and tuple notation IP address/subnet mask to binary bits

Converts a dotted decimal, hex, binary, tuple & dotted binary Subnet Mask to CIDR notation

Converts CIDR, binary, hexadecimal, dotted binary and tuple notation IP address/subnet mask to dotted decimal

Converts CIDR, binary, hexadecimal, dotted binary and tuple notation IP address/subnet mask to a 0x prefixed hexadecimal number

Converts a dotted decimal IP address or Subnet Mask, or a CIDR notation Subnet Mask, to a 4 element tuple, representing the 4 octets

Checks if the given IP address is valid

Checks if the given subnet mask is valid

Types

ip :: addr | mask
mask :: String.t | non_neg_integer

Functions

block(addr)

Specs

block(String.t) :: atom

Returns an atom describing which reserved block the address is a member of if it is a private address, returns :public otherwise.

Available blocks:

AtomRange(s)Purpose
:this_network0.0.0.0/8Used for broadcast messages to the current “this” network as specified by RFC 1700, page 4.
:rfc191810.0.0.0/8 172.16.0.0/12 192.168.0.0/16Used for local communications within a private network as specified by RFC 1918.
:rfc6598100.64.0.0/10Used for communications between a service provider and its subscribers when using a Carrier-grade NAT, as specified by RFC 6598.
:loopback127.0.0.0/8Used for loopback addresses to the local host, as specified by RFC 990.
:link_local169.254.0.0/16Used for link-local addresses between two hosts on a single link when no IP address is otherwise specified, such as would have normally been retrieved from a DHCP server, as specified by RFC 3927.
:rfc5736192.0.0.0/24Used for the IANA IPv4 Special Purpose Address Registry as specified by RFC 5736.
:rfc5737192.0.2.0/24 198.51.100.0/24 203.0.113.0/24Assigned as “TEST-NET” in RFC 5737 for use solely in documentation and example source code and should not be used publicly.
:rfc3068192.88.99.0/24Used by 6to4 anycast relays as specified by RFC 3068.
:rfc2544198.18.0.0/15Used for testing of inter-network communications between two separate subnets as specified in RFC 2544.
:multicast224.0.0.0/4Reserved for multicast assignments as specified in RFC 5771. 233.252.0.0/24 is assigned as “MCAST-TEST-NET” for use solely in documentation and example source code.
:future240.0.0.0/4Reserved for future use, as specified by RFC 6890.
:limited_broadcast255.255.255.255/32Reserved for the “limited broadcast” destination address, as specified by RFC 6890.
:publicAll other addresses are public.

Examples

iex> IPA.block("8.8.8.8")
:public
iex> IPA.block("192.168.0.1")
:rfc1918
reserved?(addr)

Specs

reserved?(String.t) :: boolean

Checks whether a given IP address is reserved.

Examples

iex> IPA.reserved?("192.168.0.1")
true
iex> IPA.reserved?("8.8.8.8")
false
to_binary(ip)

Specs

to_binary(ip) :: String.t

Converts CIDR, binary, hexadecimal, dotted binary and tuple notation IP address/subnet mask to a 0b prefixed binary number.

Example

iex> IPA.to_binary("192.168.0.1")
"0b11000000101010000000000000000001"
iex> IPA.to_binary("0xC0A80001")
"0b11000000101010000000000000000001"
iex> IPA.to_binary("11000000.10101000.00000000.00000001")
"0b11000000101010000000000000000001"
iex> IPA.to_binary({192, 168, 0, 1})
"0b11000000101010000000000000000001"
iex> IPA.to_binary("255.255.255.0")
"0b11111111111111111111111100000000"
iex> IPA.to_binary(24)
"0b11111111111111111111111100000000"
iex> IPA.to_binary("255.255.256.0")
** (IPError) Invalid IP Address
to_bits(ip)

Specs

to_bits(ip) :: String.t

Converts CIDR, binary, hexadecimal, dotted binary and tuple notation IP address/subnet mask to binary bits.

Example

iex> IPA.to_bits("192.168.0.1")
"11000000.10101000.00000000.00000001"
iex> IPA.to_bits("0xC0A80001")
"11000000.10101000.00000000.00000001"
iex> IPA.to_bits({192, 168, 0, 1})
"11000000.10101000.00000000.00000001"
iex> IPA.to_bits("0b11000000101010000000000000000001")
"11000000.10101000.00000000.00000001"
iex> IPA.to_bits("255.255.255.0")
"11111111.11111111.11111111.00000000"
iex> IPA.to_bits(24)
"11111111.11111111.11111111.00000000"
iex> IPA.to_bits("192.168.0.256")
** (IPError) Invalid IP Address
to_cidr(mask)

Converts a dotted decimal, hex, binary, tuple & dotted binary Subnet Mask to CIDR notation.

Examples

iex> IPA.to_cidr("255.255.255.0")
24
iex> IPA.to_cidr("0xFFFFFF00")
24
iex> IPA.to_cidr("0b11111111111111111111111100000000")
24
iex> IPA.to_cidr({255, 255, 255, 0})
24
iex> IPA.to_cidr("11111111.11111111.11111111.00000000")
24
iex> IPA.to_cidr("192.168.0.1")
** (SubnetError) Invalid Subnet Mask
to_dotted_dec(ip)

Specs

to_dotted_dec(ip) :: String.t

Converts CIDR, binary, hexadecimal, dotted binary and tuple notation IP address/subnet mask to dotted decimal.

Example

iex> IPA.to_dotted_dec(24)
"255.255.255.0"
iex> IPA.to_dotted_dec({192, 168, 0, 1})
"192.168.0.1"
iex> IPA.to_dotted_dec("0b11000000101010000000000000000001")
"192.168.0.1"
iex> IPA.to_dotted_dec("0xC0A80001")
"192.168.0.1"
iex> IPA.to_dotted_dec("11000000.10101000.00000000.00000001")
"192.168.0.1"
iex> IPA.to_dotted_dec(33)
** (SubnetError) Invalid Subnet Mask
to_hex(ip)

Specs

to_hex(ip) :: String.t

Converts CIDR, binary, hexadecimal, dotted binary and tuple notation IP address/subnet mask to a 0x prefixed hexadecimal number.

Example

iex> IPA.to_hex({192, 168, 0, 1})
"0xC0A80001"
iex> IPA.to_hex("255.255.255.0")
"0xFFFFFF00"
iex> IPA.to_hex("192.168.0.1")
"0xC0A80001"
iex> IPA.to_hex("0b11000000101010000000000000000001")
"0xC0A80001"
iex> IPA.to_hex("11000000.10101000.00000000.00000001")
"0xC0A80001"
iex> IPA.to_hex(24)
"0xFFFFFF00"
iex> IPA.to_hex("192.168.0.256")
** (IPError) Invalid IP Address
to_octets(ip)

Specs

to_octets(ip) :: {integer}

Converts a dotted decimal IP address or Subnet Mask, or a CIDR notation Subnet Mask, to a 4 element tuple, representing the 4 octets.

Example

iex> IPA.to_octets("192.168.0.1")
{192, 168, 0, 1}
iex> IPA.to_octets("255.255.255.0")
{255, 255, 255, 0}
iex> IPA.to_octets("0b11000000101010000000000000000001")
{192, 168, 0, 1}
iex> IPA.to_octets("0xC0A80001")
{192, 168, 0, 1}
iex> IPA.to_octets("11000000.10101000.00000000.00000001")
{192, 168, 0, 1}
iex> IPA.to_octets(24)
{255, 255, 255, 0}
iex> IPA.to_octets("192.168.0.256")
** (IPError) Invalid IP Address
valid_address?(addr)

Specs

valid_address?(addr) :: boolean

Checks if the given IP address is valid.

Does not currently take into consideration the fact that 127.1 can be considered a valid IP address that translates to 127.0.0.1.

Examples

iex> IPA.valid_address?("192.168.0.1")
true
iex> IPA.valid_address?("8.8.8.8")
true
iex> IPA.valid_address?("192.168.0.256")
false
iex> IPA.valid_address?("192.168.0")
false
iex> IPA.valid_address?("192.168.0.1.1")
false
iex> IPA.valid_address?("11000000.10101000.00000000.00000001")
true
iex> IPA.valid_address?("0xC0A80001")
true
iex> IPA.valid_address?("0b11000000101010000000000000000001")
true
iex> IPA.valid_address?({192, 168, 0, 1})
true
valid_mask?(mask)

Specs

valid_mask?(mask) :: boolean

Checks if the given subnet mask is valid.

Examples

iex> IPA.valid_mask?(24)
true
iex> IPA.valid_mask?(33)
false
iex> IPA.valid_mask?("255.255.255.0")
true
iex> IPA.valid_mask?("192.168.0.1")
false
iex> IPA.valid_mask?("11111111.11111111.11111111.00000000")
true
iex> IPA.valid_mask?("10101000.10101000.00000000.00000000")
false
iex> IPA.valid_mask?("0xFFFFFF00")
true
iex> IPA.valid_mask?("0b11111111111111111111111100000000")
true
iex> IPA.valid_mask?({255, 255, 255, 0})
true