Standard library · Network

net/dns

Finding the address of a name and asking for any kind of DNS record.

import net.dns

DNS turns names such as salamlang.ir into IP addresses. Lookup is the simplest way and does what a browser does. The Lookup... functions ask the DNS server for one particular kind of record and are what you want for building network tools or checking the configuration of a domain.

Types

Question

struct Question
One question.
NameTypeDescription
namestr = ""The domain name.
qtypeint = 0The record type.
qclassint = 0The class.
net-dns-Message.salam
import net.dns
import compress

func main:
    query := dns.EncodeQuery("salamlang.ir", dns.TypeA, 42)
    println compress.BufLen(query), compress.BufToBase64(query)
    m := dns.Decode(query.data, query.len)
    println m.id, m.qr, m.rd, m.opcode, m.rcode, m.questions.items.len()
    q := m.questions.items[0]
    println q.name, q.qtype == dns.TypeA, q.qclass == dns.ClassIN
    println m.answers.items.len(), m.authorities.items.len(), m.extras.items.len(), m.aa, m.tc, m.ra
    println dns.TypeNS, dns.TypeCNAME, dns.TypeSOA, dns.TypePTR, dns.TypeMX, dns.TypeTXT, dns.TypeAAAA
    println dns.Lookup("localhost")
    println dns.DefaultServers().len() > 0
end
Output30 ACoBAAABAAAAAAAACXNhbGFtbGFuZwJpcgAAAQAB 42 false true 0 0 1 salamlang.ir true true 0 0 0 false false false 2 5 6 12 15 16 28 127.0.0.1 true

Questions

struct Questions
A list of questions.
NameTypeDescription
itemsVectorThe questions.

The example for this item is shown with the ones above: net-dns-Message.salam

Record

struct Record
One answer record.
NameTypeDescription
namestr = ""The domain name.
rtypeint = 0The record type.
rclassint = 0The class.
ttlint = 0How many seconds the answer may be kept.
priorityint = 0The priority of an MX record; a smaller number means a higher priority.
datastr = ""The record data as text: an address, a name or some text.
net-dns-Lookup.salam
import net.dns

func main:
    println dns.LookupA("salamlang.ir")
    println dns.LookupAAAA("salamlang.ir")
    println dns.LookupCNAME("www.github.com")
    mx := dns.LookupMX("gmail.com")
    each m in mx.items:
        println m.priority, m.data, m.ttl, m.rtype, m.rclass, m.name
    end
    println dns.LookupTXT("salamlang.ir")
    println dns.LookupPTR("8.8.8.8")
    answer := dns.QueryServer("1.1.1.1", "salamlang.ir", dns.TypeA, 2000)
    println answer.answers.items.len()
end

Records

struct Records
A list of records.
NameTypeDescription
itemsVectorThe records.

The example for this item is shown with the ones above: net-dns-Lookup.salam

Message

struct Message
One DNS message, a question or an answer.
NameTypeDescription
idint = 0The id that ties an answer to its question.
qrbool = falseThe message is an answer.
opcodeint = 0The kind of operation; zero is an ordinary question.
aabool = falseThe answer comes from the authoritative server.
tcbool = falseThe answer was truncated.
rdbool = falseA recursive lookup was asked for.
rabool = falseThe server supports recursive lookups.
rcodeint = 0The result code; zero means no error, 3 means the name does not exist.
questionsQuestionsThe questions.
answersRecordsThe answers.
authoritiesRecordsThe authority records.
extrasRecordsThe additional records.

The example for this item is shown with the ones above: net-dns-Message.salam

Constants and variables

TypeA

const TypeA: i32 = 1
An IPv4 address record.

The example for this item is shown with the ones above: net-dns-Message.salam

TypeNS

const TypeNS: i32 = 2
A name server of the domain.

The example for this item is shown with the ones above: net-dns-Message.salam

TypeCNAME

const TypeCNAME: i32 = 5
An alias.

The example for this item is shown with the ones above: net-dns-Message.salam

TypeSOA

const TypeSOA: i32 = 6
The start of authority of the domain.

The example for this item is shown with the ones above: net-dns-Message.salam

TypePTR

const TypePTR: i32 = 12
The name for an address (a reverse lookup).

The example for this item is shown with the ones above: net-dns-Message.salam

TypeMX

const TypeMX: i32 = 15
A mail server.

The example for this item is shown with the ones above: net-dns-Message.salam

TypeTXT

const TypeTXT: i32 = 16
Free text, such as SPF records.

The example for this item is shown with the ones above: net-dns-Message.salam

TypeAAAA

const TypeAAAA: i32 = 28
An IPv6 address record.

The example for this item is shown with the ones above: net-dns-Message.salam

ClassIN

const ClassIN: i32 = 1
The internet class; nearly always this one.

The example for this item is shown with the ones above: net-dns-Message.salam

Functions

EncodeQuery

func EncodeQuery(name: str, qtype: int, id: int): compress.ByteBuf
Builds a DNS question message to send over UDP.

The example for this item is shown with the ones above: net-dns-Message.salam

Decode

func Decode(data: void*, n: int): Message
Reads a DNS message from bytes.

The example for this item is shown with the ones above: net-dns-Message.salam

Lookup

func Lookup(host: str): str
Finds the address of a host name using the operating system settings; the hosts file is read too.

The example for this item is shown with the ones above: net-dns-Message.salam

QueryServer

func QueryServer(server: str, name: str, qtype: int, timeout_ms: int): Message
Asks the given server directly and returns the answer message.

The example for this item is shown with the ones above: net-dns-Lookup.salam

DefaultServers

func DefaultServers(): Vector<str>
The system DNS servers, from the network configuration.

The example for this item is shown with the ones above: net-dns-Message.salam

LookupA

func LookupA(name: str): Vector<str>
The IPv4 addresses of a name.

The example for this item is shown with the ones above: net-dns-Lookup.salam

LookupAAAA

func LookupAAAA(name: str): Vector<str>
The IPv6 addresses of a name.

The example for this item is shown with the ones above: net-dns-Lookup.salam

LookupCNAME

func LookupCNAME(name: str): str
The real name behind an alias.

The example for this item is shown with the ones above: net-dns-Lookup.salam

LookupMX

func LookupMX(name: str): Records
The mail servers of a domain, with their priority.

The example for this item is shown with the ones above: net-dns-Lookup.salam

LookupTXT

func LookupTXT(name: str): Vector<str>
The text records of a domain.

The example for this item is shown with the ones above: net-dns-Lookup.salam

LookupPTR

func LookupPTR(ip: str): Vector<str>
The names of an IP address.

The example for this item is shown with the ones above: net-dns-Lookup.salam