Compare commits
No commits in common. "43411dbc8f541392c24de5298b848b902df7c1f3" and "e112e65794fc07d016677fc354f184091e5b5c56" have entirely different histories.
43411dbc8f
...
e112e65794
2 changed files with 15 additions and 9 deletions
12
src/mdns.zig
12
src/mdns.zig
|
@ -3,11 +3,13 @@ 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 {
|
||||||
|
@ -39,9 +41,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 = 5, .tv_usec = 0 };
|
const timeout: std.c.timeval = .{ .tv_sec = 1, .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.SocketInitFail;
|
return MDNSError.SocketSetTimeoutFail;
|
||||||
}
|
}
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
|
@ -85,7 +87,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%{d}", .{ip_info.interface});
|
try std.fmt.format(writer, "ff02::fb%{s}", .{ip_info.interface.?});
|
||||||
break :blk buf[0..writer.context.pos];
|
break :blk buf[0..writer.context.pos];
|
||||||
},
|
},
|
||||||
else => unreachable,
|
else => unreachable,
|
||||||
|
@ -115,7 +117,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 util.IP{}; // no reply means no matching record on network
|
return MDNSError.UDPRecvFail;
|
||||||
} else {
|
} else {
|
||||||
break :blk @bitCast(ret);
|
break :blk @bitCast(ret);
|
||||||
}
|
}
|
||||||
|
@ -179,7 +181,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(ip_info.interface));
|
addr = std.net.Address.initIp6(addr_buff, 0, 0, @intCast(std.c.if_nametoindex(ip_info.interface.?)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
src/util.zig
10
src/util.zig
|
@ -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: c_int,
|
interface: ?[:0]const u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Domain = struct {
|
pub const Domain = struct {
|
||||||
|
@ -64,10 +64,14 @@ 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;
|
||||||
|
|
||||||
const iface = try std.fmt.parseInt(c_int, args.next() orelse return ArgError.NotEnoughArgs, 10);
|
var iface: ?[:0]const u8 = null;
|
||||||
if (iface < 0) {
|
if (ip_ver != .IPv4) {
|
||||||
|
iface = args.next() orelse return ArgError.InterfaceRequired;
|
||||||
|
if (std.c.if_nametoindex(iface.?) == 0) {
|
||||||
return ArgError.InvalidInterface;
|
return ArgError.InvalidInterface;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return .{ domain, IPInfo{ .version = ip_ver, .interface = iface } };
|
return .{ domain, IPInfo{ .version = ip_ver, .interface = iface } };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue