mirror of
https://git.robbyzambito.me/zaprus/
synced 2026-02-04 11:44:49 +00:00
start porting to 0.16.0
This commit is contained in:
11
build.zig
11
build.zig
@@ -41,13 +41,10 @@ pub fn build(b: *std.Build) void {
|
|||||||
.target = target,
|
.target = target,
|
||||||
});
|
});
|
||||||
|
|
||||||
mod.addImport("network", b.dependency("network", .{}).module("network"));
|
|
||||||
mod.addImport("gatorcat", b.dependency("gatorcat", .{}).module("gatorcat"));
|
|
||||||
|
|
||||||
// Here we define an executable. An executable needs to have a root module
|
// Here we define an executable. An executable needs to have a root module
|
||||||
// which needs to expose a `main` function. While we could add a main function
|
// which needs to expose a `main` function. While we could add a main function
|
||||||
// to the module defined above, it's sometimes preferable to split business
|
// to the module defined above, it's sometimes preferable to split business
|
||||||
// business logic and the CLI into two separate modules.
|
// logic and the CLI into two separate modules.
|
||||||
//
|
//
|
||||||
// If your goal is to create a Zig library for others to use, consider if
|
// If your goal is to create a Zig library for others to use, consider if
|
||||||
// it might benefit from also exposing a CLI tool. A parser library for a
|
// it might benefit from also exposing a CLI tool. A parser library for a
|
||||||
@@ -82,7 +79,6 @@ pub fn build(b: *std.Build) void {
|
|||||||
// can be extremely useful in case of collisions (which can happen
|
// can be extremely useful in case of collisions (which can happen
|
||||||
// importing modules from different packages).
|
// importing modules from different packages).
|
||||||
.{ .name = "zaprus", .module = mod },
|
.{ .name = "zaprus", .module = mod },
|
||||||
.{ .name = "clap", .module = b.dependency("clap", .{}).module("clap") },
|
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
@@ -92,11 +88,6 @@ pub fn build(b: *std.Build) void {
|
|||||||
// step). By default the install prefix is `zig-out/` but can be overridden
|
// step). By default the install prefix is `zig-out/` but can be overridden
|
||||||
// by passing `--prefix` or `-p`.
|
// by passing `--prefix` or `-p`.
|
||||||
b.installArtifact(exe);
|
b.installArtifact(exe);
|
||||||
b.installArtifact(b.addLibrary(.{
|
|
||||||
.linkage = .static,
|
|
||||||
.name = "zaprus",
|
|
||||||
.root_module = mod,
|
|
||||||
}));
|
|
||||||
|
|
||||||
// This creates a top level step. Top level steps have a name and can be
|
// This creates a top level step. Top level steps have a name and can be
|
||||||
// invoked by name when running `zig build` (e.g. `zig build run`).
|
// invoked by name when running `zig build` (e.g. `zig build run`).
|
||||||
|
|||||||
@@ -28,27 +28,14 @@
|
|||||||
|
|
||||||
// Tracks the earliest Zig version that the package considers to be a
|
// Tracks the earliest Zig version that the package considers to be a
|
||||||
// supported use case.
|
// supported use case.
|
||||||
.minimum_zig_version = "0.14.0",
|
.minimum_zig_version = "0.16.0",
|
||||||
|
|
||||||
// This field is optional.
|
// This field is optional.
|
||||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||||
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
||||||
// Once all dependencies are fetched, `zig build` no longer requires
|
// Once all dependencies are fetched, `zig build` no longer requires
|
||||||
// internet connectivity.
|
// internet connectivity.
|
||||||
.dependencies = .{
|
.dependencies = .{},
|
||||||
.network = .{
|
|
||||||
.url = "git+https://github.com/ikskuh/zig-network#7947237eec317d9458897f82089f343a05450c2b",
|
|
||||||
.hash = "network-0.1.0-Pm-Agl8xAQBmkwohveGOfTk4zQnuqDs0Ptfbms4KP5Ce",
|
|
||||||
},
|
|
||||||
.clap = .{
|
|
||||||
.url = "git+https://github.com/Hejsil/zig-clap#9cfa61596cd44ef7be35f8d2e108d2025e09868e",
|
|
||||||
.hash = "clap-0.10.0-oBajB_TnAQB0l5UdW9WYhhJDEswbedvwFOzzZwGknYeR",
|
|
||||||
},
|
|
||||||
.gatorcat = .{
|
|
||||||
.url = "git+https://github.com/jeffective/gatorcat#db73d0f7780331d82e785e85773d1afaf154c2e6",
|
|
||||||
.hash = "gatorcat-0.3.11-WcrpTQn0BwArrCFVHy9FPBIPDJQqPrFdJlhiyH7Ng5x4",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
.paths = .{
|
.paths = .{
|
||||||
"build.zig",
|
"build.zig",
|
||||||
"build.zig.zon",
|
"build.zig.zon",
|
||||||
|
|||||||
172
src/main.zig
172
src/main.zig
@@ -1,77 +1,115 @@
|
|||||||
const is_debug = builtin.mode == .Debug;
|
const is_debug = builtin.mode == .Debug;
|
||||||
|
|
||||||
/// This creates a debug allocator that can only be referenced in debug mode.
|
const help =
|
||||||
/// You should check for is_debug around every reference to dba.
|
\\-h, --help Display this help and exit.
|
||||||
var dba: DebugAllocator =
|
\\-r, --relay <str> A relay message to send.
|
||||||
if (is_debug)
|
\\-d, --dest <str> An IPv4 or <= 4 ASCII byte string.
|
||||||
DebugAllocator.init
|
\\-c, --connect <str> A connection message to send.
|
||||||
else
|
\\
|
||||||
@compileError("Should not use debug allocator in release mode");
|
;
|
||||||
|
|
||||||
pub fn main() !void {
|
const Option = enum { help, relay, dest, connect };
|
||||||
defer if (is_debug) {
|
const to_option: StaticStringMap(Option) = .initComptime(.{
|
||||||
_ = dba.deinit();
|
.{ "-h", .help },
|
||||||
};
|
.{ "--help", .help },
|
||||||
|
.{ "-r", .relay },
|
||||||
const gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator;
|
.{ "--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
|
// 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.
|
const args = try init.minimal.args.toSlice(init.arena.allocator());
|
||||||
// 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.
|
|
||||||
\\
|
|
||||||
);
|
|
||||||
|
|
||||||
// Initialize our diagnostics, which can be used for reporting useful errors.
|
if (args.len == 1) {
|
||||||
// This is optional. You can also pass `.{}` to `clap.parse` if you don't
|
std.debug.print("{s}", .{help});
|
||||||
// care about the extra information `Diagnostics` provides.
|
|
||||||
var diag = clap.Diagnostic{};
|
|
||||||
var res = clap.parse(clap.Help, ¶ms, 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, ¶ms, .{});
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
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, ¶ms, .{});
|
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, ¶ms, .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseDest(in: ?[]const u8) [4]u8 {
|
fn parseDest(in: ?[]const u8) [4]u8 {
|
||||||
@@ -90,13 +128,11 @@ fn parseDest(in: ?[]const u8) [4]u8 {
|
|||||||
|
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const DebugAllocator = std.heap.DebugAllocator(.{});
|
|
||||||
const ArrayList = std.ArrayList;
|
const ArrayList = std.ArrayList;
|
||||||
|
const StaticStringMap = std.StaticStringMap;
|
||||||
|
|
||||||
const zaprus = @import("zaprus");
|
const zaprus = @import("zaprus");
|
||||||
const SaprusClient = zaprus.Client;
|
const SaprusClient = zaprus.Client;
|
||||||
const SaprusMessage = zaprus.Message;
|
const SaprusMessage = zaprus.Message;
|
||||||
const RawSocketWriter = zaprus.RawSocketWriter;
|
const RawSocketWriter = zaprus.RawSocketWriter;
|
||||||
const NetWriter = zaprus.NetWriter;
|
// const NetWriter = zaprus.NetWriter;
|
||||||
|
|
||||||
const clap = @import("clap");
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
pub const Client = @import("Client.zig");
|
pub const Client = @import("Client.zig");
|
||||||
pub const Connection = @import("Connection.zig");
|
pub const Connection = @import("Connection.zig");
|
||||||
pub const RawSocketWriter = @import("RawSocketWriter.zig");
|
pub const RawSocketWriter = @import("RawSocketWriter.zig");
|
||||||
pub const NetWriter = @import("NetWriter.zig");
|
// pub const NetWriter = @import("NetWriter.zig");
|
||||||
|
|
||||||
const msg = @import("message.zig");
|
const msg = @import("message.zig");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user