Monday, June 8, 2009

Linux Network Code: Big Endian and Little Endian Snack

The TCP/IP protocol is "Big Endian"processing. How does a "little endian" processor read the packets sent from "Big Endian"?

Little Endian reads CPU register value (backwards or bottom-to-top)


Big Endian reads CPU register value (forwards or top-to-botom)

This means Byte Order "functions" are needed to switch (convert) between big endian and little endian.

Linux, BSD and Solaris use the following functions:

hton = host-to-network
ntoh = network-to-host

s = short = 16 bit integer
l = long = 32 bit integer
ll= double = 64 bit integer

Function Code Sample:

htons()
htonl()
htonll()

ntohs()
ntohl()
ntohll()


This means if the routing processor is "Big endian" and the sending or receiving processing code is "Little Endian" The above byte order "functions" are required to switch (convert) between Big Endian and Little Endian. If no Byte Order "Function" is present then it will arrive as unreadable garbage to the processor.


Hope you enjoyed this Linux Network Code snack!

No comments:

Post a Comment