// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What is being asked: a name, a type and a class. //! //! The header can say there is more than one question, and nothing in RFC 1035 //! forbids it, but no server has ever answered two -- there is only one //! response code and one set of flags to answer them with. Treat a count other //! than one as a message to refuse rather than a feature to support. const std = @import("std"); const Writer = std.Io.Writer; const Name = @import("Name.zig"); const types = @import("types.zig"); const wire = @import("wire.zig"); const Question = @This(); /// The name asked about. Borrows the message it was read from. name: Name, /// The type asked for. May be a QTYPE such as `any` or `axfr`, which is /// meaningful here and nowhere else. type: types.Type, /// Almost always `in`. class: types.Class = .in, /// Reads one entry of the question section. `d` has to be a decoder over the /// whole message, because the name may be a pointer back into it. Most callers /// want `Message.question`, which does this at the right offset. pub fn parse(d: *wire.Decoder) wire.Error!Question { return .{ .name = try d.name(), .type = try d.enumeration(types.Type), .class = try d.enumeration(types.Class), }; } test parse { var d = try wire.Decoder.init("\x07example\x03com\x00\x00\x0f\x00\x01"); const q = try parse(&d); try testing.expectFmt("example.com.", "{f}", .{q.name}); try testing.expectEqual(types.Type.mx, q.type); try testing.expectEqual(types.Class.in, q.class); } /// Whether two questions ask the same thing. The name comparison ignores case, /// as every name comparison does, which is what lets a server answer a query /// for `EXAMPLE.com` from a cache entry made for `example.COM`. pub fn eql(a: Question, b: Question) bool { return a.type == b.type and a.class == b.class and a.name.eql(b.name); } test eql { const asked: Question = .{ .name = Name.literal("EXAMPLE.com"), .type = .a }; try testing.expect(asked.eql(.{ .name = Name.literal("example.COM"), .type = .a })); // The same name, a different type: a different question. try testing.expect(!asked.eql(.{ .name = Name.literal("example.com"), .type = .aaaa })); } /// As `dig` prints it in the question section: the name, the class, the type. pub fn format(self: Question, w: *Writer) Writer.Error!void { try w.print("{f}\t{f}\t{f}", .{ self.name, self.class, self.type }); } test format { const q: Question = .{ .name = Name.literal("example.com"), .type = .aaaa }; try testing.expectFmt("example.com.\tIN\tAAAA", "{f}", .{q}); } const testing = std.testing; test "a question is a name, a type and a class" { var d = try wire.Decoder.init("\x07example\x03com\x00\x00\x01\x00\x01"); const q = try Question.parse(&d); try testing.expectFmt("example.com.\tIN\tA", "{f}", .{q}); try testing.expect(d.atEnd()); var buf: Name.Buffer = undefined; try testing.expect(q.eql(.{ .name = try Name.fromText(&buf, "EXAMPLE.COM"), .type = .a, })); } test "a question cut short" { var d = try wire.Decoder.init("\x07example\x03com\x00\x00\x01\x00"); try testing.expectError(error.Truncated, Question.parse(&d)); }