construct full message

This commit is contained in:
2026-01-19 12:27:24 -05:00
parent 9947c21b4c
commit d7dedd243e

View File

@@ -99,12 +99,15 @@ pub fn main(init: std.process.Init) !void {
// 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()});
const EthIpUdp = packed struct(u336) { // 42 bytes * 8 bits = 336 const EthIpUdp = packed struct(u336) { // 42 bytes * 8 bits = 336
// --- UDP (Last in memory, defined first for LSB->MSB) --- // --- UDP (Last in memory, defined first for LSB->MSB) ---
udp: packed struct {
checksum: u16 = 0, checksum: u16 = 0,
udp_len: u16, len: u16,
dst_port: u16, dst_port: u16,
src_port: u16, src_port: u16,
},
// --- IP --- // --- IP ---
ip: packed struct {
dst_addr: u32, dst_addr: u32,
src_addr: u32, src_addr: u32,
header_checksum: u16 = 0, header_checksum: u16 = 0,
@@ -122,13 +125,14 @@ pub fn main(init: std.process.Init) !void {
} = .{}, } = .{},
id: u16, id: u16,
total_length: u16, len: u16,
tos: u8 = undefined, tos: u8 = undefined,
// ip_version (4 bits) + ihl (4 bits) = 8 bits // ip_version (4 bits) + ihl (4 bits) = 8 bits
// To have version appear first (high nibble), define it last. // To have version appear first (high nibble), define it last.
ihl: u4 = 5, ihl: u4 = 5,
ip_version: u4 = 4, ip_version: u4 = 4,
},
// --- Ethernet --- // --- Ethernet ---
eth_type: u16 = std.os.linux.ETH.P.IP, eth_type: u16 = std.os.linux.ETH.P.IP,
@@ -141,18 +145,26 @@ pub fn main(init: std.process.Init) !void {
w.writeStruct(self, .big) catch unreachable; w.writeStruct(self, .big) catch unreachable;
return res; return res;
} }
fn setPayloadLen(self: *@This(), len: usize) void {
self.ip.len = @intCast(len + @sizeOf(@TypeOf(self.udp)) + @sizeOf(@TypeOf(self.ip)));
self.udp.len = @intCast(len + @sizeOf(@TypeOf(self.udp)));
}
}; };
const headers: EthIpUdp = .{ var headers: EthIpUdp = .{
.src_mac = @splat(0x0e), .src_mac = @splat(0x0e),
.ip = .{
.id = 0, .id = 0,
.src_addr = 0, .src_addr = 0,
.dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }), .dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }),
.len = undefined,
},
.udp = .{
.src_port = undefined, // TODO: change this? .src_port = undefined, // TODO: change this?
.dst_port = 8888, .dst_port = 8888,
.len = undefined,
.total_length = undefined, },
.udp_len = undefined,
}; };
std.debug.print("headers: {any}\n", .{&headers.toBytes()}); std.debug.print("headers: {any}\n", .{&headers.toBytes()});
@@ -163,8 +175,20 @@ pub fn main(init: std.process.Init) !void {
}, },
}; };
var relay_bytes: [2048]u8 = undefined; var relay_buf: [2048]u8 = undefined;
std.debug.print("payload: {any}\n", .{relay.toBytes(&relay_bytes)}); const relay_bytes = relay.toBytes(&relay_buf);
std.debug.print("payload: {any}\n", .{relay_bytes});
headers.setPayloadLen(relay_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(relay_bytes) catch unreachable;
break :blk msg_w.buffered();
};
std.debug.print("full message = {any}\n", .{full_msg});
} }
fn parseDest(in: ?[]const u8) [4]u8 { fn parseDest(in: ?[]const u8) [4]u8 {