// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The DNS, as DMARC needs to see it: TXT records, and whether a name //! exists at all. //! //! Two questions, because DMARC asks two. Policy discovery walks up the //! namespace reading TXT records; `np` — the policy for subdomains that //! were never registered — needs to know whether the name in the //! `From:` field is one of those, and that is a different question from //! "what does it publish". //! //! It is a parameter rather than something this library reaches for, //! for the same reason it is in zig-spf: the walk is a recursive DNS //! algorithm, so handing the resolver in is what lets the whole of it be //! tested against a table with no network in sight. A caller doing SPF //! and DMARC in the same session has zig-spf's resolver already, and //! adapting it is a dozen lines — this interface is deliberately the //! smaller half of that one. const std = @import("std"); const Io = std.Io; const Resolver = @This(); context: ?*anyopaque, vtable: *const VTable, pub const Error = error{ /// The name does not exist, or exists with no TXT records. Both mean /// the same thing to a walk that is looking for a policy: keep /// going. NotFound, /// Anything that might be answered differently tomorrow. §4.10.1 /// leaves what to do about it to the receiver, which is why it is /// reported rather than swallowed. Temporary, /// The answer did not fit. Whatever was written stays written. NoSpace, } || Io.Cancelable; pub const VTable = struct { /// Every TXT record of `name`, each already joined from the /// character-strings it was published in. txt: *const fn (context: ?*anyopaque, io: Io, name: []const u8, out: *Strings) Error!void, /// Whether `name` exists in the DNS at all — which RFC 9091 defines /// for this purpose as having an A, AAAA or MX record, a broader /// test than RFC 8020's. /// /// Optional, and a resolver that leaves it out is treated as saying /// every name exists. That is the conservative answer: it falls back /// to `sp` or `p`, and `np` is generally the stricter of the three, /// so a resolver that cannot tell never causes mail to be refused on /// a guess. exists: ?*const fn (context: ?*anyopaque, io: Io, name: []const u8) Error!bool = null, }; pub fn txt(resolver: Resolver, io: Io, name: []const u8, out: *Strings) Error!void { return resolver.vtable.txt(resolver.context, io, name, out); } pub fn exists(resolver: Resolver, io: Io, name: []const u8) Error!bool { const ask = resolver.vtable.exists orelse return true; return ask(resolver.context, io, name); } /// A set of strings an answer is collected into, with both the bytes and /// the index belonging to the caller. pub const Strings = struct { storage: []u8, index: [][]const u8, /// When set, a string that does not begin with this — compared /// without case — is discarded rather than stored. /// /// A `_dmarc` name shares its TXT record set with whatever else was /// put there, and there is usually something: the filtering happens /// in `append` so that no resolver can forget it. keep_prefix: ?[]const u8 = null, len: usize = 0, used: usize = 0, pub fn init(storage: []u8, index: [][]const u8) Strings { return .{ .storage = storage, .index = index }; } pub fn append(strings: *Strings, bytes: []const u8) error{NoSpace}!void { if (strings.keep_prefix) |prefix| { if (bytes.len < prefix.len) return; if (!std.ascii.eqlIgnoreCase(bytes[0..prefix.len], prefix)) return; } if (strings.len == strings.index.len) return error.NoSpace; if (strings.used + bytes.len > strings.storage.len) return error.NoSpace; const at = strings.used; @memcpy(strings.storage[at..][0..bytes.len], bytes); strings.used += bytes.len; strings.index[strings.len] = strings.storage[at..][0..bytes.len]; strings.len += 1; } pub fn slice(strings: *const Strings) []const []const u8 { return strings.index[0..strings.len]; } pub fn reset(strings: *Strings) void { strings.len = 0; strings.used = 0; } };