Endianness

Due to accidents of history, internet protocols use big-endian numbers, while common CPU architectures such as AMD64 or the older IA-32 use little-endian numbers. There do exist functions to convert numbers between host and network byte orders, though if a number is not converted correctly, one may end up with a bad (that is, backwards) address. For some addresses (9.9.9.9 comes to mind) this is never a problem as these addresses read the same, forwards or backwards.

If you only have a little (or big) endian system it can be helpful to run a real or virtual system that uses the opposite byte order to confirm that any code you write is correct on both platforms, and that any generated numbers can be moved between any platform without an address being generated backwards. Debian may still make available a big-endian MIPS image that runs under QEMU, or I believe some of the ARM chips can also run big-endian.

To see how big- and little-endian byte orders differ, the following commands pack two different IP addresses into a 32-bit bucket, and then unpack that using either "N" for Network byte order (big) or "V" for VAX (little-endian) order.

Building an address with the incorrect byte order will result in a backwards address.

In this case, the big-endian 2130706433 was used directly on a little-endian system, resulting in a backwards address.

One may wonder how much incorrect traffic 1.0.0.127 receives. Better code might emit numbers as big-endian, and then use the htonl(3) function to, if necessary, flip the bytes around.

A better interface may be to build the address up by bytes, similar to the "C4" or "four unsigned characters" pack trick used in the perl code above. This has the advantage of better documenting the address than 2130706433 does.

Netmasks are also 32-bit numbers, and must get the bits ordered correctly. There are not many netmasks (ignoring exotic netmasks that may not even be supported by the software in question) so a table of them might be good to have.

netmasken.c

What's an exotic netmask? I'd define it as a 32-bit number that has non-contiguous bit groups, something like 255.0.255.0. There may be extremely rare uses for such in firewall rules, assuming the firewall supports them and where you cannot simply make two or more table entries to match what the exotic mask does.

more on netmasks