Course contents

02. Networking and Internet Lesson 2 of 10

IP Addresses

IPv4 and IPv6 addresses, CIDR prefixes, private ranges, ports, and NAT rewriting source addresses on a home router, shown packet by packet.

12 minBeginner9 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • An IPv4 address is 32 bits (about 4.3 billion addresses); an IPv6 address is 128 bits.
  • CIDR prefixes like 203.0.113.0/24 group addresses so routers can forward by block, using the longest match.
  • Private ranges (10/8, 172.16/12, 192.168/16) are reused everywhere and need NAT to reach the internet.
  • NAT maps private address and port pairs to a shared public address and port, and only knows connections started from inside.
  • Ports pick the program; the 5-tuple (protocol, source IP and port, destination IP and port) identifies a connection.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

An IP address identifies a network interface so that packets can be delivered to it. Every IP packet carries a source and a destination address.

  • IPv4 addresses are 32 bits, written as four decimal octets: 203.0.113.10. There are about 4.3 billion of them, and the regional internet registries handed out their last free blocks during the 2010s.
  • IPv6 addresses are 128 bits, written as eight groups of four hexadecimal digits with runs of zeros shortened to ::: 2001:db8:1:1::20. There are enough to give every device many globally unique addresses.

A port is a 16-bit number in the TCP or UDP header that selects which program on a machine receives the data. Address and port together form an endpoint; two endpoints plus the protocol (the 5-tuple) identify a connection.

Why it exists

Routers need a destination they can act on without knowing every machine in the world. IP addresses are hierarchical: a prefix identifies a network, the rest an interface inside it. That lets a router hold a table of prefixes instead of billions of individual addresses. Ports exist because one machine runs many programs and many connections at once.

NAT exists because IPv4 ran out. Instead of a public address per device, a household, an office or a whole mobile carrier shares a few public addresses. IPv6 exists to remove that scarcity.

How it works

Prefixes (CIDR). 203.0.113.0/24 means the first 24 bits are the network part, leaving 8 bits for 256 addresses. A /16 holds 65,536 addresses, a /32 a single one. Routers forward each packet to the next hop of the longest prefix that matches its destination, so a specific /24 route overrides a broad default route (0.0.0.0/0). Hosts use the same logic: destinations inside their own prefix are reached directly on the local network; everything else goes to the default gateway.

Public and private. Public addresses are globally unique and routed on the internet. RFC 1918 reserves three private ranges, 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16, for internal networks. Anyone may use them, so they are not unique and are not routed on the internet; ISPs filter them at their edges. Other special ranges include 127.0.0.0/8 (loopback), 169.254.0.0/16 (link-local), 100.64.0.0/10 (shared space for carrier-grade NAT) and the documentation ranges used in this lesson.

NAT (strictly NAPT, or port address translation). A router with one public address keeps a translation table. When a device sends a packet out, the router replaces its private source address and port with the public address and a free port, and records the pair. When a reply arrives for that public port, it looks up the entry and rewrites the destination back. Entries time out after inactivity (often tens of seconds for UDP, minutes to hours for TCP). Because the table only contains flows started from inside, unsolicited inbound packets have nowhere to go and are dropped. Port forwarding adds a static entry; peer-to-peer applications use STUN, TURN and hole punching to work around NAT. Carrier-grade NAT (CGNAT) adds a second layer at the ISP, so many customers share one public address.

Ports. Well-known services listen on fixed ports (443 for HTTPS, 22 for SSH, 53 for DNS, 5432 for PostgreSQL). Clients use ephemeral source ports chosen by the operating system, typically from a high range. A server can hold many connections on port 443 at once because each has a different client address or client port.

IPv6. An ISP delegates a prefix to each customer (commonly a /56 or /48); each local network is a /64, and devices configure their own addresses within it (SLAAC, often with randomised privacy addresses, or DHCPv6). Packets travel end to end with their real addresses, so no translation is needed. Home and enterprise routers still run stateful firewalls that allow replies to outbound traffic and block unsolicited inbound traffic by default. Most networks run dual stack (IPv4 and IPv6 side by side); clients try both and use whichever connects first (Happy Eyeballs). Google's public statistics show close to half of its users reaching it over IPv6 in recent years, with large differences between countries and networks.

When to use it

Address planning matters every time you build a network: cloud VPCs and subnets, Kubernetes pod and service ranges, VPN and office networks. It also matters for application behaviour: who the server thinks the client is, how rate limits and geo-location work, and whether peers can connect to each other directly.

When not to

  • Do not identify users by IP address. Many users share one address behind NAT and CGNAT, and one user changes addresses as they move between networks.
  • Do not hard-code IP addresses in clients when a DNS name or service discovery can provide indirection.
  • Do not rely on NAT as your security boundary. It blocks unsolicited inbound flows by accident, not by policy; use a firewall with explicit rules.

Trade-offs

Choice You gain You pay
NAT for IPv4 Many devices share scarce public addresses Broken end-to-end reachability, state in the router, complications for peer-to-peer and logging
CGNAT at the ISP ISP serves customers with few IPv4 addresses Shared addresses across customers, harder abuse tracking, no inbound connections
IPv6 Plentiful global addresses, no translation Dual-stack operation, new address-planning and firewall work
Large private subnets (e.g. a /16 per VPC) Room to grow Overlap with other networks you may later need to connect

Alternatives

  • IPv6-only networks with NAT64/DNS64: devices speak only IPv6; a gateway translates to reach IPv4-only servers. Mobile carriers use this widely.
  • Overlay networks: Kubernetes CNIs, mesh VPNs and service meshes give workloads their own address spaces, independent of the underlying network.
  • Proxies and load balancers: present one public address for many backends, which is NAT-like but at the application or transport layer.

Failure modes

  • Overlapping private ranges: two VPCs or a VPC and an office network both use 10.0.0.0/16; they cannot be peered or connected by VPN without renumbering or extra NAT.
  • NAT port or table exhaustion: a NAT gateway can map only so many simultaneous flows per public address (one per source port, about 64,000 per destination); busy services see connection failures.
  • Idle timeouts: NAT or firewall entries expire on long-lived idle connections, which then fail silently. Keepalives prevent it.
  • Lost client identity: behind a load balancer or proxy the server sees the proxy's address. Use X-Forwarded-For, Forwarded or the PROXY protocol, and only trust them from your own proxies.
  • Subnet too small: autoscaling or Kubernetes runs out of addresses in a subnet sized for today.

In production

Cloud networks are designed as private CIDR blocks per VPC and per subnet, with non-overlapping ranges planned across all environments and regions. Outbound traffic from private subnets leaves through a NAT gateway with a few static public addresses (useful for partners who allow-list IPs). Inbound traffic arrives at load balancers with public addresses and is forwarded to private backends. Services read client identity from trusted forwarding headers. Rate limits key on user or API token rather than on IP alone. New networks increasingly enable IPv6 or dual stack from the start.

Common misconceptions

  • "An IP address identifies a person or a device." It identifies an interface at a moment; NAT, CGNAT, mobile networks and privacy addresses break any one-to-one mapping.
  • "NAT is a firewall." It drops unmapped inbound packets as a side effect, but it does not inspect or filter by policy.
  • "IPv6 devices are exposed to the whole internet." They are addressable, but the default stateful firewall blocks unsolicited inbound traffic.
  • "A port is a physical thing." It is a number in the transport header that the operating system uses to deliver data to a socket.

Interview angle

IP details come up indirectly. Be ready to explain why a backend behind a load balancer sees the balancer's IP and how to recover the client's (X-Forwarded-For, PROXY protocol), why per-IP rate limiting is unfair behind CGNAT, why VPC ranges must not overlap if they will ever be connected, and why private subnets reach the internet through a NAT gateway. Knowing that a connection is the 5-tuple helps when discussing load balancer connection limits or NAT port exhaustion.

Model assumptions

This lesson is a scripted walk-through, not a simulation: every packet is placed by hand, and packet headers and the NAT table are labels. Addresses come from the documentation ranges (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32) and the private 192.168.1.0/24. One-way latencies are illustrative (3 ms from devices to the home router, 10 ms to the ISP, 20 ms on to the server). The path between ISP and server is collapsed into one hop. The home router is shown delivering the laptop-to-phone packet; in practice the router's built-in switch forwards it at layer 2 without routing. Real NAT implementations differ in port allocation, timeouts and how they treat inbound packets (most drop them silently).

The lesson as textEvery step of the simulation, written out.
  1. 1. An address for every interface

    Every packet carries a source and a destination IP address. An IPv4 address is 32 bits, written as four numbers from 0 to 255: 203.0.113.10. That allows about 4.3 billion addresses, fewer than the devices online today; the regional registries ran out of fresh IPv4 blocks in the 2010s.

    Watch a packet from the laptop to the server. Its header says where it came from and where it is going, and every router on the way reads the destination. (A scripted illustration: packets and headers are drawn by hand.)

    Scene: A packet travels from the laptop (192.168.1.20) through the home router and the ISP router to the server (203.0.113.10). Routers read its destination address.

    Key idea: An IPv4 address is 32 bits; every packet carries a source and a destination address.

  2. 2. Prefixes: networks inside addresses

    Routers do not store every address. They store prefixes in CIDR notation: 203.0.113.0/24 means “the first 24 bits are fixed”, a block of 256 addresses. The home network is 192.168.1.0/24.

    Two packets leave the laptop. The one for the phone is in the same /24, so the home router delivers it locally. The one for the server matches no local prefix and follows the default route to the ISP, whose table sends 203.0.113.0/24 on towards the server’s network.

    Scene: The laptop sends one packet to the phone, delivered inside the home network, and one to the server, which goes through the ISP router.

    Key idea: A /n prefix fixes the first n bits; routers forward by the longest prefix that matches the destination.

  3. 3. Private addresses stay at home

    192.168.1.20 is a private address. Three blocks are reserved for private networks (RFC 1918): 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Millions of homes and offices reuse the same ones, so they cannot identify anyone on the internet.

    Send a packet out with its private source unchanged (red: it is about to be dropped), and the ISP discards it at the edge. Even if it got through, no reply could find its way back to “192.168.1.20”. Something has to give the packet a public address on its way out.

    Scene: A packet with the private source address 192.168.1.20 reaches the ISP router, which drops it; nothing reaches the server.

    Key idea: Private (RFC 1918) addresses are reused everywhere and are not routed on the public internet.

  4. 4. NAT rewrites the source on the way out

    The home router has one public address from the ISP: 198.51.100.7. As the laptop’s packet passes through, the router performs NAT (network address translation, strictly NAPT): it replaces the source 192.168.1.20:51000 with 198.51.100.7:40001 and writes that pair into its translation table.

    The server never sees the private address. As far as it knows, the request came from 198.51.100.7, port 40001.

    Scene: The laptop’s packet leaves with source 192.168.1.20:51000. The home router rewrites it to 198.51.100.7:40001 and records the mapping; the packet reaches the server.

    Key idea: NAT swaps a private source address and port for the router’s public address and a port it picks, and remembers the pair.

  5. 5. Replies are translated back

    The server answers to 198.51.100.7:40001. The home router looks that port up in its table and rewrites the destination back to 192.168.1.20:51000, so the reply reaches the laptop.

    Meanwhile the phone makes its own request. It gets its own entry, port 40002. Both devices share one public address, told apart only by port. That is how one IPv4 address serves a whole household, and, with carrier-grade NAT at the ISP, sometimes thousands of customers.

    Scene: The server’s reply to 198.51.100.7:40001 is translated back by the home router and delivered to the laptop. The phone sends its own request through the router, gets port 40002, and receives its reply.

    Key idea: The NAT table maps public ports back to private addresses, so many devices share one public IPv4 address.

  6. 6. Ports pick the program

    An IP address gets a packet to a machine. A port (16 bits, 0–65535) gets it to the right program on that machine. The server runs a web server listening on port 443 and an SSH daemon on port 22.

    The laptop opens two connections, one to each. Each uses a different temporary (*ephemeral*) source port. A connection is identified by five values: protocol, source address, source port, destination address and destination port. That is how one machine keeps thousands of connections apart.

    Scene: The laptop sends one packet to port 443, delivered to the web server program, and one to port 22, delivered to the SSH daemon, both on the same server address.

    Key idea: Address → machine, port → program. A connection is the 5-tuple (protocol, src IP, src port, dst IP, dst port).

  7. 7. Nobody can call in

    A computer on the internet, 192.0.2.50, sends a packet to 198.51.100.7:8080, maybe someone trying to reach a game server on the laptop. The home router finds no entry for port 8080 in its table, so it has no idea which device it is for, and drops it.

    NAT only knows connections started from inside. That accidental firewall is why peer-to-peer apps need tricks (STUN, TURN, hole punching) and why self-hosting at home needs an explicit port-forwarding rule. Behind carrier-grade NAT, even that is impossible.

    Scene: A stranger at 192.0.2.50 sends a packet to the home’s public address on port 8080. It reaches the home router, which has no mapping for that port and drops it; neither the laptop nor the phone sees it.

    Key idea: Behind NAT, unsolicited inbound packets have no mapping and are dropped; inbound needs port forwarding or a relay.

  8. 8. IPv6: enough addresses for everyone

    IPv6 addresses are 128 bits, written in hexadecimal groups: 2001:db8:1:1::20 (:: skips a run of zeros). The ISP gives the home a whole prefix, typically a /56 or /48, and each local network is a /64, so every device gets its own global address.

    The laptop’s packet leaves with its source unchanged: the router routes it without translating anything, and the server sees the laptop’s real address. No NAT means simpler protocols and end-to-end reachability, so the router runs a stateful firewall instead, allowing replies to outbound traffic and blocking unsolicited inbound by default. Most large networks today run both IPv4 and IPv6 (dual stack).

    Scene: Over IPv6 the laptop’s packet keeps its global source address 2001:db8:1:1::20 all the way to the server, and the reply comes straight back. The home router routes and filters but does not translate.

    Key idea: IPv6 gives every device a global address and removes the need for NAT, but not the need for a firewall.

  9. 9. What to remember about addresses

    IPv4 addresses are 32 bits and scarce; prefixes group them so routers can forward by blocks. Private ranges are reused everywhere, so NAT rewrites them to a shared public address and port on the way out and back, which also blocks unsolicited inbound traffic. Ports pick the program, and the 5-tuple identifies a connection. IPv6 has enough addresses to drop NAT, but not firewalls.

    For system design: servers behind a load balancer often see the balancer’s address, not the client’s (use X-Forwarded-For or the PROXY protocol), many users can share one IPv4 address (careful with IP-based rate limits), and cloud networks are planned as private CIDR blocks that must not overlap if you ever want to connect them.

    Scene: The full picture: laptop and phone on a private home network, the home router, the ISP router, a stranger on the internet, and the server with its web and SSH programs.

    Key idea: Addresses locate, ports identify programs, NAT shares scarce IPv4 addresses, IPv6 makes them plentiful.