I’m implementing a packet parser as a toy project to try and understand Zig better. As part of this I have headers of various sizes and at various offsets into the packet. I’m trying to figure out how I should be approaching this in Zig.
An example of the situation I currently have. In a Ethernet IPv4 packet the second u4 field contains the IHL, this describes how many 32 byte sections exist in the header of the IPv4 packet, after which the body (TCP / UDP etc) will be found.
If I have a pointer to the start of the IPv4 header, how do I gain a pointer to the start of the data section? I thought I’d be able to do this using pointer arithmetic, something along the lines of dataPtr = ipv4Ptr + (IHL * 4) where the ptr is a ptr to u8.
I keep getting compilation errors about adding comptime ints or u8 to *u8 so in the end have gone down the following route of converting to a C ptr, but I feel like I’m doing things very wrong and abusing the language. What’s the proper Zig way of achieving this?
For reference data is a *const u8 and ihl is a u4
const dataCPtr: [*c]const u8 = @ptrCast(data);
const ipDataPtr = dataCPtr + (@as(u8, ihl) * 4);
I did also briefly look at converting the pointer to an Int, doing the arithmetic and then converting back. I think I probably had other errors at the time so may be why I couldn’t get it working.