Endianness puzzle

Eric S. Raymond esr at thyrsus.com
Tue Dec 13 02:06:48 UTC 2016


Hal Murray <hmurray at megapathdsl.net>:
> Disclaimer: I could easily get something backwards.  I've worked with both 
> and can easily switch even when I shouldn't.
> 
> 
> > +#define LAST32MASK	0x00000000ffffffffUL
> > +#define FIRST32MASK	0xffffffff00000000UL
> 
> FIRST and LAST seem like poor choices for words.  Do you mean first on the 
> wire?  Network protocols are almost always little endian.
> 
> In any case, those masks are not within an ifdef so they will be used in both 
> cases.
> 
> If you want the low or high bits, the masks and macros don't depend on the 
> endianness if you are extracting from a uint64_t.  You may need to invent a 
> new host2net type macro to byte swap an 8 byte chunk if you want to 
> send/receive those chunks from the network.

I found the problem a few hours ago.

There only needs to be one set of those macros, and it's the one that
I was thinking of as big-endian.  That's because at the point at which
the data is in a uint64_t it's no longer in wire format; it has
already been word-swapped to host order.

Elementary, I know - but these things are easy for even the
experienced to get backwards, as you just demonstrated.  Most network
protocols are *big-endian*, and NTP's are not an exception.
-- 
		<a href="http://www.catb.org/~esr/">Eric S. Raymond</a>


More information about the devel mailing list