netpkt/dns.rs#
Source: lib/netpkt/dns.rs
dns -- the Domain Name System message format (RFC 1035; AAAA RFC 3596; negative answers
RFC 2308; EDNS(0) RFC 6891; SRV RFC 2782; DNAME RFC 6672; the DNSSEC records RFC 4034 and
5155; SSHFP RFC 4255; TLSA RFC 6698; CDS/CDNSKEY RFC 7344; SVCB/HTTPS RFC 9460; CAA RFC 8659)
Header { id, qr, opcode, aa, tc, rd, ra, ad, cd, rcode } flags() / from_flags(id, f)
Question { name, qtype, qclass }
Rr { name, rtype, class, ttl, data: RData } Rr::new(name, ttl, data)
RData A, Aaaa, Ns, Cname, Ptr, Mx { pref, host }, Soa(Soa), Txt(strings),
Srv { priority, weight, port, target }, Dname, Caa { flags, tag, value },
Tlsa { usage, selector, matching, data }, Sshfp, Ds / Cds (Ds),
Dnskey / Cdnskey (Dnskey: key_tag()), Rrsig, Nsec { next, types },
Nsec3, Nsec3Param, Svcb / Https (Svcb: param, alpn, port, hints, ech),
Other(bytes); rtype() / text() (zone-file form, as dig prints it)
Edns { udp_size, ext_rcode, version, dnssec_ok, options } the OPT record: Message::edns()
(several or a malformed one: an error) / set_edns / rcode() (all 12 bits);
ede() an Extended DNS Error (RFC 8914)
Names inside the data of the newer types are written without compression (RFC 3597 4) and
read only without it, but for SRV, whose target may come compressed (RFC 3597 4). A record
whose data does not fit its type is refused (DnsErr::BadRecord): an SVCB/HTTPS record in
service mode with keys out of order, a mandatory key it lacks, or a known key's value of the
wrong form (RFC 9460 2.2, 8); a CAA tag that is empty or not letters and digits; a type bitmap
whose windows are out of order or of length 0 or over 32 (RFC 4034 4.1.2).
Message { header, questions, answers, authority, additional }
parse(b) (names with compression pointers; loops, forward pointers, more
than 126 pointers and names over 255 bytes are refused) / to_bytes() (names compressed against
earlier ones, RFC 1035 4.1.4) / to_bytes_max(limit) -> (bytes, truncated):
whole records only, TC set when something was left out /
query(id, name, qtype, rd) / reply_to(&query)
read_name(msg, off) -> Option<(String, usize)> / write_name(out, name, &mut Compressor)
name_eq / is_subdomain(name, zone) / parent(name) / reverse_v4(ip) / parse_reverse_v4
TYPE_* / CLASS_IN / RCODE_* / OPCODE_QUERY; type_name / type_by_name / rcode_name
Names are text without the final dot ("www.example.com"; the root is ""); a dot or a
backslash inside a label and bytes outside printable ASCII are written \. \\ \DDD as in
zone files. Comparisons ignore ASCII case (RFC 4343).
DNS_HDR: usize = 12
/// the largest answer over UDP without EDNS (RFC 1035 4.2.1)
UDP_MAX: usize = 512
TYPE_A: u16 = 1
TYPE_NS: u16 = 2
TYPE_CNAME: u16 = 5
TYPE_SOA: u16 = 6
TYPE_PTR: u16 = 12
TYPE_MX: u16 = 15
TYPE_TXT: u16 = 16
TYPE_AAAA: u16 = 28
TYPE_SRV: u16 = 33
TYPE_NAPTR: u16 = 35
TYPE_DNAME: u16 = 39
TYPE_OPT: u16 = 41
TYPE_DS: u16 = 43
TYPE_SSHFP: u16 = 44
TYPE_RRSIG: u16 = 46
TYPE_NSEC: u16 = 47
TYPE_DNSKEY: u16 = 48
TYPE_NSEC3: u16 = 50
TYPE_NSEC3PARAM: u16 = 51
TYPE_TLSA: u16 = 52
TYPE_CDS: u16 = 59
TYPE_CDNSKEY: u16 = 60
TYPE_SVCB: u16 = 64
TYPE_HTTPS: u16 = 65
TYPE_ANY: u16 = 255
TYPE_CAA: u16 = 257
CLASS_IN: u16 = 1
/// the keys of SVCB / HTTPS parameters (RFC 9460 14.3.2; dohpath RFC 9461, ohttp RFC 9540)
SVC_MANDATORY: u16 = 0
SVC_ALPN: u16 = 1
SVC_NO_DEFAULT_ALPN: u16 = 2
SVC_PORT: u16 = 3
SVC_IPV4HINT: u16 = 4
SVC_ECH: u16 = 5
SVC_IPV6HINT: u16 = 6
SVC_DOHPATH: u16 = 7
SVC_OHTTP: u16 = 8
/// EDNS(0) options (RFC 6891 and the IANA registry)
OPT_NSID: u16 = 3
OPT_CLIENT_SUBNET: u16 = 8
OPT_COOKIE: u16 = 10
OPT_TCP_KEEPALIVE: u16 = 11
OPT_PADDING: u16 = 12
OPT_EDE: u16 = 15
OPCODE_QUERY: u8 = 0
RCODE_NOERROR: u8 = 0
RCODE_FORMERR: u8 = 1
RCODE_SERVFAIL: u8 = 2
RCODE_NXDOMAIN: u8 = 3
RCODE_NOTIMP: u8 = 4
RCODE_REFUSED: u8 = 5
/// the extended RCODE of an EDNS version the server does not know (Message::rcode, RFC 6891)
RCODE_BADVERS: u16 = 16
fn type_name(t: u16) -> String"A", "MX", ... ("TYPE99" for others, RFC 3597)fn type_by_name(s: &str) -> Option<u16>the type of a mnemonic ("a", "MX", "TYPE99")fn rcode_name(r: u8) -> &'static str"NOERROR", "NXDOMAIN", ...enum DnsErrwhy a message could not be readShort-- it ends inside a header, a name or a recordBadName-- a label, a pointer or the length of a name is wrongBadRecord-- the data of a record does not fit its type or length
struct Headerthe fixed headerid: u16qr: bool-- a responseopcode: u8aa: bool-- authoritative answertc: bool-- truncatedrd: bool-- recursion desiredra: bool-- recursion availablead: bool-- authentic data (DNSSEC)cd: bool-- checking disabled (DNSSEC)rcode: u8
impl Header
fn flags(&self) -> u16the 16 flag bitsfn from_flags(id: u16, f: u16) -> Headerstruct Questionname: Stringqtype: u16qclass: u16
struct Soathe data of a start of authority recordmname: String-- the primary name serverrname: String-- the mailbox of the responsible person (the first dot is the @)serial: u32refresh: u32retry: u32expire: u32minimum: u32-- the TTL of negative answers (RFC 2308)
enum RDatathe data of a resource recordA(Ipv4)Aaaa(Ipv6)Ns(String)Cname(String)Ptr(String)Mx { pref: u16, host: String }Soa(Soa)Txt(Vec<Vec<u8>>)-- character strings of up to 255 bytes eachSrv { priority: u16, weight: u16, port: u16, target: String }-- a service's host and port (RFC 2782)Dname(String)-- the names below this one are those below the target (RFC 6672)Caa { flags: u8, tag: String, value: Vec<u8> }-- which CAs may issue certificates for the name (RFC 8659): tag "issue", "issuewild", "iodef", ...; flag 128 is criticalTlsa { usage: u8, selector: u8, matching: u8, data: Vec<u8> }-- a certificate or key of a TLS service (DANE, RFC 6698): usage 0-3, selector 0 (the whole certificate) or 1 (its public key), matching 0 (the data itself), 1 (SHA-256), 2 (SHA-512)Sshfp { algorithm: u8, fp_type: u8, fingerprint: Vec<u8> }-- an SSH host key's fingerprint (RFC 4255, 6594, 7479)Ds(Ds)Cds(Ds)-- a DS the child zone publishes for its parent (RFC 7344)Dnskey(Dnskey)Cdnskey(Dnskey)-- a DNSKEY the child zone publishes for its parent (RFC 7344)Rrsig(Rrsig)Nsec { next: String, types: Vec<u16> }-- the next name of the zone and the types this one has (RFC 4034 4)Nsec3(Nsec3)Nsec3Param(Nsec3Param)Svcb(Svcb)Https(Svcb)-- SVCB for https (RFC 9460 9)Other(Vec<u8>)-- any other type, as it was on the wire
struct Dsa delegation signer: the digest of a child zone's key (RFC 4034 5)key_tag: u16algorithm: u8digest_type: u8-- 1 SHA-1, 2 SHA-256, 4 SHA-384digest: Vec<u8>
struct Dnskeya zone's public key (RFC 4034 2)flags: u16-- 256: a zone key; 257: a zone key and a secure entry point (a KSK); 128 revoked (RFC 5011)protocol: u8-- always 3; a key with another value does not verify anything (RFC 4034 2.1.2)algorithm: u8key: Vec<u8>
impl Dnskey
fn rdata(&self) -> Vec<u8>the record's data on the wirefn key_tag(&self) -> u16the key tag that DS and RRSIG records name (RFC 4034 appendix B; not for algorithm 1)struct Rrsiga signature over the records of one name and type (RFC 4034 3)type_covered: u16algorithm: u8labels: u8-- the labels of the signed name, a leading "*" not countedoriginal_ttl: u32expiration: u32-- seconds since 1970 (serial number arithmetic, RFC 1982)inception: u32key_tag: u16signer: String-- the zone that signedsignature: Vec<u8>
struct Nsec3hashed denial of existence (RFC 5155 3)hash: u8-- 1: SHA-1flags: u8-- 1: opt-outiterations: u16salt: Vec<u8>next: Vec<u8>-- the next hashed owner name (raw, not in base32)types: Vec<u16>
struct Nsec3Paramthe NSEC3 parameters of a zone (RFC 5155 4)hash: u8flags: u8iterations: u16salt: Vec<u8>
struct Svcba service binding (RFC 9460): priority 0 is alias mode (another name for the service, its parameters ignored), above that service mode with the parameters (key, value) in the order of their keyspriority: u16target: String-- "" (the root, "."): the owner name itself in service modeparams: Vec<(u16, Vec<u8>)>
impl Svcb
fn param(&self, key: u16) -> Option<&[u8]>the value of a parameterfn is_alias(&self) -> boolalias mode: the target names the servicefn alpn(&self) -> Vec<Vec<u8>>the protocols of "alpn" (empty without)fn port(&self) -> Option<u16>the port of "port" (None: the scheme's own)fn ipv4_hints(&self) -> Vec<Ipv4>the addresses of "ipv4hint" (empty without)fn ipv6_hints(&self) -> Vec<Ipv6>the addresses of "ipv6hint" (empty without)fn ech(&self) -> Option<&[u8]>the ECHConfigList of "ech" (RFC 9849)fn text(&self) -> String"1 . alpn=\"h3,h2\" ipv4hint=192.0.2.1" (RFC 9460 appendix A)fn base32hex(b: &[u8]) -> Stringbase32 with the extended hex alphabet, no padding (RFC 4648 7; NSEC3's hashed names)
impl RData
fn rtype(&self) -> Option<u16>the record type of the data (None for Other)fn text(&self) -> Stringthe data in zone-file form ("10.0.0.1", "10 mail.example.com", "\"text\""; names in the data without the final dot, the root as "."; keys and signatures in base64, digests in hex, as dig +nosplit prints them)struct Rra resource recordname: Stringrtype: u16class: u16ttl: u32data: RData
impl Rr
fn new(name: &str, ttl: u32, data: RData) -> Rra record of class IN; the type follows the datastruct Messagea whole messageheader: Headerquestions: Vec<Question>answers: Vec<Rr>authority: Vec<Rr>additional: Vec<Rr>
struct Ednsthe OPT pseudo-record of EDNS(0) (RFC 6891 6)udp_size: u16-- the largest UDP message the sender takes (the record's class; below 512 counts as 512)ext_rcode: u8-- the upper eight bits of the RCODEversion: u8dnssec_ok: bool-- DNSSEC OK: the sender wants DNSSEC records (RFC 3225)options: Vec<(u16, Vec<u8>)>-- (code, data) of each option, in order
impl Edns
fn new(udp_size: u16) -> Ednsversion 0, this UDP size, DO clear, no optionsfn option(&self, code: u16) -> Option<&[u8]>the data of the first option with this codefn ede(&self) -> Option<(u16, String)>an Extended DNS Error (RFC 8914): its INFO-CODE and EXTRA-TEXTfn from_rr(r: &Rr) -> Option<Edns>the OPT record read (None: not OPT, not at the root, or options that do not fit)fn to_rr(&self) -> Rrthe OPT record of this (at the root; its class is the UDP size, its TTL the flags)fn labels(name: &str) -> Option<Vec<Vec<u8>>>the labels of a name in text form (None: empty label, label over 63 bytes, name over 255 bytes on the wire, bad escape)fn read_name(msg: &[u8], off: usize) -> Option<(String, usize)>the name atoffof a message and the offset after it (where the name ends in place, not where a pointer led)struct Compressorremembers where names were written so later names can point there (RFC 1035 4.1.4)off: bool-- off: every name is written in full
impl Compressor
fn new() -> Compressorfn forget_from(&mut self, off: usize)forgets the names written atoffor later (the output was cut back there)fn write_name(out: &mut Vec<u8>, name: &str, c: &mut Compressor) -> boolappends a name in wire form (with pointers to earlier names when the compressor allows); false when the name is not validfn name_eq(a: &str, b: &str) -> booltwo names are the same (ASCII case ignored)fn is_subdomain(name: &str, zone: &str) -> boolnameiszoneor lies below it ("www.lab" in "lab"; every name lies in the root "")fn parent(name: &str) -> &strthe name without its first label ("lab" for "www.lab", "" for "lab")fn reverse_v4(ip: Ipv4) -> Stringthe PTR name of an address ("4.3.2.1.in-addr.arpa")fn parse_reverse_v4(name: &str) -> Option<Ipv4>the address of a PTR name ("4.3.2.1.in-addr.arpa" -> 1.2.3.4)
impl Message
fn query(id: u16, name: &str, qtype: u16, rd: bool) -> Messagea query for one namefn reply_to(q: &Message) -> Messagean empty response to a query: same id, opcode, question and recursion wishfn edns(&self) -> Result<Option<Edns>, DnsErr>the EDNS(0) record of the additional section (None without one); several or a malformed one is an error (FORMERR, RFC 6891 6.1.1)fn set_edns(&mut self, e: Option<&Edns>)replaces the OPT record (None: removes it)fn rcode(&self) -> u16the whole RCODE: the header's four bits and EDNS's upper eight (RFC 6891 6.1.3)fn parse(b: &[u8]) -> Result<Message, DnsErr>fn to_bytes(&self) -> Vec<u8>the message in wire form, names compressedfn to_bytes_max(&self, limit: usize) -> (Vec<u8>, bool)at mostlimitbytes: records that do not fit are left out whole and TC is set (RFC 2181 9: true when something was left out); a record with an invalid name is skipped