PASV ports in FTP

I was looking to simulate an embedded device for testing and looking into FTP server implementations a simple bitwise operations question popped up: the way PASV command is standardized requires TCP port numbers to be transferred in two octets represented as integers.

This is how the server responds to a PASV command:

227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).

Which indicates and IP address and a port number. To find the IP address is easy. it’s only the first four octets. Port number requires a little math: p1 * 256 + p2. That’s all fine and documented quite a bit. But in a server implementation one needs to figure out these octets from a short integer as the port number:

p1 = port >> 8
p2 = port & 255

I just like bitwise operations so much, I though this little experiment deserved a note on the side.


March 25, 2015 | 147 Words

Ziya Suzen (Maxwell Troy)'s personal notes on software development and related technical subjects.