Every device on the internet has an address. To understand networks you need to know how IP addresses work: how they're structured, how subnets divide them, and how NAT lets millions of devices share a handful of public IPs.

IPv4 Structure#

An IPv4 address is a 32-bit number written as four decimal octets (0 to 255):

text

Two parts: the network prefix (identifies the network) and the host portion (identifies the device within that network).

Subnet Masks#

The subnet mask separates the network from the host portion. It's a 32-bit number with all 1s for the network part, then all 0s for the host part.

text

CIDR Notation#

CIDR (Classless Inter-Domain Routing) notation writes the mask as a prefix length after a slash:

text

Usable hosts = 2^(32 - prefix) - 2
(Subtract 2: network address and broadcast address are reserved)

CIDRHostsUse case
/816,777,214Large corporate / ISP blocks
/1665,534Medium networks
/24254Typical home / small office LAN
/2814Small subnet
/302Point-to-point links
/321 (host route)Single host

Subnetting#

Subnetting divides a larger network into smaller sub-networks. Useful for:

  • Security isolation (HR, finance on separate subnets)
  • Reducing broadcast domains
  • Efficient IP allocation

Example: divide 192.168.1.0/24 into 4 equal subnets.

4 subnets = 2² → borrow 2 bits from host portion → /26 mask

text

Watch the Boundary Move#

Every section above is the same operation seen from a different angle. Here it is as the bits themselves: 192.168.1.42 on top, the subnet mask under it, and the network address that falls out when the two are ANDed together, one octet at a time, exactly as the binary listings above are written.

The address never changes. All 32 bits of it are fixed from the first beat, and nothing the mask does touches them. What moves is the boundary. At /24 the mask keeps three whole octets and zeroes the last eight bits; borrow two more and the boundary slides into the fourth octet, and there are suddenly four subnets where there was one.

Watch HOSTS in the rail as the prefix grows: 254, 62, 14, 2, 1. Every borrowed bit halves what is left, and at /32 there is nothing left to halve: the network address is the machine.

Public vs Private Addresses#

Private ranges (RFC 1918), not routable on the public internet:

RangeCIDRSize
10.0.0.0 to 10.255.255.25510.0.0.0/816M addresses
172.16.0.0 to 172.31.255.255172.16.0.0/121M addresses
192.168.0.0 to 192.168.255.255192.168.0.0/1665K addresses

Your home devices (phone, laptop) have private IPs. Your router's WAN interface has a public IP assigned by your ISP.

NAT (Network Address Translation)#

NAT lets many devices share a single public IP address. Your home router does this:

text

The router maintains a NAT table mapping internal (IP, port) pairs to external port numbers.

Consequence: devices behind NAT cannot accept incoming connections without port forwarding. They're not directly reachable.

Special Addresses#

AddressMeaning
127.0.0.1 / 127.0.0.0/8Loopback (your own machine)
0.0.0.0This machine (unspecified)
255.255.255.255Limited broadcast
169.254.0.0/16Link-local (APIPA: no DHCP server found)
224.0.0.0/4Multicast

IPv6#

IPv4 has only ~4.3 billion addresses, and they have run out. IPv6 uses 128-bit addresses:

text

Features:

  • 340 undecillion addresses
  • No NAT required: every device can have a globally unique address
  • Built-in IPsec (encryption and authentication)
  • Stateless address autoconfiguration (SLAAC), no DHCP needed

Python: Working with IPs#

Python

What to Remember#

  • IPv4 addresses are 32-bit numbers; the network prefix and host are separated by the subnet mask
  • CIDR /n notation specifies how many bits are the network prefix
  • Subnetting divides a block into smaller ranges; each /1 added halves the block size
  • Private ranges (10.x, 172.16-31.x, 192.168.x) are used inside LANs; NAT maps them to public IPs
  • IPv6 solves address exhaustion with 128-bit addresses, no NAT needed