summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-04-27 18:06:11 -0400
committerRobby Zambito <contact@robbyzambito.me>2025-04-30 13:46:54 -0400
commit8d404a7c8d2db3f132c716f645ada9ef09ca0e9a (patch)
treed7048a3e7d5b6b5ff54cf60481e6de95b5250e06
parent18b04364df2829a7e30e5831e499005a1ecc24c7 (diff)
-rw-r--r--src/Client.zig4
-rw-r--r--src/main.zig4
-rw-r--r--src/message.zig57
3 files changed, 61 insertions, 4 deletions
diff --git a/src/Client.zig b/src/Client.zig
index 0d7a851..070aaef 100644
--- a/src/Client.zig
+++ b/src/Client.zig
@@ -77,9 +77,9 @@ pub fn sendInitialConnection(payload: []const u8, initial_port: u16, allocator:
}
pub fn connect(payload: []const u8, allocator: Allocator) !?SaprusConnection {
- var foo: gcat.nic.RawSocket = try .init("enp7s0");
+ var foo: gcat.nic.RawSocket = try .init("enp7s0"); // /proc/net/dev
defer foo.deinit();
- // _ = gcat.nic.RawSocket;
+
var initial_port: u16 = 0;
if (rand) |r| {
initial_port = r.intRangeAtMost(u16, 1024, 65000);
diff --git a/src/main.zig b/src/main.zig
index 7a8a34f..61cc76f 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -50,7 +50,7 @@ pub fn main() !void {
}
if (res.args.relay) |r| {
- const dest = parseDest(res.args.dest) catch .{ 70, 70, 70, 70 };
+ const dest = parseDest(res.args.dest);
try SaprusClient.sendRelay(
if (r.len > 0) r else "Hello darkness my old friend",
dest,
@@ -79,7 +79,7 @@ fn parseDest(in: ?[]const u8) [4]u8 {
const addr = std.net.Ip4Address.parse(dest, 0) catch return "FAIL".*;
return @bitCast(addr.sa.addr);
}
- return "zap".*;
+ return "zap\x00".*;
}
const builtin = @import("builtin");
diff --git a/src/message.zig b/src/message.zig
index 403bfcd..6d95e4b 100644
--- a/src/message.zig
+++ b/src/message.zig
@@ -28,6 +28,59 @@ pub const Error = error{
UnknownSaprusType,
};
+// ZERO COPY STUFF
+// &payload could be a void value that is treated as a pointer to a [*]u8
+pub const ZeroCopyMessage = packed struct {
+ pub const Relay = packed struct {
+ dest: @Vector(4, u8),
+ /// @as([*]u8, @ptrCast(&payload))
+ payload: void,
+ };
+ pub const Connection = packed struct {
+ src_port: u16, // random number > 1024
+ dest_port: u16, // random number > 1024
+ seq_num: u32 = 0,
+ msg_id: u32 = 0,
+ reserved: u8 = 0,
+ options: ConnectionOptions = .{},
+ payload: void,
+ };
+
+ const Self = @This();
+
+ type: PacketType,
+ length: u16,
+ bytes: void = {},
+
+ fn getRelay(self: *align(1) Self) *align(1) const Relay {
+ return std.mem.bytesAsValue(Relay, &self.bytes);
+ }
+ fn getConnection(self: *align(1) Self) *align(1) const Connection {
+ return std.mem.bytesAsValue(Connection, &self.bytes);
+ }
+};
+
+test "testing variable length zero copy struct" {
+ const gpa = std.testing.allocator;
+ const bytes = try gpa.alloc(u8, @sizeOf(ZeroCopyMessage) + 64);
+ defer gpa.free(bytes);
+ // const zcm = std.mem.bytesAsValue(ZeroCopyMessage, bytes).*;
+ const zcm: *align(1) ZeroCopyMessage = @ptrCast(bytes.ptr);
+ // @compileLog(@typeInfo(@TypeOf(ZeroCopyMessage{ .type = .relay, .length = 0 })));
+ // @compileLog(@typeInfo(@TypeOf(zcm)));
+ // _ = zcm;
+ zcm.type = .relay;
+ zcm.length = 3;
+ std.debug.print("{any}\n", .{zcm});
+ std.debug.print("{any}\n", .{zcm.getRelay()});
+ std.debug.print("{any}\n", .{zcm.getConnection()});
+ std.debug.print("{x}\n", .{std.mem.asBytes(zcm.getConnection())});
+ std.debug.print("{x}\n", .{std.mem.asBytes(zcm.getRelay())});
+ std.debug.print("{x}\n", .{bytes});
+
+ try std.testing.expect(true);
+}
+
/// All Saprus messages
pub const Message = union(PacketType) {
pub const Relay = struct {
@@ -209,3 +262,7 @@ test "Round trip Connection toBytes and fromBytes" {
try std.testing.expectEqualDeep(msg, from_bytes);
}
+
+test {
+ std.testing.refAllDeclsRecursive(@This());
+}