// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! A dynamic update (RFC 2136), written down. //! //! An update is a DNS message with the opcode set to `update`, and that one //! field changes what all four sections mean. What is a question in a query is //! the **zone** being updated; the answer section holds the **prerequisites** //! that must hold before anything is done; the authority section holds the //! **updates** themselves. RFC 2136 §2 names them ZOCOUNT, PRCOUNT and //! UPCOUNT for exactly that reason, and `section` below is the translation. //! //! What makes this worth a file of its own rather than a paragraph in a //! caller is that the nine shapes an update can take are *encodings* rather //! than ideas. "Delete this RRset" is a record with the class set to `any`, a //! zero TTL and no data at all; "this name must not exist" is the same shape //! with the class `none` and the type `any`. A caller writing that out by hand //! writes `.{ .unknown = .{ .type = t, .bytes = "" } }` and picks a class from //! a table in a specification it has open in another window, and gets it //! wrong. `Prerequisite` and `Change` are that table, as types, one variant //! per subsection of RFC 2136 §2.4 and §2.5. //! //! Nothing here does I/O, decides anything, or holds a clock: a `Builder` and //! a caller's buffer is the whole of it. RFC 3007's authentication of an //! update is `tsig` and `sig0` beside this, for the same reason. //! //! ### What a server will refuse //! //! §3.4.1.2's prescan is a server's job, but every one of its rules is a thing //! a requestor can check before spending a round trip finding out. `check` is //! that scan from this side: a name outside the zone is `error.NotInZone` //! rather than a `notzone` response code, and a type that never has record //! data -- `any` in an addition, `axfr` or `nxname` anywhere -- is caught here //! rather than by the primary. const std = @import("std"); const Builder = @import("Builder.zig"); const Header = @import("Header.zig"); const Name = @import("Name.zig"); const rdata = @import("rdata.zig"); const types = @import("types.zig"); /// The opcode that makes a message an update rather than a query. pub const opcode: types.Opcode = .update; /// What RFC 2136 §2 calls each section, in terms of the sections a message /// already has. The names on the left are the ones the RFC uses; the ones on /// the right are what `Builder.record` takes. pub const section = struct { /// ZOCOUNT: exactly one entry, in question format, naming the zone. Its /// type is `soa` -- §2.3 says so, and it is how a server recognizes which /// of its zones is meant. pub const zone: Header.Section = .question; /// PRCOUNT: the conditions, §2.4. pub const prerequisite: Header.Section = .answer; /// UPCOUNT: the changes, §2.5. pub const update: Header.Section = .authority; /// ADCOUNT: whatever else is relevant, §2.6 -- out-of-zone glue for the /// records being added, and the signature, when there is one. pub const additional: Header.Section = .additional; }; /// A name and a type: an RRset, named without saying what is in it. pub const Rrset = struct { name: Name, type: types.Type, }; /// One record of a set, for the shapes that name the data as well as the set. pub const Contents = struct { name: Name, data: rdata.Data, }; /// A record being added, which is the one shape that carries a TTL. pub const Addition = struct { name: Name, /// How long the record may be cached once it is in the zone. Every other /// shape here has a TTL of zero because it is not putting anything in. ttl: u32, data: rdata.Data, }; /// A condition that must hold before the update is applied (RFC 2136 §2.4). /// /// A server checks all of them and applies nothing unless every one holds, so /// a prerequisite is how an update is made conditional on the state it was /// composed against -- "add this name, but only if nobody else has taken it /// since I looked". pub const Prerequisite = union(enum) { /// §2.4.1, "RRset exists (value independent)": at least one record of this /// name and type, whatever it says. Class `any`, no data. rrset_exists: Rrset, /// §2.4.2, "RRset exists (value dependent)": the set of this name and type /// is exactly the records given here. One variant per record: they are /// gathered by name and type on the far side, and the comparison is of the /// whole set, so a set of three needs three of these and leaving one out /// is a different condition rather than a laxer one. rrset_exists_with: Contents, /// §2.4.3, "RRset does not exist": no record of this name and type. Class /// `none`, no data. /// /// The class is what tells this from §2.4.1, because "no data" cannot: /// a `null` record's data is naturally empty. no_such_rrset: Rrset, /// §2.4.4, "Name is in use": some record of some type is owned by this /// name. Class `any`, type `any`, no data. An empty non-terminal does not /// satisfy it. name_in_use: Name, /// §2.4.5, "Name is not in use": no record of any type is owned by this /// name. Class `none`, type `any`, no data. An empty non-terminal *does* /// satisfy it, which is the asymmetry with the one above and is in the RFC /// rather than here. name_not_in_use: Name, }; /// A change to make (RFC 2136 §2.5). /// /// The changes are applied in the order they are written, and a server /// silently ignores one that finds nothing -- a delete of what is not there is /// not an error, which is what makes an update idempotent when it is written /// to be. pub const Change = union(enum) { /// §2.5.1, "Add to an RRset". The class is the zone's and the TTL is the /// one the record will have. A duplicate is silently ignored. add: Addition, /// §2.5.2, "Delete an RRset": every record of this name and type. Class /// `any`, TTL zero, no data. delete_rrset: Rrset, /// §2.5.3, "Delete all RRsets from a name": everything this name owns. /// Class `any`, type `any`, TTL zero, no data. delete_name: Name, /// §2.5.4, "Delete an RR from an RRset": this record exactly, matched on /// name, type and data. Class `none`, TTL zero -- and the TTL being /// ignored is why a record can be deleted without knowing what TTL it was /// given. delete_record: Contents, }; /// The question that names the zone: `ZNAME ZCLASS SOA` (RFC 2136 §2.3). pub fn writeZone(b: *Builder, zone: Name, class: types.Class) Builder.Error!void { try b.question(.{ .name = zone, .type = .soa, .class = class }); } test writeZone { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 1, .opcode = opcode }, .{}); try writeZone(&b, Name.literal("example.com"), .in); const message = try Message.parse(b.finish()); try testing.expectFmt("example.com.\tIN\tSOA", "{f}", .{(try message.question()).?}); } /// The record a prerequisite is written as. /// /// Exposed rather than private because the shapes are the whole of what this /// file knows, and a test that asserts the class and the empty data of each of /// the five is asserting the thing that goes wrong. pub fn prerequisiteSpec(class: types.Class, p: Prerequisite) Builder.RecordSpec { return switch (p) { .rrset_exists => |set| .{ .name = set.name, .class = .any, .ttl = 0, .data = empty(set.type), }, .rrset_exists_with => |one| .{ .name = one.name, .class = class, // "TTL must be specified as zero and is ignored when comparing // RRsets for identity" -- §2.4.2. .ttl = 0, .data = one.data, }, .no_such_rrset => |set| .{ .name = set.name, .class = .none, .ttl = 0, .data = empty(set.type), }, .name_in_use => |name| .{ .name = name, .class = .any, .ttl = 0, .data = empty(.any), }, .name_not_in_use => |name| .{ .name = name, .class = .none, .ttl = 0, .data = empty(.any), }, }; } test prerequisiteSpec { // "There is no A record at www yet": class NONE, type A, and no data. const spec = prerequisiteSpec(.in, .{ .no_such_rrset = .{ .name = Name.literal("www.example.com"), .type = .a, } }); try testing.expectEqual(types.Class.none, spec.class); try testing.expectEqual(types.Type.a, spec.data.recordType()); try testing.expectEqual(@as(usize, 0), spec.data.unknown.bytes.len); } /// The record a change is written as. pub fn changeSpec(class: types.Class, c: Change) Builder.RecordSpec { return switch (c) { .add => |one| .{ .name = one.name, .class = class, .ttl = one.ttl, .data = one.data, }, .delete_rrset => |set| .{ .name = set.name, .class = .any, .ttl = 0, .data = empty(set.type), }, .delete_name => |name| .{ .name = name, .class = .any, .ttl = 0, .data = empty(.any), }, .delete_record => |one| .{ .name = one.name, .class = .none, .ttl = 0, .data = one.data, }, }; } test changeSpec { // Deleting one record is the record itself with class NONE: the same data // with class IN would add it. const spec = changeSpec(.in, .{ .delete_record = .{ .name = Name.literal("www.example.com"), .data = .{ .a = .{ .octets = .{ 192, 0, 2, 1 } } }, } }); try testing.expectEqual(types.Class.none, spec.class); try testing.expectEqual(@as(u32, 0), spec.ttl); try testing.expectEqual(types.Type.a, spec.data.recordType()); } /// Writes one prerequisite into the answer section. pub fn writePrerequisite(b: *Builder, class: types.Class, p: Prerequisite) Builder.Error!void { try b.record(section.prerequisite, prerequisiteSpec(class, p)); } test writePrerequisite { var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 1, .opcode = opcode }, .{}); try writeZone(&b, Name.literal("example.com"), .in); try writePrerequisite(&b, .in, .{ .name_not_in_use = Name.literal("www.example.com") }); const message = try Message.parse(b.finish()); var it = message.answers(); const rr = (try it.next()).?; try testing.expectEqual(types.Class.none, rr.class); try testing.expectEqual(types.Type.any, rr.type); } /// Writes one change into the authority section. pub fn writeChange(b: *Builder, class: types.Class, c: Change) Builder.Error!void { try b.record(section.update, changeSpec(class, c)); } test writeChange { // Add an address for www, but only if the name is not already taken. var buf: [512]u8 = undefined; var b = try Builder.init(&buf, .{ .id = 1, .opcode = opcode }, .{}); try writeZone(&b, Name.literal("example.com"), .in); try writePrerequisite(&b, .in, .{ .name_not_in_use = Name.literal("www.example.com") }); try writeChange(&b, .in, .{ .add = .{ .name = Name.literal("www.example.com"), .ttl = 3600, .data = .{ .a = .{ .octets = .{ 192, 0, 2, 1 } } }, } }); const message = try Message.parse(b.finish()); var it = message.authority(); try testing.expectFmt("www.example.com.\t3600\tIN\tA\t192.0.2.1", "{f}", .{(try it.next()).?}); } /// A record with a type and nothing in it, which is what most of the shapes /// above are. RFC 3597's `unknown` is the only way to say "this type, no /// data": every other variant of `rdata.Data` carries fields that would have /// to be invented. fn empty(t: types.Type) rdata.Data { return .{ .unknown = .{ .type = t, .bytes = &.{} } }; } pub const CheckError = error{ /// A name in the update is not at or below the zone being updated, which /// RFC 2136 §3.4.1.3 answers with `notzone`. NotInZone, /// A type that never has record data -- `any` where a real type is /// needed, or `axfr`, `ixfr`, `maila`, `mailb`, `nxname` anywhere at all. /// §3.4.1.2 answers this with `formerr`. NotARecordType, }; /// A requestor's half of RFC 2136 §3.4.1's prescan, for one change. /// /// Every rule here is one a server applies before it does anything, so /// checking them from this side turns a response code into an error at the /// call that got it wrong -- with the change still in hand, which is where /// somebody can see what they typed. pub fn check(zone: Name, c: Change) CheckError!void { const spec = changeSpec(.in, c); if (!spec.name.isSubdomainOf(zone)) return error.NotInZone; const t = spec.data.recordType(); switch (c) { // §3.4.1.2: for a class that is the zone's, a metatype of any kind is // a format error -- including `any`, which has a shape of its own // three lines down and is never a thing to add. .add, .delete_record => if (t.isRecordless()) return error.NotARecordType, // For class `any` the RFC allows the type `any` and no other metatype, // which is exactly the difference between `delete_name` and a // `delete_rrset` somebody typed `ANY` into. .delete_rrset => if (t.isRecordless()) return error.NotARecordType, .delete_name => {}, } } test check { const zone = Name.literal("example.com"); try check(zone, .{ .delete_rrset = .{ .name = Name.literal("www.example.com"), .type = .a } }); // Both of these would come back from the primary as errors; here they are // errors before anything is sent. try testing.expectError(error.NotInZone, check(zone, .{ .delete_name = Name.literal("www.example.net"), })); try testing.expectError(error.NotARecordType, check(zone, .{ .delete_rrset = .{ .name = Name.literal("www.example.com"), .type = .axfr, } })); } /// The same check for a prerequisite: §3.2 makes an out-of-zone name a /// `notzone` there too, and the value-dependent shape is a record like any /// other. pub fn checkPrerequisite(zone: Name, p: Prerequisite) CheckError!void { const spec = prerequisiteSpec(.in, p); if (!spec.name.isSubdomainOf(zone)) return error.NotInZone; switch (p) { .rrset_exists_with => if (spec.data.recordType().isRecordless()) { return error.NotARecordType; }, .rrset_exists, .no_such_rrset => |set| if (set.type.isRecordless()) { return error.NotARecordType; }, .name_in_use, .name_not_in_use => {}, } } test checkPrerequisite { const zone = Name.literal("example.com"); try checkPrerequisite(zone, .{ .name_in_use = Name.literal("www.example.com") }); try testing.expectError(error.NotInZone, checkPrerequisite(zone, .{ .name_in_use = Name.literal("www.example.net"), })); // ANY never has records of its own, so there is no such set to exist. try testing.expectError(error.NotARecordType, checkPrerequisite(zone, .{ .rrset_exists = .{ .name = Name.literal("www.example.com"), .type = .any, } })); } const testing = std.testing; const Message = @import("Message.zig"); /// Encodes one prerequisite and one change and reads the message back, which /// is the only way to assert what actually went on the wire. fn roundTrip( buffer: []u8, zone: Name, p: ?Prerequisite, c: ?Change, ) !Message { var b = try Builder.init(buffer, .{ .id = 0x1234, .opcode = opcode }, .{}); try writeZone(&b, zone, .in); if (p) |one| try writePrerequisite(&b, .in, one); if (c) |one| try writeChange(&b, .in, one); return Message.parse(b.finish()); } test "the zone section is a question of type soa" { var buffer: [512]u8 = undefined; const zone = Name.literal("example.com."); const message = try roundTrip(&buffer, zone, null, null); try testing.expectEqual(types.Opcode.update, message.header.opcode); try testing.expectEqual(@as(u16, 1), message.header.counts.question); var questions = message.questions(); const q = (try questions.next()).?; try testing.expect(q.name.eql(zone)); try testing.expectEqual(types.Type.soa, q.type); try testing.expectEqual(types.Class.in, q.class); } test "RFC 2136 §2.4: the five prerequisites, as bytes" { var buffer: [512]u8 = undefined; const zone = Name.literal("example.com."); const www = Name.literal("www.example.com."); // §2.4.1: class `any`, type as asked, no data. { const message = try roundTrip(&buffer, zone, .{ .rrset_exists = .{ .name = www, .type = .a, } }, null); var it = message.answers(); const rr = (try it.next()).?; try testing.expectEqual(types.Class.any, rr.class); try testing.expectEqual(types.Type.a, rr.type); try testing.expectEqual(@as(u32, 0), rr.ttl); try testing.expectEqual(@as(usize, 0), rr.rdata.len); } // §2.4.2: the zone's class, a zero TTL, and the data that must be there. { const message = try roundTrip(&buffer, zone, .{ .rrset_exists_with = .{ .name = www, .data = .{ .a = .{ .octets = .{ 192, 0, 2, 1 } } }, } }, null); var it = message.answers(); const rr = (try it.next()).?; try testing.expectEqual(types.Class.in, rr.class); try testing.expectEqual(types.Type.a, rr.type); try testing.expectEqual(@as(u32, 0), rr.ttl); try testing.expectEqualSlices(u8, &.{ 192, 0, 2, 1 }, rr.rdata); } // §2.4.3: `none` is the whole of what says this is a denial, because an // empty rdata is something a `null` record has naturally. { const message = try roundTrip(&buffer, zone, .{ .no_such_rrset = .{ .name = www, .type = .a, } }, null); var it = message.answers(); const rr = (try it.next()).?; try testing.expectEqual(types.Class.none, rr.class); try testing.expectEqual(types.Type.a, rr.type); try testing.expectEqual(@as(usize, 0), rr.rdata.len); } // §2.4.4 and §2.4.5 differ in the class alone. { const message = try roundTrip(&buffer, zone, .{ .name_in_use = www }, null); var it = message.answers(); const rr = (try it.next()).?; try testing.expectEqual(types.Class.any, rr.class); try testing.expectEqual(types.Type.any, rr.type); try testing.expectEqual(@as(usize, 0), rr.rdata.len); } { const message = try roundTrip(&buffer, zone, .{ .name_not_in_use = www }, null); var it = message.answers(); const rr = (try it.next()).?; try testing.expectEqual(types.Class.none, rr.class); try testing.expectEqual(types.Type.any, rr.type); try testing.expectEqual(@as(usize, 0), rr.rdata.len); } } test "RFC 2136 §2.5: the four changes, as bytes" { var buffer: [512]u8 = undefined; const zone = Name.literal("example.com."); const www = Name.literal("www.example.com."); // §2.5.1: the only shape with a TTL, because it is the only one putting // something into the zone. { const message = try roundTrip(&buffer, zone, null, .{ .add = .{ .name = www, .ttl = 3600, .data = .{ .a = .{ .octets = .{ 192, 0, 2, 1 } } }, } }); var it = message.authority(); const rr = (try it.next()).?; try testing.expectEqual(types.Class.in, rr.class); try testing.expectEqual(@as(u32, 3600), rr.ttl); try testing.expectEqualSlices(u8, &.{ 192, 0, 2, 1 }, rr.rdata); } // §2.5.2: the set, whatever is in it. { const message = try roundTrip(&buffer, zone, null, .{ .delete_rrset = .{ .name = www, .type = .a, } }); var it = message.authority(); const rr = (try it.next()).?; try testing.expectEqual(types.Class.any, rr.class); try testing.expectEqual(types.Type.a, rr.type); try testing.expectEqual(@as(u32, 0), rr.ttl); try testing.expectEqual(@as(usize, 0), rr.rdata.len); } // §2.5.3: everything the name owns. { const message = try roundTrip(&buffer, zone, null, .{ .delete_name = www }); var it = message.authority(); const rr = (try it.next()).?; try testing.expectEqual(types.Class.any, rr.class); try testing.expectEqual(types.Type.any, rr.type); try testing.expectEqual(@as(usize, 0), rr.rdata.len); } // §2.5.4: `none` and the data, which is what tells a deletion of one // record from an addition of the same record. { const message = try roundTrip(&buffer, zone, null, .{ .delete_record = .{ .name = www, .data = .{ .a = .{ .octets = .{ 192, 0, 2, 1 } } }, } }); var it = message.authority(); const rr = (try it.next()).?; try testing.expectEqual(types.Class.none, rr.class); try testing.expectEqual(types.Type.a, rr.type); try testing.expectEqual(@as(u32, 0), rr.ttl); try testing.expectEqualSlices(u8, &.{ 192, 0, 2, 1 }, rr.rdata); } } test "an update names its sections after what they hold" { var buffer: [512]u8 = undefined; const zone = Name.literal("example.com."); const www = Name.literal("www.example.com."); var b = try Builder.init(&buffer, .{ .id = 1, .opcode = opcode }, .{}); try writeZone(&b, zone, .in); try writePrerequisite(&b, .in, .{ .name_not_in_use = www }); try writeChange(&b, .in, .{ .add = .{ .name = www, .ttl = 3600, .data = .{ .a = .{ .octets = .{ 192, 0, 2, 1 } } }, } }); const message = try Message.parse(b.finish()); // ZOCOUNT, PRCOUNT, UPCOUNT, ADCOUNT. try testing.expectEqual(@as(u16, 1), message.header.counts.question); try testing.expectEqual(@as(u16, 1), message.header.counts.answer); try testing.expectEqual(@as(u16, 1), message.header.counts.authority); try testing.expectEqual(@as(u16, 0), message.header.counts.additional); } test "the prescan a requestor can do for itself" { const zone = Name.literal("example.com."); const www = Name.literal("www.example.com."); const elsewhere = Name.literal("www.example.net."); // A name outside the zone is `notzone`, and knowing that here costs no // round trip. try testing.expectError(error.NotInZone, check(zone, .{ .delete_name = elsewhere })); try testing.expectError(error.NotInZone, checkPrerequisite(zone, .{ .name_in_use = elsewhere })); // The apex is in its own zone. try check(zone, .{ .delete_rrset = .{ .name = zone, .type = .mx } }); // A metatype where a record type is needed. try testing.expectError(error.NotARecordType, check(zone, .{ .delete_rrset = .{ .name = www, .type = .any, } })); try testing.expectError(error.NotARecordType, check(zone, .{ .delete_rrset = .{ .name = www, .type = .axfr, } })); try testing.expectError(error.NotARecordType, checkPrerequisite(zone, .{ .rrset_exists = .{ .name = www, .type = .any, } })); // But `any` as a whole name is a shape of its own and is fine. try check(zone, .{ .delete_name = www }); try checkPrerequisite(zone, .{ .name_not_in_use = www }); }