VintageNet.IP (vintage_net v0.11.1) View Source
This module contains utilities for handling IP addresses.
By far the most important part of handling IP addresses is to pay attention to whether your addresses are names, IP addresses as strings or IP addresses at tuples. This module doesn't resolve names. While IP addresses in string form are convenient to type, nearly all Erlang and Elixir code uses IP addresses in tuple form.
Link to this section Summary
Functions
Convert an IP address w/ prefix to a CIDR-formatted string
Convert an IP address to a string
Convert an IP address to tuple form
Raising version of ip_to_tuple/1
Convert an IPv4 or IPv6 prefix length to a subnet mask.
Convert an IPv4 subnet mask to a prefix length.
Utility function to trim an IP address to its subnet
Link to this section Functions
Specs
cidr_to_string(:inet.ip_address(), VintageNet.prefix_length()) :: String.t()
Convert an IP address w/ prefix to a CIDR-formatted string
Examples:
iex> VintageNet.IP.cidr_to_string({192, 168, 0, 1}, 24)
"192.168.0.1/24"
Specs
ip_to_string(VintageNet.any_ip_address()) :: String.t()
Convert an IP address to a string
Examples:
iex> VintageNet.IP.ip_to_string({192, 168, 0, 1})
"192.168.0.1"
iex> VintageNet.IP.ip_to_string("192.168.9.1")
"192.168.9.1"
iex> VintageNet.IP.ip_to_string({65152, 0, 0, 0, 0, 0, 0, 1})
"fe80::1"
Specs
ip_to_tuple(VintageNet.any_ip_address()) :: {:ok, :inet.ip_address()} | {:error, String.t()}
Convert an IP address to tuple form
Examples:
iex> VintageNet.IP.ip_to_tuple("192.168.0.1")
{:ok, {192, 168, 0, 1}}
iex> VintageNet.IP.ip_to_tuple({192, 168, 1, 1})
{:ok, {192, 168, 1, 1}}
iex> VintageNet.IP.ip_to_tuple("fe80::1")
{:ok, {65152, 0, 0, 0, 0, 0, 0, 1}}
iex> VintageNet.IP.ip_to_tuple({65152, 0, 0, 0, 0, 0, 0, 1})
{:ok, {65152, 0, 0, 0, 0, 0, 0, 1}}
iex> VintageNet.IP.ip_to_tuple("bologna")
{:error, "Invalid IP address: bologna"}
Specs
ip_to_tuple!(VintageNet.any_ip_address()) :: :inet.ip_address()
Raising version of ip_to_tuple/1
Specs
prefix_length_to_subnet_mask(:inet | :inet6, VintageNet.prefix_length()) :: :inet.ip_address()
Convert an IPv4 or IPv6 prefix length to a subnet mask.
Examples:
iex> VintageNet.IP.prefix_length_to_subnet_mask(:inet, 24)
{255, 255, 255, 0}
iex> VintageNet.IP.prefix_length_to_subnet_mask(:inet, 28)
{255, 255, 255, 240}
iex> VintageNet.IP.prefix_length_to_subnet_mask(:inet6, 64)
{65535, 65535, 65535, 65535, 0, 0, 0, 0}
Specs
subnet_mask_to_prefix_length(:inet.ip_address()) :: {:ok, VintageNet.prefix_length()} | {:error, String.t()}
Convert an IPv4 subnet mask to a prefix length.
Examples:
iex> VintageNet.IP.subnet_mask_to_prefix_length({255, 255, 255, 0})
{:ok, 24}
iex> VintageNet.IP.subnet_mask_to_prefix_length({192, 168, 1, 1})
{:error, "{192, 168, 1, 1} is not a valid IPv4 subnet mask"}
Specs
to_subnet(:inet.ip_address(), VintageNet.prefix_length()) :: :inet.ip_address()
Utility function to trim an IP address to its subnet
Examples:
iex> VintageNet.IP.to_subnet({192, 168, 1, 50}, 24)
{192, 168, 1, 0}
iex> VintageNet.IP.to_subnet({192, 168, 255, 50}, 22)
{192, 168, 252, 0}
iex> VintageNet.IP.to_subnet({64768, 43690, 0, 0, 4144, 58623, 65276, 33158}, 64)
{64768, 43690, 0, 0, 0, 0, 0, 0}