这是linux对IP头的定义 /usr/include/linux/ip.h 或 linux/include/linux/ip.h)
struct iphdr {
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint8_t ihl:4,
version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
uint8_t version:4,
ihl:4;
#endif
uint8_t tos;
uint16_t tot_len;
uint16_t id;
uint16_t frag_off;
uint8_t ttl;
uint8_t protocol;
uint16_t check;
uint32_t saddr;
uint32_t daddr;
/*The options start here. */
};
VxWorks 的target\h\netinet\Ip.h
/*
* Structure of an internet header, naked of options.
*
* We declare ip_len and ip_off to be short, rather than u_short
* pragmatically since otherwise unsigned comparisons can result
* against negative integers quite easily, and fail in subtle ways.
*/
struct ip {
#if _BYTE_ORDER == _LITTLE_ENDIAN
u_int ip_hl:4, /* header length */
ip_v:4, /* version */
#endif
#if _BYTE_ORDER == _BIG_ENDIAN
u_int ip_v:4, /* version */
ip_hl:4, /* header length */
#endif
ip_tos:8, /* type of service */
ip_len:16; /* total length */
u_short ip_id; /* identification */
short ip_off; /* fragment offset field */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
u_char ip_ttl; /* time to live */
u_char ip_p; /* protocol */
u_short ip_sum; /* checksum */
struct in_addr ip_src,ip_dst; /* source and dest address */
};
VC下的例子:
struct BitField
{
int first:3;
int second:2;
int third:3;
}field;
field.first = 4;
field.second = 2;
field.third = 2;
char *pChar = (char *)&field;
printf("bit domain test:%d\n", *pChar);
field:100 10 010
打印出 010 10 100
总结:在小端环境下,先分配 least significant 3 bits,再分配middle 2 bits,最后分配most significant 3 bits。
推测:iphdr 中version和ihl区分大端,小端 方便对他们的处理
现在有了字节序和位域序的概念,听说还有bit序待进一步了解
阅读(1641) | 评论(0) | 转发(0) |