Learning Python Network Programming

(Sean Pound) #1
Chapter 1

Routing with IP


We mentioned that routers are able to route traffic toward a destination network,
and implied that this is somehow done by using IP addresses and routing tables.
But what's really going on here?


One perhaps obvious method for routers to determine the correct router to forward
traffic to would be to program every router's routing table with a route for every
IP address. However, in practice, with 4 billion plus IP addresses and constantly
changing network routes, this turns out to be a completely infeasible method.


So, how is routing done? The answer lies in another property of IP addresses.
An IP address can be interpreted as being made up of two logical parts: a network
prefix and a host identifier. The network prefix uniquely identifies the network a
device is on, and the device can use this to determine how to handle traffic that it
generates, or receives for forwarding. The network prefix is the first n bits of the IP
address when it's written out in binary (remember an IP address is really just a 32-bit
number). The n bits are supplied by the network administrator as a part of a device's
network configuration at the same time that it is given its IP address.


You'll see that n is written in one of two ways. It can simply be appended to the IP
address, separated by a slash, as follows:


192.168.0.186/24

This is called CIDR notation. Alternatively, it can be written as a subnet mask,
which is sometimes just called a netmask. This is the way in which you will usually
see n being specified in a device's network configuration. A subnet mask is a 32-bit
number written in dot-decimal notation, just like an IP address.


255.255.255.0

This subnet mask is equivalent to /24. We get n from it by looking at it in binary.
A few examples are as follows:


255.0.0.0 = 11111111 00000000 00000000 00000000 = /8
255.192.0.0 = 11111111 11000000 00000000 00000000 = /10
255.255.255.0 = 11111111 11111111 11111111 00000000 = /24
255.255.255.240 = 11111111 11111111 11111111 11110000 = /28

n is simply the number of 1 bits in the subnet mask. (It's always the leftmost bits that
are set to 1 because this allows us to quickly get the Network prefix in binary by
doing a bitwise AND operation on the IP address and the subnet mask).

Free download pdf