how is the byteorder stuff supposed to work?

Christoph Badura bad at bsd.de
Wed Mar 14 13:49:42 EDT 2007


Hey Carter,

while looking for potential byte order issues I found the following code
in include/compat.h:

#if defined(CYGWIN)
#define _LITTLE_ENDIAN
#define USE_IPV6
#else

#if (__FreeBSD__)
#if defined(BYTE_ORDER)
#define __BYTE_ORDER    BYTE_ORDER
#endif
#if defined(LITTLE_ENDIAN)
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#endif
#if defined(BIG_ENDIAN)
#define __BIG_ENDIAN    BIG_ENDIAN
#endif
#endif

#if !defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define _LITTLE_ENDIAN
#else
#define _BIG_ENDIAN
#endif
#endif
#endif

and later you have a lot of places that check for "#ifdef _LITTLE_ENDIAN"
all over the source code.

I don't understand how that is supposed to work.

First there is the problem of you re-defining symbols in the implementation
namespace that you aren't supposed to touch (the ones with a single underscore
in front).

Second both NetBSD (what I am using) and FreeBSD *always* define _LITTLE_ENDIAN
and _BIG_ENDIAN in system header files (sys/endian.h and machine/endian.h, but
pulling in sys/types.h or arpa/inet.h does the job).

So, _LITTLE_ENDIAN is always defined on those machines, even if they are
big-endian machines, like e.g. NetBSD/macppc or FreeBSD/sparc. So, later
testing "#ifdef _LITTLE_ENDIAN" is going to give the wrong answer.

Maybe you want to use ARGUS_LITTLE_ENDIAN and ARGUS_BIG_ENDIAN and change
the above code like so:

#if defined(CYGWIN)
#define ARGUS_LITTLE_ENDIAN
#define USE_IPV6
#else

#if defined(__FreeBSD__) || defined(__NetBSD__)
#ifndef _BYTE_ORDER
#error can't determine machine byte order
#endif
#define __BYTE_ORDER    _BYTE_ORDER
#define __LITTLE_ENDIAN _LITTLE_ENDIAN
#define __BIG_ENDIAN    _BIG_ENDIAN
#endif

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define ARGUS_LITTLE_ENDIAN
#else
#define ARGUS_BIG_ENDIAN
#endif

and change all the uses of "#ifdef _LITTLE_ENDIAN" accordingly.

--chris



More information about the argus mailing list