mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 16:24:50 +00:00
Getting some data but malformed
This is what the data shows up in the sentinal looking like: 2025/04/02 22:36:32 Error decoding message: Hello darkness my old friend::48656c6c6f206461726b6e657373206d79206f6c6420667269656e64aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2025/04/02 22:36:32 INFO: Relay message received: Hello darkness my old friend
This commit is contained in:
80
src/main.zig
80
src/main.zig
@@ -2,6 +2,8 @@
|
|||||||
//! you are building an executable. If you are making a library, the convention
|
//! you are building an executable. If you are making a library, the convention
|
||||||
//! is to delete this file and start with root.zig instead.
|
//! is to delete this file and start with root.zig instead.
|
||||||
|
|
||||||
|
const is_debug = builtin.mode == .Debug;
|
||||||
|
|
||||||
const SaprusPacketType = enum(u16) {
|
const SaprusPacketType = enum(u16) {
|
||||||
relay = 0x003C,
|
relay = 0x003C,
|
||||||
file_transfer = 0x8888,
|
file_transfer = 0x8888,
|
||||||
@@ -14,9 +16,9 @@ const SaprusHeaderFrame = struct {
|
|||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
fn toBytes(s: Self, allocator: Allocator) ![]u8 {
|
fn toBytes(s: Self, allocator: Allocator) ![]u8 {
|
||||||
const buf = allocator.alloc(u8, 32 + s.payload.len);
|
const buf = try allocator.alloc(u8, 32 + s.payload.len);
|
||||||
std.mem.writeInt(u16, buf[0..2], s.msg_type, .big);
|
std.mem.writeInt(u16, buf[0..2], @intFromEnum(s.msg_type), .big);
|
||||||
std.mem.writeInt(u16, buf[2..4], s.payload.len);
|
std.mem.writeInt(u16, buf[2..4], @intCast(s.payload.len), .big);
|
||||||
std.mem.copyForwards(u8, buf[4..], s.payload);
|
std.mem.copyForwards(u8, buf[4..], s.payload);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
@@ -29,27 +31,72 @@ const SaprusRelayMessage = struct {
|
|||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
fn toBytes(s: Self, allocator: Allocator) ![]u8 {
|
fn toBytes(s: Self, allocator: Allocator) ![]u8 {
|
||||||
const buf = allocator.alloc(u8, 4 + s.payload.len);
|
const buf = try allocator.alloc(u8, 4 + s.payload.len);
|
||||||
std.mem.copyForwards(u8, buf[0..4], s.dest);
|
std.mem.copyForwards(u8, buf[0..4], &s.dest);
|
||||||
std.mem.copyForwards(u8, buf[4..], s.payload);
|
std.mem.copyForwards(u8, buf[4..], s.payload);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
const DBA = std.heap.DebugAllocator(.{});
|
||||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
var dba: ?DBA = if (comptime is_debug) DBA.init else null;
|
||||||
|
defer if (dba) |*d| {
|
||||||
|
_ = d.deinit();
|
||||||
|
};
|
||||||
|
|
||||||
// stdout is for the actual output of your application, for example if you
|
var allocator = if (dba) |*d| d.allocator() else std.heap.smp_allocator;
|
||||||
// are implementing gzip, then only the compressed bytes should be sent to
|
|
||||||
// stdout, not any debugging messages.
|
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
|
||||||
var bw = std.io.bufferedWriter(stdout_file);
|
|
||||||
const stdout = bw.writer();
|
|
||||||
|
|
||||||
try stdout.print("Run `zig build test` to run the tests.\n", .{});
|
const relay: SaprusRelayMessage = .{
|
||||||
|
.dest = [_]u8{ 255, 255, 255, 255 },
|
||||||
|
.payload = @ptrCast(@constCast("Hello darkness my old friend")),
|
||||||
|
};
|
||||||
|
const relay_bytes = try relay.toBytes(allocator);
|
||||||
|
defer allocator.free(relay_bytes);
|
||||||
|
const msg: SaprusHeaderFrame = .{
|
||||||
|
.msg_type = .relay,
|
||||||
|
.payload = relay_bytes,
|
||||||
|
};
|
||||||
|
const msg_bytes = try msg.toBytes(allocator);
|
||||||
|
defer allocator.free(msg_bytes);
|
||||||
|
|
||||||
try bw.flush(); // Don't forget to flush!
|
try network.init();
|
||||||
|
defer network.deinit();
|
||||||
|
|
||||||
|
var sock = try network.Socket.create(.ipv4, .udp);
|
||||||
|
defer sock.close();
|
||||||
|
|
||||||
|
try sock.setBroadcast(true);
|
||||||
|
|
||||||
|
// Bind to 0.0.0.0:0
|
||||||
|
const bind_addr = network.EndPoint{
|
||||||
|
.address = network.Address{ .ipv4 = network.Address.IPv4.any },
|
||||||
|
.port = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const dest_addr = network.EndPoint{
|
||||||
|
.address = network.Address{ .ipv4 = network.Address.IPv4.broadcast },
|
||||||
|
.port = 8888,
|
||||||
|
};
|
||||||
|
|
||||||
|
try sock.bind(bind_addr);
|
||||||
|
|
||||||
|
// @breakpoint();
|
||||||
|
_ = try sock.sendTo(dest_addr, msg_bytes);
|
||||||
|
|
||||||
|
// // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
||||||
|
// std.debug.print("All your {any} are belong to us.\n", .{msg});
|
||||||
|
|
||||||
|
// // stdout is for the actual output of your application, for example if you
|
||||||
|
// // are implementing gzip, then only the compressed bytes should be sent to
|
||||||
|
// // stdout, not any debugging messages.
|
||||||
|
// const stdout_file = std.io.getStdOut().writer();
|
||||||
|
// var bw = std.io.bufferedWriter(stdout_file);
|
||||||
|
// const stdout = bw.writer();
|
||||||
|
|
||||||
|
// try stdout.print("Run `zig build test` to run the tests.\n", .{});
|
||||||
|
|
||||||
|
// try bw.flush(); // Don't forget to flush!
|
||||||
}
|
}
|
||||||
|
|
||||||
test "simple test" {
|
test "simple test" {
|
||||||
@@ -70,5 +117,8 @@ test "fuzz example" {
|
|||||||
try std.testing.fuzz(Context{}, Context.testOne, .{});
|
try std.testing.fuzz(Context{}, Context.testOne, .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const builtin = @import("builtin");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
|
|
||||||
|
const network = @import("network");
|
||||||
|
|||||||
Reference in New Issue
Block a user