Add mDNS query construction
This commit is contained in:
parent
43c93dc54a
commit
9b4f850fe5
1 changed files with 25 additions and 3 deletions
28
src/mdns.zig
28
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 {
|
fn construct_mdns_query(domain: util.Domain, ip_info: util.IPInfo, buff: []u8) !usize {
|
||||||
_ = domain;
|
|
||||||
_ = ip_info;
|
|
||||||
var n: usize = 0;
|
var n: usize = 0;
|
||||||
var buff_writer = std.io.fixedBufferStream(buff);
|
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;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue