这是linux对IP头的定义 /usr/include/linux/ip.h 或 linux/include/linux/ip.h)
版本号和首部长度是同一个字节的,这也要区分大端小端吗?我一直以为大端小端是字节间顺序的问题,不是字节内部位顺序的问题。网络数据发送时是字节流还是位流?发送时uint16_t和uint32_t的高字节必需先发送,那么同一字节的高位先发送还是低位?我找不到gcc讲结构位定义的文档,有链接么?
可以这样来解释,
MSB most significant bits
LSB least significant bits
一句话:对于 little-endian 来说 MSB 在高地址,对 big-endian 来说 MSB 在低地址。
Here is how we would write the integer 0x0a0b0c0d for both big endian and little endian systems, according to the rule above:
Write Integer for Big Endian System
byte addr 0 1 2 3
bit offset 01234567 01234567 01234567 01234567
binary 00001010 00001011 00001100 00001101
hex 0a 0b 0c 0d
Write Integer for Little Endian System
byte addr 3 2 1 0
bit offset 76543210 76543210 76543210 76543210
binary 00001010 00001011 00001100 00001101
hex 0a 0b 0c 0d
In both cases above, we can read from left to right and the number is 0x0a0b0c0d.
在小字节序机器上跑测试例1:
测试例2:
原帖地址:http://www.unixresources.net/linux/clf/program/archive/00/00/64/28/642822.html
亦可参考:http://bbs.chinaunix.net/viewthread.php?tid=823662&extra=&page=1
http://www.unixresources.net/linux/clf/linuxK/archive/00/00/63/86/638637.html
Endianness of CPU
The CPU endianness is the byte and bit order in which it interprets multi-byte integers from on-chip registers, local bus, in-line cache, memory and so on.
Little endian CPUs include Intel and DEC. Big endian CPUs include Motorola 680x0, Sun Sparc and IBM (e.g., PowerPC). MIPs and ARM can be configured either way.
Endianness of Ethernet
Ethernet is big endian. This means the most significant byte of an integer field is placed at a lower wire byte address and transmitted/received in front of the least significant byte.
Endianness of IP
IP's byte order also is big endian. The bit endianness of IP inherits that of the CPU, and the NIC takes care of converting it from/to the bit transmission/reception order on the wire.