Sub topic 8
The application layer: HTTP, DNS, TLS, and friends
Prerequisites and scope
This chapter assumes you know what a client and a server are, can run commands in a terminal, and have met the idea of layering and the four TCP/IP layers (link, internet, transport, application). It covers the top TCP/IP layer: what it contains, how programs use it, and where its boundaries with the layers below are fuzzy. It does not repeat how TCP or UDP work. That belongs to the transport layer chapter.
What the application layer is
In the TCP/IP model, the application layer is everything above the transport layer. A protocol belongs here if it defines the messages two programs exchange, and relies on the transport layer to carry them. The application layer does not care how bytes cross the network. It cares about meaning: "fetch this page", "what address belongs to this name", "send this mail".
The OSI model splits the same territory into three layers: session (5), presentation (6), and application (7). TCP/IP puts them in one layer. In practice, the work that OSI assigns to the session and presentation layers (opening a conversation, encoding data, encrypting it) is done by the application protocol itself or by a library the program links in.
| TCP/IP layer | OSI layers it covers | Example protocols |
|---|---|---|
| Application | 7 Application, 6 Presentation, 5 Session | HTTP, DNS, SMTP, IMAP, SSH, TLS, DHCP, NTP |
| Transport | 4 Transport | TCP, UDP |
What an application protocol defines
Every application protocol answers a few design questions. Knowing them lets you read an unfamiliar protocol quickly.
- Transport choice. Which transport carries it, and on which port. HTTP uses TCP port 80, HTTPS uses TCP port 443, and DNS uses UDP and TCP port 53.
- Message format. Text or binary. HTTP/1.1 and SMTP are text lines. HTTP/2, HTTP/3, and DNS are binary.
- Message flow. Request and response, long-lived stream, or one-way notification.
- State. Whether the server remembers earlier messages. HTTP is stateless by design, and applications add state with cookies or tokens. SSH and SMTP hold state during a connection.
- Error reporting. Status codes such as HTTP 404, or DNS response codes such as NXDOMAIN.
The application layer is also the only TCP/IP layer where you can invent a new protocol without any operating system or router changes. Routers only look at the internet layer headers, and the operating system only looks at the transport headers. That is why new protocols such as HTTP/3 or custom RPC systems can be deployed by updating only the endpoints.
HTTP: request and response
HTTP (Hypertext Transfer Protocol) is a request and response protocol. A client sends a request that names a method, a path, and headers. The server sends a response with a status code, headers, and usually a body.
An HTTP/1.1 request is plain text:
GET /index.html HTTP/1.1
Host: example.com
Accept: text/html
Connection: closeThe Host header is required in HTTP/1.1. One IP address can serve many sites, and the transport and internet layers only know the address and port, so the application layer must carry the site name.
You can send this by hand with Python's standard library, which shows that HTTP is just text over a TCP socket:
import socket
request = (
"GET / HTTP/1.1\r\n"
"Host: example.com\r\n"
"Connection: close\r\n"
"\r\n"
)
with socket.create_connection(("example.com", 80), timeout=5) as s:
s.sendall(request.encode("ascii"))
response = b""
while chunk := s.recv(4096):
response += chunk
print(response.split(b"\r\n")[0].decode())The first line of the reply is the status line, for example HTTP/1.1 200 OK. Lines end in \r\n, and an empty line ends the headers. Forgetting the empty line is a common mistake: the server waits for the rest of the request and the program appears to hang.
HTTP versions
| Version | Transport | Format | Main change |
|---|---|---|---|
| HTTP/1.1 | TCP | Text | Persistent connections, `Host` header |
| HTTP/2 | TCP (normally with TLS) | Binary | Many requests multiplexed on one connection |
| HTTP/3 | QUIC over UDP | Binary | Avoids TCP head-of-line blocking, built-in TLS 1.3 |
The semantics (methods, status codes, headers) stay the same across versions. Only the way messages are encoded and carried changes. HTTP/3 is a useful edge case for layering: it is an application protocol that runs over QUIC, and QUIC implements reliability and encryption on top of UDP. QUIC therefore behaves like a transport protocol but is built in the application space.
DNS: names to addresses
DNS (Domain Name System) maps names such as example.com to data, most often IP addresses. Programs call a resolver, which is usually a library plus a caching server run by your network or ISP. The resolver asks other DNS servers if it has no cached answer.
DNS normally uses UDP port 53 because a query and answer fit in one small datagram, and this avoids connection setup. It falls back to TCP when an answer is too large for UDP or for zone transfers. DNS over TLS (port 853) and DNS over HTTPS wrap the same messages in encryption.
Common record types:
| Record | Meaning |
|---|---|
| A | IPv4 address for a name |
| AAAA | IPv6 address for a name |
| CNAME | Alias to another name |
| MX | Mail server for a domain |
| TXT | Free-form text, used for verification and email policy |
Query it from a terminal:
dig +noall +answer example.com AEach answer line shows the name, the TTL (how many seconds a cache may keep it), the record type, and the data. A stale or wrong cached answer is a frequent cause of "it works for me but not for you".
DNS is a good example of a protocol that serves other protocols. A browser asks DNS first, and only then opens a TCP connection to the address it received. When you troubleshoot, always check name resolution before blaming the connection.
TLS: encryption between transport and application
TLS (Transport Layer Security) provides confidentiality, integrity, and server authentication for a byte stream. It is the "S" in HTTPS, and it also protects SMTP, IMAP, and many other protocols.
TLS runs on top of TCP. The client opens a TCP connection, then TLS performs its own handshake: the two sides agree on a protocol version and cipher, the server presents a certificate, and both derive session keys. After that, the application protocol (for example HTTP) is sent as encrypted records.
import socket
import ssl
context = ssl.create_default_context()
with socket.create_connection(("example.com", 443), timeout=5) as raw:
with context.wrap_socket(raw, server_hostname="example.com") as tls:
print(tls.version())
print(tls.cipher()[0])The server_hostname argument matters. It sets the name that the certificate is checked against, and it is sent in the handshake so that a server hosting many sites can choose the right certificate. Leaving certificate checks off removes the authentication that makes TLS worth using.
Where TLS sits in the layer models is debated. It carries application data, so it is above transport. It also does encryption and encoding, which OSI assigns to the presentation layer, and it sets up a secure session, which resembles the session layer. TCP/IP avoids the question by putting it in the application layer. The caveats chapter returns to this.
Other common application protocols
- DHCP assigns an IP address, gateway, and DNS server to a host that joins a network. It uses UDP ports 67 and 68. It runs before the host has an IP address, so the first messages are broadcast.
- SMTP moves mail between servers, typically on TCP port 25, with submission on 587. IMAP and POP3 let clients read mail.
- SSH gives encrypted remote login and file transfer on TCP port 22. It has its own encryption and does not use TLS.
- NTP synchronizes clocks over UDP port 123.
- FTP transfers files. It uses a control connection on TCP port 21 and separate data connections, which is why it is awkward behind firewalls.
How programs reach the application layer
The operating system implements the link, internet, and transport layers. Your program supplies application data and uses a socket to hand it to the transport layer. This example sends one UDP datagram:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(b"hello", (“127.0.0.1”, 9999))The program supplies only the data, an address, and a port. The operating system adds the UDP, IP, and link-layer headers. The same pattern holds for TCP with SOCK_STREAM: the program writes bytes, and the stack does the rest.
The application layer is therefore the boundary where your code meets the network stack. The socket API is the interface, and the port number is how the stack decides which program receives incoming data.
Diagram: one web request across the layers
Common errors and edge cases
- Confusing the layer of a failure. "The website is down" can mean a DNS failure, a refused TCP connection, a TLS certificate error, or an HTTP 500. Each happens at a different step in the diagram above, and each shows a different error message.
- Assuming the port tells you the protocol. Port numbers are conventions. Web traffic can run on port 8080, and unrelated programs can use port 80. Inspect the payload to be sure.
- Forgetting that DNS can fail separately.
curlby IP address can succeed whilecurlby name fails. - Expecting HTTP to keep state. Each request is independent. Login sessions come from cookies or tokens.
- Mixing up encryption layers. HTTPS encrypts HTTP content, but the destination IP address and usually the server name stay visible to the network.
- Proxies and load balancers. Some work at the application layer, reading HTTP headers to route requests. Others work at the transport layer and only see addresses and ports. The device's layer decides what it can inspect and change.
Exercise
A user reports that a site fails to load. Running curl https://203.0.113.5 -k --resolve is not possible, but ping 203.0.113.5 works and dig example.com returns no answer. Which application-layer protocol is the most likely problem, and why?
Show answer
DNS. The host is reachable by IP address, so the link, internet, and transport paths work. The name lookup returns nothing, so the browser has no address to connect to. Fixing or changing the DNS resolver, or checking the record, is the next step.
Exercise
Name the three OSI layers that the TCP/IP application layer covers, and explain why TLS does not fit cleanly into only one of them.
Show answer
Session (5), presentation (6), and application (7). TLS carries application data (application), encrypts and encodes it (presentation), and establishes a secure session with a handshake (session). It does several of these jobs, so no single OSI layer describes it well.