Compare commits

..

2 commits

2 changed files with 9 additions and 15 deletions

View file

@ -3,13 +3,11 @@ const util = @import("util.zig");
const MDNSError = error{ const MDNSError = error{
SocketInitFail, SocketInitFail,
UDPConnectFail,
UDPSendFail, UDPSendFail,
UDPRecvFail, UDPRecvFail,
NotResponse, NotResponse,
NoMatchingAddress, NoMatchingAddress,
AddressBadFormat, AddressBadFormat,
SocketSetTimeoutFail,
}; };
pub fn get_mdns(domain: util.Domain, ip_info: util.IPInfo) !util.IP { pub fn get_mdns(domain: util.Domain, ip_info: util.IPInfo) !util.IP {
@ -41,9 +39,9 @@ fn get_mdns_socket(ip_info: util.IPInfo) !socket {
if (sock == -1) { if (sock == -1) {
return MDNSError.SocketInitFail; return MDNSError.SocketInitFail;
} }
const timeout: std.c.timeval = .{ .tv_sec = 1, .tv_usec = 0 }; const timeout: std.c.timeval = .{ .tv_sec = 5, .tv_usec = 0 };
if (std.c.setsockopt(sock, std.c.SOL.SOCKET, std.c.SO.RCVTIMEO, &timeout, @intCast(@sizeOf(std.c.timeval))) == -1) { if (std.c.setsockopt(sock, std.c.SOL.SOCKET, std.c.SO.RCVTIMEO, &timeout, @intCast(@sizeOf(std.c.timeval))) == -1) {
return MDNSError.SocketSetTimeoutFail; return MDNSError.SocketInitFail;
} }
return sock; return sock;
} }
@ -87,7 +85,7 @@ fn get_target_address(ip_info: util.IPInfo) !std.net.Address {
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();
try std.fmt.format(writer, "ff02::fb%{s}", .{ip_info.interface.?}); try std.fmt.format(writer, "ff02::fb%{d}", .{ip_info.interface});
break :blk buf[0..writer.context.pos]; break :blk buf[0..writer.context.pos];
}, },
else => unreachable, else => unreachable,
@ -117,7 +115,7 @@ fn receive_response(sock: socket, ip_info: util.IPInfo) !util.IP {
const n: usize = blk: { const n: usize = blk: {
const ret = std.c.recv(sock, &buff, MSG_BUFF_SIZE, 0); const ret = std.c.recv(sock, &buff, MSG_BUFF_SIZE, 0);
if (ret < 0) { if (ret < 0) {
return MDNSError.UDPRecvFail; return util.IP{}; // no reply means no matching record on network
} else { } else {
break :blk @bitCast(ret); break :blk @bitCast(ret);
} }
@ -181,7 +179,7 @@ fn parse_mdns_response(response: []u8, ip_info: util.IPInfo) !util.IP {
if (ip_bytes[0] == 0xfd) { if (ip_bytes[0] == 0xfd) {
var addr_buff: [16]u8 = undefined; var addr_buff: [16]u8 = undefined;
@memcpy(&addr_buff, ip_bytes); @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, @intCast(ip_info.interface));
} }
} }
} }

View file

@ -32,7 +32,7 @@ pub fn merge_addrs(v4: IP, v6: IP) IP {
pub const IPInfo = struct { pub const IPInfo = struct {
version: IP_VER_ENUM, version: IP_VER_ENUM,
interface: ?[:0]const u8, interface: c_int,
}; };
pub const Domain = struct { pub const Domain = struct {
@ -64,14 +64,10 @@ pub fn get_input() !struct {
const ip_ver_str = args.next() orelse return ArgError.NotEnoughArgs; const ip_ver_str = args.next() orelse return ArgError.NotEnoughArgs;
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; const iface = try std.fmt.parseInt(c_int, args.next() orelse return ArgError.NotEnoughArgs, 10);
if (ip_ver != .IPv4) { if (iface < 0) {
iface = args.next() orelse return ArgError.InterfaceRequired; return ArgError.InvalidInterface;
if (std.c.if_nametoindex(iface.?) == 0) {
return ArgError.InvalidInterface;
}
} }
return .{ domain, IPInfo{ .version = ip_ver, .interface = iface } }; return .{ domain, IPInfo{ .version = ip_ver, .interface = iface } };
} }