Fix reconnection

Was failing to reconnect due to trying to reuse the same socket that
already had a BPF filter on it.
This commit is contained in:
2026-01-24 19:19:00 -05:00
parent c3b17f8267
commit 09152377ed
5 changed files with 20 additions and 15 deletions

View File

@@ -92,7 +92,10 @@ pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection {
};
log.debug("Setting bpf filter to port {}", .{connection.connection.src});
try self.socket.attachSaprusPortFilter(connection.connection.src);
self.socket.attachSaprusPortFilter(connection.connection.src) catch |err| {
log.err("Failed to set port filter: {t}", .{err});
return err;
};
log.debug("bpf set", .{});
var connection_buf: [2048]u8 = undefined;

View File

@@ -12,10 +12,6 @@ pub fn init(socket: RawSocket, headers: EthIpUdp, connection: SaprusMessage) Con
};
}
pub fn deinit(self: *Connection) void {
self.socket.deinit();
}
pub fn next(self: Connection, io: Io, buf: []u8) ![]const u8 {
_ = io;
log.debug("Awaiting connection message", .{});

View File

@@ -1,5 +1,7 @@
const RawSocket = @This();
const is_debug = builtin.mode == .Debug;
fd: i32,
sockaddr_ll: std.posix.sockaddr.ll,
mac: [6]u8,
@@ -70,7 +72,7 @@ pub fn init() !RawSocket {
const bind_ret = std.os.linux.bind(socket, @ptrCast(&sockaddr_ll), @sizeOf(@TypeOf(sockaddr_ll)));
if (bind_ret != 0) return error.BindError;
const timeout: std.os.linux.timeval = .{ .sec = 600, .usec = 0 };
const timeout: std.os.linux.timeval = .{ .sec = 60 * if (is_debug) 1 else 10, .usec = 0 };
const timeout_ret = std.os.linux.setsockopt(socket, std.os.linux.SOL.SOCKET, std.os.linux.SO.RCVTIMEO, @ptrCast(&timeout), @sizeOf(@TypeOf(timeout)));
if (timeout_ret != 0) return error.SetTimeoutError;

View File

@@ -57,7 +57,6 @@ export fn zaprus_connect(
export fn zaprus_deinit_connection(connection: ?*ZaprusConnection) void {
const c: ?*zaprus.Connection = @ptrCast(@alignCast(connection));
if (c) |zc| {
zc.deinit();
alloc.destroy(zc);
}
}

View File

@@ -1,3 +1,5 @@
const is_debug = builtin.mode == .Debug;
const help =
\\-h, --help Display this help and exit.
\\-r, --relay <str> A relay message to send.
@@ -82,10 +84,11 @@ pub fn main(init: std.process.Init) !void {
return error.InvalidArguments;
}
var client = try SaprusClient.init();
defer client.deinit();
var client: SaprusClient = undefined;
if (flags.relay != null) {
client = try .init();
defer client.deinit();
var chunk_writer_buf: [2048]u8 = undefined;
var chunk_writer: Writer = .fixed(&chunk_writer_buf);
if (flags.relay.?.len > 0) {
@@ -124,22 +127,24 @@ pub fn main(init: std.process.Init) !void {
return;
}
var retry_seconds: u16 = 120;
var retry_seconds: u16 = 12 * if (is_debug) 1 else 10;
var init_con_buf: [SaprusClient.max_payload_len]u8 = undefined;
var w: Writer = .fixed(&init_con_buf);
try w.print("{b64}", .{flags.connect.?});
if (flags.connect != null) {
reconnect: while (true) {
client = try .init();
defer client.deinit();
log.debug("Starting connection", .{});
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 = client.connect(init.io, w.buffered()) catch {
try init.io.sleep(.fromSeconds(retry_seconds), .boot);
continue;
};
defer connection.deinit();
retry_seconds = 600;
retry_seconds = 60 * if (is_debug) 1 else 10;
log.debug("Connection started", .{});