start porting to 0.16.0

This commit is contained in:
2026-01-11 11:33:34 -05:00
parent 7a8874ea6a
commit b3f1b00510
4 changed files with 108 additions and 94 deletions

View File

@@ -1,77 +1,115 @@
const is_debug = builtin.mode == .Debug;
/// This creates a debug allocator that can only be referenced in debug mode.
/// You should check for is_debug around every reference to dba.
var dba: DebugAllocator =
if (is_debug)
DebugAllocator.init
else
@compileError("Should not use debug allocator in release mode");
const help =
\\-h, --help Display this help and exit.
\\-r, --relay <str> A relay message to send.
\\-d, --dest <str> An IPv4 or <= 4 ASCII byte string.
\\-c, --connect <str> A connection message to send.
\\
;
pub fn main() !void {
defer if (is_debug) {
_ = dba.deinit();
};
const gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator;
const Option = enum { help, relay, dest, connect };
const to_option: StaticStringMap(Option) = .initComptime(.{
.{ "-h", .help },
.{ "--help", .help },
.{ "-r", .relay },
.{ "--relay", .relay },
.{ "-d", .dest },
.{ "--dest", .dest },
.{ "-c", .connect },
.{ "--connect", .connect },
});
pub fn main(init: std.process.Init) !void {
// CLI parsing adapted from the example here
// https://github.com/Hejsil/zig-clap/blob/e47028deaefc2fb396d3d9e9f7bd776ae0b2a43a/README.md#examples
// https://codeberg.org/ziglang/zig/pulls/30644
// First we specify what parameters our program can take.
// We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`.
const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\-r, --relay <str> A relay message to send.
\\-d, --dest <str> An IPv4 or <= 4 ASCII byte string.
\\-c, --connect <str> A connection message to send.
\\
);
const args = try init.minimal.args.toSlice(init.arena.allocator());
// Initialize our diagnostics, which can be used for reporting useful errors.
// This is optional. You can also pass `.{}` to `clap.parse` if you don't
// care about the extra information `Diagnostics` provides.
var diag = clap.Diagnostic{};
var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
.diagnostic = &diag,
.allocator = gpa,
}) catch |err| {
// Report useful error and exit.
try diag.reportToFile(.stderr(), err);
return err;
};
defer res.deinit();
if (res.args.help != 0) {
return clap.helpToFile(.stderr(), clap.Help, &params, .{});
}
var sock_buffer: [1500]u8 = undefined;
var raw_socket_writer: RawSocketWriter = try .init("enp7s0", &sock_buffer); // /proc/net/dev
var net_buffer: [1500]u8 = undefined;
var net_writer: NetWriter = try .init(&raw_socket_writer.interface, &net_buffer);
var client = try SaprusClient.init(&net_writer.interface);
defer client.deinit();
if (res.args.relay) |r| {
const dest = parseDest(res.args.dest);
try client.sendRelay(
if (r.len > 0) r else "Hello darkness my old friend",
dest,
);
if (args.len == 1) {
std.debug.print("{s}", .{help});
return;
} else if (res.args.connect) |c| {
if (false) {
_ = client.connect(if (c.len > 0) c else "Hello darkness my old friend") catch |err| switch (err) {
error.WouldBlock => null,
else => return err,
};
return;
}
@panic("Not implemented");
}
return clap.helpToFile(.stderr(), clap.Help, &params, .{});
var flags: struct {
relay: ?[]const u8 = null,
dest: ?[]const u8 = null,
connect: ?[]const u8 = null,
} = .{};
{
var i: usize = 1;
while (i < args.len) : (i += 1) {
if (to_option.get(args[i])) |opt| {
switch (opt) {
.help => {
std.debug.print("{s}\n", .{help});
return;
},
.relay => {
i += 1;
if (i < args.len) {
flags.relay = args[i];
} else {
std.debug.print("-r/--relay requires a string\n", .{});
return;
}
},
.dest => {
i += 1;
if (i < args.len) {
flags.dest = args[i];
} else {
std.debug.print("-d/--dest requires a string\n", .{});
return;
}
},
.connect => {
i += 1;
if (i < args.len) {
flags.connect = args[i];
} else {
std.debug.print("-c/--connect requires a string\n", .{});
return;
}
},
}
} else {
std.debug.print("Unknown argument: {s}\n", .{args[i]});
}
}
}
std.debug.print("relay: {s}\n", .{flags.relay orelse "<null>"});
std.debug.print("dest: {s}\n", .{flags.dest orelse "<null>"});
std.debug.print("connect: {s}\n", .{flags.connect orelse "<null>"});
// var sock_buffer: [1500]u8 = undefined;
// var raw_socket_writer: RawSocketWriter = try .init("enp7s0", &sock_buffer); // /proc/net/dev
// var net_buffer: [1500]u8 = undefined;
// var net_writer: NetWriter = try .init(&raw_socket_writer.interface, &net_buffer);
// var client = try SaprusClient.init(&net_writer.interface);
// defer client.deinit();
// if (res.args.relay) |r| {
// const dest = parseDest(res.args.dest);
// try client.sendRelay(
// if (r.len > 0) r else "Hello darkness my old friend",
// dest,
// );
// return;
// } else if (res.args.connect) |c| {
// if (false) {
// _ = client.connect(if (c.len > 0) c else "Hello darkness my old friend") catch |err| switch (err) {
// error.WouldBlock => null,
// else => return err,
// };
// return;
// }
// @panic("Not implemented");
// }
// return clap.helpToFile(.stderr(), clap.Help, &params, .{});
}
fn parseDest(in: ?[]const u8) [4]u8 {
@@ -90,13 +128,11 @@ fn parseDest(in: ?[]const u8) [4]u8 {
const builtin = @import("builtin");
const std = @import("std");
const DebugAllocator = std.heap.DebugAllocator(.{});
const ArrayList = std.ArrayList;
const StaticStringMap = std.StaticStringMap;
const zaprus = @import("zaprus");
const SaprusClient = zaprus.Client;
const SaprusMessage = zaprus.Message;
const RawSocketWriter = zaprus.RawSocketWriter;
const NetWriter = zaprus.NetWriter;
const clap = @import("clap");
// const NetWriter = zaprus.NetWriter;

View File

@@ -1,7 +1,7 @@
pub const Client = @import("Client.zig");
pub const Connection = @import("Connection.zig");
pub const RawSocketWriter = @import("RawSocketWriter.zig");
pub const NetWriter = @import("NetWriter.zig");
// pub const NetWriter = @import("NetWriter.zig");
const msg = @import("message.zig");