FTP Passive Mode

Jump to: navigation, search

Table of Contents >> Application Reference


The user may use FTP passive mode in both one-server and two-server passive models. Operation in two-server passive model is illustrated here:

User-PI - Server AUser-PI - Server B
                 
C->A : Connect  C->B : Connect
C->A : PASV
A->C : 227 Entering Passive Mode. A1,A2,A3,A4,a1,a2
    C->B : PORT  A1,A2,A3,A4,a1,a2
    B->C : 200 Okay
C->A : STOR     C->B : RETR

            B->A : Connect to HOST-A, PORT-a

Operation in one-server mode is illustrated here:

C->A : Connect
C->A : PASV
A->C : 227 Entering Passive Mode. A1,A2,A3,A4,a1,a2
C->A : Connect to HOST-A, PORT-a
C->A : RETR


Here is an example for two-server passive mode operation (STOR):

#include        <trsocket.h>
#include        <stdio.h> /* for printf() */
 
ttUserFtpHandle setupFtpCtrl(char * ipaddr)
{
    int                 retCode;
    ttUserFtpHandle     ftpHandle;
 
/* Create New Session for FTP server */
    ftpHandle = tfFtpNewSession(0, TM_BLOCKING_ON, "fsusername", "fspassword");
    if (ftpHandle == (ttUserFtpHandle) 0 )
    {
        printf("\n Failed to create FTP session!");
    }
    else
    {
/* Connect to FTP server */
        retCode = tfFtpConnect(ftpHandle, ipaddr);
        if (retCode != TM_ENOERROR )
        {
            printf("\nFailed to connect FTP server !");
            (void)tfFtpFreeSession(ftpHandle);
            ftpHandle = (ttUserFtpHandle)0;
        }
        else
        {
/* Login to FTP server */
            retCode = tfFtpLogin(ftpHandle, "ftpusername", "ftppassword","");
            if (retCode != TM_ENOERROR )
            {
                printf("\nFailed to login on FTP server !");
                (void)tfFtpClose(ftpHandle);
                (void)tfFtpFreeSession(ftpHandle);
                ftpHandle = (ttUserFtpHandle)0;
            }
            else
            {
                printf("\nSuccessfully setup CTRL conn to FTP !");
            }
        }
    }
 
    return ftpHandle;
}
 
void main(void)
{
    ttUserFtpHandle     ftpHandleA;
    ttUserFtpHandle     ftpHandleB; 
 
...
 
/* Start the Treck stack */
 
...
 
/* Setup Ctrl connection to FTP server A */
    ftpHandleA = setupFtpCtrl("192.168.1.100");
 
/* Setup Ctrl connection to FTP server B */
    ftpHandleB = setupFtpCtrl("192.168.1.200");
 
/* Set FTP session A operating in passive mode */
    (void)tfFtpTurnPasv(ftpHandleA,TM_FTP_PASSIVE_MODE_ON);
 
/* STOR: file will be transferred from server B to server A */
    (void)tfFtpStor(ftpHandleA, ftpHandleB, "testFtpFile", "testFtpFile");
 
...
 
    return;
}


Here is an example for one-server passive mode operation (STOR):

#include        <trsocket.h>
#include        <stdio.h> /* for printf() */
 
void main(void)
{
    ttUserFtpHandle     ftpHandle;
 
...
 
/* Start the Treck stack */
 
...
 
/* Setup Ctrl connection to FTP server */
    ftpHandle = setupFtpCtrl("192.168.1.100");
 
/* Set FTP session operating in passive mode */
    (void)tfFtpTurnPasv(ftpHandle,TM_FTP_PASSIVE_MODE_ON);
 
/* STOR: file will be transferred from client to server */
    (void)tfFtpStor(ftpHandle, 0, "testFtpFile", "testFtpFile");
 
...
 
    return;
}


Note Note: For the function prototype of setupFtpCtrl please refer to the example of the two-server passive mode operation.


Table of Contents >> Application Reference