Addresses and packets

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

F10.5NetworksGCSE15 min

Covers OCR J277 1.3.2 Wired and wireless networks, protocols and layers · AQA 8525 3.5 Fundamentals of computer networks · Edexcel 1CP2 4.1.3 How the internet is structured

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.
The MAC address 3C:61:05:1A:9F:02 is six parts of 8 bits, 48 bits; the IPv4 address 192.168.1.27 is four parts of 8 bits, 32 bits; the IPv6 address is eight parts of 16 bits, 128 bitsMAC48 bits3C:61:05:1A:9F:026 x 8bitshexadecimal; each part is 2 hexadecimal digits = 8 bitsIPv432 bits19211000000.16810101000.100000001.27000110114 x 8bitsIPv6128 bits2001:0db8:85a3:0000:0000:8a2e:0370:73348 x 16bitshexadecimal; each part is 4 hexadecimal digits = 16 bitsdenary; each part is 8 bits, shown in binary above, so it can only be 0 to 255
Each address is a row of parts, and the bits add up: 6 x 8 = 48 for a MAC address, 4 x 8 = 32 for IPv4 and 8 x 16 = 128 for IPv6. An IPv4 part is one byte, which is why 256 is not allowed.

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.
A packet: a header holding the source and destination IP addresses, the sequence number, the total number of packets and a checksum, followed by the datasource IP192.168.1.27destination IP8.8.8.8sequencenumber1total3checksumfrom the datadata"hello "the header: the label on the packetthe datarouters read thisto send it onthe receiver putspackets back in ordershows if the datawas damaged
Every packet carries its own header, so each one can travel on its own. The sequence number and the total let the receiver rebuild the message and notice a packet that never came.

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

Packets 1, 2 and 3 of one message cross a network of routers by different routes, avoiding a broken link, and arrive in the order 3, 1, 2brokensenderreceivereach circle is a router312arrive in the order312sorted bysequence number123the whole message again
A moment in the journey. Each router sends each packet the best way it can see, around the broken link, so packet 3 overtakes the others. The receiver does not mind: the numbers put them back in order.
# 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.