Skip to content

Commit

Permalink
update to zig 0.11.0-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed May 29, 2023
1 parent 762ff1f commit c1c6692
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 174 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ jobs:
sudo apt install -f wireguard-tools wireguard-dkms -y && \
sudo modprobe wireguard
- name: test
run: zig build test
- name: build summary
run: zig build -fsummary

- name: build
run: zig build -Drelease-safe

- name: run
- name: run wireguard-zig
run: sudo zig-out/bin/wireguard-zig
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ Module.symvers
Mkfile.old
dkms.conf
zig-*
wg.*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This project is experimental on the possibility to use zig (toolchain and langua

## Requirements

* [Zig v0.10 or master](https://ziglang.org/download)
* [Zig v0.11 or master](https://ziglang.org/download)
* [Wireguard](https://www.wireguard.com)


Expand Down
119 changes: 82 additions & 37 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,46 +1,91 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});

// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{
.whitelist = permissive_targets,
});
const optimize = b.standardOptimizeOption(.{});

const exe = b.addExecutable("wireguard-zig", "tests/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.addCSourceFile("vendor/wireguard.c", &[_][]const u8{
const libwg = b.addStaticLibrary(.{
.name = "wireguard",
.target = target,
.optimize = optimize,
});
libwg.addCSourceFile("vendor/wireguard.c", &[_][]const u8{
"-Wall",
"-Oz",
});
exe.addIncludePath("vendor");
exe.linkLibC();
exe.install();

const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
libwg.linkLibC();

const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

const exe_tests = b.addTest("tests/main.zig");
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
exe_tests.addCSourceFile("vendor/wireguard.c", &[_][]const u8{
"-Wall",
"-Oz",
const executable = b.addExecutable(.{
.name = "wireguard-zig",
.target = target,
.optimize = optimize,
.root_source_file = .{
.path = "test/main.zig",
},
});
exe_tests.addIncludePath("vendor");
exe_tests.linkLibC();
executable.linkLibrary(libwg);
executable.addIncludePath("vendor");
executable.linkLibC();

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
b.installArtifact(executable);

const run_step = b.step("run", b.fmt("Run {s} app", .{executable.name}));
run_step.dependOn(&executable.step);
}

const permissive_targets: []const std.zig.CrossTarget = &.{
.{
.cpu_arch = .x86_64,
.os_tag = .linux,
.abi = .gnu,
},
.{
.cpu_arch = .x86,
.os_tag = .linux,
.abi = .gnu,
},
.{
.cpu_arch = .x86_64,
.os_tag = .linux,
.abi = .musl,
},
.{
.cpu_arch = .x86,
.os_tag = .linux,
.abi = .musl,
},
.{
.cpu_arch = .aarch64,
.os_tag = .linux,
.abi = .gnu,
},
.{
.cpu_arch = .aarch64,
.os_tag = .linux,
.abi = .musl,
},
// .{
// .cpu_arch = .riscv64,
// .os_tag = .linux,
// .abi = .gnu,
// https://github.com/ziglang/zig/issues/3340
// },
.{
.cpu_arch = .riscv64,
.os_tag = .linux,
.abi = .musl,
},
.{
.cpu_arch = .powerpc64,
.os_tag = .linux,
.abi = .gnu,
},
.{
.cpu_arch = .powerpc64,
.os_tag = .linux,
.abi = .musl,
},
};
// see all targets list:
// run: zig targets | jq .libc (json format)
105 changes: 105 additions & 0 deletions test/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//! Rewrite test example to zig.
//! wireguard.h - Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.

const std = @import("std");
const crypto = std.crypto;
const log = std.log.scoped(.wireguard);

const wireguard = @cImport({
@cInclude("wireguard.h");
});

const X25519 = crypto.dh.X25519;

pub fn main() void {
var newPeer: wireguard.wg_peer = wireguard.wg_peer{
.flags = @bitCast(c_uint, wireguard.WGPEER_HAS_PUBLIC_KEY | wireguard.WGPEER_REPLACE_ALLOWEDIPS),
.public_key = std.mem.zeroes([32]u8),
.preshared_key = std.mem.zeroes([32]u8),
.endpoint = std.mem.zeroes(wireguard.wg_endpoint),
.last_handshake_time = std.mem.zeroes(wireguard.timespec64),
.rx_bytes = 0,
.tx_bytes = 0,
.persistent_keepalive_interval = 0,
.first_allowedip = null,
.last_allowedip = null,
.next_peer = null,
};

var newDevice: wireguard.wg_device = .{
.name = [_]u8{ 'w', 'g', '_', 't', 'e', 's', 't', '0' } ++ [_]u8{0} ** 8, // [16]u8,
.ifindex = 0,
.flags = wireguard.WGDEVICE_HAS_PRIVATE_KEY | wireguard.WGDEVICE_HAS_LISTEN_PORT,
.public_key = [_]u8{0} ** 32,
.private_key = [_]u8{0} ** 32,
.fwmark = 0,
.listen_port = 1234,
.first_peer = &newPeer,
.last_peer = &newPeer,
};

const tempKeys = X25519.KeyPair.create(null) catch @panic("Failed to generate temporary keys");
const deviceKeys = X25519.KeyPair.create(null) catch @panic("Failed to generate device keys");

std.mem.copy(u8, &newPeer.public_key, &tempKeys.public_key);
std.mem.copy(u8, &newDevice.private_key, &deviceKeys.secret_key);
std.mem.copy(u8, &newDevice.public_key, &deviceKeys.public_key);

if (wireguard.wg_add_device(&newDevice.name) < 0) {
log.err("Unable to add device", .{});
return;
}

if (wireguard.wg_set_device(&newDevice) < 0) {
log.err("Unable to set device", .{});
return;
}

listDevices();

if (wireguard.wg_del_device(&newDevice.name) < 0) {
log.err("Unable to delete device", .{});
return;
}
}

fn listDevices() void {
var deviceNames: [*c]u8 = null;
deviceNames = wireguard.wg_list_device_names() orelse null;

defer std.c.free(@ptrCast(?*anyopaque, deviceNames));

if (deviceNames == null) {
log.err("Unable to get device names", .{});
return;
}

var deviceName: [*c]u8 = deviceNames;
while (std.mem.len(deviceName) != 0) {
var device: [*c]wireguard.wg_device = null;
var peer: [*c]wireguard.wg_peer = null;
var key: wireguard.wg_key_b64_string = undefined;

if (wireguard.wg_get_device(&device, deviceName) < 0) {
log.err("Unable to get device", .{});
continue;
}

if ((device.*.flags & wireguard.WGDEVICE_HAS_PUBLIC_KEY) != 0) {
wireguard.wg_key_to_base64(&key, &device.*.public_key);
log.info("{s} has public key {s}", .{ deviceName, &key });
} else {
log.info("{s} has no public key.", .{deviceName});
}

peer = device.*.first_peer;
while (peer != null) {
wireguard.wg_key_to_base64(&key, &peer.*.public_key);
log.info("peer {s}\n", .{&key});
peer = peer.*.next_peer;
}

wireguard.wg_free_device(device);
deviceName += std.mem.len(deviceName) + 1;
}
}
130 changes: 0 additions & 130 deletions tests/main.zig

This file was deleted.

0 comments on commit c1c6692

Please sign in to comment.