User`s guide

14
K
A
DAK
KwikNet Overview
1.4 Byte Ordering and Endianness
To use a TCP/IP stack, you must adhere to the byte ordering rules defined by the TCP/IP
protocol suite. Doing so is complicated by the fact that not all target processors follow
these rules.
The TCP/IP network uses the big endian model for byte ordering. Sequential bytes in the
data stream always appear in sequential bytes in memory. The initial byte of the data
stream is at the lowest memory address and the final byte is at the highest memory
address. The network over which the transfer takes place is said to be big endian.
If a multi-byte value appears within the TCP/IP data stream, the most significant byte of
that data value will always appear first in the stream. When stored in memory, the most
significant byte of a multi-byte value will always appear at the lower memory address
with successive bytes of the value stored at sequential, higher memory addresses. Data
which resides in memory in this fashion is said to be in net endian form.
When TCP/IP is used on a processor such as the Motorola 68xxx, the network endianness
matches the processor endianness. Both are big endian. Consequently, the natural
storage of multi-byte values in memory matches that required by the TCP/IP data stream.
However, when TCP/IP is used on a processor such as the Intel 80x86, the network
endianness conflicts with the processor endianness. The network requires big endian
values in the data stream but the processor’s natural storage of multi-byte values in
memory is little endian.
Fortunately the KwikNet TCP/IP Stack can be used on target processors which are either
big or little endian. The endianness of the target processor is a configuration parameter in
the KwikNet Network Parameter File used in the construction of the KwikNet Library as
described in Chapter 2.3.
Although KwikNet may be used with either big or little endian processors, it does not
alleviate your application from the responsibility for correct presentation or interpretation
of data delivered over the TCP/IP network. KwikNet treats your application data as a byte
stream with no particular endian characteristics. It is up to your application to present the
data for delivery to a remote destination in a byte ordered format that the remote end can
understand.
For example, two little endian machines can send and receive data streams containing
multi-byte values ordered in little endian form. The multi-byte values can be directly
fetched from or stored into memory. However, if one of these machines is replaced by a
big endian machine, suddenly the application will fail even though
KwikNet will continue
to successfully deliver the data streams between the two machines. Of course, the newer
big endian machine could be reprogrammed to properly store and retrieve the little endian
values expected by the other machine.
This example illustrates the absolute need for applications to agree upon the manner in
which data values will be delivered to each other. Conventional wisdom suggests that if
multi-byte values are always stored in net endian form, then any machine can participate
in the conversation, regardless of the machine’s endianness.