Add receiving UDP impl
This commit is contained in:
parent
5b03128c39
commit
98264d26e1
1 changed files with 31 additions and 17 deletions
48
src/mdns.zig
48
src/mdns.zig
|
@ -6,6 +6,7 @@ const MDNSError = error{
|
|||
SocketInitFail,
|
||||
UDPConnectFail,
|
||||
UDPSendFail,
|
||||
UDPRecvFail,
|
||||
};
|
||||
|
||||
pub fn get_mdns(domain: util.Domain, ip_info: util.IPInfo) !util.IPs {
|
||||
|
@ -14,19 +15,9 @@ pub fn get_mdns(domain: util.Domain, ip_info: util.IPInfo) !util.IPs {
|
|||
}
|
||||
|
||||
const socket = c_int;
|
||||
const MSG_BUFF_SIZE = 200;
|
||||
|
||||
fn get_mdns_socket(ip_info: util.IPInfo) !socket {
|
||||
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 = [_]u8{0x00} ** 100;
|
||||
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];
|
||||
},
|
||||
};
|
||||
const addr = try std.net.Address.resolveIp(target_addr, 5353);
|
||||
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,
|
||||
|
@ -34,9 +25,6 @@ fn get_mdns_socket(ip_info: util.IPInfo) !socket {
|
|||
if (sock == -1) {
|
||||
return MDNSError.SocketInitFail;
|
||||
}
|
||||
if (std.c.connect(sock, &addr.any, addr.getOsSockLen()) == -1) {
|
||||
return MDNSError.UDPConnectFail;
|
||||
}
|
||||
return sock;
|
||||
}
|
||||
|
||||
|
@ -74,17 +62,43 @@ fn construct_mdns_query(domain: util.Domain, ip_info: util.IPInfo, buff: []u8) !
|
|||
fn send_query(domain: util.Domain, ip_info: util.IPInfo) !socket {
|
||||
const sock = try get_mdns_socket(ip_info);
|
||||
errdefer _ = std.c.close(sock);
|
||||
var buff = [_]u8{0x00} ** 100;
|
||||
|
||||
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 = [_]u8{0x00} ** MSG_BUFF_SIZE;
|
||||
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];
|
||||
},
|
||||
};
|
||||
const addr = try std.net.Address.resolveIp(target_addr, 5353);
|
||||
|
||||
var buff = [_]u8{0x00} ** MSG_BUFF_SIZE;
|
||||
|
||||
const n = try construct_mdns_query(domain, ip_info, &buff);
|
||||
if (std.c.send(sock, &buff, n, 0) == -1) {
|
||||
if (std.c.sendto(sock, &buff, n, 0, &addr.any, addr.getOsSockLen()) == -1) {
|
||||
return MDNSError.UDPSendFail;
|
||||
}
|
||||
return sock;
|
||||
}
|
||||
|
||||
fn receive_query(sock: socket) !util.IPs {
|
||||
_ = sock;
|
||||
defer _ = std.c.close(sock);
|
||||
var buff = [_]u8{0x00} ** MSG_BUFF_SIZE;
|
||||
|
||||
const n: usize = blk: {
|
||||
const ret = std.c.recv(sock, &buff, MSG_BUFF_SIZE, 0);
|
||||
if (ret < 0) {
|
||||
return MDNSError.UDPRecvFail;
|
||||
} else {
|
||||
break :blk @bitCast(ret);
|
||||
}
|
||||
};
|
||||
|
||||
std.debug.print("{any}\n", .{buff[0..n]});
|
||||
|
||||
return util.IPs{
|
||||
.v4 = "",
|
||||
.v6 = "",
|
||||
|
|
Loading…
Reference in a new issue