Creating a new variable in C from only part of an existing u_char

Posted by Alex Kloss on Stack Overflow See other posts from Stack Overflow or by Alex Kloss
Published on 2010-06-15T19:21:43Z Indexed on 2010/06/15 19:32 UTC
Read the original article Hit count: 163

Filed under:

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!

© Stack Overflow or respective owner

Related posts about c