mirror of
https://git.robbyzambito.me/zaprus
synced 2026-02-04 16:34:51 +00:00
Update to Saprus 0.2.1
Handle management messages instead of letting them bubble up through the connection to the consumer. Right now, this just means handling ping messages by sending a pong. Also updated to follow the new handshake flow. The sentinel will mirror the ports instead of matching them. Now filters on the full source and dest ports, which are less likely to have erroneous matches.
This commit is contained in:
@@ -28,25 +28,50 @@ pub fn init(socket: RawSocket, headers: EthIpUdp, connection: SaprusMessage) Con
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
// 'p' as base64
|
||||
const pong = "cA==";
|
||||
|
||||
log.debug("Payload was {s}", .{connection_res.payload});
|
||||
pub fn next(self: *Connection, io: Io, buf: []u8) ![]const u8 {
|
||||
while (true) {
|
||||
log.debug("Awaiting connection message", .{});
|
||||
const res = try self.socket.receive(buf);
|
||||
log.debug("Received {} byte connection message", .{res.len});
|
||||
const msg = SaprusMessage.parse(res[42..]) catch |err| {
|
||||
log.err("Failed to parse next message: {t}\n{x}\n{x}", .{ err, res[0..], res[42..] });
|
||||
return err;
|
||||
};
|
||||
|
||||
return connection_res.payload;
|
||||
switch (msg) {
|
||||
.connection => |con_res| {
|
||||
if (try con_res.management()) |mgt| {
|
||||
log.debug("Received management message {t}", .{mgt});
|
||||
switch (mgt) {
|
||||
.ping => {
|
||||
log.debug("Sending pong", .{});
|
||||
try self.send(io, .{ .management = true }, pong);
|
||||
log.debug("Sent pong message", .{});
|
||||
},
|
||||
else => |m| log.debug("Received management message that I don't know how to handle: {t}", .{m}),
|
||||
}
|
||||
} else {
|
||||
log.debug("Payload was {s}", .{con_res.payload});
|
||||
return con_res.payload;
|
||||
}
|
||||
},
|
||||
else => |m| {
|
||||
std.debug.panic("Expected connection message, instead got {x}. This means there is an error with the BPF.", .{@intFromEnum(m)});
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send(self: *Connection, io: Io, buf: []const u8) !void {
|
||||
pub fn send(self: *Connection, io: Io, options: SaprusMessage.Connection.Options, 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.options = options;
|
||||
self.connection.connection.payload = buf;
|
||||
var connection_bytes_buf: [2048]u8 = undefined;
|
||||
const connection_bytes = self.connection.toBytes(&connection_bytes_buf);
|
||||
|
||||
Reference in New Issue
Block a user