neubert
neubert

Reputation: 16802

how do ftp replies work

I've been reading the FTP specs and using Wireshark to capture the packets my FTP client is sending / receiving and have a few questions about them.

First here's the "connection greetings" (as the FTP RFC calls it) from my FTP server:

220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 2 of 50 allowed.
220-Local time is now 15:22. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.

Per RFC959#page-35 if there's a - after the three digit number it means that it's a multi-line response. As such it seems that the subsequent 220-'s are unnecessary and that the above could be rewritten as follows:

220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
You are user number 2 of 50 allowed.
Local time is now 15:22. Server port: 21.
This is a private system - No anonymous login
IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.

Is that correct?

Also, is there a limit on how long lines can be? The RFC only mentions "line length" once. Here:

  A reply is defined to contain the 3-digit code, followed by Space
  <SP>, followed by one line of text (where some maximum line length
  has been specified)

The RFC does not, however, talk about how or when such a "maximum line length" would be specified. The specific use case it gives for these multi-line responses are STAT replies but it seems to me that that example is a bit contrived as STAT responses wouldn't have new lines in them anyway I'd think.

Finally, how is one supposed to know when one is done receiving the reply? Here's how phpBB does it:

https://github.com/phpbb/phpbb3/blob/develop/phpBB/includes/functions_transfer.php#L885

do
{
    $result = @fgets($this->connection, 512);
    $response .= $result;
}
while (substr($result, 3, 1) !== ' ');

Their choice of 512 seems arbitrary *, however, ignoring that for the time being, their substr($result, 3, 1) !== ' ' would also break with the "connection greetings" rewrite I did earlier in this post.

Any insight would be appreciated - thanks!!

Upvotes: 4

Views: 933

Answers (2)

user134352
user134352

Reputation: 1

yes you r correct.But if you only implement server and client which is available on your linux system at that time your code is not working without 3-digit no. because all are follow the rfc. so you have to follow the rfc.

Upvotes: 0

Martin Prikryl
Martin Prikryl

Reputation: 202494

You are right on multi line response. But many servers uses the format with codes repeated at begginning of every line. So you need to be able to handle both unfortunately.

As for the line length, I do not know. The phpBB code does not seem to follow RFC indeed.

Upvotes: 2

Related Questions