Socket.Address (sockets v2.1.7)

Copy Markdown

Utilities for working with IP addresses and hostnames.

Handles parsing IP strings into tuples, converting them back to strings, resolving a hostname to its list of IP addresses, and checking whether a given IP address falls within a particular subnet. Works with both IPv4 and IPv6.

Summary

Functions

Get the addresses for the given host.

Get the addresses for the given host, raising if an error occurs.

Check if an IPv4/IPv6 address belongs to a network

Parse a string to an ip address tuple.

Convert an ip address tuple to a string.

Check if the passed string is a valid IP address.

Types

ipaddr()

@type ipaddr() :: :inet.ip_address()

t()

@type t() :: String.t() | charlist() | ipaddr()

Functions

for(host, family)

@spec for(t(), :inet.address_family()) :: {:ok, [t()]} | {:error, :inet.posix()}
@spec for(t(), :inet.address_family()) :: {:ok, [t()]} | {:error, :inet.posix()}
@spec for(t(), :inet.address_family()) :: {:ok, [t()]} | {:error, :inet.posix()}

Get the addresses for the given host.

for!(host, family)

@spec for!(t(), :inet.address_family()) :: [t()]

Get the addresses for the given host, raising if an error occurs.

is_in_subnet?(addr, net, netsize)

@spec is_in_subnet?(ipaddr(), ipaddr(), 1..128) :: boolean()

Check if an IPv4/IPv6 address belongs to a network

parse(text)

@spec parse(t()) :: ipaddr() | nil

Parse a string to an ip address tuple.

to_string(address)

@spec to_string(t()) :: String.t()

Convert an ip address tuple to a string.

valid?(text)

@spec valid?(t()) :: boolean()

Check if the passed string is a valid IP address.