Click to See Complete Forum and Search --> : FTP Help


dellthinker
October 29th, 2007, 02:56 PM
Hi all. Im trying to create a FTP client. So far i have the first two commands down correct, and thats the USER/PASS commands. However the PORT and PASV commands im having serious trouble with.

Im hoping someone here is skilled enough to answer all my questions and not refer me to pages i've already been to like this (http://faqs.org/rfcs/rfc959.html) one among many many other pages.

Ok so i ran Wireshark to see exactly what was going on in the ftp client when it sends a file. First thing i noticed was that it sent this message:


16 18.419699 10.23.2.2 66.1.499.32 FTP Request: PORT 10,23,2,2,8,85



Then following that it sent the actual file:


18 18.432565 10.23.2.2 67.1.332.34 FTP Request: STOR file.txt


Then a bunch of other messages followed:


19 18.436742 67.1.332.34 10.23.2.2 TCP ftp-data > 2133 [SYN] Seq=0 Len=0 MSS=1460 TSV=5949368 TSER=0 WS=0


Now what im trying to understand is the following...



21 18.439802 67.1.332.34 10.23.2.2 FTP Response: 150 Connecting to port 2115


I know that port 21 is for commands, and port 20 is for sending data. But it says 2115? Is this supposed to be a random port? Reason why i ask is because im trying to make something that falls in line with the FTP rules.

Mind everyone i did this all on MS-DOS FTP client to keep it simple. I also noticed while sniffing that when PORT was sent it looked like this...


PORT 10,23,2,2,8,85



10.23.2.2 is my local IP on my machine i know this. But what does 8,85 represent?

Any help in this matter would be very appreciated. Thanx in advance!

S_M_A
October 29th, 2007, 03:53 PM
What you see is probably an active mode transfer. I did a google for "FTP passive" and got some good hits but this is kind of a favourite http://www.slacksite.com/other/ftp.html and it explains passive vs active FTP in an easy language.

Sorry by the way for just giving you an url but I don't think I can improve the explanation on that page.

MikeAThon
October 29th, 2007, 08:37 PM
Port 2115 is the port that the server told the client to connect to, as part of the PASV command.

PORT 10,23,2,2,8,85
This encodes the PORT command, which is the concatenation of a 32-bit IP address and a 16-bit port number, broken into 8-bit segments, with each segment then being sent as a comma-separated character equivalent decimal value. So the last two digits are 8,85, which means that the port number should be 8*256+85=2133.

Port number 2133 is shown as an active participant in your Wireshark trace.

Incidentally, you might not enjoy reading the rfc for FTP (who does?) but it's really unavoidable. The above information on the format of the PORT command is found in section 4.1.2:
DATA PORT (PORT)

The argument is a HOST-PORT specification for the data port
to be used in data connection. There are defaults for both
the user and server data ports, and under normal
circumstances this command and its reply are not needed. If
this command is used, the argument is the concatenation of a
32-bit internet host address and a 16-bit TCP port address.
This address information is broken into 8-bit fields and the
value of each field is transmitted as a decimal number (in
character string representation). The fields are separated
by commas. A port command would be:

PORT h1,h2,h3,h4,p1,p2

where h1 is the high order 8 bits of the internet host
address.

dellthinker
October 30th, 2007, 07:13 PM
Ok so here is what i've done...


while(1){
Sleep(1000);
ftpmsg("USER ftp\r\n");
Sleep(1000);
ftpmsg("PASS pass\r\n");
Sleep(1000);
ftpmsg("PORT 10,23,2,2,6,217\r\n");
Sleep(1000);
ftpmsg("STOR file.txt\r\n");
Sleep(9000);
break;
}


I dont understand how i should program the PORT command. Its obviously not working. but the server accepts the command, only when it sends STOR is when it doesnt do the actual sending. Any ideas?

MikeAThon
October 30th, 2007, 07:57 PM
By sending the PORT command, the client is telling the server that data should be transmitted on a non-default port number. The expected response by the server is to connect to the port number identified in the command, and then send the data over that new connection.

So, if a client sends this command, then it must also open up the needed port, and listen for the expected connection attempt from the server. Your code only sends the PORT command; it fails to open up a new port.

There are lots of generalized guides to programming for the FTP protocol. Do some Googling. Here's one, created in connection with a computer science course at Stanford: "EE284 Programming Assignment: Simple FTP Client" at http://www.stanford.edu/class/ee284/pa.html

Mike