mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 16:24:50 +00:00
Break relay into a specific program
This commit is contained in:
71
src/main.zig
71
src/main.zig
@@ -1,71 +0,0 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user