Something went wrong. Try again.
DNS message encoding and decoding for Zig, with no I/O and no allocation.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// 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,
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), };}
/// 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);}
/// 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 });}
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));}