(original) (raw)

On Wed, Sep 16, 2009 at 4:59 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Some people have claimed that the gateway address of a
network isn't necessarily the zero address in that network.

I'll go further: I don't think it's even legal for the gateway address to be the zero address of the network (and I used to program the embedded software in routers for a living :) ).
If that's true, then you \*can't\* calculate the network
address from a host address and a netmask -- there isn't
enough information.

In a router or operating system kernel, an IPv4 address or netmask is stored in a 32-bit unsigned integer. Host addresses, network addresses, and netmasks satisfy the following properties, where & is the bitwise-AND operation:

network address & netmask == network address
host address & netmask == network address of that host

A gateway is just a host that will forward packets. A gateway address has the same properties as a host address and isn't the same as the zero address.

Every host has a routing table that contains a list of (network address, gateway, netmask) tuples. To see the list, just run "/sbin/route -n" on any Linux system (and most other Unixes; root is not typically required) or "route print" on a Windows system.

To route a packet, the operating system essentially performs the following algorithm:

def find\_gateway\_for\_destination(destination\_address):
for (network\_address, gateway, netmask) in list\_of\_routing\_table\_entires:
if network\_address == destination\_address & netmask:
return gateway
raise DestinationUnreachable

Furthermore, an IPNetwork object
needs to be able to represent a network address whose
address part contains bits that aren't in the mask.

Network addresses never contain bits that aren't in the mask (due to the rule: "network address & netmask == network address").
--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC