tcpdump mailing list archives

Re: Byte Order


From: Guy Harris <guy () alum mit edu>
Date: Wed, 30 Jul 2003 23:25:15 -0700

On Wed, Jul 30, 2003 at 02:28:41AM -0700, Muhammad Farooq-i-Azam wrote:
I think I can define a structure as below for the
fixed part of the header:

typedef struct header_ripv1 {
   u_int8_t   command;
   u_int8_t   version;
   u_int16_t  zeros;
} header_ripv1_t;

My question is can I use the above structure for both
little endian and big endian systems with the only
conversion required below:

header_ripv1_t ripv1;

ripv1.zeros = ntohs(ripv1.zeros);

Yes.

Or do I have to do something like this (which I
*doubt* has a double error, and will result in
problems on a big endian system):

typedef struct header_ripv1{
#if defined (WORDS_BIGENDIAN)
  u_int16_t   zeros;
  u_int8_t    version;
  u_int8_t    command;
#else
  u_int8_t    command;
  u_int8_t    version;
  u_int16_t   zeros;
#endif
} header_ripv1_t;

No.  The byte-order issue is only an issue of the order of the bytes
within a multi-byte numerical quantity; it isn't an issue of the order
of the bytes within a structure.  ANSI C, as I remember, specifies that,
with a structure like

        typedef struct header_ripv1 {
           u_int8_t   command;
           u_int8_t   version;
           u_int16_t  zeros;
        } header_ripv1_t;

"command" is in the first location, followed by "version", followed by
"zeroes".
-
This is the TCPDUMP workers list. It is archived at
http://www.tcpdump.org/lists/workers/index.html
To unsubscribe use mailto:tcpdump-workers-request () tcpdump org?body=unsubscribe


Current thread: