summaryrefslogtreecommitdiff
path: root/src/EthIpUdp.zig
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2026-01-20 22:41:53 -0500
committerRobby Zambito <contact@robbyzambito.me>2026-01-21 22:25:20 -0500
commit213a01afc8407749e31a350c52a5809b5394a918 (patch)
tree05cdff41612355ef6da4e534c4c84a0988817923 /src/EthIpUdp.zig
parent067a11ab232e03ad8d368beccb58b1e64ec96829 (diff)
Implemented client and connectionHEADmaster
Diffstat (limited to 'src/EthIpUdp.zig')
-rw-r--r--src/EthIpUdp.zig57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/EthIpUdp.zig b/src/EthIpUdp.zig
new file mode 100644
index 0000000..472e421
--- /dev/null
+++ b/src/EthIpUdp.zig
@@ -0,0 +1,57 @@
+pub const EthIpUdp = packed struct(u336) { // 42 bytes * 8 bits = 336
+ // --- UDP (Last in memory, defined first for LSB->MSB) ---
+ udp: packed struct {
+ checksum: u16 = 0,
+ len: u16,
+ dst_port: u16,
+ src_port: u16,
+ },
+
+ // --- IP ---
+ ip: packed struct {
+ dst_addr: u32,
+ src_addr: u32,
+ header_checksum: u16 = 0,
+ protocol: u8 = 17, // udp
+ ttl: u8 = 0x40,
+
+ // fragment_offset (13 bits) + flags (3 bits) = 16 bits
+ // In Big Endian, flags are the high bits of the first byte.
+ // To have flags appear first in the stream, define them last here.
+ fragment_offset: u13 = 0,
+ flags: packed struct(u3) {
+ reserved: u1 = 0,
+ dont_fragment: u1 = 1,
+ more_fragments: u1 = 0,
+ } = .{},
+
+ id: u16,
+ len: u16,
+ tos: u8 = undefined,
+
+ // ip_version (4 bits) + ihl (4 bits) = 8 bits
+ // To have version appear first (high nibble), define it last.
+ ihl: u4 = 5,
+ ip_version: u4 = 4,
+ },
+
+ // --- Ethernet ---
+ eth_type: u16 = std.os.linux.ETH.P.IP,
+ src_mac: @Vector(6, u8),
+ dst_mac: @Vector(6, u8) = @splat(0xff),
+
+ pub fn toBytes(self: @This()) [336 / 8]u8 {
+ var res: [336 / 8]u8 = undefined;
+ var w: Writer = .fixed(&res);
+ w.writeStruct(self, .big) catch unreachable;
+ return res;
+ }
+
+ pub fn setPayloadLen(self: *@This(), len: usize) void {
+ self.ip.len = @intCast(len + (@bitSizeOf(@TypeOf(self.udp)) / 8) + (@bitSizeOf(@TypeOf(self.ip)) / 8));
+ self.udp.len = @intCast(len + (@bitSizeOf(@TypeOf(self.udp)) / 8));
+ }
+};
+
+const std = @import("std");
+const Writer = std.Io.Writer;