tcpdump mailing list archives
Byte Order
From: Muhammad Farooq-i-Azam <farooq_a_azam () yahoo com>
Date: Wed, 30 Jul 2003 02:28:41 -0700 (PDT)
Well, this question relates to the byte order. I have searched the archives of this list but still feel like in need of some enlightenment. Please let me explain what I understand about the byte order and then my question at the end. 1- In the case of little endian format, least significant byte is stored at the least significant position (or lower address position) and reverse is true for the big endian format. Same is true for bit positions within a byte but we are not concerned about it as our compiler takes care of this. 2- Conversion from network(big endian) to host byte order is required only when a data item is stored in more than one byte. For items in which each byte is treated individually, we need not bother about the byte order. For example, in the case of character strings, where each character occupies a single byte, we do not need the conversion. The Question Part: I have this RIPv1 header before me as below: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 ~ 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-~-+-~-+-+-+ | command (1)| version (1) | must be zero (2) | +-------------+---------------+---------+---~-----+ | | ~ RIP Entry (20) ~ | | +-------------+---------------+---------+---~-----+ 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); 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; Not a libpcap specific question, but I hope that I will not be disappointed. Much thanks, Muhammad Farooq-i-Azam __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free, easy-to-use web site design software http://sitebuilder.yahoo.com - 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:
- Byte Order Muhammad Farooq-i-Azam (Jul 30)
- Re: Byte Order Guy Harris (Jul 31)