Let's assume the following:
I'd like to create a structure for UDP packets. Each frame usually consists of an Ethernet Header, an IP header, a UDP header and an optional payload followed by, finally, the FCS (Frame Checksum Sequence).
The payload length is unknown/flexible.
Meaning that when creating the struct, the payload would have to be the last member of it (flexible array member). Consequently, there's no place for the FCS.
So I thought about what possibilities would remain.
I came up with the following piece of code:
#define UDP_PKT(name, payload_length) struct __attribute((__packed__)) \
{ \
struct ether_header eth; \
struct ip iph; \
struct udphdr udph; \
unsigned char payload[payload_length]; \
u_int32_t fcs; \
} name;
As this is not allowed:
struct __attribute__((__packed__)) udp_packet
{
struct ether_header eth;
struct ip iph;
struct udphdr udph;
unsigned char payload[]; // fam, must always be the last member
u_int32_t fcs;
};
My question: Is that the only possibility I have to include the FCS in the structure without having a fixed array (payload) size?
If so, is that a good solution? Is that considered good practice?