Iptools (iptools v0.0.3) View Source

Module Version Hex Docs Total Download License Last Updated

A set of functions for working with IPv4 addresses.

  • is_ipv4?/1 - checks if a string is an IPv4 address.
  • is_rfc1918?/1 - checks if a string is an RFC1918 reserved address.
  • is_reserved?/1 - checks if a string is any kind of reserved address. More complete than just RFC1918.
  • to_integer/1 - convert IP address string to integer.
  • is_between?/3 - checks if the first IP address is between the next two addresses (inclusive).

There are also some functions for manipulating subnet masks.

Reserved IP addresses

You almost certainly want to use is_reserved?/1 to find out if an IP address is "normal" or not. There are a lot of reserved addresses, such as the 0.0.0.0/8 network which are not RFC1918 addresses. So unless you're really checking for just RFC1918, use is_reserved?/1.

Running tests

I've got 100% test coverage for the first time in my whole life! Run mix test to see if they still pass.

License

Copyright (c) 2016, Kevin Thompson.

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.

Link to this section Summary

Functions

Checks if a given IP address is between two other IP addresses (inclusive).

Checks if the given string is an IPv4 address in dotted-decimal notation.

Checks to see if an IP address is reserved for special purposes.

You probably want to use is_reserved?/1.

Counts the bits in a subnet mask. E.g. 255.0.0.0 becomes 8.

Changes a subnet mask to a binary representation. E.g. 255.0.0.0 becomes 11111111000000000000000000000000.

Convert an IP address represented as a dotted-decimal string to an integer.

Converts a dotted-decimal notation IPv4 string to a list of integers.

Link to this section Functions

Link to this function

is_between?(ip, low, high)

View Source

Specs

is_between?(String.t(), String.t(), String.t()) :: boolean()

Checks if a given IP address is between two other IP addresses (inclusive).

If the given ip equals either of the other two IP addresses then it returns true.

Specs

is_ipv4?(String.t()) :: boolean()

Checks if the given string is an IPv4 address in dotted-decimal notation.

Specs

is_reserved?(String.t()) :: boolean()

Checks to see if an IP address is reserved for special purposes.

This includes all of the RFC 1918 addresses as well as other blocks that are reserved by IETF, and IANA for various reasons.

See https://en.wikipedia.org/wiki/Reserved_IP_addresses

Specs

is_rfc1918?(String.t()) :: boolean()

You probably want to use is_reserved?/1.

Determine if an IP address is an RFC1918 address. Reserved for local communications within a private network as specified by RFC 1918.

Returns true if it is an RFC1918 address.

Specs

subnet_bit_count(String.t()) :: integer()

Counts the bits in a subnet mask. E.g. 255.0.0.0 becomes 8.

Specs

subnet_bit_string(String.t()) :: String.t()

Changes a subnet mask to a binary representation. E.g. 255.0.0.0 becomes 11111111000000000000000000000000.

Specs

to_integer(String.t()) :: integer()

Convert an IP address represented as a dotted-decimal string to an integer.

Specs

to_list(String.t()) :: [integer()]

Converts a dotted-decimal notation IPv4 string to a list of integers.