Something went wrong. Try again.
DNS message encoding and decoding for Zig, with no I/O and no allocation.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! DNS messages, decoded and encoded. Nothing else.//!//! This library turns bytes into a message you can read and a message you//! describe into bytes. It opens no sockets, reads no files, starts no//! threads, consults no configuration and holds no cache. What you do with a//! message -- send it, log it, answer it, sign it, replay it from a capture --//! is somewhere else, and that is the point: the hard parts of this format are//! the same whether the bytes came from a socket or from a fuzzer.//!//! It also never allocates. A decoded `Message` is a view over the buffer it//! was read from, and a `Builder` writes into a buffer you hand it. The//! consequence to keep in mind is that **everything borrows**: a `Name` read//! out of a message is valid only as long as that message is, and `Name.clone`//! is how one outlives it.//!//! ### Reading a message//!//! ```zig//! const message = try dns.Message.parse(bytes);//! std.debug.print("{f}\n", .{message}); // prints it the way dig does//!//! var it = message.answers();//! while (try it.next()) |record| {//! switch (try record.data()) {//! .a => |address| std.debug.print("{f}\n", .{address}),//! .cname => |name| std.debug.print("{f}\n", .{name}),//! else => {},//! }//! }//! ```//!//! ### Writing one//!//! ```zig//! var buf: [512]u8 = undefined;//! var names: dns.Name.Buffer = undefined;//!//! var builder = try dns.Builder.init(&buf, .{//! .id = 0x1234,//! .recursion_desired = true,//! }, .{});//! try builder.question(.{//! .name = try dns.Name.fromText(&names, "example.com"),//! .type = .a,//! });//! try builder.opt(.{});//! const query = builder.finish();//! ```//!//! ### Where the format is under-specified//!//! RFC 1035 §4.1.4 says a compression pointer names "a prior occurrence of the//! same name", and leaves two readings of "prior" in use. `Name.PointerRule`//! is the choice between them, and `Message.parseWithOptions` is how to make//! it://!//! ```zig//! const message = try dns.Message.parseWithOptions(bytes, .{//! .names = .{ .pointers = .descending },//! });//! ```//!//! The default is the permissive reading and still terminates; the other is//! what dnspython requires and makes termination obvious rather than argued.//! Neither accepts a loop, and no compressor can tell them apart.//!//! ### What it will not do//!//! No cryptography. `rdata.dnssec` decodes every DNSSEC record type and//! `Dnskey.keyTag` does the arithmetic that links them, but nothing here//! verifies a signature, and `Builder` deliberately refuses to compress names//! in anything signed so that what a validator is handed is what was signed.//!//! No zone files. `format` writes the presentation form of everything, because//! a decoded message that cannot be printed is hard to trust, but nothing here//! reads it back.
const std = @import("std");
pub const Builder = @import("Builder.zig");pub const Header = @import("Header.zig");pub const Message = @import("Message.zig");pub const Name = @import("Name.zig");pub const Question = @import("Question.zig");pub const Record = @import("Record.zig");
/// EDNS(0): the `opt` pseudo-record and the options it carries.pub const edns = @import("edns.zig");/// Writing record data the way a zone file does.pub const presentation = @import("presentation.zig");/// What a record holds, by type.pub const rdata = @import("rdata.zig");/// Serial number arithmetic (RFC 1982): comparing two numbers that wrap, which/// is what an `soa` serial is and what comparing one with `<` gets wrong.pub const serial = @import("serial.zig");/// The bytes a SIG(0) signature covers (RFC 2931 §3.1), for the other way of/// authenticating a request: a public key rather than a shared secret.pub const sig0 = @import("sig0.zig");/// The bytes a TSIG signature covers (RFC 8945 §4.3), written into a writer./// Which fields in which order is a question about serialisation; the keyed/// hash that consumes them is not this library's business.pub const tsig = @import("tsig.zig");/// The shapes a dynamic update takes on the wire (RFC 2136): which class, which/// TTL and which empty rdata says "delete this RRset" rather than "add a record/// with nothing in it".pub const update = @import("update.zig");/// Reading fixed-width fields out of a message.pub const wire = @import("wire.zig");/// The numbers drawn from a registry: types, classes, opcodes, response codes.pub const types = @import("types.zig");
pub const Type = types.Type;pub const Class = types.Class;pub const Opcode = types.Opcode;pub const ResponseCode = types.ResponseCode;pub const Section = Header.Section;
/// The port IANA assigns to DNS, over UDP and over TCP alike.pub const port = 53;/// The port for DNS over TLS (RFC 7858).pub const tls_port = 853;
/// The largest response RFC 1035 §4.2.1 allows over UDP without EDNS. A/// requestor that has not sent an `opt` record is taken to have advertised/// this, and a response that does not fit has to be truncated.pub const classic_udp_payload_size = 512;
/// The largest a message can be at all, being what the two-byte length prefix/// of RFC 1035 §4.2.2 can frame.pub const max_message_len = wire.max_message_len;
test { std.testing.refAllDecls(@This());}