{}DSA Atlas

Network Models — OSI & TCP/IP

Why layering exists, the seven OSI layers and the four/five TCP/IP layers with protocols, devices and PDUs at each, encapsulation and decapsulation, ports and sockets, and how to use the model to debug problems.

Beginner5 interview questions

Why layers?

Networking is a huge problem: electrical signals, local delivery, global routing, reliability, application semantics. Layering splits it so each layer:

  • offers a service to the layer above,
  • uses the service of the layer below,
  • talks to its peer layer on the other machine through a protocol.

Swap a layer (Wi-Fi → Ethernet, IPv4 → IPv6, HTTP/1.1 → HTTP/2) and the rest keeps working.

OSI vs TCP/IP

OSI (7 layers) TCP/IP (4) Protocols · PDU · devices 7 Application 6 Presentation 5 Session 4 Transport 3 Network 2 Data link 1 Physical Application Transport Internet Link HTTP, DNS, TLS, SSH, SMTP, gRPC, MQTTPDU: message / dataL7 LB, API gateway, WAF TCP, UDP, QUIC · PDU: segment / datagramports · L4 load balancer IPv4, IPv6, ICMP, BGP, OSPF · PDU: packetrouter Ethernet, Wi-Fi, ARP, VLAN · PDU: frameswitch, NIC (MAC address) cables, fibre, radio · PDU: bitshub, repeater "L4" and "L7" in load-balancer talk refer to OSI numbering
The internet runs TCP/IP; OSI numbering survives as vocabulary.

What each layer does

LayerProblem it solvesAddressingKey ideas
Physicalmoving bits—encoding, bandwidth, signal
Data linkdelivery on one local networkMAC address (48-bit)framing, switching, error detection (CRC), MTU (1500 B Ethernet)
Networkdelivery across networksIP addressrouting, fragmentation, TTL, best effort
Transportdelivery to the right process, reliabilityport (16-bit)TCP (reliable stream), UDP (datagrams), congestion control
Session/Presentation/Applicationmeaning of the dataURLs, hostnamesHTTP semantics, encoding, encryption, sessions

Encapsulation

HTTP request (data) TCP hdr data segment IP hdr TCP hdr data packet Eth hdr (MACs) IP hdr TCP hdr data FCS frame sender adds headers going down; receiver strips them going up
Headers: Ethernet 14 B (+4 B FCS), IPv4 20 B, TCP 20 B (more with options). With a 1500 B MTU, ~1460 B of payload per segment.

Important header fields

HeaderFields worth knowing
Ethernetsource/destination MAC, EtherType, FCS
IPv4source/destination IP, TTL (hop limit, decremented per router), protocol (6 = TCP, 17 = UDP), total length, fragmentation flags, checksum
TCPsource/destination port, sequence and acknowledgement numbers, flags (SYN, ACK, FIN, RST, PSH, URG), window size, options (MSS, window scale, SACK, timestamps)
UDPports, length, checksum — that's all (8 bytes)

Ports and sockets

  • Port: 16-bit number identifying a process/service on a host. Well-known: 22 SSH, 25 SMTP, 53 DNS, 80 HTTP, 443 HTTPS, 5432 Postgres, 6379 Redis, 9092 Kafka. Ephemeral (client) ports: typically 32768–60999 on Linux.
  • Socket: an endpoint = IP + port (+ protocol), exposed to programs as a file descriptor.
  • Connection (TCP): the 5-tuple (protocol, src IP, src port, dst IP, dst port).
Python
import socket

# minimal TCP client: everything below HTTP is done by the OS
with socket.create_connection(("example.com", 80), timeout=5) as s:
    s.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")
    print(s.recv(200).decode(errors="replace"))

Using layers to debug

SymptomCheck (bottom-up)Tools
no linkcable/Wi-Fi, interface upip link, ethtool
can't reach the gatewayL2/L3: IP, subnet, ARPip addr, ip neigh, ping gateway
can't reach the internetrouting, DNS vs IPping 1.1.1.1, traceroute, mtr
name doesn't resolveDNSdig, nslookup
connection refused/timeoutL4: port open? firewall?nc -vz host 443, ss -tlnp, telnet
TLS errorscertificates, SNI, versionsopenssl s_client -connect host:443 -servername host
HTTP errorsstatus codes, headerscurl -v, browser devtools
weird behaviourlook at the packetstcpdump, Wireshark

Interview questions

0 / 5 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.