mirror of
https://git.robbyzambito.me/zaprus/
synced 2026-02-04 03:34:48 +00:00
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:
@@ -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});
|
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", .{});
|
log.debug("bpf set", .{});
|
||||||
|
|
||||||
var connection_buf: [2048]u8 = undefined;
|
var connection_buf: [2048]u8 = undefined;
|
||||||
|
|||||||
@@ -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 {
|
pub fn next(self: Connection, io: Io, buf: []u8) ![]const u8 {
|
||||||
_ = io;
|
_ = io;
|
||||||
log.debug("Awaiting connection message", .{});
|
log.debug("Awaiting connection message", .{});
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
const RawSocket = @This();
|
const RawSocket = @This();
|
||||||
|
|
||||||
|
const is_debug = builtin.mode == .Debug;
|
||||||
|
|
||||||
fd: i32,
|
fd: i32,
|
||||||
sockaddr_ll: std.posix.sockaddr.ll,
|
sockaddr_ll: std.posix.sockaddr.ll,
|
||||||
mac: [6]u8,
|
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)));
|
const bind_ret = std.os.linux.bind(socket, @ptrCast(&sockaddr_ll), @sizeOf(@TypeOf(sockaddr_ll)));
|
||||||
if (bind_ret != 0) return error.BindError;
|
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)));
|
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;
|
if (timeout_ret != 0) return error.SetTimeoutError;
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,6 @@ export fn zaprus_connect(
|
|||||||
export fn zaprus_deinit_connection(connection: ?*ZaprusConnection) void {
|
export fn zaprus_deinit_connection(connection: ?*ZaprusConnection) void {
|
||||||
const c: ?*zaprus.Connection = @ptrCast(@alignCast(connection));
|
const c: ?*zaprus.Connection = @ptrCast(@alignCast(connection));
|
||||||
if (c) |zc| {
|
if (c) |zc| {
|
||||||
zc.deinit();
|
|
||||||
alloc.destroy(zc);
|
alloc.destroy(zc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/main.zig
23
src/main.zig
@@ -1,3 +1,5 @@
|
|||||||
|
const is_debug = builtin.mode == .Debug;
|
||||||
|
|
||||||
const help =
|
const help =
|
||||||
\\-h, --help Display this help and exit.
|
\\-h, --help Display this help and exit.
|
||||||
\\-r, --relay <str> A relay message to send.
|
\\-r, --relay <str> A relay message to send.
|
||||||
@@ -82,10 +84,11 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
return error.InvalidArguments;
|
return error.InvalidArguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
var client = try SaprusClient.init();
|
var client: SaprusClient = undefined;
|
||||||
defer client.deinit();
|
|
||||||
|
|
||||||
if (flags.relay != null) {
|
if (flags.relay != null) {
|
||||||
|
client = try .init();
|
||||||
|
defer client.deinit();
|
||||||
var chunk_writer_buf: [2048]u8 = undefined;
|
var chunk_writer_buf: [2048]u8 = undefined;
|
||||||
var chunk_writer: Writer = .fixed(&chunk_writer_buf);
|
var chunk_writer: Writer = .fixed(&chunk_writer_buf);
|
||||||
if (flags.relay.?.len > 0) {
|
if (flags.relay.?.len > 0) {
|
||||||
@@ -124,22 +127,24 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var retry_seconds: u16 = 120;
|
var retry_seconds: u16 = 12 * if (is_debug) 1 else 10;
|
||||||
|
|
||||||
if (flags.connect != null) {
|
|
||||||
reconnect: while (true) {
|
|
||||||
log.debug("Starting connection", .{});
|
|
||||||
|
|
||||||
var init_con_buf: [SaprusClient.max_payload_len]u8 = undefined;
|
var init_con_buf: [SaprusClient.max_payload_len]u8 = undefined;
|
||||||
var w: Writer = .fixed(&init_con_buf);
|
var w: Writer = .fixed(&init_con_buf);
|
||||||
try w.print("{b64}", .{flags.connect.?});
|
try w.print("{b64}", .{flags.connect.?});
|
||||||
|
|
||||||
|
if (flags.connect != null) {
|
||||||
|
reconnect: while (true) {
|
||||||
|
client = try .init();
|
||||||
|
defer client.deinit();
|
||||||
|
log.debug("Starting connection", .{});
|
||||||
|
|
||||||
var connection = client.connect(init.io, w.buffered()) catch {
|
var connection = client.connect(init.io, w.buffered()) catch {
|
||||||
try init.io.sleep(.fromSeconds(retry_seconds), .boot);
|
try init.io.sleep(.fromSeconds(retry_seconds), .boot);
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
defer connection.deinit();
|
|
||||||
|
|
||||||
retry_seconds = 600;
|
retry_seconds = 60 * if (is_debug) 1 else 10;
|
||||||
|
|
||||||
log.debug("Connection started", .{});
|
log.debug("Connection started", .{});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user