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
Functions
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.
Atom | Range(s) | Purpose |
---|---|---|
:this_network | 0.0.0.0/8 | Used for broadcast messages to the current “this” network as specified by RFC 1700, page 4. |
:rfc1918 | 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 | Used for local communications within a private network as specified by RFC 1918. |
:rfc6598 | 100.64.0.0/10 | Used for communications between a service provider and its subscribers when using a Carrier-grade NAT, as specified by RFC 6598. |
:loopback | 127.0.0.0/8 | Used for loopback addresses to the local host, as specified by RFC 990. |
:link_local | 169.254.0.0/16 | Used 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. |
:rfc5736 | 192.0.0.0/24 | Used for the IANA IPv4 Special Purpose Address Registry as specified by RFC 5736. |
:rfc5737 | 192.0.2.0/24 198.51.100.0/24 203.0.113.0/24 | Assigned as “TEST-NET” in RFC 5737 for use solely in documentation and example source code and should not be used publicly. |
:rfc3068 | 192.88.99.0/24 | Used by 6to4 anycast relays as specified by RFC 3068. |
:rfc2544 | 198.18.0.0/15 | Used for testing of inter-network communications between two separate subnets as specified in RFC 2544. |
:multicast | 224.0.0.0/4 | Reserved 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. |
:future | 240.0.0.0/4 | Reserved for future use, as specified by RFC 6890. |
:limited_broadcast | 255.255.255.255/32 | Reserved for the “limited broadcast” destination address, as specified by RFC 6890. |
:public | All other addresses are public. |
Examples
iex> IPA.block("8.8.8.8")
:public
iex> IPA.block("192.168.0.1")
:rfc1918
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
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
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
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
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
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
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
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
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