Ejemplo n.º 1
0
int fptc_getreply(struct ftpc_session_s *session)
{
  char tmp[5]="xxx ";
  int ret;

  /* Set up a timeout */
  
  if (session->replytimeo)
    {
      ret = wd_start(session->wdog, session->replytimeo, ftpc_timeout, 1, session);
    }

  /* Get the next line from the server */

  ret = ftpc_gets(session);

  /* Do we still have a connection? */

  if (!ftpc_sockconnected(&session->cmd))
    {
      /* No.. cancel the timer and return an error */

      wd_cancel(session->wdog);
      nvdbg("Lost connection\n");
      return ERROR;
    }

  /* Did an error occur? */

  if (ret < 0)
    {
      /* No.. cancel the timer and return an error */

      wd_cancel(session->wdog);
      nvdbg("ftpc_gets failed\n");
      return ERROR;
    }

  nvdbg("Reply: %s\n", session->reply);        

  if (session->reply[3] == '-')
    {
      /* Multi-line response */

      strncpy(tmp, session->reply, 3);
      do
        {
          if (ftpc_gets(session) == -1)
            {
              break;
            }

          nvdbg("Reply: %s\n", session->reply);
        }
      while (strncmp(tmp, session->reply, 4) != 0);
    }

  wd_cancel(session->wdog);
  return ret;
}
Ejemplo n.º 2
0
void ftpc_sockcopy(FAR struct ftpc_socket_s *dest,
                   FAR const struct ftpc_socket_s *src)
{
  memcpy(&dest->laddr, &src->laddr, sizeof(struct sockaddr_in));
  dest->connected = ftpc_sockconnected(src);
}
Ejemplo n.º 3
0
static int ftpc_gets(struct ftpc_session_s *session)
{
  int ch;
  int ndx = 0;

  /* Start wth an empty response string */

  session->reply[0] = '\0';

  /* Verify that the command channel is still connected */

  if (!ftpc_sockconnected(&session->cmd))
    {
      ndbg("Cmd channel disconnected\n");
      return ERROR;
    }

  /* Loop until the full line is obtained */

  for (;;)
    {
      /* Get the next character from incoming command stream */

      ch = ftpc_sockgetc(&session->cmd);

      /* Check if the command stream was closed */

      if (ch == EOF)
        {
          ndbg("EOF: Server closed command stream\n");
          ftpc_reset(session);
          return ERROR;
        }

      /* Handle embedded Telnet stuff */

      else if (ch == TELNET_IAC)
        {
          /* Handle TELNET commands */

          switch(ch = ftpc_sockgetc(&session->cmd))
            {
            case TELNET_WILL:
            case TELNET_WONT:
              ch = ftpc_sockgetc(&session->cmd);
              ftpc_sockprintf(&session->cmd, "%c%c%c", TELNET_IAC, TELNET_DONT, ch);
              ftpc_sockflush(&session->cmd);
              break;

            case TELNET_DO:
            case TELNET_DONT:
              ch = ftpc_sockgetc(&session->cmd);
              ftpc_sockprintf(&session->cmd, "%c%c%c", TELNET_IAC, TELNET_WONT, ch);
              ftpc_sockflush(&session->cmd);
              break;

            default:
            break;
            }

          continue;
        }

      /* Deal with carriage returns */

      else if (ch == ISO_cr)
        {
          /* What follows the carriage return? */
 
          ch = ftpc_sockgetc(&session->cmd);
          if (ch == '\0')
            {
              /* If it is followed by a NUL then keep it */

              ch = ISO_cr;
            }

          /* If it is followed by a newline then break out of the loop. */

          else if (ch == ISO_nl)
            {
              /* Newline terminates the reply */

              break;
            }

          /* If we did not lose the connection, then push the character
           * following the carriage back on the "stack" and continue to
           * examine it from scratch (if could be part of the Telnet
           * protocol).
           */

          else if (ch != EOF)
            {
              ungetc(ch, session->cmd.instream);
              continue;
            }
        }

      else if (ch == ISO_nl)
        {
          /* The ISO newline character terminates the string.  Just break
           * out of the loop.
           */

          break;
        }

      /* Put the character into the response buffer.  Is there space for
       * another character in the reply buffer?
       */

      if (ndx < CONFIG_FTP_MAXREPLY)
        {
          /* Yes.. put the character in the reply buffer */
 
          session->reply[ndx++] = (char)ch;
        }
      else
        {
          ndbg("Reply truncated\n");
        }
    }

  session->reply[ndx] = '\0';
  session->code = atoi(session->reply);
  return session->code;
}