Minor refactor
This commit is contained in:
parent
6e89c63eca
commit
e112e65794
2 changed files with 9 additions and 9 deletions
16
src/mdns.zig
16
src/mdns.zig
|
@ -34,8 +34,8 @@ const MSG_BUFF_SIZE = util.consts.MSG_BUFF_SIZE;
|
||||||
|
|
||||||
fn get_mdns_socket(ip_info: util.IPInfo) !socket {
|
fn get_mdns_socket(ip_info: util.IPInfo) !socket {
|
||||||
const sock = std.c.socket(switch (ip_info.version) {
|
const sock = std.c.socket(switch (ip_info.version) {
|
||||||
util.IP_VER_ENUM.IPv4 => std.c.AF.INET,
|
.IPv4 => std.c.AF.INET,
|
||||||
util.IP_VER_ENUM.IPv6 => std.c.AF.INET6,
|
.IPv6 => std.c.AF.INET6,
|
||||||
else => unreachable,
|
else => unreachable,
|
||||||
}, std.c.SOCK.DGRAM, std.c.IPPROTO.UDP);
|
}, std.c.SOCK.DGRAM, std.c.IPPROTO.UDP);
|
||||||
if (sock == -1) {
|
if (sock == -1) {
|
||||||
|
@ -69,8 +69,8 @@ fn construct_mdns_query(domain: util.Domain, ip_info: util.IPInfo, buff: []u8) !
|
||||||
|
|
||||||
const question_footer =
|
const question_footer =
|
||||||
[_]u8{ 0x00, switch (ip_info.version) {
|
[_]u8{ 0x00, switch (ip_info.version) {
|
||||||
util.IP_VER_ENUM.IPv4 => 0x01,
|
.IPv4 => 0x01,
|
||||||
util.IP_VER_ENUM.IPv6 => 0x1c,
|
.IPv6 => 0x1c,
|
||||||
else => unreachable,
|
else => unreachable,
|
||||||
} } ++ // A or AAAA record
|
} } ++ // A or AAAA record
|
||||||
[_]u8{ 0x00, 0x01 } // IN query
|
[_]u8{ 0x00, 0x01 } // IN query
|
||||||
|
@ -82,8 +82,8 @@ fn construct_mdns_query(domain: util.Domain, ip_info: util.IPInfo, buff: []u8) !
|
||||||
|
|
||||||
fn get_target_address(ip_info: util.IPInfo) !std.net.Address {
|
fn get_target_address(ip_info: util.IPInfo) !std.net.Address {
|
||||||
const target_addr: []const u8 = switch (ip_info.version) {
|
const target_addr: []const u8 = switch (ip_info.version) {
|
||||||
util.IP_VER_ENUM.IPv4 => "224.0.0.251",
|
.IPv4 => "224.0.0.251",
|
||||||
util.IP_VER_ENUM.IPv6 => blk: {
|
.IPv6 => blk: {
|
||||||
var buf: [50]u8 = undefined;
|
var buf: [50]u8 = undefined;
|
||||||
var byte_buf = std.io.fixedBufferStream(&buf);
|
var byte_buf = std.io.fixedBufferStream(&buf);
|
||||||
const writer = byte_buf.writer();
|
const writer = byte_buf.writer();
|
||||||
|
@ -165,8 +165,8 @@ fn parse_mdns_response(response: []u8, ip_info: util.IPInfo) !util.IP {
|
||||||
pos += ip_len;
|
pos += ip_len;
|
||||||
|
|
||||||
if (switch (ip_info.version) {
|
if (switch (ip_info.version) {
|
||||||
util.IP_VER_ENUM.IPv4 => ip_len != 4,
|
.IPv4 => ip_len != 4,
|
||||||
util.IP_VER_ENUM.IPv6 => ip_len != 16,
|
.IPv6 => ip_len != 16,
|
||||||
else => unreachable,
|
else => unreachable,
|
||||||
}) {
|
}) {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -65,7 +65,7 @@ pub fn get_input() !struct {
|
||||||
const ip_ver = std.meta.intToEnum(IP_VER_ENUM, std.fmt.parseInt(u3, ip_ver_str, 10) catch return ArgError.InvalidAddressVer) catch return ArgError.InvalidAddressVer;
|
const ip_ver = std.meta.intToEnum(IP_VER_ENUM, std.fmt.parseInt(u3, ip_ver_str, 10) catch return ArgError.InvalidAddressVer) catch return ArgError.InvalidAddressVer;
|
||||||
|
|
||||||
var iface: ?[:0]const u8 = null;
|
var iface: ?[:0]const u8 = null;
|
||||||
if (ip_ver != IP_VER_ENUM.IPv4) {
|
if (ip_ver != .IPv4) {
|
||||||
iface = args.next() orelse return ArgError.InterfaceRequired;
|
iface = args.next() orelse return ArgError.InterfaceRequired;
|
||||||
if (std.c.if_nametoindex(iface.?) == 0) {
|
if (std.c.if_nametoindex(iface.?) == 0) {
|
||||||
return ArgError.InvalidInterface;
|
return ArgError.InvalidInterface;
|
||||||
|
|
Loading…
Reference in a new issue