I’m writing some C code to parse IEEE 802.11 frames, but I’m stuck trying to create a new variable whose length depends on the size of the frame itself.
Here’s the code I currently have:
int frame_body_len = pkt_hdr->len - radio_hdr->len - wifi_hdr_len - 4;
u_char *frame_body = (u_char *) (packet + radio_hdr->len + wifi_hdr_len);
Basically, the frame consists of a header, a body, and a checksum at the end. I can calculate the length of the frame body by taking the length of the packet and subtracting the length of the two headers that appear before it (radio_hdr->len and wifi_hdr_len respectively), plus 4 bytes at the end for the checksum.
However, how can I create the frame_body variable without the trailing checksum? Right now, I’m initializing it with the contents of the packet starting at the position after the two headers, but is there some way to start at that position and end 4 bytes before the end of packet? packet is a pointer to a u_char, if it helps.
I’m a new C programmer, so any and all advice about my code you can give me would be much appreciated. Thanks!
Your
frame_bodyis just a pointer to the start of the frame, pointing into an existing buffer where the data is.That’s fine, you can just pass that around together with your
frame_body_lenand have everyone that cares about the data only inspect the data starting fromframe_bodyand don’t care about anything beyondframe_body_len– which is what you’ll have to do anyhow.Thus, you don’t really need to have a
frame_bodythat doesn’t include the trailing checksum.If you do need such a buffer, e.g. dynamically allocated, you’ll have to make space for the data, and copy the data there: