mirror of
https://git.robbyzambito.me/zaprus
synced 2026-02-04 00:14:52 +00:00
Don't create dangling references
Apparently things die at the end of blk scopes.
This commit is contained in:
@@ -22,10 +22,8 @@ pub fn deinit(self: *Client) void {
|
||||
}
|
||||
|
||||
pub fn sendRelay(self: *Client, io: Io, payload: []const u8, dest: [4]u8) !void {
|
||||
const rand = blk: {
|
||||
const io_source: std.Random.IoSource = .{ .io = io };
|
||||
break :blk io_source.interface();
|
||||
};
|
||||
const rand = io_source.interface();
|
||||
|
||||
var headers: EthIpUdp = .{
|
||||
.src_mac = self.socket.mac,
|
||||
@@ -53,22 +51,18 @@ pub fn sendRelay(self: *Client, io: Io, payload: []const u8, dest: [4]u8) !void
|
||||
const relay_bytes = relay.toBytes(&relay_buf);
|
||||
headers.setPayloadLen(relay_bytes.len);
|
||||
|
||||
const full_msg = blk: {
|
||||
var msg_buf: [max_message_size]u8 = undefined;
|
||||
var msg_w: Writer = .fixed(&msg_buf);
|
||||
msg_w.writeAll(&headers.toBytes()) catch unreachable;
|
||||
msg_w.writeAll(relay_bytes) catch unreachable;
|
||||
break :blk msg_w.buffered();
|
||||
};
|
||||
const full_msg = msg_w.buffered();
|
||||
|
||||
try self.socket.send(full_msg);
|
||||
}
|
||||
|
||||
pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
|
||||
const rand = blk: {
|
||||
const io_source: std.Random.IoSource = .{ .io = io };
|
||||
break :blk io_source.interface();
|
||||
};
|
||||
const rand = io_source.interface();
|
||||
|
||||
var headers: EthIpUdp = .{
|
||||
.src_mac = self.socket.mac,
|
||||
@@ -106,13 +100,11 @@ pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
|
||||
headers.setPayloadLen(connection_bytes.len);
|
||||
|
||||
log.debug("Building full message", .{});
|
||||
var full_msg = blk: {
|
||||
var msg_buf: [2048]u8 = undefined;
|
||||
var msg_w: Writer = .fixed(&msg_buf);
|
||||
msg_w.writeAll(&headers.toBytes()) catch unreachable;
|
||||
msg_w.writeAll(connection_bytes) catch unreachable;
|
||||
break :blk msg_w.buffered();
|
||||
};
|
||||
var full_msg = msg_w.buffered();
|
||||
log.debug("Built full message. Sending message", .{});
|
||||
|
||||
try self.socket.send(full_msg);
|
||||
@@ -128,13 +120,13 @@ pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
|
||||
headers.setPayloadLen(connection_bytes.len);
|
||||
|
||||
log.debug("Building final handshake message", .{});
|
||||
full_msg = blk: {
|
||||
var msg_buf: [2048]u8 = undefined;
|
||||
var msg_w: Writer = .fixed(&msg_buf);
|
||||
|
||||
msg_w.end = 0;
|
||||
|
||||
msg_w.writeAll(&headers.toBytes()) catch unreachable;
|
||||
msg_w.writeAll(connection_bytes) catch unreachable;
|
||||
break :blk msg_w.buffered();
|
||||
};
|
||||
full_msg = msg_w.buffered();
|
||||
|
||||
try self.socket.send(full_msg);
|
||||
|
||||
return .init(self.socket, headers, connection);
|
||||
|
||||
@@ -17,10 +17,8 @@ pub fn next(self: Connection, io: Io, buf: []u8) ![]const u8 {
|
||||
log.debug("Awaiting connection message", .{});
|
||||
const res = try self.socket.receive(buf);
|
||||
log.debug("Received {} byte connection message", .{res.len});
|
||||
const connection_res = blk: {
|
||||
const msg: SaprusMessage = try .parse(res[42..]);
|
||||
break :blk msg.connection;
|
||||
};
|
||||
const connection_res = msg.connection;
|
||||
|
||||
log.debug("Payload was {s}", .{connection_res.payload});
|
||||
|
||||
@@ -28,29 +26,23 @@ pub fn next(self: Connection, io: Io, buf: []u8) ![]const u8 {
|
||||
}
|
||||
|
||||
pub fn send(self: *Connection, io: Io, buf: []const u8) !void {
|
||||
const rand = blk: {
|
||||
const io_source: std.Random.IoSource = .{ .io = io };
|
||||
break :blk io_source.interface();
|
||||
};
|
||||
const rand = io_source.interface();
|
||||
|
||||
log.debug("Sending connection message", .{});
|
||||
|
||||
self.connection.connection.payload = buf;
|
||||
const connection_bytes = blk: {
|
||||
var connection_bytes: [2048]u8 = undefined;
|
||||
break :blk self.connection.toBytes(&connection_bytes);
|
||||
};
|
||||
var connection_bytes_buf: [2048]u8 = undefined;
|
||||
const connection_bytes = self.connection.toBytes(&connection_bytes_buf);
|
||||
|
||||
self.headers.ip.id = rand.int(u16);
|
||||
self.headers.setPayloadLen(connection_bytes.len);
|
||||
|
||||
const full_msg = blk: {
|
||||
var msg_buf: [2048]u8 = undefined;
|
||||
var msg_w: Writer = .fixed(&msg_buf);
|
||||
try msg_w.writeAll(&self.headers.toBytes());
|
||||
try msg_w.writeAll(connection_bytes);
|
||||
break :blk msg_w.buffered();
|
||||
};
|
||||
const full_msg = msg_w.buffered();
|
||||
|
||||
try self.socket.send(full_msg);
|
||||
|
||||
|
||||
19
src/main.zig
19
src/main.zig
@@ -66,12 +66,7 @@ pub fn main(init: std.process.Init) !void {
|
||||
.connect => {
|
||||
i += 1;
|
||||
if (i < args.len) {
|
||||
var w: Writer = blk: {
|
||||
var buf: [2048]u8 = undefined;
|
||||
break :blk .fixed(&buf);
|
||||
};
|
||||
try w.printBase64(args[i]);
|
||||
flags.connect = w.buffered();
|
||||
flags.connect = args[i];
|
||||
} else {
|
||||
flags.connect = "";
|
||||
}
|
||||
@@ -130,7 +125,12 @@ pub fn main(init: std.process.Init) !void {
|
||||
if (flags.connect != null) {
|
||||
reconnect: while (true) {
|
||||
log.debug("Starting connection", .{});
|
||||
var connection = try client.connect(init.io, flags.connect.?);
|
||||
|
||||
var init_con_buf: [SaprusClient.max_payload_len]u8 = undefined;
|
||||
var w: Writer = .fixed(&init_con_buf);
|
||||
try w.print("{b64}", .{flags.connect.?});
|
||||
var connection = try client.connect(init.io, w.buffered());
|
||||
|
||||
log.debug("Connection started", .{});
|
||||
|
||||
while (true) {
|
||||
@@ -158,11 +158,8 @@ pub fn main(init: std.process.Init) !void {
|
||||
|
||||
try child.collectOutput(init.gpa, &child_stdout, &child_stderr, std.math.maxInt(usize));
|
||||
|
||||
// const b64e = std.base64.standard.Encoder;
|
||||
var cmd_output: Writer = blk: {
|
||||
var cmd_output_buf: [2048]u8 = undefined;
|
||||
break :blk .fixed(&cmd_output_buf);
|
||||
};
|
||||
var cmd_output: Writer = .fixed(&cmd_output_buf);
|
||||
|
||||
var cmd_output_window_iter = std.mem.window(u8, child_stdout.items, SaprusClient.max_payload_len, SaprusClient.max_payload_len);
|
||||
while (cmd_output_window_iter.next()) |chunk| {
|
||||
|
||||
Reference in New Issue
Block a user