FTPServer.c

FTPServer.c example details
FTP Server description

/***************************************************************************//**
@file FTPServer.c
@brief FTP server example
@verbatim
_ _ _
__| | ___(_) ____ _ __ | |_
/ _` | / __| |/ _` | '_ \| __|
| (_| | _ \__ \ | (_| | | | | |_
\__,_|(_) ___/_|\__, |_| |_|\__|
Signalprocessing |___/ Technology
@endverbatim
@author D.SignT GmbH & Co. KG, Claus Hermbusche
@date 2019-06-03
@anchor FTPSERVEX
@details
This program acts as a FTP server (port 21) with either fix IP 192.168.168.200
(configurable in netconfig.c) or DHCP assigned address (e.g. 192.168.168.143,
host name mydemo).
To verify this program, first 'ping' the DSP:
>ping 192.168.168.200
or, if configured for DHCP
>ping mydemo
If the settings and network connections are correct, the pings will be
replied.
In this application 3 users are configured for FTP access:
user: admin password: xxxx
user: guest password: -
user: anonymous password: -
Use a FTP client to access the configured directories. Examples for
defining users, directories and files are shown below.
Dedicated FTP clients (like FileZilla or WsFTP) allow to configure different
users and different passwords. Opposed to that kind of clients the browser
built-in clients allow to configure only one account (usually the anonymous
or guest account). Use the following address in your browser to access this
server as admin:
ftp://admin@192.168.168.200
or
ftp://admin@mydemo
enter in the new opened dialogue window the admin password 'xxxx' to access
the FTP server.
Important note for F28xx users: activate compiler define "USE_FLASH_API" for
this application, otherwise FLASH uploads wont be possible.
@cond Software License Agreement
Copyright (C) 2001-2019 D.SignT GmbH & Co. KG - http://www.dsignt.de
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
Neither the name of D.SignT GmbH & Co. KG nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
Disclaimer
THIS SOFTWARE IS PROVIDED BY D.SIGNT GMBH & CO. KG "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL D.SIGNT GMBH & CO. KG BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@endcond
*******************************************************************************/
/*******************************************************************************
include stdtypes.h to avoid data type mismatch
*******************************************************************************/
/*******************************************************************************
include Runtime Source
*******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>
#include <time.h>
#include <string.h>
/*******************************************************************************
network support functions
*******************************************************************************/
#include <Libs/NETlib/net.h> /* D.Module network support */
#include <Libs/NETlib/ftplib.h> /* FTP support */
/*******************************************************************************
board specific functions
*******************************************************************************/
#include <BoardSupport/inc/BoardSpecific.h> /* board support functions */
/*******************************************************************************
common support functions
*******************************************************************************/
#include <Common/Common.h> /* on exit function */
#include <Common/uartio.h> /* UART support */
#include <Common/timer.h> /* timer setting */
#include <Common/CPrintf.h> /* CPrintf defines */
/*******************************************************************************
network configuration
*******************************************************************************/
#include <BoardSupport/config/netconfig.c> /* network configuration */
/*******************************************************************************
Embedded File System content
*******************************************************************************/
#include "efs.def"
#ifdef __cplusplus
extern "C" {
#endif /* !__cplusplus */
/*******************************************************************************
local prototypes
*******************************************************************************/
#pragma CODE_SECTION(FTP_CheckClients , ".commontext");
#pragma CODE_SECTION(FTP_PrintResult , ".commontext");
#ifdef __cplusplus
} // extern "C"
#endif
/*******************************************************************************
* *
* DEFINES *
* *
*******************************************************************************/
/* - */
/*******************************************************************************
* *
* GLOBALS *
* *
*******************************************************************************/
/*******************************************************************************
program name
*******************************************************************************/
char *program_name = "FTP-server";
/*******************************************************************************
time stamp for efs
*******************************************************************************/
char *_datetime = __DATE__" "__TIME__;
/*******************************************************************************
* *
* FUNCTIONS *
* *
*******************************************************************************/
/*******************************************************************************
@brief check connected clients (just for info)
@param -
@return TRUE (1) - client status changed
@return FALSE (0) - client status not changed
*******************************************************************************/
{
/***************************************************************************
locals
***************************************************************************/
static uint32_t clients;
uint32_t ret;
/***************************************************************************
get current connections
***************************************************************************/
if (ret != clients)
{
/***********************************************************************
connection status changed
***********************************************************************/
clients = ret;
CPrintf ("\r\n connection status changed: %"PRId32" client(s) connected\r\n", clients);
/***********************************************************************
activate to monitor dynamic memory utilization
***********************************************************************/
// memmap (initial_heap_size);
return (1);
}
return (0);
}
/*******************************************************************************
@brief print FTP server result
@param -
@return nothing
*******************************************************************************/
{
/***************************************************************************
locals
***************************************************************************/
char buffer[25]; /* small buffer for conversion */
direntry_type *fp; /* EFS file pointer */
switch ( ftp_ret-> reason )
{
case FTP_STOR_CMD :
CPuts (" File upload... ");
break;
case FTP_BOOT_CMD :
CPuts (" Boot command... ");
// check boot address
if (ftp_ret-> offset >= 0x30000)
boot_address = ftp_ret-> offset;
else
ftp_ret-> error_code = FSYS_OUT_OF_RANGE_ERROR;
return;
case FTP_DELE_CMD :
CPuts (" Delete command... ");
break;
case FTP_PASS_CMD :
CPrintf (" New connection established from %s\r\n", inet_ntoa(ftp_ret-> offset, buffer));
return;
case FTP_QUIT_CMD :
return;
// case TELNET_DOWNLOAD :
default:
break;
}
/***************************************************************************
further details on event
***************************************************************************/
switch ( ftp_ret-> error_code )
{
/*******************************************************************
success
*******************************************************************/
fp = ftp_ret-> fp;
if (fp==NULL) break; /* no details available */
CPrintf (VT100_GREEN" successful\r\n"VT100_DEFAULT);
if (ftp_ret-> offset==0)
{
if (!strncmp(fp->dir,"0:",2))
{
CPuts (" RAM-disc:\r\n");
}
if (!strncmp(fp->dir,"1:",2))
{
CPuts (" NAND-disc:\r\n");
}
#ifdef USE_SPIDRIVE
if (!strncmp(fp->dir,"2:",2))
{
CPuts (" SD-card:\r\n");
}
#endif
}
else
{
CPrintf (" EFS offset: 0x%08"PRIx32"\r\n", ftp_ret-> offset);
}
CPrintf (" file name: %s\r\n", fp-> name);
CPrintf (" file size: %"PRId32"\r\n", ftp_ret-> fp-> size);
break;
default:
/*******************************************************************
print error
*******************************************************************/
net_print_error (ftp_ret-> error_code, CPrintf);
break;
}
}
/*******************************************************************************
@brief Main application
@param -
@return never
*******************************************************************************/
#pragma CODE_SECTION(main , ".commontext");
int main ( void )
{
/***************************************************************************
locals
***************************************************************************/
ftp_return_type *ftp_ret; /* FTP server result */
int main_loop = 1; /* main loop switch, set to 0 to exit */
timeval stamp1, stamp2, delta; /* used to determine startup time */
uint32_t ftp_serv;
/***************************************************************************
initialize application (e.g. timer clocks, PLL settings, EMIF etc.)
(ref. \Common\Common.c)
***************************************************************************/
/***************************************************************************
select output device for CPrintf (ref. \Common\cprintf.c)
possible settings:
CPRINTF_UART_OUTPUT -> output to UART
CPRINTF_CCS_OUTPUT -> output to CCS
CPRINTF_UART_OUTPUT | CPRINTF_CCS_OUTPUT -> output to UART and CCS
***************************************************************************/
/***************************************************************************
print a start up message
***************************************************************************/
/**************************************************************************/
// CPrintfProgress (" Heap check ");
// at least 0x8000 bytes required for this app
/**************************************************************************/
// CPrintfProgressSuccess();
/**************************************************************************/
CPrintfProgress (" Setup system time ");
// 1 milli seconds resolution
/**************************************************************************/
CPrintf (" *** timer %d mapped to CPU int %d ***\r\n",
/**************************************************************************/
CPrintfProgress (" Enable interrupts ");
/**************************************************************************/
/**************************************************************************/
CPrintfProgress (" Start system timer ");
/**************************************************************************/
CPrintf (" *** timer %d running at %"PRId32" Hz ***\r\n", SystemTimerDev, RES_SECONDS/GetSystemTimerRes());
/***************************************************************************
measure network initialization time
***************************************************************************/
stamp1 = GetTimeStamp();
/**************************************************************************/
CPrintfProgress (" Initialize network ");
/**************************************************************************/
InitializeNetwork (64); // 64 bytes for ping
/***************************************************************************
initialize ftp server; parameter is the specified user list
***************************************************************************/
ftp_serv = ftp_server_init ( user, /* specified user list */
NULL, /* mmc support */
FTP_NO_LIMIT); /* max parallel connections */
/* 0 == no limit (heap limit only) */
if ( ftp_serv == FALSE )
{
prg_exit ("ftp_server_init() failed"); /* possibly insufficient heap */
}
/***************************************************************************
activate passive transfer mode. useful for clients behind a firewall.
this may be a security issue. if your network administrator does not
allow the passive mode, deactivate the following line.
***************************************************************************/
// ftp_set_passive_mode (1);
/***************************************************************************
define a connection time-out for FTP server
***************************************************************************/
ftp_server_timeout (FTP_SERVER_TIMEOUT); /* appr. 5 minutes */
stamp2 = GetTimeStamp();
tv_interval (&delta, &stamp1, &stamp2);
CPuts (" network startup time [sec]: ");
"%"PRId32".%03"PRId32"\r\n"
delta.tv_sec,
delta.tv_usec/1000);
CPuts ("\r\n 3 different users have FTP access:\r\n\n");
CPuts (" user: admin password: xxxx\r\n");
CPuts (" user: guest password: -\r\n");
CPuts (" user: anonymous password: -\r\n\n");
/***************************************************************************
entering endless loop...
***************************************************************************/
CPrintf (" %s running: waiting for connection...\r\n", program_name);
/***************************************************************************
main program loop: set main_loop to 0 to exit loop
***************************************************************************/
while ( main_loop )
{
/***********************************************************************
try to detect IP assignment
if DHCP is used, the assigned IP address may change
***********************************************************************/
/***********************************************************************
process net_isq()
***********************************************************************/
net_isq ();
/***********************************************************************
monitor link status
***********************************************************************/
/***********************************************************************
check client connections
***********************************************************************/
/***********************************************************************
process ftp_server()
***********************************************************************/
ftp_ret = ftp_server ();
if ((uint32_t)ftp_ret > FTP_NO_ERROR)
{
/*******************************************************************
print result
*******************************************************************/
FTP_PrintResult (ftp_ret);
{
main_loop = 0;
}
}
/***********************************************************************
show that the program is running, perform symbol animation
***********************************************************************/
}
{
// DM2_delay (1000);
// DM2_bootload (boot_address);
}
/***************************************************************************
exit program, shut down peripherals
***************************************************************************/
return (0);
}