message
ModuleDNS messages (RFC 1035 §4): the header, questions and the answer, authority and additional records, in wire format.
Contents
4 submodules, and what the module defines itself.
use utopia_dns::message::Message;pub struct MessageA DNS message: a header and four sections whose sizes the header counts.
Message::new checks that the counts match the sections, and that an
authoritative, complete NXDOMAIN or empty NOERROR response carries
the zone's SOA in its authority section (RFC 2308).
use utopia_dns::message::{Message, Question, Record, RCODE_NOERROR};
let query = Message::query(Question::new("example.com", Record::TYPE_A), Some(0x1234), true);
let response = Message::response(query.header(), RCODE_NOERROR)
.questions(query.questions().to_vec())
.answers(vec![Record::new("example.com", Record::TYPE_A).with_ttl(60).with_rdata("192.0.2.1")])
.authoritative(true)
.build()?;
let wire = response.encode(Some(512))?;
let decoded = Message::decode(&wire)?;
assert_eq!(decoded.header().id, 0x1234);
assert_eq!(decoded.answers()[0].rdata(), b"192.0.2.1");use utopia_dns::message::ResponseBuilder;pub struct ResponseBuilderA response being built by Message::response.
pub const MAX_SIZE: usize = 65535The largest message: TCP frames carry a 2-byte length (RFC 1035 §4.2.2).
pub const MAX_UDP_SIZE: usize = 512The largest UDP payload without EDNS0 (RFC 1035 §4.2.1).
pub const RCODE_FORMERR: u8 = 1FORMERR: the query could not be interpreted.
pub const RCODE_NOERROR: u8 = 0NOERROR
pub const RCODE_NOTAUTH: u8 = 9NOTAUTH
pub const RCODE_NOTIMP: u8 = 4NOTIMP: the kind of query is not supported.
pub const RCODE_NOTZONE: u8 = 10NOTZONE
pub const RCODE_NXDOMAIN: u8 = 3NXDOMAIN: the name does not exist.
pub const RCODE_NXRRSET: u8 = 8NXRRSET
pub const RCODE_REFUSED: u8 = 5REFUSED
pub const RCODE_SERVFAIL: u8 = 2SERVFAIL: the server failed.
pub const RCODE_YXDOMAIN: u8 = 6YXDOMAIN
pub const RCODE_YXRRSET: u8 = 7YXRRSET
message::domain
Own pageDomain names in DNS wire format (RFC 1035 §3.1, §4.1.4).
use utopia_dns::message::domain::decode;pub fn decode(data: &[u8], offset: usize) -> Result<(Vec<u8>, usize), Error>Decodes the name at offset in a whole packet, following compression
pointers (which must point backwards, and never to the same place twice).
Returns the dotted name and the offset after it in the packet.
use utopia_dns::message::domain::encode;pub fn encode(name: &[u8]) -> Result<Vec<u8>, Error>Encodes a name as length-prefixed labels ending with the root. An empty
name, . or a name ending in one . is absolute; @ and empty labels
are refused.
use utopia_dns::message::domain::encode_into;pub fn encode_into(name: &[u8], out: &mut Vec<u8>) -> Result<(), Error>encode appended to out.
pub const MAX_DOMAIN_NAME_LEN: usize = 255The longest encoded name, length octets and the root included.
pub const MAX_LABEL_LEN: usize = 63The longest label.
pub const MAX_LABELS: usize = 127The most labels a name has.
message::header
Own pageThe 12-byte message header (RFC 1035 §4.1.1).
use utopia_dns::message::header::Header;pub struct HeaderA message header: the id, flags and section counts.
The reserved Z bits are ignored when decoding, as many clients and proxies set them.
message::question
Own pageThe question section (RFC 1035 §4.1.2).
use utopia_dns::message::question::Question;pub struct QuestionA question: a name, a record type and a class.
message::record
Own pageResource records (RFC 1035 §3.2, §4.1.3) and their data.
use utopia_dns::message::record::Record;pub struct RecordA resource record: an owner name, type, class, TTL and data.
The data is in presentation form, as zone files write it: an address
for A and AAAA, a name for CNAME, NS, PTR, MX and SRV
(whose numbers are separate fields), mname rname serial refresh retry expire minimum for SOA, flags tag "value" for CAA, the text for
TXT, and hex for other types. It is checked when the record is
encoded, so a zone may hold templates that are never sent.
use utopia_dns::message::Record;
let record = Record::new("Mail.Example.com", Record::TYPE_MX)
.with_ttl(3600)
.with_rdata("mail.exchange.example.com")
.with_priority(10);
assert_eq!(record.name(), b"mail.example.com");
let wire = record.encode()?;
let (decoded, _) = Record::decode(&wire, 0)?;
assert_eq!(decoded, record);