aboutsummaryrefslogtreecommitdiff
path: root/src/EthIpUdp.zig
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2026-04-15 21:02:32 -0400
committerTangled <noreply@tangled.org>2026-04-16 04:56:23 +0300
commit91fdb2c6f8d90032f233a0ea65d4b8f4b8d89321 (patch)
treeef06c43760fcac9d18b8ab7aa3dd61aa0ed94796 /src/EthIpUdp.zig
parent7077aae9ceedc48fd5329f204163f1fbdd9d0ba8 (diff)
refactor: use packed struct for IpAddrdev
This is more intuitive than using u32 directly, and follows the same pattern as the new MAC addr handling
Diffstat (limited to 'src/EthIpUdp.zig')
-rw-r--r--src/EthIpUdp.zig39
1 files changed, 25 insertions, 14 deletions
diff --git a/src/EthIpUdp.zig b/src/EthIpUdp.zig
index b286db2..a65a96f 100644
--- a/src/EthIpUdp.zig
+++ b/src/EthIpUdp.zig
@@ -14,6 +14,28 @@
// You should have received a copy of the GNU General Public License along with
// Zaprus. If not, see <https://www.gnu.org/licenses/>.
+pub const IpAddr = packed struct {
+ int: I,
+
+ const V = @Vector(4, u8);
+ const I = u32;
+
+ pub fn fromBytes(s: V) IpAddr {
+ return .{ .int = @bitCast(s) };
+ }
+};
+
+pub const MacAddr = packed struct {
+ int: I,
+
+ const V = @Vector(6, u8);
+ const I = @Int(.unsigned, @bitSizeOf(V));
+
+ pub fn fromBytes(s: V) MacAddr {
+ return .{ .int = @bitCast(s) };
+ }
+};
+
pub const EthIpUdp = packed struct(u336) { // 42 bytes * 8 bits = 336
// --- UDP (Last in memory, defined first for LSB->MSB) ---
udp: packed struct {
@@ -25,8 +47,8 @@ pub const EthIpUdp = packed struct(u336) { // 42 bytes * 8 bits = 336
// --- IP ---
ip: packed struct {
- dst_addr: u32,
- src_addr: u32,
+ dst_addr: IpAddr,
+ src_addr: IpAddr,
header_checksum: u16 = 0,
protocol: u8 = 17, // udp
ttl: u8 = 0x40,
@@ -54,18 +76,7 @@ pub const EthIpUdp = packed struct(u336) { // 42 bytes * 8 bits = 336
// --- Ethernet ---
eth_type: u16 = std.os.linux.ETH.P.IP,
src_mac: MacAddr,
- dst_mac: MacAddr = .fromSlice(@splat(0xff)),
-
- pub const MacAddr = packed struct {
- int: I,
-
- pub const V = @Vector(6, u8);
- pub const I = @Int(.unsigned, @bitSizeOf(V));
-
- pub fn fromSlice(s: V) MacAddr {
- return .{ .int = @bitCast(s) };
- }
- };
+ dst_mac: MacAddr = .fromBytes(@splat(0xff)),
pub fn toBytes(self: @This()) [336 / 8]u8 {
var res: [336 / 8]u8 = undefined;