From 7cf3bccc7640970f445b29f1b512282e6ec3d1d7 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Tue, 14 May 2024 13:39:40 +0500 Subject: [PATCH] Update mdns to not use the interface + minor cleanup of enums --- src/mdns.zig | 60 +++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/src/mdns.zig b/src/mdns.zig index 61a1e1d..552d441 100644 --- a/src/mdns.zig +++ b/src/mdns.zig @@ -12,19 +12,17 @@ const MDNSError = error{ SocketSetTimeoutFail, }; -pub fn get_mdns(domain: util.Domain, ip_info: util.IPInfo) !util.IP { - if (ip_info.version != .Both) { +pub fn get_mdns(domain: util.Domain, ip_info: util.IP_VER) !util.IP { + if (ip_info != .Both) { const sock = try send_query(domain, ip_info); return receive_response(sock, ip_info); } - const ip_info4 = util.IPInfo{ .version = .IPv4, .interface = ip_info.interface }; - var sock = try send_query(domain, ip_info4); - const addr_v4 = try receive_response(sock, ip_info4); + var sock = try send_query(domain, .IPv4); + const addr_v4 = try receive_response(sock, .IPv4); - const ip_info6 = util.IPInfo{ .version = .IPv6, .interface = ip_info.interface }; - sock = try send_query(domain, ip_info6); - const addr_v6 = try receive_response(sock, ip_info6); + sock = try send_query(domain, .IPv4); + const addr_v6 = try receive_response(sock, .IPv6); return util.merge_addrs(addr_v4, addr_v6); } @@ -32,10 +30,10 @@ pub fn get_mdns(domain: util.Domain, ip_info: util.IPInfo) !util.IP { const socket = c_int; const MSG_BUFF_SIZE = util.consts.MSG_BUFF_SIZE; -fn get_mdns_socket(ip_info: util.IPInfo) !socket { - const sock = std.c.socket(switch (ip_info.version) { - util.IP_VER_ENUM.IPv4 => std.c.AF.INET, - util.IP_VER_ENUM.IPv6 => std.c.AF.INET6, +fn get_mdns_socket(ip_info: util.IP_VER) !socket { + const sock = std.c.socket(switch (ip_info) { + .IPv4 => std.c.AF.INET, + .IPv6 => std.c.AF.INET6, else => unreachable, }, std.c.SOCK.DGRAM, std.c.IPPROTO.UDP); if (sock == -1) { @@ -48,7 +46,7 @@ fn get_mdns_socket(ip_info: util.IPInfo) !socket { return sock; } -fn construct_mdns_query(domain: util.Domain, ip_info: util.IPInfo, buff: []u8) !usize { +fn construct_mdns_query(domain: util.Domain, ip_info: util.IP_VER, buff: []u8) !usize { var n: usize = 0; var buff_writer = std.io.fixedBufferStream(buff); @@ -68,9 +66,9 @@ fn construct_mdns_query(domain: util.Domain, ip_info: util.IPInfo, buff: []u8) ! 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, + [_]u8{ 0x00, switch (ip_info) { + .IPv4 => 0x01, + .IPv6 => 0x1c, else => unreachable, } } ++ // A or AAAA record [_]u8{ 0x00, 0x01 } // IN query @@ -80,22 +78,16 @@ fn construct_mdns_query(domain: util.Domain, ip_info: util.IPInfo, buff: []u8) ! return n; } -fn get_target_address(ip_info: util.IPInfo) !std.net.Address { - const target_addr: []const u8 = switch (ip_info.version) { - util.IP_VER_ENUM.IPv4 => "224.0.0.251", - util.IP_VER_ENUM.IPv6 => blk: { - var buf: [50]u8 = undefined; - var byte_buf = std.io.fixedBufferStream(&buf); - const writer = byte_buf.writer(); - try std.fmt.format(writer, "ff02::fb%{s}", .{ip_info.interface.?}); - break :blk buf[0..writer.context.pos]; - }, +fn get_target_address(ip_info: util.IP_VER) !std.net.Address { + const target_addr: []const u8 = switch (ip_info) { + .IPv4 => "224.0.0.251", + .IPv6 => "ff02::fb", else => unreachable, }; return std.net.Address.resolveIp(target_addr, 5353); } -fn send_query(domain: util.Domain, ip_info: util.IPInfo) !socket { +fn send_query(domain: util.Domain, ip_info: util.IP_VER) !socket { const sock = try get_mdns_socket(ip_info); errdefer _ = std.c.close(sock); @@ -110,7 +102,7 @@ fn send_query(domain: util.Domain, ip_info: util.IPInfo) !socket { return sock; } -fn receive_response(sock: socket, ip_info: util.IPInfo) !util.IP { +fn receive_response(sock: socket, ip_info: util.IP_VER) !util.IP { defer _ = std.c.close(sock); var buff: [MSG_BUFF_SIZE]u8 = undefined; @@ -141,7 +133,7 @@ inline fn read_u16(bytes: []u8) u16 { return std.mem.nativeToBig(u16, std.mem.bytesToValue(u16, bytes)); } -fn parse_mdns_response(response: []u8, ip_info: util.IPInfo) !util.IP { +fn parse_mdns_response(response: []u8, ip_info: util.IP_VER) !util.IP { var pos: usize = 0; if (response[2] & 0x80 == 0) { // check packet is response @@ -164,9 +156,9 @@ fn parse_mdns_response(response: []u8, ip_info: util.IPInfo) !util.IP { const ip_bytes = response[pos .. pos + ip_len]; pos += ip_len; - if (switch (ip_info.version) { - util.IP_VER_ENUM.IPv4 => ip_len != 4, - util.IP_VER_ENUM.IPv6 => ip_len != 16, + if (switch (ip_info) { + .IPv4 => ip_len != 4, + .IPv6 => ip_len != 16, else => unreachable, }) { continue; @@ -181,14 +173,14 @@ fn parse_mdns_response(response: []u8, ip_info: util.IPInfo) !util.IP { if (ip_bytes[0] == 0xfd) { var addr_buff: [16]u8 = undefined; @memcpy(&addr_buff, ip_bytes); - addr = std.net.Address.initIp6(addr_buff, 0, 0, @intCast(std.c.if_nametoindex(ip_info.interface.?))); + addr = std.net.Address.initIp6(addr_buff, 0, 0, 0); } } } if (addr == null) { return MDNSError.NoMatchingAddress; } - return switch (ip_info.version) { + return switch (ip_info) { .IPv4 => util.IP{ .v4 = addr }, .IPv6 => util.IP{ .v6 = addr }, else => unreachable,