Grav v1.14.0

dns

import "std/net" · namespace std.net

DNS lookups straight from the name servers: SRV records (service discovery: XMPP, SIP, LDAP, Kubernetes headless services), TXT records (SPF, DKIM, domain verification), and A, AAAA, MX, CNAME, NS, PTR, CAA and the other types. Answers are cached for their time to live.

grav
import "std"
import "std/net"

fn main() {
    for (r in Dns.srv("_xmpp-server._tcp.example.com")) {
        print("${r.target()}:${r.port} (priority ${r.priority}, weight ${r.weight})")
    }
    for (spf in Dns.txt("example.com")) { print(spf) }
    print(Dns.mx("example.com")[0].target())
    print(Dns.a("example.com"))
    print(Dns.aaaa("example.com"))
    print(Dns.reverse("1.1.1.1"))

    let r = new DnsResolver(["1.1.1.1", "[2606:4700:4700::1111]:53"]).timeoutMs(1500).attempts(2)
    for (rec in r.lookup("example.com", "CAA")) { print("${rec.tag} ${rec.data}") }
}
  • The system's servers come from /etc/resolv.conf (often 127.0.0.53 under systemd-resolved; 127.0.0.1 when the file is missing). new DnsResolver(servers) asks others. A server is written "1.1.1.1", "host:port" or "[v6]:port".
  • Queries go over UDP with EDNS0, offering 4 KB answers. Each server is tried in turn for attempts rounds. A truncated answer is asked for again over TCP, and if the server refuses TCP (as some home routers do) the records that fit are used. Answers must carry the query's random id.
  • Results: a name that does not exist, or has no records of the type, gives an empty list. A server failure is a NetError, and no answer at all is a TimeoutError. Dns.srv sorts by priority, then weight (highest first); Dns.mx by preference.
  • Records (DnsRecord): name, type, ttl, data (the address, target name, joined text or CAA value), and per type priority, weight, port (SRV), priority (MX), texts (each TXT string), flags and tag (CAA). Unknown types keep their data as hex.
  • Caching: answers are kept for their TTL (at most 5 minutes), and "no such records" for 30 s. caching(false) and clearCache() control it. ask(name, type) returns the raw DnsMessage without caching.
  • DnsMessage.query and DnsMessage.parse build and read the packets themselves, for tools, tests and servers of your own.

API

class DnsRecord

One resource record from an answer.

Constructors

  • new DnsRecord(name: string, type: string, ttl: long, data: string)

Fields

  • name: string — The owner name ("example.com"), the type ("A", "SRV", …) and seconds to keep it.
  • type: string
  • ttl: long
  • data: string — A, AAAA: the address. CNAME, NS, PTR: the name. TXT: the strings joined. MX, SRV: the target. CAA: the value. Other types: the data as hex.
  • texts: Array<string> = new Array<string>() — TXT: each character-string of the record.
  • priority: int = 0 — MX: the preference; SRV: the priority (lower first).
  • weight: int = 0 — SRV: the weight (higher more often among equal priorities) and port.
  • port: int = 0
  • flags: int = 0 — CAA: the flags and tag ("issue", "iodef", …).
  • tag: string = ""

Methods

  • fn target() -> string — SRV and MX: the target name.
  • fn toString() -> string

class DnsMessage

A parsed DNS message (for tests, tools, and servers of your own).

Constructors

  • new DnsMessage()

Static members

  • static fn query(id: int, name: string, type: string) -> Bytes — A standard query for name and a type ("A", "SRV", …) or type number, with recursion desired and an EDNS0 record offering 4096-byte UDP answers.
  • static fn parse(data: Bytes) -> DnsMessage — Parses an answer; ValueError when it is malformed.
  • static fn typeCode(type: string) -> int — The number for a type name ("A" is 1, "SRV" 33); a number in text passes through.
  • static fn typeName(code: int) -> string

Fields

  • id: int = 0
  • rcode: int = 0 — The response code: 0 no error, 2 server failure, 3 the name does not exist, ...
  • truncated: bool = false
  • answers: Array<DnsRecord> = new Array<DnsRecord>()

class DnsResolver

Asks name servers, caching answers for their time to live.

Constructors

  • new DnsResolver(servers: Array<string>) — A resolver for these servers ("1.1.1.1", "2606:4700:4700::1111"; "host:port" or "[v6]:port" for another port).

Static members

  • static fn systemResolver() -> DnsResolver — The servers in /etc/resolv.conf (made once and shared).
  • static fn readResolvConf(path: string) -> Array<string> — The nameserver lines of a resolv.conf file (127.0.0.1 when there are none).

Fields

  • servers: Array<string>

Methods

  • fn timeoutMs(ms: int) -> DnsResolver — How long to wait for each server, in milliseconds.
  • fn attempts(n: int) -> DnsResolver — How many rounds through the servers before giving up.
  • fn caching(on: bool) -> DnsResolver — Whether answers are cached for their TTL (on by default).
  • fn clearCache()
  • fn lookup(name: string, type: string) -> Array<DnsRecord> — The records of type for name (an empty list when there are none).
  • fn ask(name: string, type: string) -> DnsMessage — The answer message itself (uncached), for the rcode and every record.

class Dns

Lookups through the system's name servers.

Static members

  • static fn srv(name: string) -> Array<DnsRecord> — SRV records, by priority (lowest first) then weight (highest first).
  • static fn txt(name: string) -> Array<string> — The text of each TXT record (a record's strings joined).
  • static fn mx(name: string) -> Array<DnsRecord> — MX records, most preferred first.
  • static fn a(name: string) -> Array<string> — IPv4 and IPv6 addresses.
  • static fn aaaa(name: string) -> Array<string>
  • static fn cname(name: string) -> Array<string>
  • static fn ns(name: string) -> Array<string>
  • static fn reverse(ip: string) -> string — The name of an IPv4 or IPv6 address (reverse lookup), or "".
  • static fn reverseName(ip: string) -> string — The in-addr.arpa or ip6.arpa name of an address.
  • static fn lookup(name: string, type: string) -> Array<DnsRecord> — Any record type ("CAA", "NS", "HTTPS", …).

Edit this page on GitHub