Both IpAddr and SocketAddr are copyable value types — they hold no OS resources, so copying is safe and cheap.

IP address parsing uses the OS inet_pton(3) function for correctness on both IPv4 and IPv6; formatting uses inet_ntop(3).

Structs

struct IpAddr An IP address: either IPv4 or IPv6.
struct SocketAddr A socket address: an IP address combined with a port number.
Detail Documentation

Structs

struct IpAddr §

struct IpAddr

An IP address: either IPv4 or IPv6.

The address is stored as a validated string produced by inet_ntop after successful inet_pton parsing. This guarantees all instances hold a canonical, valid representation.

Fields: _addr: Canonical string form (e.g. "127.0.0.1" or "::1"). _is_v6: True for IPv6, False for IPv4.

Example:

var lo = IpAddr.parse("127.0.0.1")
print(lo) # 127.0.0.1
print(lo.is_loopback()) # True

Methods

fn __init__ Construct an ``IpAddr`` directly from a pre-validated string.
fn __eq__ Return ``True`` if both addresses have the same family and string.
fn __ne__ Return ``True`` if the addresses differ.
fn parse Parse and validate an IP address string.
fn localhost Return the IPv4 loopback address ``127.0.0.1``.
fn localhost_v6 Return the IPv6 loopback address ``::1``.
fn unspecified Return the IPv4 wildcard address ``0.0.0.0`` (all interfaces).
fn unspecified_v6 Return the IPv6 wildcard address ``::`` (all interfaces).
fn is_v4 Return ``True`` if this is an IPv4 address.
fn is_v6 Return ``True`` if this is an IPv6 address.
fn is_loopback Return ``True`` if this is a loopback address.
fn is_unspecified Return ``True`` if this is the wildcard/unspecified address.
fn is_private Return ``True`` if this is an RFC 1918 private address.
fn is_multicast Return ``True`` if this is a multicast address.
fn write_to Write the canonical address string to ``writer``.

fn __init__ static §

__init__(out self, addr: String, is_v6: Bool)

Construct an ``IpAddr`` directly from a pre-validated string.

Prefer IpAddr.parse for user-supplied strings.

Args
addr String

Canonical IP address string.

is_v6 Bool

True if this is an IPv6 address.

self out Self
Returns
Self

fn __eq__ §

__eq__(self, other: Self) -> Bool

Return ``True`` if both addresses have the same family and string.

Args
self Self
other Self

The address to compare with.

Returns
Bool

fn __ne__ §

__ne__(self, other: Self) -> Bool

Return ``True`` if the addresses differ.

Args
self Self
other Self

The address to compare with.

Returns
Bool

fn parse static §

parse(s: String) -> Self

Parse and validate an IP address string.

Accepts dotted-decimal IPv4 ("192.168.1.1") and colon-separated IPv6 ("::1", "2001:db8::1"). Uses inet_pton for correctness, then inet_ntop to obtain the canonical form.

Example:

var v4 = IpAddr.parse("192.168.0.1")
var v6 = IpAddr.parse("::1")
Args
s String

The address string to parse.

Returns
Self

A validated IpAddr.

Raises

AddressParseError: If s is not a valid IPv4 or IPv6 address.

fn localhost static §

localhost() -> Self

Return the IPv4 loopback address ``127.0.0.1``.

Returns
Self

IpAddr for 127.0.0.1.

fn localhost_v6 static §

localhost_v6() -> Self

Return the IPv6 loopback address ``::1``.

Returns
Self

IpAddr for ::1.

fn unspecified static §

unspecified() -> Self

Return the IPv4 wildcard address ``0.0.0.0`` (all interfaces).

Returns
Self

IpAddr for 0.0.0.0.

fn unspecified_v6 static §

unspecified_v6() -> Self

Return the IPv6 wildcard address ``::`` (all interfaces).

Returns
Self

IpAddr for "::"

fn is_v4 §

is_v4(self) -> Bool

Return ``True`` if this is an IPv4 address.

Args
self Self
Returns
Bool

fn is_v6 §

is_v6(self) -> Bool

Return ``True`` if this is an IPv6 address.

Args
self Self
Returns
Bool

fn is_loopback §

is_loopback(self) -> Bool

Return ``True`` if this is a loopback address.

IPv4 loopback range is 127.0.0.0/8; IPv6 loopback is ::1.

Args
self Self
Returns
Bool

fn is_unspecified §

is_unspecified(self) -> Bool

Return ``True`` if this is the wildcard/unspecified address.

IPv4: "0.0.0.0"; IPv6: "::".

Args
self Self
Returns
Bool

fn is_private §

is_private(self) -> Bool

Return ``True`` if this is an RFC 1918 private address.

Covers 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16. IPv6 ULA (fc00::/7) is not yet detected and returns False.

Args
self Self
Returns
Bool

fn is_multicast §

is_multicast(self) -> Bool

Return ``True`` if this is a multicast address.

IPv4 multicast: 224.0.0.0/4 (first octet 224–239). IPv6 multicast: addresses starting with "ff".

Args
self Self
Returns
Bool

fn write_to §

write_to[W: Writer](self, mut writer: W)

Write the canonical address string to ``writer``.

Parameters
W Writer
Args
self Self
writer mut W

Destination writer.

struct SocketAddr §

struct SocketAddr

A socket address: an IP address combined with a port number.

Fields: ip: The IP address component. port: The port number (0–65535).

Example:

var addr = SocketAddr(IpAddr.localhost(), 8080)
print(addr) # 127.0.0.1:8080

var v6 = SocketAddr(IpAddr.localhost_v6(), 443)
print(v6) # [::1]:443

Fields

ip IpAddr
port UInt16

Methods

fn __init__ Initialise a ``SocketAddr``.
fn __eq__ Return ``True`` if IP and port both match.
fn __ne__ Return ``True`` if IP or port differ.
fn localhost Return ``SocketAddr("127.0.0.1", port)``.
fn unspecified Return ``SocketAddr("0.0.0.0", port)`` — bind on all interfaces.
fn parse Parse a ``"host:port"`` or ``"[ipv6]:port"`` string.
fn write_to Write ``"ip:port"`` or ``"[ip]:port"`` to ``writer``.

fn __init__ static §

__init__(out self, ip: IpAddr, port: UInt16)

Initialise a ``SocketAddr``.

Args
ip IpAddr

The IP address component.

port UInt16

The port number (0–65535).

self out Self
Returns
Self

fn __eq__ §

__eq__(self, other: Self) -> Bool

Return ``True`` if IP and port both match.

Args
self Self
other Self

The address to compare.

Returns
Bool

fn __ne__ §

__ne__(self, other: Self) -> Bool

Return ``True`` if IP or port differ.

Args
self Self
other Self

The address to compare.

Returns
Bool

fn localhost static §

localhost(port: UInt16) -> Self

Return ``SocketAddr("127.0.0.1", port)``.

Args
port UInt16

The port number.

Returns
Self

A loopback SocketAddr.

fn unspecified static §

unspecified(port: UInt16) -> Self

Return ``SocketAddr("0.0.0.0", port)`` — bind on all interfaces.

Args
port UInt16

The port number.

Returns
Self

A SocketAddr for the wildcard address.

fn parse static §

parse(s: String) -> Self

Parse a ``"host:port"`` or ``"[ipv6]:port"`` string.

Example:

var a = SocketAddr.parse("127.0.0.1:9000")
var b = SocketAddr.parse("[::1]:9000")
Args
s String

Address string in one of these forms:

  • "1.2.3.4:8080" for IPv4.
  • "[::1]:443" for IPv6.
Returns
Self

A SocketAddr.

Raises

AddressParseError: If the string cannot be parsed.

fn write_to §

write_to[W: Writer](self, mut writer: W)

Write ``"ip:port"`` or ``"[ip]:port"`` to ``writer``.

Parameters
W Writer
Args
self Self
writer mut W

Destination writer.