L
lancer6238
Hi,
I'm trying to write a structure to represent the header of a GRE
packet, then access each component in the header. However, I have
problems getting the right values for the bit fields.
Here's what I have:
struct grehdrv0
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int version:3;
unsigned int flags:5;
unsigned int recur:3;
unsigned int s:1;
unsigned int S:1;
unsigned int K:1;
unsigned int R:1;
unsigned int C:1;
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int C:1;
unsigned int R:1;
unsigned int K:1;
unsigned int S:1;
unsigned int s:1;
unsigned int recur:3;
unsigned int flags:5;
unsigned int version:3;
#else
#error "Please fix <bits/endian.h>
u_int16_t protocol;
/* Optional fields */
}
I'm trying to access the bit fields using:
struct grehdrv0 *gre;
....
// gre now points to the gre header portion of the packet
printf("%d %d %d %d %d %d %d %d %x\n", gre->C, gre->R, gre->K, gre->S,
gre->s, gre->recur, gre->flags, gre->version, ntohs(gre->protocol));
The first 16 bits should be (in binary) 0011 0000 1000 0001. However,
I am unable to get the correct values for the first 16 bits of the
header (I got in decimal 1 0 0 0 0 1 6 0), but gre->protocol gives me
the correct value.
How do I access the bit fields?
Thank you.
I'm trying to write a structure to represent the header of a GRE
packet, then access each component in the header. However, I have
problems getting the right values for the bit fields.
Here's what I have:
struct grehdrv0
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int version:3;
unsigned int flags:5;
unsigned int recur:3;
unsigned int s:1;
unsigned int S:1;
unsigned int K:1;
unsigned int R:1;
unsigned int C:1;
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int C:1;
unsigned int R:1;
unsigned int K:1;
unsigned int S:1;
unsigned int s:1;
unsigned int recur:3;
unsigned int flags:5;
unsigned int version:3;
#else
#error "Please fix <bits/endian.h>
u_int16_t protocol;
/* Optional fields */
}
I'm trying to access the bit fields using:
struct grehdrv0 *gre;
....
// gre now points to the gre header portion of the packet
printf("%d %d %d %d %d %d %d %d %x\n", gre->C, gre->R, gre->K, gre->S,
gre->s, gre->recur, gre->flags, gre->version, ntohs(gre->protocol));
The first 16 bits should be (in binary) 0011 0000 1000 0001. However,
I am unable to get the correct values for the first 16 bits of the
header (I got in decimal 1 0 0 0 0 1 6 0), but gre->protocol gives me
the correct value.
How do I access the bit fields?
Thank you.