Add mDNS query construction

This commit is contained in:
Muaz Ahmad 2024-05-11 16:12:19 +05:00
parent 43c93dc54a
commit 9b4f850fe5

View file

@ -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;
}