first tests
This commit is contained in:
@@ -16,6 +16,10 @@ pub fn main() !void {
|
||||
try bw.flush(); // don't forget to flush!
|
||||
}
|
||||
|
||||
const Tunnel = struct {
|
||||
name: []u8,
|
||||
};
|
||||
|
||||
test "simple test" {
|
||||
var list = std.ArrayList(i32).init(std.testing.allocator);
|
||||
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
|
||||
|
||||
10
src/root.zig
10
src/root.zig
@@ -1,10 +0,0 @@
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
export fn add(a: i32, b: i32) i32 {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
test "basic add functionality" {
|
||||
try testing.expect(add(3, 7) == 10);
|
||||
}
|
||||
4
src/test_all.zig
Normal file
4
src/test_all.zig
Normal file
@@ -0,0 +1,4 @@
|
||||
test "nyotun test suite" {
|
||||
_ = @import("main.zig");
|
||||
_ = @import("tunnel.zig");
|
||||
}
|
||||
62
src/tunnel.zig
Normal file
62
src/tunnel.zig
Normal file
@@ -0,0 +1,62 @@
|
||||
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; // <linux/if_tun.h>
|
||||
const IFF_NO_PI = 0x1000; // <linux/if_tun.h>
|
||||
const TUNSETIFF = 0x800454ca; // same value on all archs, see go/src/syscall/*.go
|
||||
|
||||
dev: [posix.IFNAMESIZE - 1:0]u8,
|
||||
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,
|
||||
},
|
||||
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;
|
||||
|
||||
if (devname) |name| {
|
||||
if (name.len >= posix.IFNAMESIZE - 1) {
|
||||
return error.BadInterfaceName;
|
||||
}
|
||||
@memcpy(ifr.ifrn.name[0..name.len], name);
|
||||
}
|
||||
|
||||
const err = posix.system.ioctl(tunFile, TUNSETIFF, @intFromPtr(&ifr));
|
||||
if (posix.errno(err) != .SUCCESS) {
|
||||
log.err("unable to set TUNSETIFF: {s}", .{@tagName(posix.errno(err))});
|
||||
return error.OpenDevTun;
|
||||
}
|
||||
|
||||
var tunnel = Tunnel{
|
||||
.dev = undefined,
|
||||
.tunFile = tunFile,
|
||||
};
|
||||
@memcpy(tunnel.dev[0..posix.IFNAMESIZE], mem.sliceTo(&ifr.ifrn.name, 0));
|
||||
|
||||
return tunnel;
|
||||
}
|
||||
|
||||
pub fn close(self: *Tunnel) void {
|
||||
posix.close(self.tunFile);
|
||||
}
|
||||
|
||||
const Tunnel = @This();
|
||||
|
||||
test "init tunnel" {
|
||||
var tun = try init("nyotun");
|
||||
tun.close();
|
||||
}
|
||||
Reference in New Issue
Block a user