Something went wrong. Try again.
DNS message encoding and decoding for Zig, with no I/O and no allocation.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Messages captured from a real resolver.//!//! Every one of these is the exact bytes of a query sent to or an answer//! received from 1.1.1.1, recorded once and checked in. They are worth more//! than messages this library built itself: a round trip through code that//! agrees with itself proves nothing about whether it agrees with the rest of//! the internet, and these are full of things nobody would think to construct//! -- compression pointers into the middle of record data, an `https` record//! with three parameters, thirteen name servers that are almost entirely//! pointers, a signed denial that says a name exists and holds nothing.//!//! They are here rather than in `messages.zig` because the fuzzer wants them//! too, and for the same reason: a mutation of a real message reaches shapes//! that a mutation of a made-up one does not, and that corpus is what stands//! in for the coverage feedback this toolchain cannot provide.
const std = @import("std");
/// Turns a fixture's hex back into bytes. The fixtures are written as hex/// because that is the form they were captured in, and because a Zig string/// escape per byte would be unreadable and impossible to check against a/// packet capture.fn unhex(comptime hex: []const u8) [hex.len / 2]u8 { comptime { @setEvalBranchQuota(10 * hex.len); var out: [hex.len / 2]u8 = undefined; _ = std.fmt.hexToBytes(&out, hex) catch unreachable; return out; }}
pub const Fixture = struct { /// What is interesting about this message. what: []const u8, /// The bytes, decoded once at compile time from the hex it was recorded /// as. Hex because that is the form a packet capture is read in, and /// because a Zig string escape per byte could not be checked against one. bytes: []const u8,};
fn capture(comptime what: []const u8, comptime hex: []const u8) Fixture { const bytes = comptime unhex(hex); return .{ .what = what, .bytes = &bytes };}
// `dig` was asked for each of these through 1.1.1.1, with EDNS and, where it// says so, with DNSSEC records requested. They are named rather than indexed// so that adding one cannot quietly re-point a test at a different message.pub const query = capture( "a query for example.com A, with an OPT record", "123401000001000000000001076578616d706c6503636f6d00" ++ "000100010000291000000000000000",);pub const a_answer = capture( "the answer: two A records, both compressed against the question", "123481800001000200000001076578616d706c6503636f6d00" ++ "0001000" ++ "1c00c00010001000000150004ac4293f3c00c0001" ++ "00010000001500046814179a00002904d0000000000000",);pub const mx_at_the_root = capture( "an MX record whose exchange is the root", "123481800001000100000001076578616d706c6503636f6d00" ++ "000f0001c00c000f00010000010c000300000000002904d0000000000000",);pub const tlsa_nodata = capture( "a TLSA query answered with an SOA: the name has no such type", "123481800001000000010001035f3235045f746370046d61696c" ++ "076578616d706c6503636f6d0000340001c01a000600010000070800" ++ "3207656c6c696f7474026e730a636c6f7564666c617265c02203646e" ++ "73c0428fe0888d000027100000096000093a800000070800002904d0" ++ "000000000000",);pub const https_record = capture( "an HTTPS record with alpn, ipv4hint and ipv6hint", "1234818000010001000000010a636c6f7564666c61726503636f6d" ++ "0000410001c00c004100010000012c003d000100000100060268330268" ++ "3200040008681084e5681085e500060020260647000000000000000000" ++ "681084e5260647000000000000000000681085e500002904d000000000" ++ "0000",);pub const root_servers = capture( "the root's thirteen name servers, every one of them compressed", "123481800001000d00000001000002000100000200010007ddc100" ++ "1401610c726f6f742d73657276657273036e65740000000200010007" ++ "ddc100040162c01e00000200010007ddc100040163c01e0000020001" ++ "0007ddc100040164c01e00000200010007ddc100040165c01e000002" ++ "00010007ddc100040166c01e00000200010007ddc100040167c01e00" ++ "000200010007ddc100040168c01e00000200010007ddc10004016" ++ "9c01e00000200010007ddc10004016ac01e00000200010007ddc10004" ++ "016bc01e00000200010007ddc10004016cc01e00000200010007ddc10" ++ "004016dc01e00002904d0000000000000",);pub const dnskeys = capture( "four DNSKEYs and the RRSIG over them", "123481a00001000500000001076578616d706c6503636f6d000030" ++ "0001c00c003000010000003900440100030d323c9989e94fd06aa7888d" ++ "7e8fec001bfdedd089830c8963d42c51d28a1b91940748a1f616ac7ab6" ++ "6d8d718bb9ecf389f374b30962e4a888d91d2d66879c1b6bc00c003000" ++ "010000003900440100030d9318a98e821b3590ec5aa10a698686aba7cc" ++ "fd74e1ad1a75b9ee8057d47f9f7796c691827825896f8ecf104123fd0e" ++ "95018156ffb505a39997a197f129865adcc00c00300001000000390044" ++ "0100030da09311112cf9138818cd2feae970ebbd4d6a30f6088c25b325" ++ "a39abbc5cd1197aa098283e5aaf421177c2aa5d714992a9957d1bcc18f" ++ "98cd71f1f1806b65e148c00c003000010000003900440101030d99db2c" ++ "c14cabdc33d6d77da63a2f15f71112584f234e8d1dc428e39e8a4a97e1" ++ "aa271a555dc90701e17e2a4c4b6f120b7c32d44f4ac02bd894cf2d4be7" ++ "778a19c00c002e000100000039005f00300d0200000e106ae77b046a97" ++ "0f840943076578616d706c6503636f6d005df1f015f887a7cba84520fc" ++ "9bbbf8b5f308d766481f4f7d16d4b3ba007944a57b4eecf83dfff220ef" ++ "4f5ec0fbcc2ba26c3a16d218abf036aa58512baf4d5c9200002904d000" ++ "0080000000",);pub const signed_denial = capture( "a signed denial of existence: SOA, NSEC and two RRSIGs", "123481a00001000000040001116e6f6e6578697374656e742d7879" ++ "7a7a79076578616d706c6503636f6d0000010001c01e00060001000007" ++ "08003207656c6c696f7474026e730a636c6f7564666c617265c0260364" ++ "6e73c0468fe0888d000027100000096000093a8000000708c01e002e00" ++ "0100000708005f00060d02000007086aa868806aa5a96086c907657861" ++ "6d706c6503636f6d0096e628523ebc48936243ba346d07ee3c1337fcb2" ++ "782cc8db52f2d195a5eeecf886c59cb89f9d7647d5c36ff8988a7d23e0" ++ "eb98f6b16b507dddd05a4d3829aa11c00c002f000100000708003401001" ++ "16e6f6e6578697374656e742d78797a7a79076578616d706c6503636f6d" ++ "0000110000000000030000000000000000000080c00c002e00010000070" ++ "8005f002f0d03000007086aa868806aa5a96086c9076578616d706c6503" ++ "636f6d003ae261b4e96e867d77f7d6fc091be622211e9ec1eb7b88cd759" ++ "7fd3ea94790ef25a37c7cbed12ad72903c0b131ed81440d6dd16ca9ba14" ++ "0623c6b62712c94b4a00002904d0000080000000",);/// Every capture, for the properties that must hold of all of them.pub const all = [_]Fixture{ query, a_answer, mx_at_the_root, tlsa_nodata, https_record, root_servers, dnskeys, signed_denial,};
/// The captured messages as bare bytes, which is the shape a fuzz corpus/// wants.pub const messages = blk: { var out: [all.len][]const u8 = undefined; for (all, 0..) |fixture, i| out[i] = fixture.bytes; break :blk out;};