vera:net/dns

DNS hostname resolution.

import "vera:net/dns"

Overview

Resolves hostnames to IPv4 address strings using DNS A record queries over UDP. Handles three cases:

  1. Raw IP addresses (digits and dots only) -- passed through directly without a DNS query.
  2. "localhost" -- returns "127.0.0.1" immediately.
  3. Hostnames -- performs a DNS A record query via UDP to a nameserver and parses the response.

Uses connected UDP sockets for DNS communication. The default nameserver is 8.8.8.8 (Google Public DNS). A 5-second timeout is applied when waiting for DNS responses.

Dependencies: vera:net, vera:binary, vera:string, vera:memory

Functions


dns_resolve

function dns_resolve(hostname: ptr, hostname_len: i64, out_buf: ptr, out_size: i64) -> i64

Resolve a hostname to an IPv4 address string using the default DNS server (8.8.8.8).

Parameters:

  • hostname (ptr) — pointer to the hostname bytes (not necessarily null-terminated)
  • hostname_len (i64) — length of the hostname
  • out_buf (ptr) — buffer to write the dotted-decimal IP string into
  • out_size (i64) — size of the output buffer

Returns: length of the IP string written to out_buf, or -1 on failure


dns_resolve_with

function dns_resolve_with(hostname: ptr, hostname_len: i64, server_ip: ptr, server_ip_len: i64, out_buf: ptr, out_size: i64) -> i64

Resolve a hostname to an IPv4 address string using a specific DNS server.

Parameters:

  • hostname (ptr) — pointer to the hostname bytes
  • hostname_len (i64) — length of the hostname
  • server_ip (ptr) — IP address string of the DNS server (e.g., "8.8.8.8")
  • server_ip_len (i64) — length of the server IP string
  • out_buf (ptr) — buffer to write the dotted-decimal IP string into
  • out_size (i64) — size of the output buffer

Returns: length of the IP string written to out_buf, or -1 on failure