Start adding connection message

This commit is contained in:
2026-01-19 14:01:57 -05:00
parent abf2cfb366
commit cc765dc91b
2 changed files with 73 additions and 30 deletions

View File

@@ -90,10 +90,10 @@ pub fn main(init: std.process.Init) !void {
std.debug.print("dest: {s}\n", .{flags.dest orelse "<null>"}); std.debug.print("dest: {s}\n", .{flags.dest orelse "<null>"});
std.debug.print("connect: {s}\n", .{flags.connect orelse "<null>"}); std.debug.print("connect: {s}\n", .{flags.connect orelse "<null>"});
// const rand = blk: { const rand = blk: {
// const io_source: std.Random.IoSource = .{ .io = init.io }; const io_source: std.Random.IoSource = .{ .io = init.io };
// break :blk io_source.interface(); break :blk io_source.interface();
// }; };
// const net_interface: std.Io.net.Interface = .{ .index = 1 }; // const net_interface: std.Io.net.Interface = .{ .index = 1 };
// std.debug.print("Interface: {s}\n", .{(try net_interface.name(init.io)).toSlice()}); // std.debug.print("Interface: {s}\n", .{(try net_interface.name(init.io)).toSlice()});
@@ -153,46 +153,89 @@ pub fn main(init: std.process.Init) !void {
}; };
var headers: EthIpUdp = .{ var headers: EthIpUdp = .{
.src_mac = @splat(0x0e), .src_mac = undefined, // TODO: REAL MAC
.ip = .{ .ip = .{
.id = 0, .id = 0,
.src_addr = 0, .src_addr = rand.int(u32),
.dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }), .dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }),
.len = undefined, .len = undefined,
}, },
.udp = .{ .udp = .{
.src_port = undefined, // TODO: change this? .src_port = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16)),
.dst_port = 8888, .dst_port = 8888,
.len = undefined, .len = undefined,
}, },
}; };
std.debug.print("headers: {any}\n", .{&headers.toBytes()}); std.debug.print("headers: {any}\n", .{&headers.toBytes()});
const relay: SaprusMessage = .{ if (flags.relay != null) {
.relay = .{ const relay: SaprusMessage = .{
.dest = .fromBytes(&parseDest(flags.dest)), .relay = .{
.payload = flags.relay.?, .dest = .fromBytes(&parseDest(flags.dest)),
}, .payload = flags.relay.?,
}; },
};
var relay_buf: [2048]u8 = undefined; var relay_buf: [2048]u8 = undefined;
const relay_bytes = relay.toBytes(&relay_buf); const relay_bytes = relay.toBytes(&relay_buf);
std.debug.print("payload: {any}\n", .{relay_bytes}); std.debug.print("payload: {any}\n", .{relay_bytes});
headers.setPayloadLen(relay_bytes.len); headers.setPayloadLen(relay_bytes.len);
const full_msg = blk: { 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);
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;
break :blk msg_w.buffered(); break :blk msg_w.buffered();
}; };
std.debug.print("full message = {any}\n", .{full_msg}); std.debug.print("full message = {any}\n", .{full_msg});
var socket: RawSocket = try .init("enp7s0"); var socket: RawSocket = try .init("enp7s0");
defer socket.deinit(); defer socket.deinit();
try socket.send(full_msg); try socket.send(full_msg);
return;
}
if (flags.connect != null) {
const dest = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
const src = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
// udp dest port should not be 8888 after first
const udp_dest_port = rand.intRangeAtMost(u16, 1025, std.math.maxInt(u16));
const connection: SaprusMessage = .{
.connection = .{
.src = src,
.dest = dest,
.seq = undefined,
.id = undefined,
.payload = flags.connect.?,
},
};
_ = udp_dest_port;
var connection_buf: [2048]u8 = undefined;
const connection_bytes = connection.toBytes(&connection_buf);
std.debug.print("connection: {any}\n", .{connection_bytes});
headers.setPayloadLen(connection_bytes.len);
const full_msg = blk: {
var msg_buf: [2048]u8 = undefined;
var msg_w: Writer = .fixed(&msg_buf);
msg_w.writeAll(&headers.toBytes()) catch unreachable;
msg_w.writeAll(connection_bytes) catch unreachable;
break :blk msg_w.buffered();
};
std.debug.print("full message = {any}\n", .{full_msg});
var socket: RawSocket = try .init("enp7s0");
defer socket.deinit();
try socket.send(full_msg);
return;
}
unreachable;
} }
fn parseDest(in: ?[]const u8) [4]u8 { fn parseDest(in: ?[]const u8) [4]u8 {

View File

@@ -123,7 +123,7 @@ const Relay = struct {
pub fn toBytes(self: Relay, buf: []u8) []u8 { pub fn toBytes(self: Relay, buf: []u8) []u8 {
var out: Writer = .fixed(buf); var out: Writer = .fixed(buf);
out.writeInt(u16, @intFromEnum(PacketType.relay), .big) catch unreachable; out.writeInt(u16, @intFromEnum(PacketType.relay), .big) catch unreachable;
out.writeInt(u16, @intCast(self.payload.len), .big) catch unreachable; // Length field, but unread. Will switch to checksum out.writeInt(u16, @intCast(self.payload.len + 4), .big) catch unreachable; // Length field, but unread. Will switch to checksum
out.writeAll(&self.dest.bytes) catch unreachable; out.writeAll(&self.dest.bytes) catch unreachable;
out.writeAll(self.payload) catch unreachable; out.writeAll(self.payload) catch unreachable;
return out.buffered(); return out.buffered();
@@ -177,7 +177,7 @@ const Connection = struct {
pub fn toBytes(self: Connection, buf: []u8) []u8 { pub fn toBytes(self: Connection, buf: []u8) []u8 {
var out: Writer = .fixed(buf); var out: Writer = .fixed(buf);
out.writeInt(u16, @intFromEnum(PacketType.connection), .big) catch unreachable; out.writeInt(u16, @intFromEnum(PacketType.connection), .big) catch unreachable;
out.writeInt(u16, undefined, .big) catch unreachable; // Saprus length field, unread. out.writeInt(u16, @intCast(self.payload.len + 14), .big) catch unreachable; // Saprus length field, unread.
out.writeInt(u16, self.src, .big) catch unreachable; out.writeInt(u16, self.src, .big) catch unreachable;
out.writeInt(u16, self.dest, .big) catch unreachable; out.writeInt(u16, self.dest, .big) catch unreachable;
out.writeInt(u32, self.seq, .big) catch unreachable; out.writeInt(u32, self.seq, .big) catch unreachable;