the following is the IP structure from library on a linux machine
struct ip
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int ip_hl:4; /* header length */
unsigned int ip_v:4; /* version */
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
unsigned int ip_v:4; /* version */
unsigned int ip_hl:4; /* header length */
#endif
u_int8_t ip_tos; /* type of service */
u_short ip_len; /* total length */
u_short ip_id; /* identification */
u_short ip_off; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
u_int8_t ip_ttl; /* time to live */
u_int8_t ip_p; /* protocol */
u_short ip_sum; /* checksum */
struct in_addr ip_src, ip_dst; /* source and dest address */
};
for these lines:
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int ip_hl:4; /* header length */
unsigned int ip_v:4; /* version */
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
unsigned int ip_v:4; /* version */
unsigned int ip_hl:4; /* header length */
#endif
why endianess matters inside a byte? I think endianess only affect multi-byte integers, but here it seems to me that endianess also affect bits arrangement inside a byte?
besides, it is only a byte, why it is unsigned int
, which is 4 bytes.
I notice in wireshark, ip_v and ip_hl is shown as 0x45
If I capture an IP packet. The first byte is composed of ip_v and ip_hl
I put it into a character variable x
then what is the result of x & 0b11110000
? is it always 4 no matther what endianess, or it may be 5?
There is byte ordering which is relevant in case of multiple byte data. But in your case what matters is bit field ordering
, which deals with the order of bits in case of a single byte data. There are no rules put forward by the C standard regarding bit field ordering. It is implementation dependent and decided by the compiler.
The size of the variable is not 4 bytes. It is just 4 bits. They are not independent variables. They are bit fields inside a structure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With