This commit is contained in:
Motiejus Jakštys 2024-10-11 09:40:21 +02:00
parent 519137df05
commit c3858b01a2
1 changed files with 7 additions and 15 deletions

View File

@ -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; // <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; // 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;