diff --git a/src/tunnel.zig b/src/tunnel.zig index 6fc6d69..1d1a2f3 100644 --- a/src/tunnel.zig +++ b/src/tunnel.zig @@ -2,12 +2,11 @@ const std = @import("std"); const os = std.os; const mem = std.mem; const posix = std.posix; - const log = std.log.scoped(.nyotun); const IFF_TUN = 0x0001; // const IFF_NO_PI = 0x1000; // -const TUNSETIFF = 0x400454ca; // compile a C program, run and see +const TUNSETIFF = 0x400454ca; // write a C program, run and see dev: [posix.IFNAMESIZE:0]u8, tunFile: posix.fd_t, @@ -15,18 +14,15 @@ tunFile: posix.fd_t, pub fn init(devname: ?[]const u8) !Tunnel { const tunFile = try posix.open( "/dev/net/tun", - posix.O{ - .ACCMODE = .RDWR, - .CLOEXEC = true, - }, + posix.O{ .ACCMODE = .RDWR, .CLOEXEC = true }, 0, ); errdefer posix.close(tunFile); - var ifr: posix.ifreq = undefined; - @memset(@as([*]u8, @ptrCast(&ifr))[0..@sizeOf(posix.ifreq)], 0); - - ifr.ifru.flags = IFF_TUN | IFF_NO_PI; + var ifr = mem.zeroInit( + posix.ifreq, + .{ .ifru = .{ .flags = IFF_TUN | IFF_NO_PI } }, + ); if (devname) |name| { if (name.len >= posix.IFNAMESIZE - 1) return error.BadInterfaceName; @@ -39,11 +35,7 @@ pub fn init(devname: ?[]const u8) !Tunnel { return error.OpenDevTun; } - var tunnel = Tunnel{ - .dev = .{0} ** posix.IFNAMESIZE, - .tunFile = tunFile, - }; - + var tunnel = Tunnel{ .dev = .{0} ** posix.IFNAMESIZE, .tunFile = tunFile }; const ifname = mem.sliceTo(&ifr.ifrn.name, 0); @memcpy(tunnel.dev[0..ifname.len], ifname); return tunnel;