Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!ucbvax!alias.com!mandrews From: mandrews@alias.com (Mark Andrews) Newsgroups: comp.protocols.tcp-ip Subject: Byte and bit order within packet headers Message-ID: <9104241753.AA21589@dino.alias.com> Date: 24 Apr 91 17:53:59 GMT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The Internet Lines: 101 I have a question concerning concering the byte and bit order of fields within packet headers. Many of the RFCS (including RFC1060) state rules about the byte (octet) order: Data Notations The convention in the documentation of Internet Protocols is to express numbers in decimal and to picture data in "big-endian" order [21]. That is, fields are described left to right, with the most significant octet on the left and the least significant octet on the right. The order of transmission of the header and data described in this document is resolved to the octet level. Whenever a diagram shows a group of octets, the order of transmission of those octets is the normal order in which they are read in English. For example, in the following diagram the octets are transmitted in the order they are numbered. 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 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 1 | 2 | 3 | 4 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 5 | 6 | 7 | 8 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 9 | 10 | 11 | 12 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Transmission Order of Bytes So, all multi-octet fields are transmitted in big-endian byte order. This is a small problem for little endian machines (most significant byte is on the right). They must correct their byte order before the packet is transmitted. In BSD systems, this job is performed by the htonl() and htons() functions (host to network long, host to network short), but what about the bit order? It is still little-endian (bits are numbered right to left instead of left to right)! What order is are the bits transmitted in? This is further complicated by the following code fragment from /usr/include/netinet/ip.h: struct ip { #if BYTE_ORDER == LITTLE_ENDIAN u_char ip_hl:4, /* header length */ ip_v:4; /* version */ #endif #if BYTE_ORDER == BIG_ENDIAN u_char ip_v:4, /* version */ ip_hl:4; /* header length */ #endif When all this is translated, there are two views of the first byte of the ip structure; one little endian: 7 0 +------+-------+ | ip_v | ip_hl | +------+-------+ and one big endian: 0 7 +------+-------+ | ip_v | ip_hl | +------+-------+ Now according to the 4.3BSD C Reference Manual by B. Kernighan, the addresses of a structure increase as the declarations are read left to right (irrelevant of the bit or byte order), so in terms of a C addressing model, the first byte of the ip structure is: 0 7 +------+-------+----- | ip_v | ip_hl | other elements of ip structure +------+-------+----- Perhaps the bits are transmitted MSB to LSB based on the C model. I don't know. Any clarifications on my confusion or any other help would be appreciated. Thanks, Mark ------------------------------------------------------------------------------ Mark Andrews Systems Programmer, Alias Research, Toronto, Canada Phone: (416)-362-9181 Mail box: mark@alias.com