Networking

IP — Internet Protocol

The layer responsible for addressing and best-effort delivery of packets between hosts, potentially across many intermediate networks. IP itself makes no promise of delivery, order, or integrity beyond a basic header checksum — those guarantees are TCP's job, not IP's.

Quick reference
What it is
The layer that addresses hosts and routes packets between networks, with no delivery guarantee of its own.
Mental model
The postal system: it addresses and routes the envelope, but doesn't promise it arrives, arrives in order, or arrives at all.
Key numbers
32-bit addresses (IPv4) or 128-bit (IPv6); common subnet sizes /24 (254 hosts), /16 (65,534 hosts).
Reach for it when
You don't choose it directly — every IP-based protocol (TCP, UDP, and everything built on them) sits on top of this layer.

Addressing

An IPv4 address is 32 bits, usually written as four dotted decimal octets (e.g. 192.168.1.10). It's split into a network portion and a host portion by a subnet mask (or CIDR prefix, e.g. /24).

CIDRMaskUsable hosts
/24255.255.255.0254
/16255.255.0.065,534
/30255.255.255.2522 (common for point-to-point links)

Two hosts can talk directly only if they're on the same network (same network portion of their address). Otherwise, a packet is handed to a gateway/router, which forwards it based on its routing table — repeating hop by hop until it reaches a router directly attached to the destination's network.

An IPv4 address split into a network portion and a host portion by a subnet mask 192 168 1 42 network portion — "which neighborhood" host portion — "which house" subnet mask 255.255.255.0 → first 3 bytes = network, last byte = host every device here shares 192.168.1.* — only the last number differs
Same shape as a mailing address: a shared "neighborhood" prefix, then a number unique to one device in it.

Ports and sockets — finding the program, not just the machine

An IP address gets a packet to the right machine, but a machine runs many programs at once. A port — a number from 0 to 65535 — says which one of those programs a packet is actually for. If the IP address is the building's street address, a port number is the apartment door inside it: the network only needs the street address to reach the building, and the OS routes the rest internally once it arrives, using a table of "which program is listening on which port."

Ports 0–1023 are "well-known" and reserved by convention for standard services (80/443 for web, 53 for DNS, 22 for SSH). A program has to explicitly bind to a port before anything addressed to it arrives — data sent to a port nobody is listening on is simply dropped.

Put an IP address and a port together and you get a socket — the actual address both ends of a conversation use. A server binds a fixed, well-known port and listens on it forever; a client doesn't need a fixed port at all, so its OS grabs any free number above 1024 (an ephemeral port) just for that one conversation and discards it afterward.

A client at a random ephemeral source port sending a request to a server's fixed well-known port, and the server replying back to that exact source port Client 192.168.1.42 : 51422 Server 203.0.113.9 : 443 request → dest port 443 (fixed, always listening) ← reply — addressed back to port 51422 (ephemeral, this conversation only)
The server's port is fixed and known in advance; the client's port is picked fresh by its own OS, for this one exchange only.

Key fields worth knowing by name: TTL (Time To Live — decremented each hop, packet dropped at zero, preventing infinite routing loops), Protocol (which transport protocol follows — 6 for TCP, 17 for UDP), Source/Destination address, and Fragmentation fields (used when a packet exceeds the outgoing link's MTU).

TTL and hop-by-hop routing

Every router a packet passes through decrements TTL by one before forwarding it. This isn't optional bookkeeping — it's the only thing standing between a routing-table misconfiguration and a packet looping between two routers forever, silently eating bandwidth. If TTL hits zero, the router drops the packet and sends back an ICMP "Time Exceeded" message to the original sender — which is exactly the mechanism traceroute deliberately exploits: it sends packets with TTL 1, then 2, then 3, and reconstructs the path to a destination one dropped packet at a time.

TTL decrements by one at every router a packet passes through; when it hits zero the packet is dropped and an ICMP Time Exceeded message is sent back Host A Router 1 TTL 64 → 63 Router 2 TTL 63 → 62 Host B TTL reaches 0 → the packet is dropped and an ICMP "Time Exceeded" reply goes back to Host A — the same mechanism traceroute uses, one TTL value at a time, to map the path hop by hop
TTL is a hop-count safety valve, not a timer despite the name — it protects the whole network from one bad route entry, not any single packet's latency.

Why IP has no reliability built in

This is a deliberate design tradeoff, not an oversight. Every router on the internet has to forward packets from an enormous number of unrelated connections as fast as possible — if IP itself tracked per-connection state, acknowledgments, and retransmissions, every router in every path would need to carry that state too, which does not scale to internet size. By keeping IP stateless and pushing reliability up to the endpoints (TCP, or the application itself on UDP), the network core stays simple, fast, and able to route around failures without needing to know or care what any given packet's delivery guarantees are supposed to be. The cost of that simplicity is pushed to whichever layer actually needs the guarantee — which is also why an application that doesn't need reliability (UDP-based telemetry, for instance) never has to pay for TCP's overhead just because it exists.

IP does provide one honesty signal when something goes wrong: ICMP (Internet Control Message Protocol) — the "Time Exceeded" message above, "Destination Unreachable", and the echo request/reply pair that ping uses. ICMP is not a delivery guarantee either; it's diagnostic signaling, and routers are free to rate-limit or drop it under load.

Private address ranges (RFC 1918)

Not routable on the public internet directly — devices behind a NAT gateway typically get an address from one of these ranges.

IPv4 vs. IPv6

IPv4's 32-bit address space allows roughly 4.3 billion unique addresses. That sounded limitless in the 1980s; it doesn't in a world with billions of phones, laptops, servers, and IoT devices all needing one. IANA formally handed out the last free blocks of IPv4 address space in 2011 — since then, the internet has run almost entirely on NAT to make one public IPv4 address serve an entire household or company's worth of devices. IPv6 exists specifically to make that workaround unnecessary again, by making the address space so large that giving every device its own globally unique address stops being a scarce resource.

IPv4's 32-bit address space compared to IPv6's 128-bit address space — IPv6 is not merely bigger, it is a different order of magnitude entirely IPv4 — 32 bits — ~4.3 billion addresses ← the entire IPv4 space fits in a bar this short IPv6 — 128 bits — ~340 undecillion addresses enough to give every atom on the surface of the Earth its own IPv6 address, many times over
128 bits isn't "IPv4 but a bit bigger" — each additional bit doubles the space, so 96 more bits is a change of roughly 29 orders of magnitude.

IPv6 also simplifies the packet itself, not just the address:

IPv4IPv6
Address length32 bits128 bits
Address formatDotted decimal — 192.168.1.10Colon-separated hex groups — 2001:db8::1 (:: compresses a run of zeros)
Header sizeVariable, 20–60 bytes (options field)Fixed 40 bytes — simpler and faster for routers to process
FragmentationRouters may fragment packets in flightOnly the sending host fragments; routers never do — pushes the cost to the endpoint that can best absorb it
Header checksumPresent — recomputed at every hopRemoved — left to the link and transport layers, since re-checksumming at every router added cost for little real benefit
BroadcastSupported (e.g. 255.255.255.255)Removed entirely — replaced by more targeted multicast
Address configurationManual or DHCPDHCPv6, or built-in stateless autoconfiguration (SLAAC) — a device can generate its own address without asking anyone
NATEffectively required at internet scaleNot needed for address scarcity — though still sometimes used for other reasons (privacy, topology hiding)

The two are not backward compatible — an IPv4-only device cannot talk directly to an IPv6-only one; something has to translate or tunnel between them. In practice, most networks run dual-stack (both protocols active side by side, picking whichever the other end supports) during what has turned into a multi-decade transition, with techniques like NAT64 and 6to4 bridging the two where dual-stack isn't available. IP's core job — best-effort packet delivery — is identical in both; only the addressing and header details change.

Real-world applications

Gotchas

Field notes