IP

The IP module provides functions for parsing and classifying IPv4 and IPv6 addresses.

IPv4 Functions

IP.parse-v4
String -> Result {a, b, c, d} String
Parses an IPv4 address string into a record with four octets.
match IP.parse-v4 "192.168.1.1"
  | Ok ip -> println "${ip.a}.${ip.b}.${ip.c}.${ip.d}"
  | Err msg -> println "Invalid: ${msg}"
# => "192.168.1.1"
IP.v4-to-string
{a, b, c, d} -> String
Converts an IPv4 record to a string.
IP.v4-to-int
{a, b, c, d} -> Int
Converts an IPv4 address to a 32-bit integer.
match IP.parse-v4 "192.168.1.1"
  | Ok ip -> println (IP.v4-to-int ip)
  | Err _ -> println "Error"
# => 3232235777
IP.v4-from-int
Int -> {a, b, c, d}
Creates an IPv4 address from a 32-bit integer.
IP.is-valid-v4?
String -> Bool
Returns true if the string is a valid IPv4 address.
println (IP.is-valid-v4? "192.168.1.1")
# => true

println (IP.is-valid-v4? "256.1.1.1")
# => false
IP.is-v4-loopback?
{a, b, c, d} -> Bool
Returns true if the address is a loopback address (127.0.0.0/8).
IP.is-v4-private?
{a, b, c, d} -> Bool
Returns true if the address is in a private range (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
match IP.parse-v4 "192.168.1.1"
  | Ok ip -> println (IP.is-v4-private? ip)
  | Err _ -> println "Error"
# => true
IP.is-v4-multicast?
{a, b, c, d} -> Bool
Returns true if the address is in the multicast range (224.0.0.0/4).
IP.is-v4-broadcast?
{a, b, c, d} -> Bool
Returns true if the address is the broadcast address (255.255.255.255).
IP.is-v4-link-local?
{a, b, c, d} -> Bool
Returns true if the address is link-local (169.254.0.0/16).

IPv6 Functions

IP.parse-v6
String -> Result IPv6 String
Parses an IPv6 address string.
match IP.parse-v6 "::1"
  | Ok ip -> println (IP.v6-to-string ip)
  | Err msg -> println "Invalid: ${msg}"
# => "::1"
IP.v6-to-string
IPv6 -> String
Converts an IPv6 address to a string.
IP.is-valid-v6?
String -> Bool
Returns true if the string is a valid IPv6 address.
println (IP.is-valid-v6? "2001:db8::1")
# => true

println (IP.is-valid-v6? "::1")
# => true
IP.is-v6-loopback?
IPv6 -> Bool
Returns true if the address is the IPv6 loopback address (::1).
IP.is-v6-private?
IPv6 -> Bool
Returns true if the address is in a private/unique local range (fc00::/7).
IP.is-v6-multicast?
IPv6 -> Bool
Returns true if the address is multicast (ff00::/8).
IP.is-v6-link-local?
IPv6 -> Bool
Returns true if the address is link-local (fe80::/10).
IP.v6-to-v4
IPv6 -> Option {a, b, c, d}
Extracts an embedded IPv4 address from an IPv4-mapped IPv6 address (::ffff:x.x.x.x).