FTP
File Transfer Protocol
The classic file transfer protocol with two connections.
System design›Protocols
Basics
What the protocol is for, and the ideas it is built on.
1.1What it does
The protocol in plain words, before any bytes.
FTP separates control from data. Commands and replies travel on one connection (port 21). Every file or directory listing opens a new data connection. This was elegant in 1971 and awkward for firewalls and NAT today.
In active mode the server connects back to the client. In passive mode the server opens a port and the client connects to it, which works better through NAT.
1.2Key ideas
The handful of ideas everything else follows from.
Control and data
Two sockets per session. It is why FTP needs firewall helpers.
Active and passive
Passive is almost always used now.
Reply codes
1xx preliminary, 2xx success, 3xx need more, 4xx temporary failure, 5xx failure.
Successors
SFTP (over SSH) and FTPS (FTP over TLS) add security. SFTP is a different protocol.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- Why is FTP awkward with NAT?
- Is SFTP the same as FTP over TLS?
The answers are in part 4.
Detailed answer
Watch a real exchange step by step, then take each message apart field by field.
3.1Follow the packets
Step through the exchange between the two sides.
Over the control connection. Credentials are plain text.
3.2Decode it byte by byte
Click any byte or row. Each colour is one field.
FTP uses two connections. Commands travel on port 21; files travel on a separate data connection.
On connect.
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| Greeting | 0 | 22 | 32 32 30 20 46 54 50 20 … | |
| USER | 22 | 16 | 55 53 45 52 20 61 6e 6f … | |
| PASS | 38 | 24 | 50 41 53 53 20 67 75 65 … | |
| PASV | 62 | 6 | 50 41 53 56 0d 0a | |
| PASV reply | 68 | 50 | 32 32 37 20 45 6e 74 65 … | |
| RETR | 118 | 15 | 52 45 54 52 20 66 69 6c … | |
| Done | 133 | 23 | 32 32 36 20 54 72 61 6e … |
3.3Decode it in code
A small decoder, tested against the exact bytes shown above.
Decoding FTP's passive reply
FTP uses a second connection for data, and the address travels as text.
1/** Reply 227 packs the data connection's address into six numbers. */2export function parsePasv(reply: string): { host: string; port: number } {3 const m = reply.match(/\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)/);4 if (!m || !reply.startsWith("227")) throw new Error("not a PASV reply");5 const [h1, h2, h3, h4, p1, p2] = m.slice(1).map(Number);6 return { host: `${h1}.${h2}.${h3}.${h4}`, port: p1 * 256 + p2 }; // the port needs two bytes7}8 9/** The first digit says what happened. 1xx means the transfer has only started. */10export function replyClass(code: number): "wait" | "done" | "more" | "retry" | "failed" {11 return (["wait", "done", "more", "retry", "failed"] as const)[Math.floor(code / 100) - 1];12}p1 * 256 + p2- The port needs two bytes, so it's written as two numbers.
replyClass- The first digit: 1xx means the transfer has only started.
Tested by npm run test:modules.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
Why is FTP awkward with NAT?
Is SFTP the same as FTP over TLS?
Extra depth
The mistakes people make with this protocol in real systems.
5.1Watch out for
- Credentials and files are readable on the wire.
- Firewalls must allow the passive port range.
- ASCII versus binary mode once corrupted files by changing line endings.