Notes on porting Vxworks FTP to Sylixos

Keywords: ftp TCP/IP

categorycontent
key wordSylixos FTP Vxworks
AbstractPorting and using Vxworks component FTP

1. Scope of application

This article is applicable to engineers who need to use FTP, and describes the migration from FTP to use.

2. Principle overview

File Transfer Protocol (FTP) is a set of standard protocols used for file transmission on the network. It works in the seventh layer of OSI model and the fourth layer of TCP model, that is, the application layer. TCP transmission is used instead of UDP. The customer has to go through a "three-time handshake" process before establishing a connection with the server, Ensure that the connection between the client and the server is reliable and connection oriented, providing a reliable guarantee for data transmission.
FTP allows users to communicate with another host in the form of file operation (such as file addition, deletion, modification, query, transmission, etc.). However, users do not really log in to the computer they want to access and become full users. FTP programs can be used to access remote resources to realize users' round-trip transmission of files, directory management and access to e-mail, even though the computers of both sides may be equipped with different operating systems and file storage methods.

2.1 FTP principle

FTP is mainly used for file transmission, including command channel and data channel. Command channel is used for interactive command control, and data channel is used to transmit file data.
First, the client needs to log in to the FTP server, establish a connection through TCP, send a login command through the command channel, interact with the data port number through the command channel to establish a data connection with each other, and the data can be transmitted through the data connection.
File transfer optional file types: ASCII file (default) / image file type (binary) / local file type (used to transfer binary data between hosts with different byte sizes)
There are two modes of connection to the server:
Active FTP:
Command connection: client > 1024 port → server 21 port
Data connection: client > 1024 port ← server 20 port
Passive FTP:
Command connection: client > 1024 port → server 21 port
Data connection: client > 1024 port ← server > 1024 port
The connection process of PORT mode is: the client sends a connection request to the FTP PORT (21 by default) of the server, and the server accepts the connection and establishes a command link. When data needs to be transmitted, the client uses the PORT command on the command link to tell the server: "I opened * * * X PORT, you come and connect me". Therefore, the server sends a connection request from PORT 20 to * * * X PORT of the client to establish a data link to transmit data.
The connection process of PASV (passive) mode is: the client sends a connection request to the FTP port of the server (21 by default), and the server accepts the connection and establishes a command link. When it is necessary to transmit data, the server uses PASV command on the command link to tell the client: "I have opened * * * X port, you come and connect me". Therefore, the client sends a connection request to the * * * X port of the server and establishes a data link to transmit data.
Previously, all clients used active mode and worked well. Now, due to the existence of client firewall, some ports will be closed, so the active mode will fail. In this case, the passive mode is used, but some ports may also be blocked by the server's firewall. However, because the FTP server needs its FTP service to connect to a certain number of clients, they always support the passive mode. This is why we use the passive mode. In order to ensure that data can be transmitted correctly, the passive mode is obviously better than the active mode. (Note: the establishment of data transmission channel in the active (PORT) mode is initiated by the server, and the server uses PORT 20 to connect to a PORT greater than 1024 of the client; in the passive (PASV) mode, the establishment of data transmission channel is initiated by the FTP client, which uses a PORT greater than 1024 to connect to a PORT greater than 1024 of the server.)
When transmitting data in active mode, it is the port from "server" to "client";
Passive mode transfer data is the port where the client connects to the server.
Active mode requires that the client must open the port to the server. Many clients are in the firewall. It is difficult to open the port to the FTP server.
Passive mode only requires the server to open the port to the client to connect.
FTP servers generally support active and passive modes. The mode of connection is determined by FTP client software.
Causes of FTP passive mode
In the history of FTP, there was only active mode, but why did passive mode appear again? This involves another problem.
A long time ago (I don't know how long), there was no shared internet technology on earth, but later it appeared, so there were the following problems. As we all know, shared internet is that many computers share a public IP to use the internet. Let's make another analogy. A LAN shares 210.33.25.1, which is a public IP to access the internet, When an intranet user 192.168.0.100 accesses the FTP server of the external network, if the active mode is adopted, 192.168.0.1 tells the FTP server that I need a file and I open the X port. Due to shared internet access, 192.168.0.1's IP address has been translated into 210.33.25.1 when leaving the gateway, Therefore, when the server receives a message that 210.33.25.1 needs a file and opens port x, the FTP server will transmit data to port X of 210.33.25.1. Of course, the connection will not succeed, because the address of port x is not 210.33.25.1. In this case, the passive mode is useful, I believe you can understand how passive mode is a connection method.
In the active mode, the two FTP ports are relatively fixed. If the command port is x, the data port is x-1, that is, by default, the command port is 21 and the data port is 20; If you change the command port to 123, the data port is 122. In this way, it is very convenient to use the firewall. As long as you open these two ports, but if the client is sharing the Internet, it can't use FTP normally. This still doesn't work. You must need the passive mode.
In passive mode, it is more troublesome. By default, the command port is 21, but the data port is random
Therefore, passive mode is recommended.

3. Preparation

3.1 environmental preparation

  • FTP server turn off firewall
  • FTP server enable FTP

3.2 resource preparation

  • Vxworks source code (including component ftpLib.c)
  • Project file of Sylixos

4. Technical realization

4.1 setting of base project and app project

Select libVxWorks for the base project and connect to the so dynamic library for the app project.


4.2 ftpLib transplantation

The FTP implementation relies on TCP for communication, and uses POSIX standard interfaces, which hardly need to be modified, mainly for warnings and missing functions.

#ifndef SYLIXOS
#include <remLib.h>
#endif
#include <hostLib.h>
#ifndef SYLIXOS
#include <ftpLib.h>
#include <applUtilLib.h>
#else
#include "ftpLib.h"
#include "taskLibCommon.h"
#include <SylixOS.h>

#define log_err(__LOG_LEVEL, ...)      \
    do{\
    if((__LOG_LEVEL) & 1)\
    {\
        if((__LOG_LEVEL) & LOG_ERRNO)\
        {\
            printf("error #");\
        }\
    printf(__VA_ARGS__);printf("\r\n");\
    }\
}while(0);
#define log_info      log_err
#define log_warning   log_err

#endif

STATUS logLevelChange
    (
    int category,    /* logger category */
    UINT mask        /* bit mask of levels to log at - 0x00..0xff */
    )
    {

    return OK;
    }

#ifndef SYLIXOS
            log_err (FTP_LOG, "Timeout %lu sec.", replyTimeOut.tv_sec);
#else
            log_err (FTP_LOG, "Timeout %lu sec.", (ULONG)replyTimeOut.tv_sec);
#endif

#ifndef SYLIXOS
    int len;
#else
    UINT len;
#endif

#ifndef SYLIXOS
        bzero ((char *) &dataAddr, sizeof (SOCKADDR));
#else
        bzero ((char *) &dataAddr, sizeof (SOCKADDR_IN));
#endif

#ifndef SYLIXOS
    int len;
#else
    UINT len;
#endif

#ifndef SYLIXOS
    int fromlen = sizeof (from);
#else
    UINT fromlen = sizeof (from);
#endif

#ifndef SYLIXOS
             log_err (FTP_LOG, "select timeout after %lu sec.", replyTime.tv_sec);
#else
             log_err (FTP_LOG, "select timeout after %lu sec.", (ULONG)replyTime.tv_sec);
#endif

c file needs to optimize the warning part and confirm the implementation of functions. h file mainly lacks some macro definitions, which are completed and transplanted.

#ifndef SYLIXOS
#include <vxWorks.h>
#include <vwModNum.h>
#endif

#ifdef SYLIXOS
#include <vxWorksCommon.h>

#define MAX_IDENTITY_LEN              100
#define MIN_RESV_PORT                 600
#define MAX_RESV_PORT                 1023

#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN                64
#endif

#define FTP_LOG                       1

#define LOG_ERRNO                     0x10 /* log errno */

#undef  FTP_TRANSIENT_MAX_RETRY_COUNT
#define FTP_TRANSIENT_MAX_RETRY_COUNT 100
#undef  FTP_TRANSIENT_RETRY_INTERVAL
#define FTP_TRANSIENT_RETRY_INTERVAL  0
#undef  FTP_DEBUG_OPTIONS
#define FTP_DEBUG_OPTIONS             0
#undef  FTP_TIMEOUT
#define FTP_TIMEOUT                   0

extern UINT32  ftplTransientMaxRetryCount;   /* Retry just once for now */
extern UINT32  ftplTransientRetryInterval;   /* Default with no delay */
#endif

5. How to use FTP

General applications need to be initialized before they can be used. Initialization can be omitted, but only for some time settings.

/*******************************************************************************
*
* ftpTransientFatal - example applette to terminate FTP transient host responses
*
* ftpXfer will normally retry a command if the host responds with a 4xx
*     reply.   If this applette is installed,  it can immediately terminate
*     the retry sequence.
*
*
* RETURNS : TRUE - Terminate retry attempts
*           FALSE - Continue retry attempts
*
*
* SEE ALSO : ftpTransientFatalInstall(), ftpTransientConfigSet()
*
*/

LOCAL BOOL ftpTransientFatal
    (
    UINT32 reply /* Three digit code defined in RFC #640 */
    )
    {
    switch (reply)
        {
        case (421): /* Service not available */
        case (450): /* File unavailable */
        case (451): /* error in processing */
        case (452): /* insufficient storage */
            {
            /* yes, these are actually non-recoverable replies */
            return (TRUE);
            }
            /* attempt to retry the last command */
        default:
        return (FALSE);
        }
    }

#define FTP_TRANSIENT_FATAL ftpTransientFatal
/******************************************************************************
*
* usrFtpInit - initialize FTP parameters
*
* This routine configures ftp client parameters.
* defined by the user via the project facility.
*
* RETURNS: OK or ERROR
*
* NOMANUAL
*/

STATUS usrFtpInit (void)
    {
    ftpLibInit (FTP_TIMEOUT);
    ftplTransientMaxRetryCount = FTP_TRANSIENT_MAX_RETRY_COUNT; /* retry count */
    ftplTransientRetryInterval = FTP_TRANSIENT_RETRY_INTERVAL; /* retry interval  */
    ftpTransientFatalInstall ( FTP_TRANSIENT_FATAL ); /* Can be omitted */
    ftpLibDebugOptionsSet ( FTP_DEBUG_OPTIONS );    /* Set print level */

    return OK;
    }

FTP mainly uses the ftpXfer function to upload and download files. The functions inside the ftpXfer function can also be used separately. If necessary, you can refer to the ftpXfer function for customization.

STATUS ftpXfer
    (
    char *host,         /* name of server host */
    char *user,         /* user name for host login */
    char *passwd,       /* password for host login */
    char *acct,         /* account for host login */
    char *cmd,          /* command to send to host */
    char *dirname,      /* directory to 'cd' to before sending command */
    char *filename,     /* filename to send with command */
    int *pCtrlSock,     /* where to return control socket fd */
    int *pDataSock      /* where to return data socket fd, */
                        /* (NULL == don't open data connection) */
    )

File upload and download examples.

#include <stdio.h>
#include "ftpLib.h"
#include "socket.h"

#define HOST          "192.168.1.10"
#Define user "anonymous" / * anonymous account name*/
#define PWD "" / * password arbitrary*/
#define W_ Dir "/ ftptest" / * folder path*/

/*
 * This command causes the server to send the client a file specified in the pathname
 * Copy of. This does not affect the status and content of the file on the server site.
 */
#define RD_CMD        "RETR %s"
#define WR_ CMD "stor% s" / * let the server receive a file from the data connection*/
#define FILE          "fromserver.txt"
#define TOSERVER_FILE "toserver.txt"

/*
 *  ftp Download a file
 */
void FtpDownload()
{
 int ctrlSock;
 int dataSock;
 char buf[512];
 int nBytes;

 if(ERROR == ftpXfer(HOST, USER, PWD, "", RD_CMD, W_DIR, FILE, &ctrlSock, &dataSock))
 {
  printf("\r\n ftp connected failed!");
  return ;
 }

 while((nBytes = read(dataSock, buf, sizeof(buf))) > 0)
 {
  printf("\r\n it is [%s]", buf);
 }

 close(dataSock);

 if(ERROR == nBytes)
  printf("\r\nit is reading error.");

 if(ftpCommand(ctrlSock, "QUIT", 0, 0, 0, 0, 0, 0) == FTP_COMPLETE)
  printf("\r\nftp is completed");
 close(ctrlSock);
}

/*
 *  ftp Upload a file
 */
void FtpUpload ()
{
    int ctrlSock;
    int dataSock;
    int nBytes;

#define FTP_WRITE_TIMEOUT_DEFAULT 30

    struct timeval tm;
    tm.tv_sec = FTP_WRITE_TIMEOUT_DEFAULT;
    tm.tv_usec = 0;

    if(ERROR == ftpXfer(HOST, USER, PWD, "", WR_CMD, W_DIR, TOSERVER_FILE, &ctrlSock, &dataSock))
    {
     printf("\r\n ftp connected failed!");
     return ;
    }

    setsockopt(dataSock,SOL_SOCKET,SO_SNDTIMEO,(char*)&tm,sizeof(struct timeval));

    const PCHAR const ptr = "This is a test!.";

    nBytes = write (dataSock, ptr, strlen(ptr));

    close(dataSock);

    if(ERROR == nBytes)
        printf("\r\nit is reading error.");

    if(ftpCommand(ctrlSock, "QUIT", 0, 0, 0, 0, 0, 0) == FTP_COMPLETE)
        printf("\r\nftp is completed");
    close(ctrlSock);
}

You can see that two channels are used, command channel and data channel.
command
The control channel transmits commands, and some control commands are briefly listed.

6. Summary

You can probably understand the principle of FTP. ftpLib has realized the FTP function. Knowing two channels, it is easy to use.

Posted by hezll on Mon, 11 Oct 2021 18:56:25 -0700