OSPF endianess fix

Rani Assaf rani at paname.org
Sat Sep 6 16:23:51 CEST 2003


Hello,

On Sat, Sep 06, 2003 at 11:44:24AM +0200, Martin Mares wrote:
> | +#ifdef CPU_LITTLE_ENDIAN
> |    u8 ms:1;
> |    u8 m:1;
> |    u8 i:1;
> |    u8 padding:5;
> | +#else
> | +  u8 padding:5;
> | +  u8 i:1;
> | +  u8 m:1;
> | +  u8 ms:1;
> | +#endif
> |  };
> 
> I don't really understand this piece -- either it's a piece of packet as seen
> on the wire and then it must not depend on CPU endianity or it's already

It's a  piece of  packet coming on  the wire... The  problem is  that bit
ordering in  a bit  structure depends  on the endianess  of the  host (at
least for GCC):

Please try  the following program  on x86 and  PPC (with and  without the
"#ifdef BIG_ENDIAN"):

#include <stdio.h>

union mes_bits {
	struct {
#ifdef BIG_ENDIAN
		unsigned char toto:2;
		unsigned char titi:1;
		unsigned char tata:5;
#else
		unsigned char tata:5;
		unsigned char titi:1;
		unsigned char toto:2;
#endif
	};
	unsigned char data;
};

int main(void)
{
	union mes_bits bits;

	 bits.toto = 1;
	 bits.titi = 0;
	 bits.tata = 2;

	 printf("bits.data: 0x%.2x\n", bits.data);

	 return 0;
}


Regards,
Rani



More information about the Bird-users mailing list