CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: FTP Help

  1. #1
    Join Date
    Nov 2006
    Posts
    329

    FTP Help

    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 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!

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: FTP Help

    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.
    Last edited by S_M_A; October 29th, 2007 at 04:00 PM. Reason: Faulty info

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: FTP Help

    Port 2115 is the port that the server told the client to connect to, as part of the PASV command.

    Code:
    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:
    Quote Originally Posted by 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.

  4. #4
    Join Date
    Nov 2006
    Posts
    329

    Question Re: FTP Help

    Ok so here is what i've done...

    Code:
    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?

  5. #5
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: FTP Help

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured