pointer chasing

This commit is contained in:
Motiejus Jakštys 2024-10-11 08:37:13 +02:00
parent f159377fe8
commit c5489bbefb
2 changed files with 13 additions and 6 deletions

View File

@ -70,7 +70,6 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
exe_unit_tests.setExecCmd(&.{"/run/current-system/sw/bin/false"});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to // Similar to creating the run step earlier, this exposes a `test` step to

View File

@ -9,7 +9,8 @@ const IFF_TUN = 0x0001; // <linux/if_tun.h>
const IFF_NO_PI = 0x1000; // <linux/if_tun.h> const IFF_NO_PI = 0x1000; // <linux/if_tun.h>
const TUNSETIFF = 0x400454ca; // compile a C program, run and see const TUNSETIFF = 0x400454ca; // compile a C program, run and see
dev: [posix.IFNAMESIZE - 1:0]u8, dev_buf: [posix.IFNAMESIZE:0]u8,
dev: [:0]u8,
tunFile: posix.fd_t, tunFile: posix.fd_t,
pub fn init(devname: ?[]const u8) !Tunnel { pub fn init(devname: ?[]const u8) !Tunnel {
@ -29,9 +30,7 @@ pub fn init(devname: ?[]const u8) !Tunnel {
ifr.ifru.flags = IFF_TUN | IFF_NO_PI; ifr.ifru.flags = IFF_TUN | IFF_NO_PI;
if (devname) |name| { if (devname) |name| {
if (name.len >= posix.IFNAMESIZE - 1) { if (name.len >= posix.IFNAMESIZE - 1) return error.BadInterfaceName;
return error.BadInterfaceName;
}
@memcpy(ifr.ifrn.name[0..name.len], name); @memcpy(ifr.ifrn.name[0..name.len], name);
} }
@ -43,9 +42,15 @@ pub fn init(devname: ?[]const u8) !Tunnel {
var tunnel = Tunnel{ var tunnel = Tunnel{
.dev = undefined, .dev = undefined,
.dev_buf = .{0} ** posix.IFNAMESIZE,
.tunFile = tunFile, .tunFile = tunFile,
}; };
@memcpy(tunnel.dev[0..posix.IFNAMESIZE], mem.sliceTo(&ifr.ifrn.name, 0));
const ifname = mem.sliceTo(&ifr.ifrn.name, 0);
@memcpy(tunnel.dev_buf[0..ifname.len], ifname);
tunnel.dev = tunnel.dev_buf[0..ifname.len :0];
std.debug.print("&tunnel = {*}\n", .{&tunnel});
std.debug.print("&tunnel.dev = {*}\n", .{tunnel.dev});
return tunnel; return tunnel;
} }
@ -58,5 +63,8 @@ const Tunnel = @This();
test "init" { test "init" {
var tun = try init("nyotun"); var tun = try init("nyotun");
std.debug.print("&tun = {*}\n", .{&tun});
std.debug.print("&tun.dev = {*}\n", .{tun.dev});
try std.testing.expectEqualStrings("nyotun", tun.dev);
tun.close(); tun.close();
} }