Skip to main content

std.net — DNS

dns_lookup resolves a hostname to its IP addresses (both IPv4 and IPv6 when available). No socket is opened — it just queries the system resolver.

Usage

var ips = std.net.dns_lookup("localhost")
print(ips) // ["::1", "127.0.0.1"] (order varies by OS)

// handle lookup failure like any error
var ok = std.net.dns_lookup("example.com")
print(ok) // ["93.184.215.14", ...]

// example: pick the first IPv4 address
var addrs = std.net.dns_lookup("example.com")
var first = std.net.dns_lookup("example.com")[0]
print(first)

Reference

dns_lookup(hostname) // -> [ip, ...] (array of strings)
  • Returns an array of strings, each an IP address. May contain both "127.0.0.1" (v4) and "::1" (v6) for localhost.
  • Throws on lookup failure (unknown host, no network) — catch via the engine's normal error propagation (see Errors). There is no null return for failure.
  • Results are not cached by Scrii — each call re-queries the OS resolver.

Tips

  • Pair with URL: parse_url(url).hostdns_lookup(host) when you need to log or choose an IP explicitly. Normally tcp_client.connect / http_client.request resolve for you.
  • For HTTP/TLS debugging, resolving manually helps confirm DNS vs. connect vs. HTTP errors.

See also