Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Networking 1: Addresses and Routing

    Routers and Routing

    A router is a device that connects different networks and forwards packets between them. Every

    Chapter 7 of 116 min read

    The Plain Picture#

    A letter from Bangalore to a small village in Punjab. The Bangalore post office doesn't know the village. It only knows: "Anything for the north goes to the Delhi sorting centre." Delhi knows: "Anything for Punjab goes to Chandigarh." Chandigarh knows the village's local post office.

    Each office only knows the next step, never the whole path. And every office has a fallback rule: "If I don't recognise the address at all, send it to the main hub."

    The Real Names#

    • A router is a device that connects different networks and forwards packets between them. Every post office in the story is a router.

    • Each router has a routing table: a list of "for destinations in this network, send to this next router" rules.

    • Each step from one router to the next is a hop.

    • The fallback rule is the default route, written 0.0.0.0/0 ("every address").

    • On your laptop, the router your packets go to for anything outside your subnet is your default gateway — usually your home Wi-Fi router, e.g. 192.168.1.1.

    Under the Hood#

    Step 1 — your laptop decides: neighbour or not?#

    Your laptop is 192.168.1.37/24, gateway 192.168.1.1.

    Output
      destination 192.168.1.50   → in my /24 subnet (Ch 5) → send DIRECTLY on the LAN
      destination 13.235.10.20   → not in my subnet        → send to the GATEWAY 192.168.1.1

    Your laptop's routing table (see it with netstat -rn on Mac, ip route on Linux) looks like:

      Destination        Gateway          Meaning
      ───────────        ───────          ────────────────────────────────────────
      192.168.1.0/24     (direct)         my own LAN: deliver directly
      0.0.0.0/0          192.168.1.1      everything else: hand to the home router

    Step 2 — each router picks the most specific match#

    A router's table may have several rules matching one destination. It always uses the most specific one — the one with the longest prefix. This is longest prefix match.

    Output
      Routing table:
        10.0.0.0/8         → router A
        10.1.0.0/16        → router B
        10.1.5.0/24        → router C
        0.0.0.0/0          → router D (default)
    
      Packet to 10.1.5.9    → matches /8, /16 and /24 → pick /24 → router C
      Packet to 10.1.99.9   → matches /8 and /16      → pick /16 → router B
      Packet to 10.200.0.1  → matches /8 only         → router A
      Packet to 8.8.8.8     → matches only /0         → router D

    Like postal addresses: a rule for "Koramangala, Bangalore" beats a rule for "Bangalore", which beats "India".

    Step 3 — TTL stops packets from looping forever#

    A mistake in routing tables could send a packet in a circle. To prevent it, the IP header's TTL (time to live, Chapter 4) starts at a number (commonly 64 on Linux and macOS, 128 on Windows), and every router subtracts 1. At 0, the router drops the packet and sends an error message back to the sender.

    traceroute uses this cleverly: it sends packets with TTL 1, then 2, then 3… Each router along the way drops one and replies, revealing itself. Try:

    Terminal
    traceroute google.com

    You'll see each hop from your home router, through your internet provider, to Google, with the time to each in milliseconds (How Computers Work Ch 6).

    A packet's full path#

      Laptop                 Home router            ISP routers                 Data centre            Server
      192.168.1.37 ───────▶  192.168.1.1   ──────▶  hop  ──▶  hop  ──▶  ──▶     edge router  ────▶     13.235.10.20
         "not my subnet →      "not my LAN →          each uses longest            "that's my
          use gateway"         send to ISP"           prefix match                  network"

    At every hop: the IP addresses stay the same (source = you, destination = the server), but the MAC addresses change (Chapter 3) — each hop builds a new link-layer frame for its own LAN.

    Gotchas

    • A missing or wrong default route = "I can reach local devices but not the internet."

    • Routes must work in both directions. A reply needs a route back to you. In the cloud, "I can send but get no reply" is often a missing return route or a firewall rule on one side.

    Check yourself

    5 questions
    1

    What does a router do?

    Show answer

    Connects networks and forwards each packet one hop closer to its destination using its routing table.

    2

    What is a default gateway?

    Show answer

    The router your device sends packets to when the destination isn't in its own subnet.

    3

    A router has routes for 10.0.0.0/8, 10.20.0.0/16 and 0.0.0.0/0. Where does a packet to

    10.20.3.4 go? And to 10.21.3.4?

    Show answer

    10.20.3.4 → the /16 route (longest match). 10.21.3.4 → the /8 route.

    4

    What does TTL do, and how does traceroute use it?

    Show answer

    Each router subtracts 1 and drops the packet at 0, preventing endless loops. traceroute sends packets with TTL 1, 2, 3… so each router in turn drops one and reveals itself.

    5

    As a packet crosses 10 routers, what stays the same: the MAC addresses, or the IP addresses?

    Show answer

    The IP addresses. MAC addresses change at every hop.