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 {
|
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 };
|
||||||
const io_source: std.Random.IoSource = .{ .io = io };
|
const rand = io_source.interface();
|
||||||
break :blk io_source.interface();
|
|
||||||
};
|
|
||||||
|
|
||||||
var headers: EthIpUdp = .{
|
var headers: EthIpUdp = .{
|
||||||
.src_mac = self.socket.mac,
|
.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);
|
const relay_bytes = relay.toBytes(&relay_buf);
|
||||||
headers.setPayloadLen(relay_bytes.len);
|
headers.setPayloadLen(relay_bytes.len);
|
||||||
|
|
||||||
const full_msg = blk: {
|
var msg_buf: [max_message_size]u8 = undefined;
|
||||||
var msg_buf: [max_message_size]u8 = undefined;
|
var msg_w: Writer = .fixed(&msg_buf);
|
||||||
var msg_w: Writer = .fixed(&msg_buf);
|
msg_w.writeAll(&headers.toBytes()) catch unreachable;
|
||||||
msg_w.writeAll(&headers.toBytes()) catch unreachable;
|
msg_w.writeAll(relay_bytes) catch unreachable;
|
||||||
msg_w.writeAll(relay_bytes) catch unreachable;
|
const full_msg = msg_w.buffered();
|
||||||
break :blk msg_w.buffered();
|
|
||||||
};
|
|
||||||
|
|
||||||
try self.socket.send(full_msg);
|
try self.socket.send(full_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
|
pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
|
||||||
const rand = blk: {
|
const io_source: std.Random.IoSource = .{ .io = io };
|
||||||
const io_source: std.Random.IoSource = .{ .io = io };
|
const rand = io_source.interface();
|
||||||
break :blk io_source.interface();
|
|
||||||
};
|
|
||||||
|
|
||||||
var headers: EthIpUdp = .{
|
var headers: EthIpUdp = .{
|
||||||
.src_mac = self.socket.mac,
|
.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);
|
headers.setPayloadLen(connection_bytes.len);
|
||||||
|
|
||||||
log.debug("Building full message", .{});
|
log.debug("Building full message", .{});
|
||||||
var full_msg = blk: {
|
var msg_buf: [2048]u8 = undefined;
|
||||||
var msg_buf: [2048]u8 = undefined;
|
var msg_w: Writer = .fixed(&msg_buf);
|
||||||
var msg_w: Writer = .fixed(&msg_buf);
|
msg_w.writeAll(&headers.toBytes()) catch unreachable;
|
||||||
msg_w.writeAll(&headers.toBytes()) catch unreachable;
|
msg_w.writeAll(connection_bytes) catch unreachable;
|
||||||
msg_w.writeAll(connection_bytes) catch unreachable;
|
var full_msg = msg_w.buffered();
|
||||||
break :blk msg_w.buffered();
|
|
||||||
};
|
|
||||||
log.debug("Built full message. Sending message", .{});
|
log.debug("Built full message. Sending message", .{});
|
||||||
|
|
||||||
try self.socket.send(full_msg);
|
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);
|
headers.setPayloadLen(connection_bytes.len);
|
||||||
|
|
||||||
log.debug("Building final handshake message", .{});
|
log.debug("Building final handshake message", .{});
|
||||||
full_msg = blk: {
|
|
||||||
var msg_buf: [2048]u8 = undefined;
|
msg_w.end = 0;
|
||||||
var msg_w: Writer = .fixed(&msg_buf);
|
|
||||||
msg_w.writeAll(&headers.toBytes()) catch unreachable;
|
msg_w.writeAll(&headers.toBytes()) catch unreachable;
|
||||||
msg_w.writeAll(connection_bytes) 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);
|
try self.socket.send(full_msg);
|
||||||
|
|
||||||
return .init(self.socket, headers, connection);
|
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", .{});
|
log.debug("Awaiting connection message", .{});
|
||||||
const res = try self.socket.receive(buf);
|
const res = try self.socket.receive(buf);
|
||||||
log.debug("Received {} byte connection message", .{res.len});
|
log.debug("Received {} byte connection message", .{res.len});
|
||||||
const connection_res = blk: {
|
const msg: SaprusMessage = try .parse(res[42..]);
|
||||||
const msg: SaprusMessage = try .parse(res[42..]);
|
const connection_res = msg.connection;
|
||||||
break :blk msg.connection;
|
|
||||||
};
|
|
||||||
|
|
||||||
log.debug("Payload was {s}", .{connection_res.payload});
|
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 {
|
pub fn send(self: *Connection, io: Io, buf: []const u8) !void {
|
||||||
const rand = blk: {
|
const io_source: std.Random.IoSource = .{ .io = io };
|
||||||
const io_source: std.Random.IoSource = .{ .io = io };
|
const rand = io_source.interface();
|
||||||
break :blk io_source.interface();
|
|
||||||
};
|
|
||||||
|
|
||||||
log.debug("Sending connection message", .{});
|
log.debug("Sending connection message", .{});
|
||||||
|
|
||||||
self.connection.connection.payload = buf;
|
self.connection.connection.payload = buf;
|
||||||
const connection_bytes = blk: {
|
var connection_bytes_buf: [2048]u8 = undefined;
|
||||||
var connection_bytes: [2048]u8 = undefined;
|
const connection_bytes = self.connection.toBytes(&connection_bytes_buf);
|
||||||
break :blk self.connection.toBytes(&connection_bytes);
|
|
||||||
};
|
|
||||||
|
|
||||||
self.headers.ip.id = rand.int(u16);
|
self.headers.ip.id = rand.int(u16);
|
||||||
self.headers.setPayloadLen(connection_bytes.len);
|
self.headers.setPayloadLen(connection_bytes.len);
|
||||||
|
|
||||||
const full_msg = blk: {
|
var msg_buf: [2048]u8 = undefined;
|
||||||
var msg_buf: [2048]u8 = undefined;
|
var msg_w: Writer = .fixed(&msg_buf);
|
||||||
var msg_w: Writer = .fixed(&msg_buf);
|
try msg_w.writeAll(&self.headers.toBytes());
|
||||||
try msg_w.writeAll(&self.headers.toBytes());
|
try msg_w.writeAll(connection_bytes);
|
||||||
try msg_w.writeAll(connection_bytes);
|
const full_msg = msg_w.buffered();
|
||||||
break :blk msg_w.buffered();
|
|
||||||
};
|
|
||||||
|
|
||||||
try self.socket.send(full_msg);
|
try self.socket.send(full_msg);
|
||||||
|
|
||||||
|
|||||||
21
src/main.zig
21
src/main.zig
@@ -66,12 +66,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
.connect => {
|
.connect => {
|
||||||
i += 1;
|
i += 1;
|
||||||
if (i < args.len) {
|
if (i < args.len) {
|
||||||
var w: Writer = blk: {
|
flags.connect = args[i];
|
||||||
var buf: [2048]u8 = undefined;
|
|
||||||
break :blk .fixed(&buf);
|
|
||||||
};
|
|
||||||
try w.printBase64(args[i]);
|
|
||||||
flags.connect = w.buffered();
|
|
||||||
} else {
|
} else {
|
||||||
flags.connect = "";
|
flags.connect = "";
|
||||||
}
|
}
|
||||||
@@ -130,7 +125,12 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
if (flags.connect != null) {
|
if (flags.connect != null) {
|
||||||
reconnect: while (true) {
|
reconnect: while (true) {
|
||||||
log.debug("Starting connection", .{});
|
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", .{});
|
log.debug("Connection started", .{});
|
||||||
|
|
||||||
while (true) {
|
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));
|
try child.collectOutput(init.gpa, &child_stdout, &child_stderr, std.math.maxInt(usize));
|
||||||
|
|
||||||
// const b64e = std.base64.standard.Encoder;
|
var cmd_output_buf: [2048]u8 = undefined;
|
||||||
var cmd_output: Writer = blk: {
|
var cmd_output: Writer = .fixed(&cmd_output_buf);
|
||||||
var cmd_output_buf: [2048]u8 = undefined;
|
|
||||||
break :blk .fixed(&cmd_output_buf);
|
|
||||||
};
|
|
||||||
|
|
||||||
var cmd_output_window_iter = std.mem.window(u8, child_stdout.items, SaprusClient.max_payload_len, SaprusClient.max_payload_len);
|
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| {
|
while (cmd_output_window_iter.next()) |chunk| {
|
||||||
|
|||||||
Reference in New Issue
Block a user