Rust

message

Module

DNS 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;
Rust
pub struct Message

A 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).

Rust
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;
Rust
pub struct ResponseBuilder

A response being built by Message::response.

Rust
pub const MAX_SIZE: usize = 65535

The largest message: TCP frames carry a 2-byte length (RFC 1035 §4.2.2).

Rust
pub const MAX_UDP_SIZE: usize = 512

The largest UDP payload without EDNS0 (RFC 1035 §4.2.1).

Rust
pub const RCODE_FORMERR: u8 = 1

FORMERR: the query could not be interpreted.

Rust
pub const RCODE_NOERROR: u8 = 0

NOERROR

Rust
pub const RCODE_NOTAUTH: u8 = 9

NOTAUTH

Rust
pub const RCODE_NOTIMP: u8 = 4

NOTIMP: the kind of query is not supported.

Rust
pub const RCODE_NOTZONE: u8 = 10

NOTZONE

Rust
pub const RCODE_NXDOMAIN: u8 = 3

NXDOMAIN: the name does not exist.

Rust
pub const RCODE_NXRRSET: u8 = 8

NXRRSET

Rust
pub const RCODE_REFUSED: u8 = 5

REFUSED

Rust
pub const RCODE_SERVFAIL: u8 = 2

SERVFAIL: the server failed.

Rust
pub const RCODE_YXDOMAIN: u8 = 6

YXDOMAIN

Rust
pub const RCODE_YXRRSET: u8 = 7

YXRRSET

message::domain

Own page

Domain names in DNS wire format (RFC 1035 §3.1, §4.1.4).

use utopia_dns::message::domain::decode;
Rust
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;
Rust
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;
Rust
pub fn encode_into(name: &[u8], out: &mut Vec<u8>) -> Result<(), Error>

encode appended to out.

Rust
pub const MAX_DOMAIN_NAME_LEN: usize = 255

The longest encoded name, length octets and the root included.

Rust
pub const MAX_LABEL_LEN: usize = 63

The longest label.

Rust
pub const MAX_LABELS: usize = 127

The most labels a name has.

message::header

Own page

The 12-byte message header (RFC 1035 §4.1.1).

use utopia_dns::message::header::Header;
Rust
pub struct Header

A 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 page

The question section (RFC 1035 §4.1.2).

use utopia_dns::message::question::Question;
Rust
pub struct Question

A question: a name, a record type and a class.

message::record

Own page

Resource records (RFC 1035 §3.2, §4.1.3) and their data.

use utopia_dns::message::record::Record;
Rust
pub struct Record

A 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.

Rust
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);