mirror of
https://git.robbyzambito.me/zaprus
synced 2026-02-04 16:34:51 +00:00
145 lines
4.5 KiB
Zig
145 lines
4.5 KiB
Zig
const base64Enc = std.base64.Base64Encoder.init(std.base64.standard_alphabet_chars, '=');
|
|
const base64Dec = std.base64.Base64Decoder.init(std.base64.standard_alphabet_chars, '=');
|
|
|
|
writer: *std.Io.Writer,
|
|
|
|
const Self = @This();
|
|
|
|
const max_message_size = 2048;
|
|
|
|
pub fn init(writer: *std.Io.Writer) !Self {
|
|
return .{
|
|
.writer = writer,
|
|
};
|
|
}
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
self.writer.flush() catch {};
|
|
}
|
|
|
|
/// Used for relay messages and connection handshake.
|
|
/// Assumes Client .init has been called.
|
|
fn broadcastInitialInterestMessage(self: *Self, msg_bytes: []u8) !void {
|
|
const writer = self.writer;
|
|
|
|
// Ensure the writer is in a valid state
|
|
std.debug.assert(writer.buffer.len - writer.end >= msg_bytes.len);
|
|
|
|
// Saprus
|
|
const msg_target_bytes = try writer.writableSlice(msg_bytes.len);
|
|
@memcpy(msg_target_bytes, msg_bytes);
|
|
var msg_target: *align(1) SaprusMessage = try .bytesAsValue(msg_target_bytes);
|
|
try msg_target.networkFromNativeEndian();
|
|
std.debug.print("{x}\n", .{writer.buffer[0..writer.end]});
|
|
try writer.flush();
|
|
}
|
|
|
|
fn broadcastSaprusMessage(msg_bytes: []u8, udp_port: u16) !void {
|
|
const msg: *align(1) SaprusMessage = try .bytesAsValue(msg_bytes);
|
|
try msg.networkFromNativeEndian();
|
|
defer msg.nativeFromNetworkEndian() catch unreachable;
|
|
|
|
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 = udp_port,
|
|
};
|
|
|
|
try sock.bind(bind_addr);
|
|
|
|
std.debug.print("{x}\n", .{msg_bytes});
|
|
|
|
_ = try sock.sendTo(dest_addr, msg_bytes);
|
|
}
|
|
|
|
pub fn sendRelay(self: *Self, payload: []const u8, dest: [4]u8) !void {
|
|
var buf: [max_message_size]u8 align(@alignOf(SaprusMessage)) = undefined;
|
|
const msg_bytes = buf[0..try SaprusMessage.calcSize(
|
|
.relay,
|
|
base64Enc.calcSize(payload.len),
|
|
)];
|
|
const msg: *align(1) SaprusMessage = .init(.relay, msg_bytes);
|
|
|
|
const relay = (try msg.getSaprusTypePayload()).relay;
|
|
relay.dest = dest;
|
|
_ = base64Enc.encode(relay.getPayload(), payload);
|
|
|
|
try self.broadcastInitialInterestMessage(msg_bytes);
|
|
}
|
|
|
|
// pub fn sendInitialConnection(
|
|
// self: Self,
|
|
// payload: []const u8,
|
|
// output_bytes: []u8,
|
|
// initial_port: u16,
|
|
// ) !*align(1) SaprusMessage {
|
|
// const dest_port = self.randomPort();
|
|
// const msg_bytes = output_bytes[0..try SaprusMessage.calcSize(
|
|
// .connection,
|
|
// base64Enc.calcSize(payload.len),
|
|
// )];
|
|
// const msg: *align(1) SaprusMessage = .init(.connection, msg_bytes);
|
|
|
|
// const connection = (try msg.getSaprusTypePayload()).connection;
|
|
// connection.src_port = initial_port;
|
|
// connection.dest_port = dest_port;
|
|
// _ = base64Enc.encode(connection.getPayload(), payload);
|
|
|
|
// try broadcastSaprusMessage(msg_bytes, 8888);
|
|
|
|
// return msg;
|
|
// }
|
|
|
|
// pub fn connect(self: Self, payload: []const u8) !?SaprusConnection {
|
|
// const initial_port = self.randomPort();
|
|
|
|
// var initial_conn_res: ?*align(1) SaprusMessage = null;
|
|
|
|
// var sock = try network.Socket.create(.ipv4, .udp);
|
|
// defer sock.close();
|
|
|
|
// // Bind to 255.255.255.255:8888
|
|
// const bind_addr = network.EndPoint{
|
|
// .address = network.Address{ .ipv4 = network.Address.IPv4.broadcast },
|
|
// .port = 8888,
|
|
// };
|
|
|
|
// // timeout 1s
|
|
// try sock.setReadTimeout(1 * std.time.us_per_s);
|
|
// try sock.bind(bind_addr);
|
|
|
|
// var sent_msg_bytes: [max_message_size]u8 align(@alignOf(SaprusMessage)) = undefined;
|
|
// const msg = try self.sendInitialConnection(payload, &sent_msg_bytes, initial_port);
|
|
|
|
// var response_buf: [max_message_size]u8 align(@alignOf(SaprusMessage)) = undefined;
|
|
// _ = try sock.receive(&response_buf); // Ignore message that I sent.
|
|
// const len = try sock.receive(&response_buf);
|
|
|
|
// initial_conn_res = try .networkBytesAsValue(response_buf[0..len]);
|
|
|
|
// // Complete handshake after awaiting response
|
|
// try broadcastSaprusMessage(msg.asBytes(), self.randomPort());
|
|
|
|
// if (false) {
|
|
// return initial_conn_res.?;
|
|
// }
|
|
// return null;
|
|
// }
|
|
|
|
const SaprusMessage = @import("message.zig").Message;
|
|
const SaprusConnection = @import("Connection.zig");
|
|
|
|
const std = @import("std");
|
|
|
|
const network = @import("network");
|