mirror of
https://git.robbyzambito.me/zaprus/
synced 2026-02-04 03:34:48 +00:00
Was failing to reconnect due to trying to reuse the same socket that already had a BPF filter on it.
62 lines
1.8 KiB
Zig
62 lines
1.8 KiB
Zig
socket: RawSocket,
|
|
headers: EthIpUdp,
|
|
connection: SaprusMessage,
|
|
|
|
const Connection = @This();
|
|
|
|
pub fn init(socket: RawSocket, headers: EthIpUdp, connection: SaprusMessage) Connection {
|
|
return .{
|
|
.socket = socket,
|
|
.headers = headers,
|
|
.connection = connection,
|
|
};
|
|
}
|
|
|
|
pub fn next(self: Connection, io: Io, buf: []u8) ![]const u8 {
|
|
_ = io;
|
|
log.debug("Awaiting connection message", .{});
|
|
const res = try self.socket.receive(buf);
|
|
log.debug("Received {} byte connection message", .{res.len});
|
|
const msg: SaprusMessage = try .parse(res[42..]);
|
|
const connection_res = msg.connection;
|
|
|
|
log.debug("Payload was {s}", .{connection_res.payload});
|
|
|
|
return connection_res.payload;
|
|
}
|
|
|
|
pub fn send(self: *Connection, io: Io, buf: []const u8) !void {
|
|
const io_source: std.Random.IoSource = .{ .io = io };
|
|
const rand = io_source.interface();
|
|
|
|
log.debug("Sending connection message", .{});
|
|
|
|
self.connection.connection.payload = buf;
|
|
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);
|
|
|
|
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);
|
|
const full_msg = msg_w.buffered();
|
|
|
|
try self.socket.send(full_msg);
|
|
|
|
log.debug("Sent {} byte connection message", .{full_msg.len});
|
|
}
|
|
|
|
const std = @import("std");
|
|
const Io = std.Io;
|
|
const Writer = std.Io.Writer;
|
|
|
|
const log = std.log;
|
|
|
|
const SaprusMessage = @import("./message.zig").Message;
|
|
|
|
const EthIpUdp = @import("./EthIpUdp.zig").EthIpUdp;
|
|
const RawSocket = @import("./RawSocket.zig");
|