Addresses and packets

MAC and IP addresses, IPv4 and IPv6, and packet switching.

F10.5NetworksGCSE15 min

Do this lesson in the simulator

When you send a photo across the internet, it does not travel as one piece. It is chopped into packets, each labelled with where it is going, and each may take a different route. At the other end, the packets are put back in order. This lesson covers the two kinds of address every device has, and how packet switching works.

MAC addresses

A MAC address (media access control) identifies a device's network interface. It is:

  • set when the network interface is made, and normally never changes;
  • 48 bits, written as 6 pairs of hexadecimal digits: 3C:61:05:1A:9F:02;
  • used to deliver data within a local network, by switches.

IP addresses

An IP address (internet protocol) identifies a device on a network, and is used to route data between networks, by routers. It can change: your phone gets a different IP address at school and at home.

  • IPv4 addresses are 32 bits, written as four denary numbers from 0 to 255, separated by dots: 192.168.1.27. That gives about 4.3 billion addresses, which is not enough for every device in the world.
  • IPv6 addresses are 128 bits, written as eight groups of hexadecimal digits: 2001:0db8:85a3:0000:0000:8a2e:0370:7334. That is enough for every grain of sand on Earth to have billions.

An address can be static, set by hand and always the same (useful for servers and printers), or dynamic, given out automatically by the network each time a device joins.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def valid_ipv4(address):
    parts = address.split(".")
    if len(parts) != 4:
        return False
    for part in parts:
        if not part.isdigit() or int(part) > 255:
            return False
    return True

for address in ["192.168.1.27", "10.0.0.256", "172.16.4", "8.8.8.8"]:
    print(address, valid_ipv4(address))

Run this in the simulator

Packet switching

  1. The data is split into small packets.
  2. Each packet gets a header: the source and destination IP addresses, its sequence number, and how many packets there are in total. It may also carry a checksum, a number worked out from the data so the receiver can check nothing was damaged.
  3. Routers send each packet on towards its destination by whichever route is best at that moment. Packets from one message can take different routes.
  4. Packets arrive out of order, so the receiver puts them back in order using the sequence numbers.
  5. If a packet is missing or damaged, the receiver asks for it to be sent again.

Why bother? If one route is busy or broken, packets simply go another way, and many people's packets can share the same links.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

arrived = ["3/3:ld", "1/3:hello ", "2/3:wor"]
arrived.sort(key=lambda packet: int(packet.split("/")[0]))    # by sequence number
print(arrived)
print("".join(packet.split(":", 1)[1] for packet in arrived))

Run this in the simulator

Task: packets

Write make_packets(message, size), which splits message into pieces of size characters and returns them as packets in the form <number>/<total>:<piece>. Make the packets for message with a size of 10, print each one, and send each by radio. The packets arrive in the order [packets[2], packets[0], packets[1]]: sort them back into order by their sequence numbers, and print reassembled: <message> and matches: True (or False).

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

message = "robot 7 at 25,40 battery 87%"

Challenges

  1. Shuffle the packets with random.shuffle and check they still reassemble.
  2. Add a checksum to each packet: the total of the ord() values of its characters, modulo 100. Check it on arrival.
  3. How many IPv4 addresses are there exactly? How many IPv6? Use 2 ** 32 and 2 ** 128.