Ignore:
Timestamp:
04/02/99 14:39:19 (24 years ago)
Author:
Joel Sherrill <joel.sherrill@…>
Branches:
4.10, 4.11, 4.8, 4.9, 5, master
Children:
bea606a
Parents:
5345873
Message:

Patch from Emmanuel Rauget (raguet@…) to add a htons on the
sin_port.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • c/src/libnetworking/rtems_servers/ftpd.c

    r5345873 r85e24a3  
    1 /*
    2  *  FTP Server Daemon
     1/* FIXME: 1. Parse command is a hack.  We can do better.
     2 *        2. chdir is a hack.  We can do better.
     3 *        3. PWD doesn't work.
     4 *        4. Some sort of access control?
     5 *
     6 *  FTP Server Daemon
    37 *
    48 *  Submitted by: Jake Janovetz <janovetz@tempest.ece.uiuc.edu>
     
    711 */
    812
    9 /*
    10  * Current state:
    11  *   To untar, put as "untar"
    12  * CWD uses chdir
    13  *   This is bad due to global setting of chdir.
    14  *
    15  * Stored files come into RAM and are saved later.  This is an artifact
    16  *   of a previous implementation (no filesystem -- had to do stuff with
    17  *   the "files" later).  This can be eliminated once all of Jake's stuff
    18  *   is moved to devices/filesystems.
    19  *
    20  * CLOSE(S) doesn't seem to work.  This causes problems in
    21  *          several areas.  It lets too many file descriptors pile up
    22  *          and it doesn't seem to flush the stream.
    23  *
    24  * Is 'recv' what I want to use to get commands from the control port?
    25  *
    26  */
    27 
    2813/**************************************************************************
    2914 *                                 ftpd.c                                 *
     
    3217 *                                                                        *
    3318 *    This file contains the daemon which services requests that appear   *
    34  *    on the 'FTP' port.  This server is compatible with FTP, but it      *
    35  *    also provides services specific to the Erithacus system.            *
    36  *    This server is started at boot-time and runs forever.               *
     19 *    on the FTP port.  This server is compatible with FTP, but it        *
     20 *    also provides 'hooks' to make it usable in situations where files   *
     21 *    are not used/necessary.  Once the server is started, it runs        *
     22 *    forever.                                                            *
     23 *                                                                        *
    3724 *                                                                        *
    3825 *    Organization:                                                       *
     
    4431 *       is complete, the session task deletes itself.  The daemon still  *
    4532 *       runs, however.                                                   *
    46  *                                                                        *
    47  *    Implementation Notes:                                               *
    48  *                                                                        *
    49  *       The 'current working directory' implementation utilizes the      *
    50  *       RTEMS filesystem cwd.  This is no good since other processes     *
    51  *       inherit the same cwd.                                            *
    5233 *                                                                        *
    5334 *                                                                        *
     
    7152 *                                                                        *
    7253 *                                                                        *
    73  *                                                                        *
    7454 * The public routines contained in this file are:                        *
    7555 *                                                                        *
    76  *    FTPD_Start - Starts the server daemon, then returns to its caller.  *
     56 *    rtems_initialize_ftpd_start - Starts the server daemon, then        *
     57 *                                  returns to its caller.                *
    7758 *                                                                        *
    7859 *                                                                        *
    7960 * The private routines contained in this file are:                       *
    8061 *                                                                        *
    81  *    FTPD_SendReply    - Sends a reply code and text through the control *
    82  *                        port.                                           *
    83  *    FTPD_CommandStore - Performs the "STOR" command.                    *
    84  *    FTPD_CommandList  - Performs the "LIST" command.                    *
    85  *    FTPD_CommandPort  - Opens a data port (the "PORT" command).         *
    86  *    FTPD_ParseCommand - Parses an incoming command.                     *
    87  *    FTPD_Session      - Begins a service session.                       *
    88  *    FTPD_Daemon       - Listens on the FTP port for service requests.   *
    89  *                                                                        *
    90  *                                                                        *
     62 *    rtems_ftpd_send_reply    - Sends a reply code and text through the *
     63 *                               control port.                            *
     64 *    rtems_ftpd_command_retrieve - Performs to "RETR" command.           *
     65 *    rtems_ftpd_command_store - Performs the "STOR" command.             *
     66 *    rtems_ftpd_command_list  - Performs the "LIST" command.             *
     67 *    rtems_ftpd_command_port  - Opens a data port (the "PORT" command).  *
     68 *    rtems_ftpd_parse_command - Parses an incoming command.              *
     69 *    rtmes_ftpd_session       - Begins a service session.                *
     70 *    rtems_ftpd_daemon        - Listens on the FTP port for service      *
     71 *                               requests.                                *
    9172 *------------------------------------------------------------------------*
    92  *                                                                        *
    9373 * Jake Janovetz                                                          *
    9474 * University of Illinois                                                 *
    9575 * 1406 West Green Street                                                 *
    9676 * Urbana IL  61801                                                       *
    97  *                                                                        *
    9877 **************************************************************************
    9978 * Change History:                                                        *
    10079 *  12/01/97 - Creation (JWJ)                                             *
    10180 *************************************************************************/
    102 
    103 /* Revision Control Information:
    104  *
    105  * $Source$
    106  * $Id$
    107  * $Log$
    108  * Revision 1.3  1998/05/19 21:28:17  erithacus
    109  * Update control socket to file I/O.
    110  *
    111  * Revision 1.2  1998/05/19 20:13:50  erithacus
    112  * Remodeled to be entirely reentrant.
    113  *
    114  *
    115  */
    116 
    11781
    11882#include <stdio.h>
     
    13498
    13599#include "ftpd.h"
    136 #include "untar.h"
    137 
     100
     101
     102extern struct rtems_ftpd_configuration rtems_ftpd_configuration;
    138103
    139104/**************************************************************************
     
    165130
    166131/**************************************************************************
    167  * Maximum buffer size for use by the transfer protocol.
    168  *  This will be eliminated when the filesystem is complete enough that
    169  *  we don't have to store the received data until we have something to
    170  *  do with it.
    171  *************************************************************************/
    172 #define FTPD_MAX_RECEIVESIZE  (512*1024)
    173 
    174 /**************************************************************************
    175132 * SessionInfo structure.
    176133 *
     
    181138{
    182139   struct sockaddr_in  data_addr;   /* Data address for PORT commands */
    183    int                 ctrl_sock;   /* Control connection socker */
     140   FILE                *ctrl_fp;    /* File pointer for control connection */
    184141   char                cwd[255];    /* Current working directory */
    185142                                    /* Login -- future use -- */
     
    188145
    189146
     147#define FTPD_SERVER_MESSAGE  "RTEMS FTP server (Version 1.0-JWJ) ready."
    190148#define FTPD_WELCOME_MESSAGE \
    191149   "Welcome to the RTEMS FTP server.\n" \
     
    195153
    196154/**************************************************************************
    197  * Function: FTPD_SendReply                                               *
     155 * Function: rtems_ftpd_send_reply                                        *
    198156 **************************************************************************
    199157 * Description:                                                           *
     
    217175 *************************************************************************/
    218176static void
    219 FTPD_SendReply(int code, char *text)
     177rtems_ftpd_send_reply(int code, char *text)
    220178{
    221179   rtems_status_code   sc;
     
    242200   {
    243201      sprintf(str, "%d %.70s\r\n", code, text);
     202      fprintf(info->ctrl_fp, "%d %.70s\r\n", code, text);
    244203   }
    245204   else
    246205   {
    247206      sprintf(str, "%d\r\n", code);
    248    }
    249    send(info->ctrl_sock, str, strlen(str), 0);
     207      fprintf(info->ctrl_fp, "%d\r\n", code);
     208   }
     209   fflush(info->ctrl_fp);
    250210}
    251211
    252212
    253213/**************************************************************************
    254  * Function: FTPD_CommandRetrieve                                         *
     214 * Function: rtems_ftpd_command_retrieve                                  *
    255215 **************************************************************************
    256216 * Description:                                                           *
     
    275235 *************************************************************************/
    276236static int
    277 FTPD_CommandRetrieve(char *filename)
     237rtems_ftpd_command_retrieve(char *filename)
    278238{
    279239   int                 s;
    280240   int                 n;
    281 
    282    FILE                *fp;
     241   int                 fd;
    283242   unsigned char       *bufr;
    284243   rtems_status_code   sc;
     
    289248                            (rtems_unsigned32 *)&info);
    290249
    291 
    292    if ((fp = fopen(filename, "r")) == NULL)
    293    {
    294       FTPD_SendReply(450, "Error opening file.");
     250   if ((fd = open(filename, O_RDONLY)) == -1)
     251   {
     252      rtems_ftpd_send_reply(450, "Error opening file.");
    295253      return(0);
    296254   }
     
    299257   if (bufr == NULL)
    300258   {
    301       FTPD_SendReply(440, "Server error - malloc fail.");
    302       fclose(fp);
     259      rtems_ftpd_send_reply(440, "Server error - malloc fail.");
     260      close(fd);
    303261      return(0);
    304262   }
     
    307265    * Connect to the data connection (PORT made in an earlier PORT call).
    308266    **********************************************************************/
    309    FTPD_SendReply(150, "BINARY data connection.");
     267   rtems_ftpd_send_reply(150, "BINARY data connection.");
    310268   s = socket(AF_INET, SOCK_STREAM, 0);
    311269   if (connect(s, (struct sockaddr *)&info->data_addr,
    312270               sizeof(struct sockaddr)) < 0)
    313271   {
    314       FTPD_SendReply(420, "Server error - could not connect socket.");
     272      rtems_ftpd_send_reply(420, "Server error - could not connect socket.");
    315273      free(bufr);
    316       fclose(fp);
     274      close(fd);
    317275      close(s);
    318276      return(1);
     
    322280    * Send the data over the ether.
    323281    **********************************************************************/
    324    while ((n = fread(bufr, 1, BUFSIZ, fp)) != 0)
     282   while ((n = read(fd, bufr, BUFSIZ)) > 0)
    325283   {
    326284      send(s, bufr, n, 0);
    327    }
    328 
    329    if (feof(fp))
    330    {
    331       FTPD_SendReply(210, "File sent successfully.");
     285      bufr[n-1] = '\0';
     286   }
     287
     288   if (n == 0)
     289   {
     290      rtems_ftpd_send_reply(210, "File sent successfully.");
    332291   }
    333292   else
    334293   {
    335       FTPD_SendReply(450, "Retrieve failed.");
     294      rtems_ftpd_send_reply(450, "Retrieve failed.");
    336295   }
    337296
     
    342301
    343302   free(bufr);
    344    fclose(fp);
     303   close(fd);
    345304   return(0);
    346305}
     
    348307
    349308/**************************************************************************
    350  * Function: FTPD_CommandStore                                            *
     309 * Function: rtems_ftpd_command_store                                     *
    351310 **************************************************************************
    352311 * Description:                                                           *
     
    371330 *************************************************************************/
    372331static int
    373 FTPD_CommandStore(char *filename)
     332rtems_ftpd_command_store(char *filename)
    374333{
    375    char                *bufr;
    376    char                *bigBufr;
    377    int                 s;
    378    int                 n;
    379    unsigned long       size = 0;
    380    rtems_status_code   sc;
    381    FTPD_SessionInfo_t  *info = NULL;
     334   char                   *bufr;
     335   int                    s;
     336   int                    n;
     337   unsigned long          size = 0;
     338   rtems_status_code      sc;
     339   FTPD_SessionInfo_t     *info = NULL;
     340   struct rtems_ftpd_hook *usehook = NULL;
    382341
    383342
     
    388347   if (bufr == NULL)
    389348   {
    390       FTPD_SendReply(440, "Server error - malloc fail.");
     349      rtems_ftpd_send_reply(440, "Server error - malloc fail.");
    391350      return(1);
    392351   }
    393352
    394    bigBufr = (char *)malloc(FTPD_MAX_RECEIVESIZE * sizeof(char));
    395    if (bigBufr == NULL)
    396    {
    397       FTPD_SendReply(440, "Server error - malloc fail.");
    398       free(bufr);
    399       return(1);
    400    }
    401 
    402    FTPD_SendReply(150, "BINARY data connection.");
     353   rtems_ftpd_send_reply(150, "BINARY data connection.");
    403354
    404355   s = socket(AF_INET, SOCK_STREAM, 0);
     
    407358   {
    408359      free(bufr);
    409       free(bigBufr);
    410360      close(s);
    411361      return(1);
     
    415365   /***********************************************************************
    416366    * File: "/dev/null" just throws the data away.
     367    * Otherwise, search our list of hooks to see if we need to do something
     368    *   special.
    417369    **********************************************************************/
    418370   if (!strncmp("/dev/null", filename, 9))
     
    420372      while ((n = read(s, bufr, BUFSIZ)) > 0);
    421373   }
    422    else
    423    {
     374   else if (rtems_ftpd_configuration.hooks != NULL)
     375   {
     376      struct rtems_ftpd_hook *hook;
     377      int i;
     378
     379      i = 0;
     380      hook = &rtems_ftpd_configuration.hooks[i++];
     381      while (hook->filename != NULL)
     382      {
     383         if (!strcmp(hook->filename, filename))
     384         {
     385            usehook = hook;
     386            break;
     387         }
     388         hook = &rtems_ftpd_configuration.hooks[i++];
     389      }
     390   }
     391
     392   if (usehook != NULL)
     393   {
     394      char                *bigBufr;
     395
     396      /***********************************************************************
     397       * Allocate space for our "file".
     398       **********************************************************************/
     399      bigBufr = (char *)malloc(
     400                  rtems_ftpd_configuration.max_hook_filesize * sizeof(char));
     401      if (bigBufr == NULL)
     402      {
     403         rtems_ftpd_send_reply(440, "Server error - malloc fail.");
     404         free(bufr);
     405         return(1);
     406      }
     407
    424408      /***********************************************************************
    425409       * Retrieve the file into our buffer space.
     
    428412      while ((n = read(s, bufr, BUFSIZ)) > 0)
    429413      {
    430          if (size + n > FTPD_MAX_RECEIVESIZE)
     414         if (size + n >
     415               rtems_ftpd_configuration.max_hook_filesize * sizeof(char))
    431416         {
    432             FTPD_SendReply(440, "Server error - Buffer size exceeded.");
     417            rtems_ftpd_send_reply(440, "Server error - Buffer size exceeded.");
    433418            free(bufr);
    434419            free(bigBufr);
     
    439424         size += n;
    440425      }
    441    }
     426      close(s);
     427
     428      /***********************************************************************
     429       * Call our hook.
     430       **********************************************************************/
     431      if ((usehook->hook_function)(bigBufr, size) == 0)
     432      {
     433         rtems_ftpd_send_reply(210, "File transferred successfully.");
     434      }
     435      else
     436      {
     437         rtems_ftpd_send_reply(440, "File transfer failed.");
     438      }
     439      free(bigBufr);
     440   }
     441   else
     442   {
     443      int    fd;
     444      size_t written;
     445
     446      fd = creat(filename, S_IRUSR | S_IWUSR |
     447                           S_IRGRP | S_IWGRP |
     448                           S_IROTH | S_IWOTH);
     449      if (fd == -1)
     450      {
     451         rtems_ftpd_send_reply(450, "Could not open file.");
     452         close(s);
     453         free(bufr);
     454         return(1);
     455      }
     456      while ((n = read(s, bufr, BUFSIZ)) > 0)
     457      {
     458         written = write(fd, bufr, n);
     459         if (written == -1)
     460         {
     461            rtems_ftpd_send_reply(450, "Error during write.");
     462            close(fd);
     463            close(s);
     464            free(bufr);
     465            return(1);
     466         }
     467      }
     468      close(fd);
     469      close(s);
     470      rtems_ftpd_send_reply(226, "Transfer complete.");
     471   }
     472
    442473   free(bufr);
    443    close(s);
    444 
    445 
    446    /***********************************************************************
    447     * Figure out what to do with the data we just received.
    448     **********************************************************************/
    449    if (!strncmp("untar", filename, 5))
    450    {
    451       Untar_FromMemory(bigBufr, size);
    452       FTPD_SendReply(210, "Untar successful.");
    453    }
    454    else
    455    {
    456       FILE   *fp;
    457       size_t len;
    458       size_t written;
    459 
    460       fp = fopen(filename, "w");
    461       if (fp == NULL)
    462       {
    463          FTPD_SendReply(440, "Could not open file.");
    464          free(bigBufr);
    465          return(1);
    466       }
    467      
    468       n = 0;
    469       written = 0;
    470       while (n<size)
    471       {
    472          len = ((size-n>BUFSIZ)?(BUFSIZ):(size-n));
    473          written = fwrite(&bigBufr[n], 1, len, fp);
    474          n += written;
    475          if (written != len)
    476          {
    477             break;
    478          }
    479       }
    480       fclose(fp);
    481 
    482       if (n == size)
    483       {
    484          FTPD_SendReply(226, "Transfer complete.");
    485       }
    486       else
    487       {
    488          FTPD_SendReply(440, "Error during write.");
    489          free(bigBufr);
    490          return(1);
    491       }
    492    }
    493 
    494    free(bigBufr);
    495474   return(0);
    496475}
     
    498477
    499478/**************************************************************************
    500  * Function: FTPD_CommandList                                             *
     479 * Function: rtems_ftpd_command_list                                      *
    501480 **************************************************************************
    502481 * Description:                                                           *
     
    519498 *************************************************************************/
    520499static void
    521 FTPD_CommandList(char *fname)
     500rtems_ftpd_command_list(char *fname)
    522501{
    523502   int                 s;
     
    533512                            (rtems_unsigned32 *)&info);
    534513
    535    FTPD_SendReply(150, "ASCII data connection for LIST.");
     514   rtems_ftpd_send_reply(150, "ASCII data connection for LIST.");
    536515
    537516   s = socket(AF_INET, SOCK_STREAM, 0);
     
    549528      send(s, dirline, strlen(dirline), 0);
    550529      close(s);
    551       FTPD_SendReply(226, "Transfer complete.");
     530      rtems_ftpd_send_reply(226, "Transfer complete.");
    552531      return;
    553532   }
     
    579558
    580559   close(s);
    581    FTPD_SendReply(226, "Transfer complete.");
     560   rtems_ftpd_send_reply(226, "Transfer complete.");
    582561}
    583562
     
    587566 */
    588567static void
    589 FTPD_CWD(char *dir)
     568rtems_ftpd_CWD(char *dir)
    590569{
    591570   rtems_status_code   sc;
     
    598577   if (chdir(dir) == 0)
    599578   {
    600       FTPD_SendReply(250, "CWD command successful.");
     579      rtems_ftpd_send_reply(250, "CWD command successful.");
    601580   }
    602581   else
    603582   {
    604       FTPD_SendReply(550, "CWD command failed.");
     583      rtems_ftpd_send_reply(550, "CWD command failed.");
    605584   }
    606585}
     
    608587
    609588/**************************************************************************
    610  * Function: FTPD_CommandPort                                             *
     589 * Function: rtems_ftpd_command_port                                      *
    611590 **************************************************************************
    612591 * Description:                                                           *
     
    631610 *************************************************************************/
    632611static void
    633 FTPD_CommandPort(char *bufr)
     612rtems_ftpd_command_port(char *bufr)
    634613{
    635614   char                *ip;
     
    657636
    658637/**************************************************************************
    659  * Function: FTPD_ParseCommand                                            *
     638 * Function: rtems_ftpd_parse_command                                     *
    660639 **************************************************************************
    661640 * Description:                                                           *
     
    664643 *    connection.                                                         *
    665644 *                                                                        *
     645 * FIXME: This section is somewhat of a hack.  We should have a better    *
     646 *        way to parse commands.                                          *
    666647 *                                                                        *
    667648 * Inputs:                                                                *
     
    679660 *************************************************************************/
    680661static void
    681 FTPD_ParseCommand(char *bufr)
     662rtems_ftpd_parse_command(char *bufr)
    682663{
    683664   char fname[255];
     
    691672   if (!strncmp("PORT", bufr, 4))
    692673   {
    693       FTPD_SendReply(200, "PORT command successful.");
    694       FTPD_CommandPort(&bufr[5]);
     674      rtems_ftpd_send_reply(200, "PORT command successful.");
     675      rtems_ftpd_command_port(&bufr[5]);
    695676   }
    696677   else if (!strncmp("RETR", bufr, 4))
    697678   {
    698679      sscanf(&bufr[5], "%254s", fname);
    699       FTPD_CommandRetrieve(fname);
     680      rtems_ftpd_command_retrieve(fname);
    700681   }
    701682   else if (!strncmp("STOR", bufr, 4))
    702683   {
    703684      sscanf(&bufr[5], "%254s", fname);
    704       FTPD_CommandStore(fname);
     685      rtems_ftpd_command_store(fname);
    705686   }
    706687   else if (!strncmp("LIST", bufr, 4))
     
    708689      if (bufr[5] == '\n')
    709690      {
    710          FTPD_CommandList(".");
     691         rtems_ftpd_command_list(".");
    711692      }
    712693      else
    713694      {
    714695         sscanf(&bufr[5], "%254s", fname);
    715          FTPD_CommandList(fname);
     696         rtems_ftpd_command_list(fname);
    716697      }
    717698   }
    718699   else if (!strncmp("USER", bufr, 4))
    719700   {
    720       FTPD_SendReply(230, "User logged in.");
     701      rtems_ftpd_send_reply(230, "User logged in.");
    721702   }
    722703   else if (!strncmp("SYST", bufr, 4))
    723704   {
    724       FTPD_SendReply(240, "RTEMS");
     705      rtems_ftpd_send_reply(240, "RTEMS");
    725706   }
    726707   else if (!strncmp("TYPE", bufr, 4))
     
    729710      {
    730711         info->xfer_mode = TYPE_I;
    731          FTPD_SendReply(200, "Type set to I.");
     712         rtems_ftpd_send_reply(200, "Type set to I.");
    732713      }
    733714      else if (bufr[5] == 'A')
    734715      {
    735716         info->xfer_mode = TYPE_A;
    736          FTPD_SendReply(200, "Type set to A.");
     717         rtems_ftpd_send_reply(200, "Type set to A.");
    737718      }
    738719      else
    739720      {
    740721         info->xfer_mode = TYPE_I;
    741          FTPD_SendReply(504, "Type not implemented.  Set to I.");
     722         rtems_ftpd_send_reply(504, "Type not implemented.  Set to I.");
    742723      }
    743724   }
    744725   else if (!strncmp("PASS", bufr, 4))
    745726   {
    746       FTPD_SendReply(230, "User logged in.");
     727      rtems_ftpd_send_reply(230, "User logged in.");
    747728   }
    748729   else if (!strncmp("DELE", bufr, 4))
     
    751732      if (unlink(fname) == 0)
    752733      {
    753          FTPD_SendReply(257, "DELE successful.");
     734         rtems_ftpd_send_reply(257, "DELE successful.");
    754735      }
    755736      else
    756737      {
    757          FTPD_SendReply(550, "DELE failed.");
     738         rtems_ftpd_send_reply(550, "DELE failed.");
    758739      }
    759740   }
     
    765746      if (chmod(fname, (mode_t)mask) == 0)
    766747      {
    767          FTPD_SendReply(257, "CHMOD successful.");
     748         rtems_ftpd_send_reply(257, "CHMOD successful.");
    768749      }
    769750      else
    770751      {
    771          FTPD_SendReply(550, "CHMOD failed.");
     752         rtems_ftpd_send_reply(550, "CHMOD failed.");
    772753      }
    773754   }
     
    777758      if (rmdir(fname) == 0)
    778759      {
    779          FTPD_SendReply(257, "RMD successful.");
     760         rtems_ftpd_send_reply(257, "RMD successful.");
    780761      }
    781762      else
    782763      {
    783          FTPD_SendReply(550, "RMD failed.");
     764         rtems_ftpd_send_reply(550, "RMD failed.");
    784765      }
    785766   }
     
    789770      if (mkdir(fname, S_IRWXU | S_IRWXG | S_IRWXO) == 0)
    790771      {
    791          FTPD_SendReply(257, "MKD successful.");
     772         rtems_ftpd_send_reply(257, "MKD successful.");
    792773      }
    793774      else
    794775      {
    795          FTPD_SendReply(550, "MKD failed.");
     776         rtems_ftpd_send_reply(550, "MKD failed.");
    796777      }
    797778   }
     
    799780   {
    800781      sscanf(&bufr[4], "%254s", fname);
    801       FTPD_CWD(fname);
     782      rtems_ftpd_CWD(fname);
    802783   }
    803784   else if (!strncmp("PWD", bufr, 3))
     
    805786      char *cwd = getcwd(0, 0);
    806787      sprintf(bufr, "\"%s\" is the current directory.", cwd);
    807       FTPD_SendReply(250, bufr);
     788      rtems_ftpd_send_reply(250, bufr);
    808789      free(cwd);
    809790   }
    810791   else
    811792   {
    812       FTPD_SendReply(500, "Unrecognized/unsupported command.");
     793      rtems_ftpd_send_reply(500, "Unrecognized/unsupported command.");
    813794   }
    814795}
    815796
    816 /**************************************************************************
    817  * Function: FTPD_Session                                                 *
     797
     798/**************************************************************************
     799 * Function: rtems_ftpd_session                                           *
    818800 **************************************************************************
    819801 * Description:                                                           *
     
    828810 * Inputs:                                                                *
    829811 *                                                                        *
    830  *    rtems_task_argument arg - The FTPD_Daemon task passes the socket    *
     812 *    rtems_task_argument arg - The daemon task passes the socket         *
    831813 *                              which serves as the control connection.   *
    832814 *                                                                        *
     
    840822 *************************************************************************/
    841823static void
    842 FTPD_Session(rtems_task_argument arg)
     824rtems_ftpd_session(rtems_task_argument arg)
    843825{
    844826   char                cmd[256];
     
    850832                            (rtems_unsigned32 *)&info);
    851833
    852    FTPD_SendReply(220, "Erithacus FTP server (Version 1.0) ready.");
     834   rtems_ftpd_send_reply(220, FTPD_SERVER_MESSAGE);
    853835
    854836   /***********************************************************************
     
    859841   while (1)
    860842   {
    861       if (recv(info->ctrl_sock, cmd, 256, 0) == -1)
     843      if (fgets(cmd, 256, info->ctrl_fp) == NULL)
    862844      {
    863845         syslog(LOG_INFO, "ftpd: Connection aborted.");
     
    867849      if (!strncmp("QUIT", cmd, 4))
    868850      {
    869          FTPD_SendReply(221, "Goodbye.");
     851         rtems_ftpd_send_reply(221, "Goodbye.");
    870852         break;
    871853      }
    872854      else
    873855      {
    874          FTPD_ParseCommand(cmd);
    875       }
    876    }
    877 
    878    if (close(info->ctrl_sock) < 0)
     856         rtems_ftpd_parse_command(cmd);
     857      }
     858   }
     859
     860   if (fclose(info->ctrl_fp) != 0)
    879861   {
    880862      syslog(LOG_ERR, "ftpd: Could not close session.");
     
    896878
    897879/**************************************************************************
    898  * Function: FTPD_Daemon                                                  *
     880 * Function: rtems_ftpd_daemon                                            *
    899881 **************************************************************************
    900882 * Description:                                                           *
     
    919901 *************************************************************************/
    920902static void
    921 FTPD_Daemon()
     903rtems_ftpd_daemon()
    922904{
    923905   int                 s;
     
    942924
    943925   localAddr.sin_family      = AF_INET;
    944    localAddr.sin_port        = FTPD_CONTROL_PORT;
     926   localAddr.sin_port        = htons(rtems_ftpd_configuration.port);
    945927   localAddr.sin_addr.s_addr = INADDR_ANY;
    946928   memset(localAddr.sin_zero, '\0', sizeof(localAddr.sin_zero));
     
    1003985       * Send the socket on to the new session.
    1004986       *******************************************************************/
    1005       info->ctrl_sock = s1;
    1006       sc = rtems_task_set_note(tid, RTEMS_NOTEPAD_0,
    1007                                (rtems_unsigned32)info);
    1008       sc = rtems_task_start(tid, FTPD_Session, 0);
    1009       if (sc != RTEMS_SUCCESSFUL)
    1010       {
    1011          syslog(LOG_ERR, "ftpd: Could not start FTPD session: %s",
    1012                 rtems_status_text(sc));
     987      if ((info->ctrl_fp = fdopen(s1, "r+")) == NULL)
     988      {
     989         syslog(LOG_ERR, "ftpd: fdopen() on socket failed.");
     990         close(s1);
     991      }
     992      else
     993      {
     994         sc = rtems_task_set_note(tid, RTEMS_NOTEPAD_0,
     995                                  (rtems_unsigned32)info);
     996         sc = rtems_task_start(tid, rtems_ftpd_session, 0);
     997         if (sc != RTEMS_SUCCESSFUL)
     998         {
     999            syslog(LOG_ERR, "ftpd: Could not start FTPD session: %s",
     1000                   rtems_status_text(sc));
     1001         }
    10131002      }
    10141003   }
     
    10171006
    10181007/**************************************************************************
    1019  * Function: FTPD_Start                                                   *
     1008 * Function: rtems_ftpd_start                                             *
    10201009 **************************************************************************
    10211010 * Description:                                                           *
     
    10321021 * Output:                                                                *
    10331022 *                                                                        *
    1034  *    none                                                                *
     1023 *    int - RTEMS_SUCCESSFUL on successful start of the daemon.           *
    10351024 *                                                                        *
    10361025 **************************************************************************
     
    10381027 *  12/01/97 - Creation (JWJ)                                             *
    10391028 *************************************************************************/
    1040 void
    1041 FTPD_Start(rtems_task_priority priority)
     1029int
     1030rtems_initialize_ftpd()
    10421031{
    10431032   rtems_status_code   sc;
     
    10451034
    10461035
     1036   if (rtems_ftpd_configuration.port == 0)
     1037   {
     1038      rtems_ftpd_configuration.port = FTPD_CONTROL_PORT;
     1039   }
     1040
     1041   /***********************************************************************
     1042    * Default FTPD priority.
     1043    **********************************************************************/
     1044   if (rtems_ftpd_configuration.priority == 0)
     1045   {
     1046      rtems_ftpd_configuration.priority = 40;
     1047   }
    10471048   sc = rtems_task_create(rtems_build_name('F', 'T', 'P', 'D'),
    1048                           priority, 8*1024,
     1049                          rtems_ftpd_configuration.priority, 8*1024,
    10491050                          RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR |
    10501051                          RTEMS_INTERRUPT_LEVEL(0),
     
    10551056      syslog(LOG_ERR, "ftpd: Could not create FTP daemon: %s",
    10561057             rtems_status_text(sc));
    1057    }
    1058 
    1059    sc = rtems_task_start(tid, FTPD_Daemon, 0);
     1058      return(RTEMS_UNSATISFIED);
     1059   }
     1060
     1061   sc = rtems_task_start(tid, rtems_ftpd_daemon, 0);
    10601062   if (sc != RTEMS_SUCCESSFUL)
    10611063   {
    10621064      syslog(LOG_ERR, "ftpd: Could not start FTP daemon: %s",
    10631065             rtems_status_text(sc));
     1066      return(RTEMS_UNSATISFIED);
    10641067   }   
    10651068
    10661069   syslog(LOG_INFO, "ftpd: FTP daemon started.");
     1070   return(RTEMS_SUCCESSFUL);
    10671071}
     1072
Note: See TracChangeset for help on using the changeset viewer.