skip to content

How do you read the address and port encoded in an FTP PORT command or a 227 Entering Passive Mode reply?

level: middleimportance: should knowfreq 24%

answer

  1. six comma-separated decimals
  2. first four are the IPv4 address
  3. high byte, then low byte
  4. multiply the fifth by 256

basics

~10 s

Both carry six comma-separated decimals h1,h2,h3,h4,p1,p2: the first four are the IPv4 address and the port is p1 x 256 + p2. So 198,51,100,20,195,149 means 198.51.100.20, port 50069.

solid answer

~40 s

`PORT` (client to server, active mode) and the `227 Entering Passive Mode` reply (server to client, passive mode) share one RFC 959 encoding: six decimal numbers, `h1,h2,h3,h4,p1,p2`. The first four are the bytes of an IPv4 address, high-order first. The last two are the TCP port split into a high and a low byte, so the port is `p1 × 256 + p2`. Reading `227 Entering Passive Mode (198,51,100,20,195,149)` gives 198.51.100.20, port 195 × 256 + 149 = 50069; encoding port 50000 for `PORT` gives `195,80`. Two practical points: the text around the numbers in a 227 reply was never tightly specified, so clients scan for the six numbers, and the address is whatever the sender believes its own address to be.

code

pseudocode · 10 lines
pseudocode
reply = "227 Entering Passive Mode (198,51,100,20,195,149)"
nums  = first six comma-separated decimals in reply   # 198 51 100 20 195 149
host  = nums[0] . nums[1] . nums[2] . nums[3]         # 198.51.100.20
port  = nums[4] * 256 + nums[5]                       # 195*256 + 149 = 50069
connect(host, port)                                   # passive: the client dials

# active: client listening on 192.0.2.15 port 50000
p1 = 50000 div 256                                    # 195
p2 = 50000 mod 256                                    # 80
send "PORT 192,0,2,15," + p1 + "," + p2              # PORT 192,0,2,15,195,80

go deeper

for a junior

Remember the shape: four address numbers then two port numbers, with the port rebuilt as p1 x 256 + p2.

for a middle

Decode a real 227 line and encode a PORT argument for a given listening port in your head, and say whose listener each one describes.

for a senior

Read the decoded address critically: when a 227 address differs from the control connection's peer, the server is advertising an endpoint the client may not reach.

for a principal

Note what the 1985 encoding cannot express — no IPv6, only an address the sender asserts about itself — which is why the extended commands dropped the address from the passive reply.

## The encoding RFC 959 uses FTP carries an IPv4 endpoint inside two messages: - the **`PORT` command**, sent by the client in active mode to say where it is listening, and - the **`227 Entering Passive Mode` reply**, sent by the server in passive mode to say where *it* is listening. Both use the format RFC 959 defines for a host-port specification: a **32-bit IPv4 address** and a **16-bit TCP port**, broken into **six 8-bit fields**, each written as a decimal number and separated by commas: `h1,h2,h3,h4,p1,p2` - `h1` to `h4` are the four bytes of the address, high-order byte first — read them with dots instead of commas. - `p1` is the **high byte** of the port and `p2` the **low byte**, so the port is **`p1 × 256 + p2`**. ## Who sends which | Message | Sent by | Mode | Endpoint it names | Who then connects | |---|---|---|---|---| | `PORT h1,h2,h3,h4,p1,p2` | client | active | the client's listener | the server, from its port 20 | | `227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)` | server | passive | the server's listener | the client | The arithmetic is identical; only the owner of the endpoint changes, and confusing the two is the commonest reading error. ## Decoding step by step Take the reply `227 Entering Passive Mode (198,51,100,20,195,149)`: 1. Extract the six numbers: 198, 51, 100, 20, 195, 149. 2. Join the first four with dots: **198.51.100.20**. 3. Multiply the fifth by 256: 195 × 256 = 49920. 4. Add the sixth: 49920 + 149 = **50069**. 5. The client should open a TCP connection to 198.51.100.20 port 50069. | Encoded fields | Address | Port arithmetic | Port | |---|---|---|---| | `198,51,100,20,195,149` | 198.51.100.20 | 195 × 256 + 149 | 50069 | | `192,0,2,15,195,80` | 192.0.2.15 | 195 × 256 + 80 | 50000 | | `203,0,113,7,117,48` | 203.0.113.7 | 117 × 256 + 48 | 30000 | | `203,0,113,7,4,1` | 203.0.113.7 | 4 × 256 + 1 | 1025 | A quick sanity check: a high byte in the 190s means a port near 50000, a high byte near 117 means a port near 30000, and a high byte of 0 means a port below 256. ## Encoding a port for PORT Going the other way — a client listening on 192.0.2.15 port 50000 — divide the port by 256: 1. `p1` = 50000 div 256 = **195**. 2. `p2` = 50000 mod 256 = 50000 − 49920 = **80**. 3. Send `PORT 192,0,2,15,195,80`; the server answers `200 Command okay` and will connect to that endpoint, from its own port 20, when a transfer command arrives. ## Parsing a 227 reply in practice RFC 959 shows the reply as `227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)`, but the text around the numbers was never tightly specified: RFC 1579 records that RFC 1123 found the format of the `PASV` response not well-defined and recommends following RFC 1123's advice. In practice a robust client: - scans the reply for the first run of six comma-separated decimal numbers rather than matching a fixed string, - rejects a field outside 0–255, and - treats the result as the server's **claim** about where it listens. That last point matters. The address inside `PORT` or `227` is whatever the sender believes its own address to be. If the sender sits behind address translation, it can advertise an address the other side cannot reach — the core of FTP's NAT trouble, which the material on FTP's NAT and firewall issues covers. ## What the six numbers cannot carry - **No IPv6.** Four bytes is an IPv4 address and nothing else. RFC 2428 added `EPRT`, which names an address family (1 for IPv4, 2 for IPv6) and writes the address as text, and `EPSV`, whose `229 Entering Extended Passive Mode (|||port|)` reply carries only a port. - **No address family.** The format assumes IPv4; there is no field to say otherwise. - **No proof.** Nothing in the encoding shows that the advertised endpoint is reachable; a `200` to `PORT` only means the server parsed it. ## Common slips - Concatenating the last two numbers — `195,149` is not port 195149, which would not even fit in 16 bits. - Adding them — `195 + 149` = 344 is wrong; `p1` is a byte worth 256 each. - Swapping the bytes — `149 × 256 + 195` gives 38339, a different port. - Mixing up whose endpoint it is: `PORT` names the **client's** listener, a `227` reply names the **server's**.

  • Why does FTP split the port into two numbers instead of writing it as one?
    RFC 959 defines the argument as a 32-bit address plus a 16-bit port broken into 8-bit fields, each sent as a decimal number. The port's two fields are simply its high and low byte, so the receiver rebuilds it as `p1 × 256 + p2`. The format serialises six bytes; readability was never the goal.
  • Can an FTP PORT command or 227 reply carry an IPv6 address?
    No — the format has exactly four address bytes. RFC 2428 added `EPRT`, which names an address family (1 for IPv4, 2 for IPv6) and writes the address as text, and `EPSV`, whose `229 Entering Extended Passive Mode (|||port|)` reply carries only the port and reuses the control connection's address.

  • The FTP port 195,149 means 195149, the two numbers concatenated
  • The port is p1 plus p2
  • A 227 reply tells the server where the client is listening
  • PORT can carry an IPv6 address if you add more numbers
  • Every 227 reply uses one fixed text that clients can match exactly