I wrote a small tool/service for WireGuard endpoint switching and I was using the function from the title, but after Io MR, whole std.net got deleted. I was using it to convert src_addr from std.posix.recvfrom to log it:
fn serverToWg(
packet_arrived: *bool,
wg_sock: c_int,
serv_sock: c_int,
srv_buf: []u8,
servers: []std.net.Address,
current_id: *usize,
other_addr: *std.posix.sockaddr,
other_addrlen: *std.posix.socklen_t,
wg_addr: std.net.Address,
) !void {
// Assign correct memory alignment so initPosix can work with it
const tmp_addr: *align(4) std.posix.sockaddr = @alignCast(other_addr);
while (true) {
// --- Handle server -> WireGuard ---
if (std.posix.recvfrom(
serv_sock,
srv_buf[0..],
0,
other_addr,
other_addrlen,
)) |recv| {
// Converts other_addr to a correct struct that can be pretty formatted with {f}
tmp_addr.* = other_addr.*;
const addr = std.net.Address.initPosix(tmp_addr);
std.log.debug("Received {d} bytes, server: {f}\n", .{ recv, addr });
....
Now I need help to find equivalent so I can refactor my code. If anyone is interested how all of this looks, here is he repo. Any tips, suggestions, logic changes etc are appreciated.
P.S.
I wanted to showcase the whole project as well, but since I need help and technically it’s not compiling after the Io MR, I decided to stick to help flag instead
It looks like it got moved to std.Io.net and got refactored a bit. I can’t tell for sure, but you may also be looking for PosixAddress. One of those should allow you to get it parsed like you want.
Thanks. I managed to get some time, was digging around and encountered another issue. std.net.Address.parseIp4() was returning a union that contained a posix.socaddr struct which was used in std.posix.bind() as an address. As well as getOsSockLen() which was returning posix.socklen_t.
Should I even use std.posix.bind() or there is a different way how to handle all of this in a new implementation?
Old approach I was using was creating a UDP blocking std.posix.socket(), parsing IPv4, and binding it with std.posix.bind()
I’m not sure what the tradeoffs are, but you may consider using the IpAddress.bind method for it. That will give you a UDP Socket which has the IpAddress as part of it. This will get you more in the Zig std types and may prevent some of this headache. If you really do need the posix apis, then you will have to find some ways to parse between the 2 namespaces to get the types.
The downside with this is you may not have all the functionality you need (i.e. no recvfrom). Since this is actively being refactored, if you are running off master branch, you will be dealing with a lot of changes coming.