ftpfs: Import from RTEMS

RTEMS Git commit 251c94d3d3d27e0039f01b718e5c2eb06f39fdf7.
This commit is contained in:
Sebastian Huber 2016-06-09 11:19:09 +02:00
parent 71f66e761d
commit 68d406b3b8
5 changed files with 1865 additions and 0 deletions

View File

@ -107,6 +107,7 @@ def rtems(mm):
'rtems/rtems-kvm.c',
'rtems/syslog.c',
'ftpd/ftpd.c',
'ftpfs/ftpfs.c',
'mdns/mdns.c',
'mdns/mdns-hostname-default.c',
'pppd/auth.c',
@ -2428,6 +2429,7 @@ def tests(mm):
runTest = False, netTest = True))
mod.addTest(mm.generator['test']('unix01', ['test_main']))
mod.addTest(mm.generator['test']('ftpd01', ['test_main'], netTest = True))
mod.addTest(mm.generator['test']('ftpd02', ['test_main']))
mod.addTest(mm.generator['test']('ping01', ['test_main'], netTest = True))
mod.addTest(mm.generator['test']('selectpollkqueue01', ['test_main']))
mod.addTest(mm.generator['test']('rwlock01', ['test_main']))

View File

@ -943,6 +943,7 @@ def build(bld):
'mDNSResponder/mDNSShared/dnssd_clientshim.c',
'mDNSResponder/mDNSShared/mDNSDebug.c',
'rtemsbsd/ftpd/ftpd.c',
'rtemsbsd/ftpfs/ftpfs.c',
'rtemsbsd/local/bus_if.c',
'rtemsbsd/local/cryptodev_if.c',
'rtemsbsd/local/device_if.c',
@ -1192,6 +1193,16 @@ def build(bld):
lib = ["m", "z"],
install_path = None)
test_ftpd02 = ['testsuite/ftpd02/test_main.c']
bld.program(target = "ftpd02.exe",
features = "cprogram",
cflags = cflags,
includes = includes,
source = test_ftpd02,
use = ["bsd"],
lib = ["m", "z"],
install_path = None)
test_init01 = ['testsuite/init01/test_main.c']
bld.program(target = "init01.exe",
features = "cprogram",

1428
rtemsbsd/ftpfs/ftpfs.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,159 @@
/**
* @file
*
* @brief File Transfer Protocol file system (FTP client).
*/
/*
* Copyright (c) 2009
* embedded brains GmbH
* Obere Lagerstr. 30
* D-82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* (c) Copyright 2002
* Thomas Doerfler
* IMD Ingenieurbuero fuer Microcomputertechnik
* Herbststr. 8
* 82178 Puchheim, Germany
* <Thomas.Doerfler@imd-systems.de>
*
* Modified by Sebastian Huber <sebastian.huber@embedded-brains.de>.
*
* This code has been created after closly inspecting "tftpdriver.c" from Eric
* Norum.
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.org/license/LICENSE.
*/
#ifndef _RTEMS_FTPFS_H
#define _RTEMS_FTPFS_H
#include <sys/time.h>
#include <sys/ioctl.h>
#include <rtems/libio.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup rtems_ftpfs File Transfer Protocol File System
*
* @brief The FTP file system (FTP client) can be used to transfer files from
* or to remote hosts.
*
* You can mount the FTP file system with a call to mount() or
* mount_and_make_target_path() with the @ref RTEMS_FILESYSTEM_TYPE_FTPFS file
* system type.
*
* You have to add @ref CONFIGURE_FILESYSTEM_FTPFS to your application
* configuration.
*
* You can open files either read-only or write-only. A seek is not allowed.
* A close terminates the control and data connections.
*
* To open a file @c file.txt in the directory @c dir (relative to home
* directory of the server) on a server named @c host using the user name
* @c user and the password @c pw you must specify the following path:
* <tt>/FTP/user:pw@@host/dir/file.txt</tt>.
*
* If the server is the default server specified in BOOTP, it can be ommitted:
* <tt>/FTP/user:pw/dir/file.txt</tt>.
*
* The user name will be used for the password if it is ommitted:
* <tt>/FTP/user@@host/dir/file.txt</tt>.
*
* For the data transfer passive (= default) and active (= fallback) mode are
* supported.
*/
/**@{**/
/**
* @brief Well-known port number for FTP control connection.
*/
#define RTEMS_FTPFS_CTRL_PORT 21
/**
* @brief Default mount point for FTP file system.
*/
#define RTEMS_FTPFS_MOUNT_POINT_DEFAULT "/FTP"
/**
* @brief FTP file system IO control requests.
*/
typedef enum {
RTEMS_FTPFS_IOCTL_GET_VERBOSE = _IOR( 'd', 1, bool *),
RTEMS_FTPFS_IOCTL_SET_VERBOSE = _IOW( 'd', 1, bool *),
RTEMS_FTPFS_IOCTL_GET_TIMEOUT = _IOR( 'd', 2, struct timeval *),
RTEMS_FTPFS_IOCTL_SET_TIMEOUT = _IOW( 'd', 2, struct timeval *)
} rtems_ftpfs_ioctl_numbers;
/**
* @brief Returns in @a verbose if the verbose mode is enabled or disabled for
* the file system at @a mount_point.
*
* If @a mount_point is @c NULL the default mount point
* @ref RTEMS_FTPFS_MOUNT_POINT_DEFAULT will be used.
*/
rtems_status_code rtems_ftpfs_get_verbose( const char *mount_point, bool *verbose);
/**
* @brief Enables or disables the verbose mode if @a verbose is @c true or
* @c false respectively for the file system at @a mount_point.
*
* In the enabled verbose mode the commands and replies of the FTP control
* connections will be printed to standard error.
*
* If @a mount_point is @c NULL the default mount point
* @ref RTEMS_FTPFS_MOUNT_POINT_DEFAULT will be used.
*/
rtems_status_code rtems_ftpfs_set_verbose( const char *mount_point, bool verbose);
/**
* @brief Returns the current timeout value in @a timeout for the file system
* at @a mount_point.
*
* If @a mount_point is @c NULL the default mount point
* @ref RTEMS_FTPFS_MOUNT_POINT_DEFAULT will be used.
*/
rtems_status_code rtems_ftpfs_get_timeout(
const char *mount_point,
struct timeval *timeout
);
/**
* @brief Sets the timeout value to @a timeout for the file system at
* @a mount_point.
*
* The timeout value will be used during connection establishment of active
* data connections. It will be also used for send and receive operations on
* data and control connections.
*
* If @a mount_point is @c NULL the default mount point
* @ref RTEMS_FTPFS_MOUNT_POINT_DEFAULT will be used.
*/
rtems_status_code rtems_ftpfs_set_timeout(
const char *mount_point,
const struct timeval *timeout
);
/** @} */
/**
* @brief Do not call directly, use mount().
*/
int rtems_ftpfs_initialize(
rtems_filesystem_mount_table_entry_t *mt_entry,
const void *data
);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,265 @@
/*
* Copyright (c) 2011, 2016 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
*/
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <stdlib.h>
#include <rtems.h>
#include <rtems/ftpd.h>
#include <rtems/ftpfs.h>
#include <rtems/shell.h>
#include <rtems/console.h>
#include <machine/rtems-bsd-commands.h>
#define TEST_NAME "LIBBSD FTPD 2"
#define FTP_WORKER_TASK_COUNT 2
#define FTP_WORKER_TASK_EXTRA_STACK (FTP_WORKER_TASK_COUNT * FTPD_STACKSIZE)
struct rtems_ftpd_configuration rtems_ftpd_configuration = {
/* FTPD task priority */
.priority = 100,
/* Maximum buffersize for hooks */
.max_hook_filesize = 0,
/* Well-known port */
.port = 21,
/* List of hooks */
.hooks = NULL,
/* Root for FTPD or NULL for "/" */
.root = NULL,
/* Max. connections */
.tasks_count = 4,
/* Idle timeout in seconds or 0 for no (infinite) timeout */
.idle = 5 * 60,
/* Access: 0 - r/w, 1 - read-only, 2 - write-only, 3 - browse-only */
.access = 0
};
static const char content[] =
" LICENSE INFORMATION\n"
"\n"
"RTEMS is free software; you can redistribute it and/or modify it under\n"
"terms of the GNU General Public License as published by the\n"
"Free Software Foundation; either version 2, or (at your option) any\n"
"later version. RTEMS is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
"General Public License for more details. You should have received\n"
"a copy of the GNU General Public License along with RTEMS; see\n"
"file COPYING. If not, write to the Free Software Foundation, 675\n"
"Mass Ave, Cambridge, MA 02139, USA.\n"
"\n"
"As a special exception, including RTEMS header files in a file,\n"
"instantiating RTEMS generics or templates, or linking other files\n"
"with RTEMS objects to produce an executable application, does not\n"
"by itself cause the resulting executable application to be covered\n"
"by the GNU General Public License. This exception does not\n"
"however invalidate any other reasons why the executable file might be\n"
"covered by the GNU Public License.\n";
static void
initialize_ftpfs(void)
{
rtems_status_code sc = RTEMS_SUCCESSFUL;
int rv = 0;
struct timeval to = {
.tv_sec = 10,
.tv_usec = 0
};
const char *target = RTEMS_FTPFS_MOUNT_POINT_DEFAULT;
rv = mount_and_make_target_path(
NULL,
target,
RTEMS_FILESYSTEM_TYPE_FTPFS,
RTEMS_FILESYSTEM_READ_WRITE,
NULL
);
assert(rv == 0);
sc = rtems_ftpfs_set_verbose(target, true);
assert(sc == RTEMS_SUCCESSFUL);
sc = rtems_ftpfs_set_timeout(target, &to);
assert(sc == RTEMS_SUCCESSFUL);
}
static void
change_self_priority(void)
{
rtems_status_code sc = RTEMS_SUCCESSFUL;
rtems_task_priority cur = 0;
sc = rtems_task_set_priority(RTEMS_SUCCESSFUL, 110, &cur);
assert(sc == RTEMS_SUCCESSFUL);
}
static void
create_file(const char *path, const void *begin, size_t size)
{
int rv = 0;
int fd = open(path, O_WRONLY);
ssize_t n = 0;
assert(fd >= 0);
n = write(fd, begin, size);
assert(n == (ssize_t) size);
rv = close(fd);
assert(rv == 0);
}
static void
copy_file(const char *src_path, const char *dest_path)
{
int rv = 0;
int in = open(src_path, O_RDONLY);
int out = open(dest_path, O_WRONLY);
ssize_t n_in = 0;
char buf [64];
struct stat st_in;
struct stat st_out;
memset(&st_in, 0xff, sizeof(st_in));
memset(&st_out, 0xff, sizeof(st_out));
assert(in >= 0);
assert(out >= 0);
rv = fstat(out, &st_out);
assert(rv == 0);
assert(st_out.st_size == 0);
while ((n_in = read(in, buf, sizeof(buf))) > 0) {
ssize_t n_out = write(out, buf, (size_t) n_in);
assert(n_out == n_in);
}
rv = fstat(out, &st_out);
assert(rv == 0);
rv = fstat(in, &st_in);
assert(rv == 0);
assert(st_in.st_size == st_out.st_size);
rv = close(out);
assert(rv == 0);
rv = close(in);
assert(rv == 0);
}
static void
check_file_size(const char *path, size_t size)
{
struct stat st;
int rv = lstat(path, &st);
assert(rv == 0);
assert(st.st_size == (off_t) size);
}
static void
check_file(const char *path)
{
int rv = 0;
int fd = open(path, O_RDONLY);
ssize_t n = 0;
char buf [64];
const char *current = &content [0];
size_t done = 0;
assert(fd >= 0);
while ((n = read(fd, buf, sizeof(buf))) > 0) {
done += (size_t) n;
assert(done <= sizeof(content));
assert(memcmp(current, buf, (size_t) n) == 0);
current += (size_t) n;
}
assert(done == sizeof(content));
rv = close(fd);
assert(rv == 0);
}
static void
test_main(void)
{
const char file_a[] = "/FTP/127.0.0.1/a.txt";
const char file_b[] = "/FTP/127.0.0.1/b.txt";
char *lo0[] = {
"ifconfig",
"lo0",
"inet",
"127.0.0.1",
"netmask",
"255.255.255.0",
NULL
};
int rv;
int exit_code;
exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(lo0), lo0);
assert(exit_code == EXIT_SUCCESS);
rv = rtems_initialize_ftpd();
assert(rv == 0);
initialize_ftpfs();
change_self_priority();
create_file(file_a, &content [0], sizeof(content));
copy_file(file_a, file_b);
check_file(file_b);
check_file_size(file_a, sizeof(content));
check_file_size(file_b, sizeof(content));
exit(0);
}
#define CONFIGURE_FILESYSTEM_FTPFS
#include <rtems/bsd/test/default-init.h>