mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-21 16:54:49 +00:00
Compare commits
2 Commits
2302e6930d
...
push-lwzrp
| Author | SHA1 | Date | |
|---|---|---|---|
| 53a84b10bf | |||
| 0f4a7c9bcd |
@@ -48,6 +48,8 @@ pub fn main() !void {
|
|||||||
return clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms, .{});
|
return clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms, .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std.debug.print("main\n", .{});
|
||||||
|
|
||||||
if (res.args.relay) |r| {
|
if (res.args.relay) |r| {
|
||||||
try Saprus.sendRelay(if (r.len > 0) r else "Hello darkness my old friend", gpa);
|
try Saprus.sendRelay(if (r.len > 0) r else "Hello darkness my old friend", gpa);
|
||||||
std.debug.print("Sent: {s}\n", .{r});
|
std.debug.print("Sent: {s}\n", .{r});
|
||||||
|
|||||||
@@ -85,9 +85,11 @@ pub fn connect(payload: []const u8, allocator: Allocator) !?SaprusMessage {
|
|||||||
var initial_conn_res: ?SaprusMessage = null;
|
var initial_conn_res: ?SaprusMessage = null;
|
||||||
errdefer if (initial_conn_res) |c| c.deinit(allocator);
|
errdefer if (initial_conn_res) |c| c.deinit(allocator);
|
||||||
|
|
||||||
|
std.debug.print("creating socket\n", .{});
|
||||||
var sock = try network.Socket.create(.ipv4, .udp);
|
var sock = try network.Socket.create(.ipv4, .udp);
|
||||||
defer sock.close();
|
defer sock.close();
|
||||||
|
|
||||||
|
std.debug.print("creating endpoint\n", .{});
|
||||||
// Bind to 255.255.255.255:8888
|
// Bind to 255.255.255.255:8888
|
||||||
const bind_addr = network.EndPoint{
|
const bind_addr = network.EndPoint{
|
||||||
.address = network.Address{ .ipv4 = network.Address.IPv4.broadcast },
|
.address = network.Address{ .ipv4 = network.Address.IPv4.broadcast },
|
||||||
@@ -96,8 +98,10 @@ pub fn connect(payload: []const u8, allocator: Allocator) !?SaprusMessage {
|
|||||||
|
|
||||||
// timeout 1s
|
// timeout 1s
|
||||||
try sock.setReadTimeout(1 * std.time.us_per_s);
|
try sock.setReadTimeout(1 * std.time.us_per_s);
|
||||||
|
std.debug.print("binding to socket\n", .{});
|
||||||
try sock.bind(bind_addr);
|
try sock.bind(bind_addr);
|
||||||
|
|
||||||
|
std.debug.print("sending initial connection payload\n", .{});
|
||||||
const msg = try sendInitialConnection(payload, initial_port, allocator);
|
const msg = try sendInitialConnection(payload, initial_port, allocator);
|
||||||
|
|
||||||
var response_buf: [4096]u8 = undefined;
|
var response_buf: [4096]u8 = undefined;
|
||||||
|
|||||||
@@ -117,12 +117,12 @@ pub const SaprusMessage = union(SaprusPacketType) {
|
|||||||
|
|
||||||
fn fromBytesAux(
|
fn fromBytesAux(
|
||||||
comptime packet: SaprusPacketType,
|
comptime packet: SaprusPacketType,
|
||||||
|
len: u16,
|
||||||
r: std.io.FixedBufferStream([]const u8).Reader,
|
r: std.io.FixedBufferStream([]const u8).Reader,
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
) !SaprusMessage {
|
) !SaprusMessage {
|
||||||
const Header = @field(@FieldType(SaprusMessage, @tagName(packet)), "Header");
|
const Header = @field(@FieldType(SaprusMessage, @tagName(packet)), "Header");
|
||||||
// Read the length of the header + base64 encoded payload.
|
|
||||||
const len = try r.readInt(u16, .big);
|
|
||||||
// Read the header for the current message type.
|
// Read the header for the current message type.
|
||||||
var header_bytes: [@sizeOf(Header)]u8 = undefined;
|
var header_bytes: [@sizeOf(Header)]u8 = undefined;
|
||||||
_ = try r.read(header_bytes[0 .. @bitSizeOf(Header) / 8]);
|
_ = try r.read(header_bytes[0 .. @bitSizeOf(Header) / 8]);
|
||||||
@@ -130,13 +130,13 @@ pub const SaprusMessage = union(SaprusPacketType) {
|
|||||||
const header = try header_stream.reader().readStructEndian(Header, .big);
|
const header = try header_stream.reader().readStructEndian(Header, .big);
|
||||||
|
|
||||||
// Read the base64 bytes into a list to be able to call the decoder on it.
|
// Read the base64 bytes into a list to be able to call the decoder on it.
|
||||||
var payload_buf = std.ArrayList(u8).init(allocator);
|
const payload_buf = try allocator.alloc(u8, len - @bitSizeOf(Header) / 8);
|
||||||
defer payload_buf.deinit();
|
defer allocator.free(payload_buf);
|
||||||
try r.readAllArrayList(&payload_buf, len);
|
_ = try r.readAll(payload_buf);
|
||||||
|
|
||||||
// Create a buffer to store the payload in, and decode the base64 bytes into the payload field.
|
// Create a buffer to store the payload in, and decode the base64 bytes into the payload field.
|
||||||
const payload = try allocator.alloc(u8, try base64Dec.calcSizeForSlice(payload_buf.items));
|
const payload = try allocator.alloc(u8, try base64Dec.calcSizeForSlice(payload_buf));
|
||||||
try base64Dec.decode(payload, payload_buf.items);
|
try base64Dec.decode(payload, payload_buf);
|
||||||
|
|
||||||
// Return the type of SaprusMessage specified by the `packet` argument.
|
// Return the type of SaprusMessage specified by the `packet` argument.
|
||||||
return @unionInit(SaprusMessage, @tagName(packet), .{
|
return @unionInit(SaprusMessage, @tagName(packet), .{
|
||||||
@@ -153,9 +153,12 @@ pub const SaprusMessage = union(SaprusPacketType) {
|
|||||||
// Read packet type
|
// Read packet type
|
||||||
const packet_type = @as(SaprusPacketType, @enumFromInt(try r.readInt(u16, .big)));
|
const packet_type = @as(SaprusPacketType, @enumFromInt(try r.readInt(u16, .big)));
|
||||||
|
|
||||||
|
// Read the length of the header + base64 encoded payload.
|
||||||
|
const len = try r.readInt(u16, .big);
|
||||||
|
|
||||||
switch (packet_type) {
|
switch (packet_type) {
|
||||||
.relay => return fromBytesAux(.relay, r, allocator),
|
.relay => return fromBytesAux(.relay, len, r, allocator),
|
||||||
.connection => return fromBytesAux(.connection, r, allocator),
|
.connection => return fromBytesAux(.connection, len, r, allocator),
|
||||||
.file_transfer => return SaprusError.NotImplementedSaprusType,
|
.file_transfer => return SaprusError.NotImplementedSaprusType,
|
||||||
else => return SaprusError.UnknownSaprusType,
|
else => return SaprusError.UnknownSaprusType,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user