From 9b4f850fe5d618548683f2782c66365f58cfd81a Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Sat, 11 May 2024 16:12:19 +0500 Subject: [PATCH] Add mDNS query construction --- src/mdns.zig | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/mdns.zig b/src/mdns.zig index 107a8ea..76fbf60 100644 --- a/src/mdns.zig +++ b/src/mdns.zig @@ -41,11 +41,33 @@ fn get_mdns_socket(ip_info: util.IPInfo) !socket { } fn construct_mdns_query(domain: util.Domain, ip_info: util.IPInfo, buff: []u8) !usize { - _ = domain; - _ = ip_info; var n: usize = 0; var buff_writer = std.io.fixedBufferStream(buff); - n += try buff_writer.write("test"); + + const header = + [_]u8{0x00} ** 2 ++ // transaction ID + [_]u8{ 0x00, 0x00 } ++ // Standard Query flags + [_]u8{ 0x00, 0x01 } ++ // 1 question + [_]u8{0x00} ** 6 // Response only + ; + n += try buff_writer.write(&header); + + for (domain.labels) |label| { + if (label.len == 0) break; + n += try buff_writer.write(&[_]u8{@truncate(label.len)}); + n += try buff_writer.write(label); + } + n += try buff_writer.write(&[_]u8{0x00}); // End null byte + + const question_footer = + [_]u8{ 0x00, switch (ip_info.version) { + util.IP_VER_ENUM.IPv4 => 0x01, + util.IP_VER_ENUM.IPv6 => 0x1c, + } } ++ // A or AAAA record + [_]u8{ 0x00, 0x01 } // IN query + ; + n += try buff_writer.write(&question_footer); + return n; }