diff options
| author | Robby Zambito <contact@robbyzambito.me> | 2025-04-02 18:36:45 -0400 | 
|---|---|---|
| committer | Robby Zambito <contact@robbyzambito.me> | 2025-04-06 13:08:09 -0400 | 
| commit | 3e265aeadbe2b3d10734612882927e0b08391f2b (patch) | |
| tree | f7249f7c0977350999937827ad71770a69f97255 | |
| parent | 3b5c8e397eeabcdbbde8b588ad6a96e58f14531d (diff) | |
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
| -rw-r--r-- | src/main.zig | 80 | 
1 files changed, 65 insertions, 15 deletions
diff --git a/src/main.zig b/src/main.zig index ebb7d33..e86fc03 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,8 @@  //! you are building an executable. If you are making a library, the convention  //! is to delete this file and start with root.zig instead. +const is_debug = builtin.mode == .Debug; +  const SaprusPacketType = enum(u16) {      relay = 0x003C,      file_transfer = 0x8888, @@ -14,9 +16,9 @@ const SaprusHeaderFrame = struct {      const Self = @This();      fn toBytes(s: Self, allocator: Allocator) ![]u8 { -        const buf = allocator.alloc(u8, 32 + s.payload.len); -        std.mem.writeInt(u16, buf[0..2], s.msg_type, .big); -        std.mem.writeInt(u16, buf[2..4], s.payload.len); +        const buf = try allocator.alloc(u8, 32 + s.payload.len); +        std.mem.writeInt(u16, buf[0..2], @intFromEnum(s.msg_type), .big); +        std.mem.writeInt(u16, buf[2..4], @intCast(s.payload.len), .big);          std.mem.copyForwards(u8, buf[4..], s.payload);          return buf;      } @@ -29,27 +31,72 @@ const SaprusRelayMessage = struct {      const Self = @This();      fn toBytes(s: Self, allocator: Allocator) ![]u8 { -        const buf = allocator.alloc(u8, 4 + s.payload.len); -        std.mem.copyForwards(u8, buf[0..4], s.dest); +        const buf = try allocator.alloc(u8, 4 + s.payload.len); +        std.mem.copyForwards(u8, buf[0..4], &s.dest);          std.mem.copyForwards(u8, buf[4..], s.payload);          return buf;      }  };  pub fn main() !void { -    // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) -    std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); +    const DBA = std.heap.DebugAllocator(.{}); +    var dba: ?DBA = if (comptime is_debug) DBA.init else null; +    defer if (dba) |*d| { +        _ = d.deinit(); +    }; + +    var allocator = if (dba) |*d| d.allocator() else std.heap.smp_allocator; -    // 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(); +    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 stdout.print("Run `zig build test` to run the tests.\n", .{}); +    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, +    }; -    try bw.flush(); // Don't forget to flush! +    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" { @@ -70,5 +117,8 @@ test "fuzz example" {      try std.testing.fuzz(Context{}, Context.testOne, .{});  } +const builtin = @import("builtin");  const std = @import("std");  const Allocator = std.mem.Allocator; + +const network = @import("network");  | 
