2011-06-05 16:08:26 +02:00
|
|
|
/****************************************************************************
|
2021-06-08 20:05:01 +02:00
|
|
|
* apps/examples/ftpc/ftpc_main.c
|
2011-06-05 16:08:26 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-06-05 16:08:26 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-06-05 16:08:26 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-06-05 16:08:26 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2021-06-08 20:05:01 +02:00
|
|
|
#include <assert.h>
|
2012-02-01 20:47:12 +01:00
|
|
|
#include <errno.h>
|
2011-06-05 16:08:26 +02:00
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
2011-06-05 16:08:26 +02:00
|
|
|
#include <arpa/inet.h>
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/ftpc.h"
|
2011-06-05 16:08:26 +02:00
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "system/readline.h"
|
2012-02-01 20:47:12 +01:00
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
#include "ftpc.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define FTPC_MAX_ARGUMENTS 4
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-05-15 01:41:09 +02:00
|
|
|
* Private Types
|
2011-06-05 16:08:26 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct cmdmap_s
|
|
|
|
{
|
|
|
|
const char *cmd; /* Name of the command */
|
|
|
|
cmd_t handler; /* Function that handles the command */
|
|
|
|
uint8_t minargs; /* Minimum number of arguments (including command) */
|
|
|
|
uint8_t maxargs; /* Maximum number of arguments (including command) */
|
|
|
|
const char *usage; /* Usage instructions for 'help' command */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const char g_delim[] = " \t\n";
|
|
|
|
|
|
|
|
static int cmd_lhelp(SESSION handle, int argc, char **argv);
|
|
|
|
static int cmd_lunrecognized(SESSION handle, int argc, char **argv);
|
|
|
|
|
|
|
|
static const struct cmdmap_s g_cmdmap[] =
|
|
|
|
{
|
|
|
|
{ "cd", cmd_rchdir, 2, 2, "<directory>" },
|
|
|
|
{ "chmod", cmd_rchmod, 3, 3, "<permissions> <path>" },
|
|
|
|
{ "get", cmd_rget, 2, 4, "[-a|b] <rname> [<lname>]" },
|
|
|
|
{ "help", cmd_lhelp, 1, 2, "" },
|
|
|
|
{ "idle", cmd_ridle, 1, 2, "[<idletime>]" },
|
|
|
|
{ "login", cmd_rlogin, 2, 3, "<uname> [<password>]" },
|
|
|
|
{ "ls", cmd_rls, 1, 2, "[<dirpath>]" },
|
|
|
|
{ "quit", cmd_rquit, 1, 1, "" },
|
|
|
|
{ "mkdir", cmd_rmkdir, 2, 2, "<directory>" },
|
|
|
|
{ "noop", cmd_rnoop, 1, 1, "" },
|
|
|
|
{ "put", cmd_rput, 2, 4, "[-a|b] <lname> [<rname>]" },
|
|
|
|
{ "pwd", cmd_rpwd, 1, 1, "" },
|
|
|
|
{ "rename", cmd_rrename, 3, 3, "<oldname> <newname>" },
|
|
|
|
{ "rhelp", cmd_rhelp, 1, 2, "[<command>]" },
|
|
|
|
{ "rm", cmd_runlink, 2, 2, "" },
|
|
|
|
{ "rmdir", cmd_rrmdir, 2, 2, "<directory>" },
|
|
|
|
{ "size", cmd_rsize, 2, 2, "<filepath>" },
|
|
|
|
{ "time", cmd_rtime, 2, 2, "<filepath>" },
|
|
|
|
{ "up", cmd_rcdup, 1, 1, "" },
|
|
|
|
{ NULL, NULL, 1, 1, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static char g_line[CONFIG_FTPC_LINELEN];
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_lhelp
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cmd_lhelp(SESSION handle, int argc, char **argv)
|
|
|
|
{
|
|
|
|
const struct cmdmap_s *ptr;
|
|
|
|
|
|
|
|
printf("Local FTPC commands:\n");
|
|
|
|
for (ptr = g_cmdmap; ptr->cmd; ptr++)
|
|
|
|
{
|
|
|
|
if (ptr->usage)
|
|
|
|
{
|
|
|
|
printf(" %s %s\n", ptr->cmd, ptr->usage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf(" %s\n", ptr->cmd);
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 01:41:09 +02:00
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_lunrecognized
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cmd_lunrecognized(SESSION handle, int argc, char **argv)
|
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
fprintf(stderr, "Command %s unrecognized\n", argv[0]);
|
2011-06-05 16:08:26 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_argument
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
char *ftpc_argument(char **saveptr)
|
|
|
|
{
|
|
|
|
char *pbegin = *saveptr;
|
|
|
|
char *pend = NULL;
|
|
|
|
const char *term;
|
|
|
|
|
|
|
|
/* Find the beginning of the next token */
|
|
|
|
|
|
|
|
for (;
|
|
|
|
*pbegin && strchr(g_delim, *pbegin) != NULL;
|
|
|
|
pbegin++);
|
|
|
|
|
|
|
|
/* If we are at the end of the string with nothing
|
|
|
|
* but delimiters found, then return NULL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!*pbegin)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does the token begin with '#' -- comment */
|
|
|
|
|
|
|
|
else if (*pbegin == '#')
|
|
|
|
{
|
|
|
|
/* Return NULL meaning that we are at the end of the line */
|
|
|
|
|
|
|
|
*saveptr = pbegin;
|
|
|
|
pbegin = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, we are going to have to parse to find the end of
|
|
|
|
* the token. Does the token begin with '"'?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*pbegin == '"')
|
|
|
|
{
|
|
|
|
/* Yes.. then only another '"' can terminate the string */
|
|
|
|
|
|
|
|
pbegin++;
|
|
|
|
term = "\"";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-15 01:41:09 +02:00
|
|
|
/* No, then any of the usual terminators
|
|
|
|
* will terminate the argument
|
|
|
|
*/
|
2011-06-05 16:08:26 +02:00
|
|
|
|
|
|
|
term = g_delim;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the end of the string */
|
|
|
|
|
|
|
|
for (pend = pbegin + 1;
|
|
|
|
*pend && strchr(term, *pend) == NULL;
|
|
|
|
pend++);
|
|
|
|
|
|
|
|
/* pend either points to the end of the string or to
|
|
|
|
* the first delimiter after the string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*pend)
|
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
/* Turn the delimiter into a NUL terminator */
|
2011-06-05 16:08:26 +02:00
|
|
|
|
|
|
|
*pend++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the pointer where we left off */
|
|
|
|
|
|
|
|
*saveptr = pend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the beginning of the token. */
|
|
|
|
|
|
|
|
return pbegin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_execute
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftpc_execute(SESSION handle, int argc, char *argv[])
|
|
|
|
{
|
2020-05-15 01:41:09 +02:00
|
|
|
const struct cmdmap_s *cmdmap;
|
|
|
|
const char *cmd;
|
|
|
|
cmd_t handler = cmd_lunrecognized;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* The form of argv is:
|
|
|
|
*
|
|
|
|
* argv[0]: The command name. This is argv[0] when the arguments
|
|
|
|
* are, finally, received by the command handler
|
|
|
|
* argv[1]: The beginning of argument (up to FTPC_MAX_ARGUMENTS)
|
|
|
|
* argv[argc]: NULL terminating pointer
|
|
|
|
*/
|
|
|
|
|
|
|
|
cmd = argv[0];
|
|
|
|
|
|
|
|
/* See if the command is one that we understand */
|
|
|
|
|
|
|
|
for (cmdmap = g_cmdmap; cmdmap->cmd; cmdmap++)
|
|
|
|
{
|
|
|
|
if (strcmp(cmdmap->cmd, cmd) == 0)
|
|
|
|
{
|
|
|
|
/* Check if a valid number of arguments was provided. We
|
|
|
|
* do this simple, imperfect checking here so that it does
|
|
|
|
* not have to be performed in each command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (argc < cmdmap->minargs)
|
|
|
|
{
|
|
|
|
/* Fewer than the minimum number were provided */
|
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
fprintf(stderr, "Too few arguments for '%s'\n", cmd);
|
2020-05-15 01:41:09 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
else if (argc > cmdmap->maxargs)
|
|
|
|
{
|
|
|
|
/* More than the maximum number were provided */
|
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
fprintf(stderr, "Too many arguments for '%s'\n", cmd);
|
2020-05-15 01:41:09 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* A valid number of arguments were provided (this does
|
|
|
|
* not mean they are right).
|
|
|
|
*/
|
|
|
|
|
|
|
|
handler = cmdmap->handler;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = handler(handle, argc, argv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
fprintf(stderr, "%s failed: %d\n", cmd, errno);
|
2020-05-15 01:41:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2011-06-05 16:08:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_parse
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpc_parse(SESSION handle, char *cmdline)
|
|
|
|
{
|
2020-05-15 01:41:09 +02:00
|
|
|
FAR char *argv[FTPC_MAX_ARGUMENTS + 1];
|
2011-06-05 16:08:26 +02:00
|
|
|
FAR char *saveptr;
|
|
|
|
FAR char *cmd;
|
|
|
|
int argc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize parser state */
|
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
memset(argv, 0, FTPC_MAX_ARGUMENTS * sizeof(FAR char *));
|
2011-06-05 16:08:26 +02:00
|
|
|
|
|
|
|
/* Parse out the command at the beginning of the line */
|
|
|
|
|
|
|
|
saveptr = cmdline;
|
|
|
|
cmd = ftpc_argument(&saveptr);
|
|
|
|
|
|
|
|
/* Check if any command was provided -OR- if command processing is
|
|
|
|
* currently disabled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!cmd)
|
|
|
|
{
|
|
|
|
/* An empty line is not an error */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse all of the arguments following the command name. */
|
|
|
|
|
|
|
|
argv[0] = cmd;
|
|
|
|
for (argc = 1; argc < FTPC_MAX_ARGUMENTS; argc++)
|
|
|
|
{
|
|
|
|
argv[argc] = ftpc_argument(&saveptr);
|
|
|
|
if (!argv[argc])
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 01:41:09 +02:00
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
argv[argc] = NULL;
|
|
|
|
|
|
|
|
/* Check if the maximum number of arguments was exceeded */
|
|
|
|
|
|
|
|
if (argc > FTPC_MAX_ARGUMENTS)
|
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
fprintf(stderr, "Too many arguments\n");
|
2011-06-05 16:08:26 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Then execute the command */
|
|
|
|
|
|
|
|
ret = ftpc_execute(handle, argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: ftpc [-46n] host [port]\n\
|
|
|
|
\t-4 Use IPv4\n\
|
|
|
|
\t-6 Use IPv6\n\
|
|
|
|
\t-n Allow numeric IP address only\n\
|
|
|
|
");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2011-06-05 16:08:26 +02:00
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
union ftpc_sockaddr_u server;
|
2011-06-05 16:08:26 +02:00
|
|
|
SESSION handle;
|
2020-12-14 14:59:57 +01:00
|
|
|
char *host = NULL;
|
|
|
|
char *port = NULL;
|
|
|
|
#ifdef CONFIG_LIBC_NETDB
|
|
|
|
struct addrinfo hints;
|
|
|
|
FAR struct addrinfo *info;
|
|
|
|
FAR struct addrinfo *next;
|
2017-11-07 00:41:30 +01:00
|
|
|
#endif
|
2020-12-14 14:59:57 +01:00
|
|
|
int option;
|
2012-02-01 20:47:12 +01:00
|
|
|
int ret;
|
2020-12-14 14:59:57 +01:00
|
|
|
int family = AF_UNSPEC;
|
|
|
|
bool nflag = false;
|
|
|
|
bool badarg = false;
|
2011-06-05 16:08:26 +02:00
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
memset(&server, 0, sizeof(union ftpc_sockaddr_u));
|
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
while ((option = getopt(argc, argv, "46n")) != ERROR)
|
2011-06-05 16:08:26 +02:00
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case '4':
|
|
|
|
family = AF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
family = AF_INET6;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nflag = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
usage();
|
2017-11-07 00:41:30 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
/* There should be one or two parameters remaining on the command line */
|
2017-11-07 00:41:30 +01:00
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
if (optind >= argc)
|
2017-11-07 00:41:30 +01:00
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
fprintf(stderr, "%s: Missing required arguments\n", argv[0]);
|
|
|
|
usage();
|
2011-06-05 16:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
host = argv[optind];
|
|
|
|
optind++;
|
|
|
|
|
|
|
|
if (optind < argc)
|
2017-11-07 00:41:30 +01:00
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
port = argv[optind];
|
|
|
|
optind++;
|
2017-11-07 00:41:30 +01:00
|
|
|
}
|
2011-06-05 16:08:26 +02:00
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
if (optind != argc)
|
2011-06-05 16:08:26 +02:00
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
fprintf(stderr, "%s: Too many arguments\n", argv[0]);
|
|
|
|
usage();
|
2011-06-05 16:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
#ifdef CONFIG_LIBC_NETDB
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = family;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
if (nflag)
|
2017-11-07 00:41:30 +01:00
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
hints.ai_flags |= AI_NUMERICHOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We now get all addresses for hostname or IP address from the
|
|
|
|
* command line.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = getaddrinfo(host, port, &hints, &info);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: getaddrinfo: %s\n", gai_strerror(ret));
|
2017-11-07 00:41:30 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2020-12-14 14:59:57 +01:00
|
|
|
|
|
|
|
for (next = info; next != NULL; next = next->ai_next)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (next->ai_family == AF_INET6)
|
|
|
|
{
|
|
|
|
memcpy(&server.in6, next->ai_addr, next->ai_addrlen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (next->ai_family == AF_INET)
|
|
|
|
{
|
|
|
|
memcpy(&server.in4, next->ai_addr, next->ai_addrlen);
|
|
|
|
}
|
2017-11-07 00:41:30 +01:00
|
|
|
#endif
|
2011-06-05 16:08:26 +02:00
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
/* Connect to the FTP server */
|
|
|
|
|
|
|
|
handle = ftpc_connect(&server);
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(info);
|
|
|
|
if (!handle)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to connect to the server: %d\n", errno);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* No getaddrinfo(), use IP address only, implies nflag. */
|
|
|
|
|
|
|
|
UNUSED(nflag);
|
|
|
|
DEBUGASSERT(host != NULL);
|
|
|
|
|
|
|
|
/* Try IPv6 first, then IPv4. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (family != AF_INET)
|
|
|
|
{
|
|
|
|
ret = inet_pton(AF_INET6, host, &server.in6.sin6_addr);
|
|
|
|
if (ret == 1)
|
|
|
|
{
|
|
|
|
server.in6.sin6_family = AF_INET6;
|
|
|
|
if (port != NULL)
|
|
|
|
{
|
|
|
|
server.in6.sin6_port = htons(atoi(port));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto do_connect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (family != AF_INET6)
|
|
|
|
{
|
|
|
|
ret = inet_pton(AF_INET, host, &server.in4.sin_addr);
|
|
|
|
if (ret == 1)
|
|
|
|
{
|
|
|
|
server.in4.sin_family = AF_INET;
|
|
|
|
if (port != NULL)
|
|
|
|
{
|
|
|
|
server.in4.sin_port = htons(atoi(port));
|
|
|
|
}
|
2011-06-05 16:08:26 +02:00
|
|
|
|
2020-12-14 14:59:57 +01:00
|
|
|
goto do_connect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
/* Did not get a valid address. */
|
|
|
|
|
|
|
|
fprintf(stderr, "ERROR: Invalid IP address\n");
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
/* Connect to the FTP server. */
|
|
|
|
|
|
|
|
do_connect:
|
2017-11-07 00:41:30 +01:00
|
|
|
handle = ftpc_connect(&server);
|
2011-06-05 16:08:26 +02:00
|
|
|
if (!handle)
|
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
fprintf(stderr, "ERROR: Failed to connect to the server: %d\n", errno);
|
2011-06-05 16:08:26 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2020-12-14 14:59:57 +01:00
|
|
|
#endif /* CONFIG_LIBC_NETDB */
|
2011-06-05 16:08:26 +02:00
|
|
|
|
|
|
|
/* Present a greeting */
|
|
|
|
|
|
|
|
printf("NuttX FTP Client:\n");
|
|
|
|
FFLUSH();
|
|
|
|
|
|
|
|
/* Then enter the command line parsing loop */
|
|
|
|
|
2020-05-15 01:41:09 +02:00
|
|
|
for (; ; )
|
2011-06-05 16:08:26 +02:00
|
|
|
{
|
|
|
|
/* Display the prompt string */
|
|
|
|
|
|
|
|
fputs("nfc> ", stdout);
|
|
|
|
FFLUSH();
|
|
|
|
|
|
|
|
/* Get the next line of input */
|
|
|
|
|
2012-02-01 20:47:12 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_FTPC_FGETS
|
|
|
|
/* fgets returns NULL on end-of-file or any I/O error */
|
|
|
|
|
|
|
|
if (fgets(g_line, CONFIG_FTPC_LINELEN, stdin) == NULL)
|
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
fprintf(stderr, "ERROR: fgets failed: %d\n", errno);
|
2012-02-01 20:47:12 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ret = readline(g_line, CONFIG_FTPC_LINELEN, stdin, stdout);
|
|
|
|
|
|
|
|
/* Readline normally returns the number of characters read,
|
2013-02-10 01:46:27 +01:00
|
|
|
* but will return EOF on end of file or if an error occurs.
|
|
|
|
* Either will cause the session to terminate.
|
2012-02-01 20:47:12 +01:00
|
|
|
*/
|
|
|
|
|
2013-02-10 01:46:27 +01:00
|
|
|
if (ret == EOF)
|
2012-02-01 20:47:12 +01:00
|
|
|
{
|
2020-12-14 14:59:57 +01:00
|
|
|
fprintf(stderr, "ERROR: readline failed: %d\n", errno);
|
2012-02-01 20:47:12 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
2011-06-05 16:08:26 +02:00
|
|
|
{
|
2012-02-01 20:47:12 +01:00
|
|
|
/* Parse and process the command */
|
2011-06-05 16:08:26 +02:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
ftpc_parse(handle, g_line);
|
2011-06-05 16:08:26 +02:00
|
|
|
FFLUSH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|