字节序

这是linux对IP头的定义 /usr/include/linux/ip.h 或 linux/include/linux/ip.h)

  1. struct iphdr { 
  2. #if __BYTE_ORDER == __LITTLE_ENDIAN 
  3. uint8_t ihl:4, 
  4. version:4; 
  5. #elif __BYTE_ORDER == __BIG_ENDIAN 
  6. uint8_t version:4, 
  7. ihl:4; 
  8. #endif 
  9. uint8_t tos; 
  10. uint16_t tot_len; 
  11. uint16_t id; 
  12. uint16_t frag_off; 
  13. uint8_t ttl; 
  14. uint8_t protocol; 
  15. uint16_t check; 
  16. uint32_t saddr; 
  17. uint32_t daddr; 
  18. /*The options start here. */ 
  19. }; 

版本号和首部长度是同一个字节的,这也要区分大端小端吗?我一直以为大端小端是字节间顺序的问题,不是字节内部位顺序的问题。网络数据发送时是字节流还是位流?发送时uint16_t和uint32_t的高字节必需先发送,那么同一字节的高位先发送还是低位?我找不到gcc讲结构位定义的文档,有链接么?

可以这样来解释,
1)从道理上来说,little endian中的位应该这样排列:
01234567
即排在前面的是低位。因此,先分配least significant bits
2)而在Big endian中,位应该这样排列:
76543210
即排在前面的是高位。因此,先分配most significant bits。

可以这样来理解,
1)在Big Endian的情况下,"排在前面的是高位"
a. 对于顺序的两个字节来说,第一个字节是高位(排在前面),第二个字节是低位(排在后面)。
b. 对于字节内部的位来说,
-------most significant bits排在前面,是高位,
-------least significant bits排在后面,是低位。
2)在Little Endian的情况下,"排在前面的是低位"
a. 对于顺序的两个字节来说,第一个字节是低位(排在前面),第二个字节是高位(排在后面)。
b. 对于字节内部的位来说,
-------least significant bits排在前面,是低位,
-------most significant bits排在后面,是高位。

这样,在对struct中的成员进行分配的时候,"按排列顺序分配,先分配排在前面的"
1)big endian从高位向低位分配,
a. 对字节,是先分配低地址的字节,再分配高地址的字节。
b. 对位域,先分配most significant bits,再分配least significant bits。
1)little endian从低位向高位分配,
a. 对字节,是先分配低地址的字节,再分配高地址的字节。
b. 对位域,先分配least significant bits,再分配most significant bits。

======================================

以上说的都是分配的顺序。

对于IP协议来说,
1)IP's byte order is big endian.
2)The bit endianness of IP inherits that of the CPU,
3)and the NIC takes care of converting it from/to the bit transmission/reception order on the wire.

并且,按照IP协议,
1)"version" is the most significant four bits of the first byte of an IP header.
2)"ihl" is the least significant four bits of the first byte of the IP header.

也就是说,version必须分配在most significant four bits,
按照上面说的分配顺序,在big endian中,version必须放在前面。

 

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:

  1. int value = 0x12345678;
  2.         union ValueT
  3.         {
  4.             int value;
  5.             char data[4];
  6.         } a;
  7.         a.value = 0x12345678;
  8.         printf("value is 0x%x/n", a.value);
  9.         printf("address is %p, 0x%x/n",&a.data[0], a.data[0]);
  10.         printf("address is %p, 0x%x/n",&a.data[1], a.data[1]);
  11.         printf("address is %p, 0x%x/n",&a.data[2],  a.data[2]);
  12.         printf("address is %p, 0x%x/n",&a.data[3], a.data[3]);
  13.         //value is 0x12345678
  14.         //address is 0012FF6C, 0x78
  15.         //address is 0012FF6D, 0x56
  16.         //address is 0012FF6E, 0x34
  17.         //address is 0012FF6F, 0x12

测试例2:

  1. struct bitfield{ 
  2.             int ia:2; 
  3.             int ib:6; 
  4.         } field; 
  5.         field.ia=1; 
  6.         field.ib=4; 
  7.         char * c; 
  8.         c=(char *)&field; 
  9.         printf("%d/n",*c);
  10.         // 17 = 000100 01

原帖地址: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.

                  

你可能感兴趣的:(字节序)