summaryrefslogtreecommitdiff
path: root/src/saprus_relay.zig
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-04-04 22:09:06 -0400
committerRobby Zambito <contact@robbyzambito.me>2025-04-06 13:08:09 -0400
commit448e900004053012e63078682e34b48799050794 (patch)
treea1a67548873b2adc3f48dfa0353cac18ba5fd70a /src/saprus_relay.zig
parent93161ff4bd6830a6b74be8ed9e13f284511e20d8 (diff)
Break relay into a specific program
Diffstat (limited to 'src/saprus_relay.zig')
-rw-r--r--src/saprus_relay.zig71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/saprus_relay.zig b/src/saprus_relay.zig
new file mode 100644
index 0000000..0df66e9
--- /dev/null
+++ b/src/saprus_relay.zig
@@ -0,0 +1,71 @@
+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");
+
+pub fn main() !void {
+ defer if (is_debug) {
+ _ = dba.deinit();
+ };
+
+ var gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator;
+
+ const args = try std.process.argsAlloc(gpa);
+ defer std.process.argsFree(gpa, args);
+
+ var message = ArrayList(u8).init(gpa);
+ defer message.deinit();
+
+ for (args[1..], 0..) |arg, i| {
+ try message.appendSlice(arg);
+ if (i < args.len - 1)
+ try message.append(' ');
+ }
+
+ const msg = SaprusMessage{
+ .relay = .{
+ .header = .{ .dest = .{ 255, 255, 255, 255 } },
+ .payload = if (message.items.len > 0) message.items else "Hello darkness my old friend",
+ },
+ };
+
+ const msg_bytes = try msg.toBytes(gpa);
+ defer gpa.free(msg_bytes);
+
+ 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);
+
+ _ = try sock.sendTo(dest_addr, msg_bytes);
+}
+
+const builtin = @import("builtin");
+const std = @import("std");
+const DebugAllocator = std.heap.DebugAllocator(.{});
+const ArrayList = std.ArrayList;
+
+const network = @import("network");
+
+const SaprusMessage = @import("./saprus_message.zig").SaprusMessage;