From afdba8a6e1840c8aee8ef35006a4c179fbbad20b Mon Sep 17 00:00:00 2001 From: Altagos Date: Wed, 18 Jun 2025 14:45:32 +0200 Subject: [PATCH] define types for basic A query --- src/root.zig | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/root.zig b/src/root.zig index 9e9e432..86d9d07 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,6 +1,7 @@ const std = @import("std"); const assert = std.debug.assert; const io = std.io; +const net = std.net; pub const BytePacketBuffer = struct { buf: [512]u8 = undefined, @@ -284,3 +285,62 @@ test "BytePacketBuffer.read_qname_alloc google.com" { try testing.expectEqualStrings(expected, outstr); } +pub const DnsHeader = packed struct { + id: u16 = 0, + + recursion_desired: bool = false, + truncated_message: bool = false, + authoritative_answer: bool = false, + opcode: u4 = 0, + response: bool = false, + + rescode: enum(u4) { + noerror = 0, + formerr = 1, + servfail = 2, + nxdomain = 3, + notimp = 4, + refused = 5, + } = .noerror, + checking_disabled: bool = false, + authed_data: bool = false, + z: bool = false, + recursion_available: bool = false, + + questions: u16 = 0, + answers: u16 = 0, + authoritative_entries: u16 = 0, + resource_entries: u16 = 0, +}; + +pub const QueryType = enum(u16) { + a = 1, + unknown, +}; + +pub const DnsQuestion = struct { + name: []const u8, + qtype: QueryType, + + pub fn read(self: *DnsQuestion, buffer: *BytePacketBuffer, alloc: std.mem.Allocator) !void { + self.name = try buffer.readQnameAlloc(alloc); + self.qtype = @enumFromInt(try buffer.reader().readInt(u16, .big)); + _ = try buffer.reader().readInt(u16, .big); // class + } +}; + +// TODO: add DnsQuestion.read test + +pub const DnsRecord = union(QueryType) { + unknown: struct { + domain: []const u8, + qtype: u16, + data_len: u16, + ttl: u32, + }, + a: struct { + domain: []const u8, + addr: net.Ip4Address, + ttl: u32, + }, +}; -- knotmirror 0.0.1