From 116c6cf409e7a6d366f824018a84fd27d2a306c3 Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Fri, 3 Aug 2012 08:40:27 -0500 Subject: ftpfs: Add to services --- freebsd-userspace/include/netdb.h | 297 ------- freebsd-userspace/rtems/include/netdb.h | 297 +++++++ services/ftpfs/Makefile | 38 + services/ftpfs/ftpfs.c | 1317 +++++++++++++++++++++++++++++++ services/ftpfs/include/rtems/ftpfs.h | 160 ++++ 5 files changed, 1812 insertions(+), 297 deletions(-) delete mode 100644 freebsd-userspace/include/netdb.h create mode 100644 freebsd-userspace/rtems/include/netdb.h create mode 100644 services/ftpfs/Makefile create mode 100644 services/ftpfs/ftpfs.c create mode 100644 services/ftpfs/include/rtems/ftpfs.h diff --git a/freebsd-userspace/include/netdb.h b/freebsd-userspace/include/netdb.h deleted file mode 100644 index c15b2845..00000000 --- a/freebsd-userspace/include/netdb.h +++ /dev/null @@ -1,297 +0,0 @@ -/*- - * Copyright (c) 1980, 1983, 1988, 1993 - * The Regents of the University of California. All rights reserved. - * - * 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. - * - * - - * Portions Copyright (c) 1993 by Digital Equipment Corporation. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies, and that - * the name of Digital Equipment Corporation not be used in advertising or - * publicity pertaining to distribution of the document or software without - * specific, written prior permission. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL - * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT - * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL - * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR - * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS - * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS - * SOFTWARE. - * - - * --Copyright-- - */ - -/* - * @(#)netdb.h 8.1 (Berkeley) 6/2/93 - * From: Id: netdb.h,v 8.9 1996/11/19 08:39:29 vixie Exp $ - * $FreeBSD$ - */ - -#ifndef _NETDB_H_ -#define _NETDB_H_ - -#include -#include - -#ifndef _SIZE_T_DECLARED -typedef __size_t size_t; -#define _SIZE_T_DECLARED -#endif - -#ifndef _SOCKLEN_T_DECLARED -typedef __socklen_t socklen_t; -#define _SOCKLEN_T_DECLARED -#endif - -#ifndef _UINT32_T_DECLARED -typedef __uint32_t uint32_t; -#define _UINT32_T_DECLARED -#endif - -#ifndef _PATH_HEQUIV -# define _PATH_HEQUIV "/etc/hosts.equiv" -#endif -#define _PATH_HOSTS "/etc/hosts" -#define _PATH_NETWORKS "/etc/networks" -#define _PATH_PROTOCOLS "/etc/protocols" -#define _PATH_SERVICES "/etc/services" -#define _PATH_SERVICES_DB "/var/db/services.db" - -#define h_errno (*__h_errno()) - -/* - * Structures returned by network data base library. All addresses are - * supplied in host order, and returned in network order (suitable for - * use in system calls). - */ -struct hostent { - char *h_name; /* official name of host */ - char **h_aliases; /* alias list */ - int h_addrtype; /* host address type */ - int h_length; /* length of address */ - char **h_addr_list; /* list of addresses from name server */ -#define h_addr h_addr_list[0] /* address, for backward compatibility */ -}; - -struct netent { - char *n_name; /* official name of net */ - char **n_aliases; /* alias list */ - int n_addrtype; /* net address type */ - uint32_t n_net; /* network # */ -}; - -struct servent { - char *s_name; /* official service name */ - char **s_aliases; /* alias list */ - int s_port; /* port # */ - char *s_proto; /* protocol to use */ -}; - -struct protoent { - char *p_name; /* official protocol name */ - char **p_aliases; /* alias list */ - int p_proto; /* protocol # */ -}; - -struct addrinfo { - int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */ - int ai_family; /* PF_xxx */ - int ai_socktype; /* SOCK_xxx */ - int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ - socklen_t ai_addrlen; /* length of ai_addr */ - char *ai_canonname; /* canonical name for hostname */ - struct sockaddr *ai_addr; /* binary address */ - struct addrinfo *ai_next; /* next structure in linked list */ -}; - -/* - * Error return codes from gethostbyname() and gethostbyaddr() - * (left in h_errno). - */ - -#define NETDB_INTERNAL -1 /* see errno */ -#define NETDB_SUCCESS 0 /* no problem */ -#define HOST_NOT_FOUND 1 /* Authoritative Answer Host not found */ -#define TRY_AGAIN 2 /* Non-Authoritative Host not found, or SERVERFAIL */ -#define NO_RECOVERY 3 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */ -#define NO_DATA 4 /* Valid name, no data record of requested type */ -#define NO_ADDRESS NO_DATA /* no address, look for MX record */ - -/* - * Error return codes from getaddrinfo() - */ -#if 0 -/* obsoleted */ -#define EAI_ADDRFAMILY 1 /* address family for hostname not supported */ -#endif -#define EAI_AGAIN 2 /* temporary failure in name resolution */ -#define EAI_BADFLAGS 3 /* invalid value for ai_flags */ -#define EAI_FAIL 4 /* non-recoverable failure in name resolution */ -#define EAI_FAMILY 5 /* ai_family not supported */ -#define EAI_MEMORY 6 /* memory allocation failure */ -#if 0 -/* obsoleted */ -#define EAI_NODATA 7 /* no address associated with hostname */ -#endif -#define EAI_NONAME 8 /* hostname nor servname provided, or not known */ -#define EAI_SERVICE 9 /* servname not supported for ai_socktype */ -#define EAI_SOCKTYPE 10 /* ai_socktype not supported */ -#define EAI_SYSTEM 11 /* system error returned in errno */ -#define EAI_BADHINTS 12 /* invalid value for hints */ -#define EAI_PROTOCOL 13 /* resolved protocol is unknown */ -#define EAI_OVERFLOW 14 /* argument buffer overflow */ -#define EAI_MAX 15 - -/* - * Flag values for getaddrinfo() - */ -#define AI_PASSIVE 0x00000001 /* get address to use bind() */ -#define AI_CANONNAME 0x00000002 /* fill ai_canonname */ -#define AI_NUMERICHOST 0x00000004 /* prevent host name resolution */ -#define AI_NUMERICSERV 0x00000008 /* prevent service name resolution */ -/* valid flags for addrinfo (not a standard def, apps should not use it) */ -#define AI_MASK \ - (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV | \ - AI_ADDRCONFIG) - -#define AI_ALL 0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */ -#define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */ -#define AI_ADDRCONFIG 0x00000400 /* only if any address is assigned */ -#define AI_V4MAPPED 0x00000800 /* accept IPv4-mapped IPv6 address */ -/* special recommended flags for getipnodebyname */ -#define AI_DEFAULT (AI_V4MAPPED_CFG | AI_ADDRCONFIG) - -/* - * Constants for getnameinfo() - */ -#define NI_MAXHOST 1025 -#define NI_MAXSERV 32 - -/* - * Flag values for getnameinfo() - */ -#define NI_NOFQDN 0x00000001 -#define NI_NUMERICHOST 0x00000002 -#define NI_NAMEREQD 0x00000004 -#define NI_NUMERICSERV 0x00000008 -#define NI_DGRAM 0x00000010 -#if 0 /* obsolete */ -#define NI_WITHSCOPEID 0x00000020 -#endif - -/* - * Scope delimit character - */ -#define SCOPE_DELIMITER '%' - -__BEGIN_DECLS -void endhostent(void); -void endnetent(void); -void endprotoent(void); -void endservent(void); -#if __BSD_VISIBLE || (__POSIX_VISIBLE && __POSIX_VISIBLE <= 200112) -struct hostent *gethostbyaddr(const void *, socklen_t, int); -struct hostent *gethostbyname(const char *); -#endif -struct hostent *gethostent(void); -struct netent *getnetbyaddr(uint32_t, int); -struct netent *getnetbyname(const char *); -struct netent *getnetent(void); -struct protoent *getprotobyname(const char *); -struct protoent *getprotobynumber(int); -struct protoent *getprotoent(void); -struct servent *getservbyname(const char *, const char *); -struct servent *getservbyport(int, const char *); -struct servent *getservent(void); -void sethostent(int); -/* void sethostfile(const char *); */ -void setnetent(int); -void setprotoent(int); -int getaddrinfo(const char *, const char *, - const struct addrinfo *, struct addrinfo **); -int getnameinfo(const struct sockaddr *, socklen_t, char *, - size_t, char *, size_t, int); -void freeaddrinfo(struct addrinfo *); -const char *gai_strerror(int); -void setservent(int); - -#if __BSD_VISIBLE -void endnetgrent(void); -void freehostent(struct hostent *); -int gethostbyaddr_r(const void *, socklen_t, int, struct hostent *, - char *, size_t, struct hostent **, int *); -int gethostbyname_r(const char *, struct hostent *, char *, size_t, - struct hostent **, int *); -struct hostent *gethostbyname2(const char *, int); -int gethostbyname2_r(const char *, int, struct hostent *, char *, - size_t, struct hostent **, int *); -int gethostent_r(struct hostent *, char *, size_t, - struct hostent **, int *); -struct hostent *getipnodebyaddr(const void *, size_t, int, int *); -struct hostent *getipnodebyname(const char *, int, int, int *); -int getnetbyaddr_r(uint32_t, int, struct netent *, char *, size_t, - struct netent**, int *); -int getnetbyname_r(const char *, struct netent *, char *, size_t, - struct netent **, int *); -int getnetent_r(struct netent *, char *, size_t, struct netent **, - int *); -int getnetgrent(char **, char **, char **); -int getprotobyname_r(const char *, struct protoent *, char *, - size_t, struct protoent **); -int getprotobynumber_r(int, struct protoent *, char *, size_t, - struct protoent **); -int getprotoent_r(struct protoent *, char *, size_t, - struct protoent **); -int getservbyname_r(const char *, const char *, struct servent *, - char *, size_t, struct servent **); -int getservbyport_r(int, const char *, struct servent *, char *, - size_t, struct servent **); -int getservent_r(struct servent *, char *, size_t, - struct servent **); -void herror(const char *); -__const char *hstrerror(int); -int innetgr(const char *, const char *, const char *, const char *); -void setnetgrent(const char *); -#endif - - -/* - * PRIVATE functions specific to the FreeBSD implementation - */ - -/* DO NOT USE THESE, THEY ARE SUBJECT TO CHANGE AND ARE NOT PORTABLE!!! */ -int * __h_errno(void); -__END_DECLS - -#endif /* !_NETDB_H_ */ diff --git a/freebsd-userspace/rtems/include/netdb.h b/freebsd-userspace/rtems/include/netdb.h new file mode 100644 index 00000000..c15b2845 --- /dev/null +++ b/freebsd-userspace/rtems/include/netdb.h @@ -0,0 +1,297 @@ +/*- + * Copyright (c) 1980, 1983, 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * - + * Portions Copyright (c) 1993 by Digital Equipment Corporation. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies, and that + * the name of Digital Equipment Corporation not be used in advertising or + * publicity pertaining to distribution of the document or software without + * specific, written prior permission. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + * - + * --Copyright-- + */ + +/* + * @(#)netdb.h 8.1 (Berkeley) 6/2/93 + * From: Id: netdb.h,v 8.9 1996/11/19 08:39:29 vixie Exp $ + * $FreeBSD$ + */ + +#ifndef _NETDB_H_ +#define _NETDB_H_ + +#include +#include + +#ifndef _SIZE_T_DECLARED +typedef __size_t size_t; +#define _SIZE_T_DECLARED +#endif + +#ifndef _SOCKLEN_T_DECLARED +typedef __socklen_t socklen_t; +#define _SOCKLEN_T_DECLARED +#endif + +#ifndef _UINT32_T_DECLARED +typedef __uint32_t uint32_t; +#define _UINT32_T_DECLARED +#endif + +#ifndef _PATH_HEQUIV +# define _PATH_HEQUIV "/etc/hosts.equiv" +#endif +#define _PATH_HOSTS "/etc/hosts" +#define _PATH_NETWORKS "/etc/networks" +#define _PATH_PROTOCOLS "/etc/protocols" +#define _PATH_SERVICES "/etc/services" +#define _PATH_SERVICES_DB "/var/db/services.db" + +#define h_errno (*__h_errno()) + +/* + * Structures returned by network data base library. All addresses are + * supplied in host order, and returned in network order (suitable for + * use in system calls). + */ +struct hostent { + char *h_name; /* official name of host */ + char **h_aliases; /* alias list */ + int h_addrtype; /* host address type */ + int h_length; /* length of address */ + char **h_addr_list; /* list of addresses from name server */ +#define h_addr h_addr_list[0] /* address, for backward compatibility */ +}; + +struct netent { + char *n_name; /* official name of net */ + char **n_aliases; /* alias list */ + int n_addrtype; /* net address type */ + uint32_t n_net; /* network # */ +}; + +struct servent { + char *s_name; /* official service name */ + char **s_aliases; /* alias list */ + int s_port; /* port # */ + char *s_proto; /* protocol to use */ +}; + +struct protoent { + char *p_name; /* official protocol name */ + char **p_aliases; /* alias list */ + int p_proto; /* protocol # */ +}; + +struct addrinfo { + int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */ + int ai_family; /* PF_xxx */ + int ai_socktype; /* SOCK_xxx */ + int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ + socklen_t ai_addrlen; /* length of ai_addr */ + char *ai_canonname; /* canonical name for hostname */ + struct sockaddr *ai_addr; /* binary address */ + struct addrinfo *ai_next; /* next structure in linked list */ +}; + +/* + * Error return codes from gethostbyname() and gethostbyaddr() + * (left in h_errno). + */ + +#define NETDB_INTERNAL -1 /* see errno */ +#define NETDB_SUCCESS 0 /* no problem */ +#define HOST_NOT_FOUND 1 /* Authoritative Answer Host not found */ +#define TRY_AGAIN 2 /* Non-Authoritative Host not found, or SERVERFAIL */ +#define NO_RECOVERY 3 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */ +#define NO_DATA 4 /* Valid name, no data record of requested type */ +#define NO_ADDRESS NO_DATA /* no address, look for MX record */ + +/* + * Error return codes from getaddrinfo() + */ +#if 0 +/* obsoleted */ +#define EAI_ADDRFAMILY 1 /* address family for hostname not supported */ +#endif +#define EAI_AGAIN 2 /* temporary failure in name resolution */ +#define EAI_BADFLAGS 3 /* invalid value for ai_flags */ +#define EAI_FAIL 4 /* non-recoverable failure in name resolution */ +#define EAI_FAMILY 5 /* ai_family not supported */ +#define EAI_MEMORY 6 /* memory allocation failure */ +#if 0 +/* obsoleted */ +#define EAI_NODATA 7 /* no address associated with hostname */ +#endif +#define EAI_NONAME 8 /* hostname nor servname provided, or not known */ +#define EAI_SERVICE 9 /* servname not supported for ai_socktype */ +#define EAI_SOCKTYPE 10 /* ai_socktype not supported */ +#define EAI_SYSTEM 11 /* system error returned in errno */ +#define EAI_BADHINTS 12 /* invalid value for hints */ +#define EAI_PROTOCOL 13 /* resolved protocol is unknown */ +#define EAI_OVERFLOW 14 /* argument buffer overflow */ +#define EAI_MAX 15 + +/* + * Flag values for getaddrinfo() + */ +#define AI_PASSIVE 0x00000001 /* get address to use bind() */ +#define AI_CANONNAME 0x00000002 /* fill ai_canonname */ +#define AI_NUMERICHOST 0x00000004 /* prevent host name resolution */ +#define AI_NUMERICSERV 0x00000008 /* prevent service name resolution */ +/* valid flags for addrinfo (not a standard def, apps should not use it) */ +#define AI_MASK \ + (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV | \ + AI_ADDRCONFIG) + +#define AI_ALL 0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */ +#define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */ +#define AI_ADDRCONFIG 0x00000400 /* only if any address is assigned */ +#define AI_V4MAPPED 0x00000800 /* accept IPv4-mapped IPv6 address */ +/* special recommended flags for getipnodebyname */ +#define AI_DEFAULT (AI_V4MAPPED_CFG | AI_ADDRCONFIG) + +/* + * Constants for getnameinfo() + */ +#define NI_MAXHOST 1025 +#define NI_MAXSERV 32 + +/* + * Flag values for getnameinfo() + */ +#define NI_NOFQDN 0x00000001 +#define NI_NUMERICHOST 0x00000002 +#define NI_NAMEREQD 0x00000004 +#define NI_NUMERICSERV 0x00000008 +#define NI_DGRAM 0x00000010 +#if 0 /* obsolete */ +#define NI_WITHSCOPEID 0x00000020 +#endif + +/* + * Scope delimit character + */ +#define SCOPE_DELIMITER '%' + +__BEGIN_DECLS +void endhostent(void); +void endnetent(void); +void endprotoent(void); +void endservent(void); +#if __BSD_VISIBLE || (__POSIX_VISIBLE && __POSIX_VISIBLE <= 200112) +struct hostent *gethostbyaddr(const void *, socklen_t, int); +struct hostent *gethostbyname(const char *); +#endif +struct hostent *gethostent(void); +struct netent *getnetbyaddr(uint32_t, int); +struct netent *getnetbyname(const char *); +struct netent *getnetent(void); +struct protoent *getprotobyname(const char *); +struct protoent *getprotobynumber(int); +struct protoent *getprotoent(void); +struct servent *getservbyname(const char *, const char *); +struct servent *getservbyport(int, const char *); +struct servent *getservent(void); +void sethostent(int); +/* void sethostfile(const char *); */ +void setnetent(int); +void setprotoent(int); +int getaddrinfo(const char *, const char *, + const struct addrinfo *, struct addrinfo **); +int getnameinfo(const struct sockaddr *, socklen_t, char *, + size_t, char *, size_t, int); +void freeaddrinfo(struct addrinfo *); +const char *gai_strerror(int); +void setservent(int); + +#if __BSD_VISIBLE +void endnetgrent(void); +void freehostent(struct hostent *); +int gethostbyaddr_r(const void *, socklen_t, int, struct hostent *, + char *, size_t, struct hostent **, int *); +int gethostbyname_r(const char *, struct hostent *, char *, size_t, + struct hostent **, int *); +struct hostent *gethostbyname2(const char *, int); +int gethostbyname2_r(const char *, int, struct hostent *, char *, + size_t, struct hostent **, int *); +int gethostent_r(struct hostent *, char *, size_t, + struct hostent **, int *); +struct hostent *getipnodebyaddr(const void *, size_t, int, int *); +struct hostent *getipnodebyname(const char *, int, int, int *); +int getnetbyaddr_r(uint32_t, int, struct netent *, char *, size_t, + struct netent**, int *); +int getnetbyname_r(const char *, struct netent *, char *, size_t, + struct netent **, int *); +int getnetent_r(struct netent *, char *, size_t, struct netent **, + int *); +int getnetgrent(char **, char **, char **); +int getprotobyname_r(const char *, struct protoent *, char *, + size_t, struct protoent **); +int getprotobynumber_r(int, struct protoent *, char *, size_t, + struct protoent **); +int getprotoent_r(struct protoent *, char *, size_t, + struct protoent **); +int getservbyname_r(const char *, const char *, struct servent *, + char *, size_t, struct servent **); +int getservbyport_r(int, const char *, struct servent *, char *, + size_t, struct servent **); +int getservent_r(struct servent *, char *, size_t, + struct servent **); +void herror(const char *); +__const char *hstrerror(int); +int innetgr(const char *, const char *, const char *, const char *); +void setnetgrent(const char *); +#endif + + +/* + * PRIVATE functions specific to the FreeBSD implementation + */ + +/* DO NOT USE THESE, THEY ARE SUBJECT TO CHANGE AND ARE NOT PORTABLE!!! */ +int * __h_errno(void); +__END_DECLS + +#endif /* !_NETDB_H_ */ diff --git a/services/ftpfs/Makefile b/services/ftpfs/Makefile new file mode 100644 index 00000000..bad88034 --- /dev/null +++ b/services/ftpfs/Makefile @@ -0,0 +1,38 @@ +include ../../config.inc + +include $(RTEMS_MAKEFILE_PATH)/Makefile.inc +include $(RTEMS_CUSTOM) +include $(PROJECT_ROOT)/make/leaf.cfg + +CFLAGS += -I $(INSTALL_BASE)/include + +CFLAGS += -w +CFLAGS += -I include +CFLAGS += -std=gnu99 +CFLAGS += -MT $@ -MD -MP -MF $(basename $@).d + +C_FILES = +C_FILES += ftpfs.c + +C_O_FILES = $(C_FILES:%.c=%.o) +C_D_FILES = $(C_FILES:%.c=%.d) + +LIB = libftpfs.a + +all: $(LIB) + +$(LIB): $(C_O_FILES) + $(AR) rcu $@ $^ + +install: $(LIB) + install -d $(INSTALL_BASE)/include/rtems + install -c -m 644 include/rtems/ftpfs.h $(INSTALL_BASE)/ + install -c -m 644 $(LIB) $(INSTALL_BASE) + +clean: + rm -f $(LIB) $(C_O_FILES) $(C_D_FILES) $(GEN_FILES) + +-include $(C_D_FILES) + +doc: + diff --git a/services/ftpfs/ftpfs.c b/services/ftpfs/ftpfs.c new file mode 100644 index 00000000..3fd0ffa6 --- /dev/null +++ b/services/ftpfs/ftpfs.c @@ -0,0 +1,1317 @@ +/** + * @file + * + * File Transfer Protocol file system (FTP client). + */ + +/* + * Copyright (c) 2009-2012 + * embedded brains GmbH + * Obere Lagerstr. 30 + * D-82178 Puchheim + * Germany + * + * + * (c) Copyright 2002 + * Thomas Doerfler + * IMD Ingenieurbuero fuer Microcomputertechnik + * Herbststr. 8 + * 82178 Puchheim, Germany + * + * + * Modified by Sebastian Huber . + * + * 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.com/license/LICENSE. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#ifdef DEBUG + #define DEBUG_PRINTF(...) printf(__VA_ARGS__) +#else + #define DEBUG_PRINTF(...) +#endif + +/** + * Connection entry for each open file stream. + */ +typedef struct { + /** + * Control connection socket. + */ + int ctrl_socket; + + /** + * Data transfer socket. + */ + int data_socket; + + /** + * Current index into the reply buffer. + */ + size_t reply_current; + + /** + * End index of the reply buffer. + */ + size_t reply_end; + + /** + * Buffer for relpy data. + */ + char reply_buffer [128]; + + /** + * End of file flag. + */ + bool eof; +} rtems_ftpfs_entry; + +/** + * Mount entry for each file system instance. + */ +typedef struct { + /** + * Verbose mode enabled or disabled. + */ + bool verbose; + + /** + * Timeout value + */ + struct timeval timeout; +} rtems_ftpfs_mount_entry; + +static const rtems_filesystem_operations_table rtems_ftpfs_ops; + +static const rtems_filesystem_file_handlers_r rtems_ftpfs_handlers; + +static const rtems_filesystem_file_handlers_r rtems_ftpfs_root_handlers; + +static bool rtems_ftpfs_use_timeout(const struct timeval *to) +{ + return to->tv_sec != 0 || to->tv_usec != 0; +} + +static int rtems_ftpfs_set_connection_timeout( + int socket, + const struct timeval *to +) +{ + if (rtems_ftpfs_use_timeout(to)) { + int rv = 0; + + rv = setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, to, sizeof(*to)); + if (rv != 0) { + return EIO; + } + + rv = setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, to, sizeof(*to)); + if (rv != 0) { + return EIO; + } + } + + return 0; +} + +static rtems_status_code rtems_ftpfs_do_ioctl( + const char *mount_point, + ioctl_command_t req, + ... +) +{ + rtems_status_code sc = RTEMS_SUCCESSFUL; + int rv = 0; + int fd = 0; + va_list ap; + + if (mount_point == NULL) { + mount_point = RTEMS_FTPFS_MOUNT_POINT_DEFAULT; + } + + fd = open(mount_point, O_RDWR); + if (fd < 0) { + return RTEMS_INVALID_NAME; + } + + va_start(ap, req); + rv = ioctl(fd, req, va_arg(ap, void *)); + va_end(ap); + if (rv != 0) { + sc = RTEMS_INVALID_NUMBER; + } + + rv = close(fd); + if (rv != 0 && sc == RTEMS_SUCCESSFUL) { + sc = RTEMS_IO_ERROR; + } + + return sc; +} + +rtems_status_code rtems_ftpfs_get_verbose(const char *mount_point, bool *verbose) +{ + return rtems_ftpfs_do_ioctl( + mount_point, + RTEMS_FTPFS_IOCTL_GET_VERBOSE, + verbose + ); +} + +rtems_status_code rtems_ftpfs_set_verbose(const char *mount_point, bool verbose) +{ + return rtems_ftpfs_do_ioctl( + mount_point, + RTEMS_FTPFS_IOCTL_SET_VERBOSE, + &verbose + ); +} + +rtems_status_code rtems_ftpfs_get_timeout( + const char *mount_point, + struct timeval *timeout +) +{ + return rtems_ftpfs_do_ioctl( + mount_point, + RTEMS_FTPFS_IOCTL_GET_TIMEOUT, + timeout + ); +} + +rtems_status_code rtems_ftpfs_set_timeout( + const char *mount_point, + const struct timeval *timeout +) +{ + return rtems_ftpfs_do_ioctl( + mount_point, + RTEMS_FTPFS_IOCTL_SET_TIMEOUT, + timeout + ); +} + +typedef void (*rtems_ftpfs_reply_parser)( + const char * /* reply fragment */, + size_t /* reply fragment length */, + void * /* parser argument */ +); + +typedef enum { + RTEMS_FTPFS_REPLY_START, + RTEMS_FTPFS_REPLY_SINGLE_LINE, + RTEMS_FTPFS_REPLY_DONE, + RTEMS_FTPFS_REPLY_MULTI_LINE, + RTEMS_FTPFS_REPLY_MULTI_LINE_START +} rtems_ftpfs_reply_state; + +typedef enum { + RTEMS_FTPFS_REPLY_ERROR = 0, + RTEMS_FTPFS_REPLY_1 = '1', + RTEMS_FTPFS_REPLY_2 = '2', + RTEMS_FTPFS_REPLY_3 = '3', + RTEMS_FTPFS_REPLY_4 = '4', + RTEMS_FTPFS_REPLY_5 = '5' +} rtems_ftpfs_reply; + +#define RTEMS_FTPFS_REPLY_SIZE 3 + +static bool rtems_ftpfs_is_reply_code_valid(unsigned char *reply) +{ + return isdigit(reply [0]) && isdigit(reply [1]) && isdigit(reply [2]); +} + +static rtems_ftpfs_reply rtems_ftpfs_get_reply( + rtems_ftpfs_entry *e, + rtems_ftpfs_reply_parser parser, + void *parser_arg, + bool verbose +) +{ + rtems_ftpfs_reply_state state = RTEMS_FTPFS_REPLY_START; + unsigned char reply_code [RTEMS_FTPFS_REPLY_SIZE] = { 'a', 'a', 'a' }; + size_t reply_code_index = 0; + + while (state != RTEMS_FTPFS_REPLY_DONE) { + char *buf = NULL; + size_t i = 0; + size_t n = 0; + + /* Receive reply fragment from socket */ + if (e->reply_current == e->reply_end) { + ssize_t rv = 0; + + e->reply_current = 0; + e->reply_end = 0; + + rv = recv( + e->ctrl_socket, + &e->reply_buffer [0], + sizeof(e->reply_buffer), + 0 + ); + + if (rv > 0) { + e->reply_end = (size_t) rv; + } else { + return RTEMS_FTPFS_REPLY_ERROR; + } + } + + buf = &e->reply_buffer [e->reply_current]; + n = e->reply_end - e->reply_current; + + /* Invoke parser if necessary */ + if (parser != NULL) { + parser(buf, n, parser_arg); + } + + /* Parse reply fragment */ + for (i = 0; i < n && state != RTEMS_FTPFS_REPLY_DONE; ++i) { + char c = buf [i]; + + switch (state) { + case RTEMS_FTPFS_REPLY_START: + if (reply_code_index < RTEMS_FTPFS_REPLY_SIZE) { + reply_code [reply_code_index] = c; + ++reply_code_index; + } else if (rtems_ftpfs_is_reply_code_valid(reply_code)) { + if (c == '-') { + state = RTEMS_FTPFS_REPLY_MULTI_LINE; + } else { + state = RTEMS_FTPFS_REPLY_SINGLE_LINE; + } + } else { + return RTEMS_FTPFS_REPLY_ERROR; + } + break; + case RTEMS_FTPFS_REPLY_SINGLE_LINE: + if (c == '\n') { + state = RTEMS_FTPFS_REPLY_DONE; + } + break; + case RTEMS_FTPFS_REPLY_MULTI_LINE: + if (c == '\n') { + state = RTEMS_FTPFS_REPLY_MULTI_LINE_START; + reply_code_index = 0; + } + break; + case RTEMS_FTPFS_REPLY_MULTI_LINE_START: + if (reply_code_index < RTEMS_FTPFS_REPLY_SIZE) { + if (reply_code [reply_code_index] == c) { + ++reply_code_index; + } else { + state = RTEMS_FTPFS_REPLY_MULTI_LINE; + } + } else { + if (c == ' ') { + state = RTEMS_FTPFS_REPLY_SINGLE_LINE; + } else { + state = RTEMS_FTPFS_REPLY_MULTI_LINE; + } + } + break; + default: + return RTEMS_FTPFS_REPLY_ERROR; + } + } + + /* Be verbose if necessary */ + if (verbose) { + write(STDERR_FILENO, buf, i); + } + + /* Update reply index */ + e->reply_current += i; + } + + return reply_code [0]; +} + +static rtems_ftpfs_reply rtems_ftpfs_send_command_with_parser( + rtems_ftpfs_entry *e, + const char *cmd, + const char *arg, + rtems_ftpfs_reply_parser parser, + void *parser_arg, + bool verbose +) +{ + rtems_ftpfs_reply reply = RTEMS_FTPFS_REPLY_ERROR; + size_t cmd_len = strlen(cmd); + size_t arg_len = arg != NULL ? strlen(arg) : 0; + size_t len = cmd_len + arg_len + 2; + char *buf = malloc(len); + + if (buf != NULL) { + ssize_t n = 0; + char *buf_arg = buf + cmd_len; + char *buf_eol = buf_arg + arg_len; + + memcpy(buf, cmd, cmd_len); + memcpy(buf_arg, arg, arg_len); + buf_eol [0] = '\r'; + buf_eol [1] = '\n'; + + /* Send */ + n = send(e->ctrl_socket, buf, len, 0); + if (n == (ssize_t) len) { + if (verbose) { + write(STDERR_FILENO, buf, len); + } + + /* Reply */ + reply = rtems_ftpfs_get_reply(e, parser, parser_arg, verbose); + } + + free(buf); + } + + return reply; +} + +static rtems_ftpfs_reply rtems_ftpfs_send_command( + rtems_ftpfs_entry *e, + const char *cmd, + const char *arg, + bool verbose +) +{ + return rtems_ftpfs_send_command_with_parser( + e, + cmd, + arg, + NULL, + NULL, + verbose + ); +} + +typedef enum { + STATE_USER_NAME, + STATE_START_PASSWORD, + STATE_START_HOST_NAME, + STATE_START_HOST_NAME_OR_PATH, + STATE_START_PATH, + STATE_PASSWORD, + STATE_HOST_NAME, + STATE_DONE, + STATE_INVALID +} split_state; + +static bool rtems_ftpfs_split_names ( + char *s, + const char **user, + const char **password, + const char **hostname, + const char **path +) +{ + split_state state = STATE_USER_NAME; + size_t len = strlen(s); + size_t i = 0; + + *user = s; + *password = NULL; + *hostname = NULL; + *path = NULL; + + for (i = 0; i < len; ++i) { + char c = s [i]; + + switch (state) { + case STATE_USER_NAME: + if (c == ':') { + state = STATE_START_PASSWORD; + s [i] = '\0'; + } else if (c == '@') { + state = STATE_START_HOST_NAME; + s [i] = '\0'; + } else if (c == '/') { + state = STATE_START_HOST_NAME_OR_PATH; + s [i] = '\0'; + } + break; + case STATE_START_PASSWORD: + state = STATE_PASSWORD; + *password = &s [i]; + --i; + break; + case STATE_START_HOST_NAME: + state = STATE_HOST_NAME; + *hostname = &s [i]; + --i; + break; + case STATE_START_HOST_NAME_OR_PATH: + if (c == '@') { + state = STATE_START_HOST_NAME; + } else { + state = STATE_DONE; + *path = &s [i]; + goto done; + } + break; + case STATE_START_PATH: + state = STATE_DONE; + *path = &s [i]; + goto done; + case STATE_PASSWORD: + if (c == '@') { + state = STATE_START_HOST_NAME; + s [i] = '\0'; + } else if (c == '/') { + state = STATE_START_HOST_NAME_OR_PATH; + s [i] = '\0'; + } + break; + case STATE_HOST_NAME: + if (c == '/') { + state = STATE_START_PATH; + s [i] = '\0'; + } + break; + default: + state = STATE_INVALID; + goto done; + } + } + +done: + + /* This is a special case with no username and password */ + if (*hostname == NULL) { + *hostname = &s [0]; + *user = "anonymous"; + *password = *user; + } + + /* If we have no password use the user name */ + if (*password == NULL) { + *password = *user; + } + + return state == STATE_DONE; +} + +static socklen_t rtems_ftpfs_create_address( + struct sockaddr_in *sa, + unsigned long address, + unsigned short port +) +{ + memset(sa, sizeof(*sa), 0); + + sa->sin_family = AF_INET; + sa->sin_addr.s_addr = address; + sa->sin_port = port; + sa->sin_len = sizeof(*sa); + + return sizeof(*sa); +} + +static int rtems_ftpfs_terminate(rtems_libio_t *iop, bool error) +{ + int eno = 0; + int rv = 0; + rtems_ftpfs_entry *e = iop->data1; + rtems_ftpfs_mount_entry *me = iop->pathinfo.mt_entry->fs_info; + bool verbose = me->verbose; + rtems_ftpfs_reply reply = RTEMS_FTPFS_REPLY_ERROR; + + if (e != NULL) { + /* Close data connection if necessary */ + if (e->data_socket >= 0) { + rv = close(e->data_socket); + if (rv != 0) { + eno = EIO; + } + + /* For write connections we have to obtain the transfer reply */ + if ( + e->ctrl_socket >= 0 + && (iop->flags & LIBIO_FLAGS_WRITE) != 0 + && !error + ) { + reply = rtems_ftpfs_get_reply(e, NULL, NULL, verbose); + if (reply != RTEMS_FTPFS_REPLY_2) { + eno = EIO; + } + } + } + + /* Close control connection if necessary */ + if (e->ctrl_socket >= 0) { + reply = rtems_ftpfs_send_command(e, "QUIT", NULL, verbose); + if (reply != RTEMS_FTPFS_REPLY_2) { + eno = EIO; + } + + rv = close(e->ctrl_socket); + if (rv != 0) { + eno = EIO; + } + } + + /* Free connection entry */ + free(e); + } + + /* Invalidate IO entry */ + iop->data1 = NULL; + + return eno; +} + +static int rtems_ftpfs_open_ctrl_connection( + rtems_ftpfs_entry *e, + const char *user, + const char *password, + const char *hostname, + uint32_t *client_address, + bool verbose, + const struct timeval *timeout +) +{ + int rv = 0; + int eno = 0; + rtems_ftpfs_reply reply = RTEMS_FTPFS_REPLY_ERROR; + struct in_addr address = { .s_addr = 0 }; + struct sockaddr_in sa; + socklen_t size = 0; + + /* Create the socket for the control connection */ + e->ctrl_socket = socket(AF_INET, SOCK_STREAM, 0); + if (e->ctrl_socket < 0) { + return ENOMEM; + } + + /* Set up the server address from the hostname */ + if (inet_aton(hostname, &address) == 0) { + /* Try to get the address by name */ + struct hostent *he = gethostbyname(hostname); + + if (he != NULL) { + memcpy(&address, he->h_addr, sizeof(address)); + } else { + return ENOENT; + } + } + rtems_ftpfs_create_address(&sa, address.s_addr, htons(RTEMS_FTPFS_CTRL_PORT)); + DEBUG_PRINTF("server = %s\n", inet_ntoa(sa.sin_addr)); + + /* Open control connection */ + rv = connect( + e->ctrl_socket, + (struct sockaddr *) &sa, + sizeof(sa) + ); + if (rv != 0) { + return ENOENT; + } + + /* Set control connection timeout */ + eno = rtems_ftpfs_set_connection_timeout(e->ctrl_socket, timeout); + if (eno != 0) { + return eno; + } + + /* Get client address */ + size = rtems_ftpfs_create_address(&sa, INADDR_ANY, 0); + rv = getsockname( + e->ctrl_socket, + (struct sockaddr *) &sa, + &size + ); + if (rv != 0) { + return ENOMEM; + } + *client_address = ntohl(sa.sin_addr.s_addr); + DEBUG_PRINTF("client = %s\n", inet_ntoa(sa.sin_addr)); + + /* Now we should get a welcome message from the server */ + reply = rtems_ftpfs_get_reply(e, NULL, NULL, verbose); + if (reply != RTEMS_FTPFS_REPLY_2) { + return ENOENT; + } + + /* Send USER command */ + reply = rtems_ftpfs_send_command(e, "USER ", user, verbose); + if (reply == RTEMS_FTPFS_REPLY_3) { + /* Send PASS command */ + reply = rtems_ftpfs_send_command(e, "PASS ", password, verbose); + if (reply != RTEMS_FTPFS_REPLY_2) { + return EACCES; + } + + /* TODO: Some server may require an account */ + } else if (reply != RTEMS_FTPFS_REPLY_2) { + return EACCES; + } + + /* Send TYPE command to set binary mode for all data transfers */ + reply = rtems_ftpfs_send_command(e, "TYPE I", NULL, verbose); + if (reply != RTEMS_FTPFS_REPLY_2) { + return EIO; + } + + return 0; +} + +static int rtems_ftpfs_open_data_connection_active( + rtems_ftpfs_entry *e, + uint32_t client_address, + const char *file_command, + const char *filename, + bool verbose, + const struct timeval *timeout +) +{ + int rv = 0; + int eno = 0; + rtems_ftpfs_reply reply = RTEMS_FTPFS_REPLY_ERROR; + struct sockaddr_in sa; + socklen_t size = 0; + int port_socket = -1; + char port_command [] = "PORT 000,000,000,000,000,000"; + uint16_t data_port = 0; + + /* Create port socket to establish a data data connection */ + port_socket = socket(AF_INET, SOCK_STREAM, 0); + if (port_socket < 0) { + eno = ENOMEM; + goto cleanup; + } + + /* Bind port socket */ + rtems_ftpfs_create_address(&sa, INADDR_ANY, 0); + rv = bind( + port_socket, + (struct sockaddr *) &sa, + sizeof(sa) + ); + if (rv != 0) { + eno = EBUSY; + goto cleanup; + } + + /* Get port number for data socket */ + size = rtems_ftpfs_create_address(&sa, INADDR_ANY, 0); + rv = getsockname( + port_socket, + (struct sockaddr *) &sa, + &size + ); + if (rv != 0) { + eno = ENOMEM; + goto cleanup; + } + data_port = ntohs(sa.sin_port); + + /* Send PORT command to set data connection port for server */ + snprintf( + port_command, + sizeof(port_command), + "PORT %lu,%lu,%lu,%lu,%lu,%lu", + (client_address >> 24) & 0xffUL, + (client_address >> 16) & 0xffUL, + (client_address >> 8) & 0xffUL, + (client_address >> 0) & 0xffUL, + (data_port >> 8) & 0xffUL, + (data_port >> 0) & 0xffUL + ); + reply = rtems_ftpfs_send_command(e, port_command, NULL, verbose); + if (reply != RTEMS_FTPFS_REPLY_2) { + eno = ENOTSUP; + goto cleanup; + } + + /* Listen on port socket for incoming data connections */ + rv = listen(port_socket, 1); + if (rv != 0) { + eno = EBUSY; + goto cleanup; + } + + /* Send RETR or STOR command with filename */ + reply = rtems_ftpfs_send_command(e, file_command, filename, verbose); + if (reply != RTEMS_FTPFS_REPLY_1) { + eno = EIO; + goto cleanup; + } + + /* Wait for connect on data connection if necessary */ + if (rtems_ftpfs_use_timeout(timeout)) { + struct timeval to = *timeout; + fd_set fds; + + FD_ZERO(&fds); + FD_SET(port_socket, &fds); + + rv = select(port_socket + 1, &fds, NULL, NULL, &to); + if (rv <= 0) { + eno = EIO; + goto cleanup; + } + } + + /* Accept data connection */ + size = sizeof(sa); + e->data_socket = accept( + port_socket, + (struct sockaddr *) &sa, + &size + ); + if (e->data_socket < 0) { + eno = EIO; + goto cleanup; + } + +cleanup: + + /* Close port socket if necessary */ + if (port_socket >= 0) { + rv = close(port_socket); + if (rv != 0) { + eno = EIO; + } + } + + return eno; +} + +typedef enum { + RTEMS_FTPFS_PASV_START = 0, + RTEMS_FTPFS_PASV_JUNK, + RTEMS_FTPFS_PASV_DATA, + RTEMS_FTPFS_PASV_DONE +} rtems_ftpfs_pasv_state; + +typedef struct { + rtems_ftpfs_pasv_state state; + uint8_t data [6]; + size_t index; +} rtems_ftpfs_pasv_entry; + +static void rtems_ftpfs_pasv_parser( + const char* buf, + size_t len, + void *arg +) +{ + rtems_ftpfs_pasv_entry *e = arg; + size_t i = 0; + + for (i = 0; i < len; ++i) { + int c = buf [i]; + + switch (e->state) { + case RTEMS_FTPFS_PASV_START: + if (!isdigit(c)) { + e->state = RTEMS_FTPFS_PASV_JUNK; + e->index = 0; + } + break; + case RTEMS_FTPFS_PASV_JUNK: + if (isdigit(c)) { + e->state = RTEMS_FTPFS_PASV_DATA; + e->data [e->index] = (uint8_t) (c - '0'); + } + break; + case RTEMS_FTPFS_PASV_DATA: + if (isdigit(c)) { + e->data [e->index] = (uint8_t) (e->data [e->index] * 10 + c - '0'); + } else if (c == ',') { + ++e->index; + if (e->index < sizeof(e->data)) { + e->data [e->index] = 0; + } else { + e->state = RTEMS_FTPFS_PASV_DONE; + } + } else { + e->state = RTEMS_FTPFS_PASV_DONE; + } + break; + default: + return; + } + } +} + +static int rtems_ftpfs_open_data_connection_passive( + rtems_ftpfs_entry *e, + uint32_t client_address, + const char *file_command, + const char *filename, + bool verbose, + const struct timeval *timeout +) +{ + int rv = 0; + rtems_ftpfs_reply reply = RTEMS_FTPFS_REPLY_ERROR; + struct sockaddr_in sa; + uint32_t data_address = 0; + uint16_t data_port = 0; + + rtems_ftpfs_pasv_entry pe = { + .state = RTEMS_FTPFS_PASV_START + }; + + /* Send PASV command */ + reply = rtems_ftpfs_send_command_with_parser( + e, + "PASV", + NULL, + rtems_ftpfs_pasv_parser, + &pe, + verbose + ); + if (reply != RTEMS_FTPFS_REPLY_2) { + return ENOTSUP; + } + data_address = ((uint32_t)(pe.data [0]) << 24) + ((uint32_t)(pe.data [1]) << 16) + + ((uint32_t)(pe.data [2]) << 8) + ((uint32_t)(pe.data [3])); + data_port = (uint16_t) ((pe.data [4] << 8) + pe.data [5]); + rtems_ftpfs_create_address(&sa, htonl(data_address), htons(data_port)); + DEBUG_PRINTF( + "server data = %s:%u\n", + inet_ntoa(sa.sin_addr), + (unsigned) ntohs(sa.sin_port) + ); + + /* Create data socket */ + e->data_socket = socket(AF_INET, SOCK_STREAM, 0); + if (e->data_socket < 0) { + return ENOMEM; + } + + /* Open data connection */ + rv = connect( + e->data_socket, + (struct sockaddr *) &sa, + sizeof(sa) + ); + if (rv != 0) { + return EIO; + } + + /* Send RETR or STOR command with filename */ + reply = rtems_ftpfs_send_command(e, file_command, filename, verbose); + if (reply != RTEMS_FTPFS_REPLY_1) { + return EIO; + } + + return 0; +} + +static int rtems_ftpfs_open( + rtems_libio_t *iop, + const char *path, + int oflag, + mode_t mode +) +{ + int eno = 0; + bool ok = false; + rtems_ftpfs_entry *e = NULL; + rtems_ftpfs_mount_entry *me = iop->pathinfo.mt_entry->fs_info; + bool verbose = me->verbose; + const struct timeval *timeout = &me->timeout; + const char *user = NULL; + const char *password = NULL; + const char *hostname = NULL; + const char *filename = NULL; + const char *file_command = (iop->flags & LIBIO_FLAGS_WRITE) != 0 + ? "STOR " + : "RETR "; + uint32_t client_address = 0; + char *location = iop->pathinfo.node_access; + + /* Invalidate data handle */ + iop->data1 = NULL; + + /* Split location into parts */ + ok = rtems_ftpfs_split_names( + location, + &user, + &password, + &hostname, + &filename + ); + if (!ok) { + rtems_set_errno_and_return_minus_one(ENOENT); + } + DEBUG_PRINTF( + "user = '%s', password = '%s', filename = '%s'\n", + user, + password, + filename + ); + + /* Check for either read-only or write-only flags */ + if ( + (iop->flags & LIBIO_FLAGS_WRITE) != 0 + && (iop->flags & LIBIO_FLAGS_READ) != 0 + ) { + rtems_set_errno_and_return_minus_one(ENOTSUP); + } + + /* Allocate connection entry */ + e = calloc(1, sizeof(*e)); + if (e == NULL) { + rtems_set_errno_and_return_minus_one(ENOMEM); + } + + /* Initialize connection entry */ + e->ctrl_socket = -1; + e->data_socket = -1; + + /* Save connection state */ + iop->data1 = e; + + /* Open control connection */ + eno = rtems_ftpfs_open_ctrl_connection( + e, + user, + password, + hostname, + &client_address, + verbose, + timeout + ); + if (eno != 0) { + goto cleanup; + } + + /* Open passive data connection */ + eno = rtems_ftpfs_open_data_connection_passive( + e, + client_address, + file_command, + filename, + verbose, + timeout + ); + if (eno == ENOTSUP) { + /* Open active data connection */ + eno = rtems_ftpfs_open_data_connection_active( + e, + client_address, + file_command, + filename, + verbose, + timeout + ); + } + if (eno != 0) { + goto cleanup; + } + + /* Set data connection timeout */ + eno = rtems_ftpfs_set_connection_timeout(e->data_socket, timeout); + +cleanup: + + if (eno == 0) { + return 0; + } else { + /* Free all resources if an error occured */ + rtems_ftpfs_terminate(iop, true); + + rtems_set_errno_and_return_minus_one(eno); + } +} + +static ssize_t rtems_ftpfs_read( + rtems_libio_t *iop, + void *buffer, + size_t count +) +{ + rtems_ftpfs_entry *e = iop->data1; + rtems_ftpfs_mount_entry *me = iop->pathinfo.mt_entry->fs_info; + bool verbose = me->verbose; + char *in = buffer; + size_t todo = count; + + if (e->eof) { + return 0; + } + + while (todo > 0) { + ssize_t rv = recv(e->data_socket, in, todo, 0); + + if (rv <= 0) { + if (rv == 0) { + rtems_ftpfs_reply reply = + rtems_ftpfs_get_reply(e, NULL, NULL, verbose); + + if (reply == RTEMS_FTPFS_REPLY_2) { + e->eof = true; + break; + } + } + + rtems_set_errno_and_return_minus_one(EIO); + } + + in += rv; + todo -= (size_t) rv; + } + + return (ssize_t) (count - todo); +} + +static ssize_t rtems_ftpfs_write( + rtems_libio_t *iop, + const void *buffer, + size_t count +) +{ + rtems_ftpfs_entry *e = iop->data1; + const char *out = buffer; + size_t todo = count; + + while (todo > 0) { + ssize_t rv = send(e->data_socket, out, todo, 0); + + if (rv <= 0) { + if (rv == 0) { + break; + } else { + rtems_set_errno_and_return_minus_one(EIO); + } + } + + out += rv; + todo -= (size_t) rv; + } + + return (ssize_t) (count - todo); +} + +static int rtems_ftpfs_close(rtems_libio_t *iop) +{ + int eno = rtems_ftpfs_terminate(iop, false); + + if (eno == 0) { + return 0; + } else { + rtems_set_errno_and_return_minus_one(eno); + } +} + +/* Dummy version to let fopen(*,"w") work properly */ +static int rtems_ftpfs_ftruncate(rtems_libio_t *iop, off_t count) +{ + return 0; +} + +static void rtems_ftpfs_eval_path( + rtems_filesystem_eval_path_context_t *self +) +{ + rtems_filesystem_eval_path_eat_delimiter(self); + + if (rtems_filesystem_eval_path_has_path(self)) { + const char *path = rtems_filesystem_eval_path_get_path(self); + size_t pathlen = rtems_filesystem_eval_path_get_pathlen(self); + char *pathdup = malloc(pathlen + 1); + + rtems_filesystem_eval_path_clear_path(self); + + if (pathdup != NULL) { + rtems_filesystem_location_info_t *currentloc = + rtems_filesystem_eval_path_get_currentloc(self); + + memcpy(pathdup, path, pathlen); + pathdup [pathlen] = '\0'; + currentloc->node_access = pathdup; + currentloc->handlers = &rtems_ftpfs_handlers; + } else { + rtems_filesystem_eval_path_error(self, ENOMEM); + } + } +} + +static void rtems_ftpfs_free_node(const rtems_filesystem_location_info_t *loc) +{ + free(loc->node_access); +} + +static rtems_filesystem_node_types_t rtems_ftpfs_node_type( + const rtems_filesystem_location_info_t *loc +) +{ + return RTEMS_FILESYSTEM_MEMORY_FILE; +} + +int rtems_ftpfs_initialize( + rtems_filesystem_mount_table_entry_t *e, + const void *d +) +{ + rtems_ftpfs_mount_entry *me = malloc(sizeof(rtems_ftpfs_mount_entry)); + + /* Mount entry for FTP file system instance */ + e->fs_info = me; + if (e->fs_info == NULL) { + rtems_set_errno_and_return_minus_one(ENOMEM); + } + me->verbose = false; + me->timeout.tv_sec = 0; + me->timeout.tv_usec = 0; + + /* Set handler and oparations table */ + e->mt_fs_root->location.handlers = &rtems_ftpfs_root_handlers; + e->ops = &rtems_ftpfs_ops; + + /* We maintain no real file system nodes, so there is no real root */ + e->mt_fs_root->location.node_access = NULL; + + return 0; +} + +static void rtems_ftpfs_unmount_me( + rtems_filesystem_mount_table_entry_t *e +) +{ + free(e->fs_info); +} + +static int rtems_ftpfs_ioctl( + rtems_libio_t *iop, + uint32_t command, + void *arg +) +{ + rtems_ftpfs_mount_entry *me = iop->pathinfo.mt_entry->fs_info; + bool *verbose = arg; + struct timeval *timeout = arg; + + if (arg == NULL) { + rtems_set_errno_and_return_minus_one(EINVAL); + } + + switch (command) { + case RTEMS_FTPFS_IOCTL_GET_VERBOSE: + *verbose = me->verbose; + break; + case RTEMS_FTPFS_IOCTL_SET_VERBOSE: + me->verbose = *verbose; + break; + case RTEMS_FTPFS_IOCTL_GET_TIMEOUT: + *timeout = me->timeout; + break; + case RTEMS_FTPFS_IOCTL_SET_TIMEOUT: + me->timeout = *timeout; + break; + default: + rtems_set_errno_and_return_minus_one(EINVAL); + } + + return 0; +} + +/* + * The stat() support is intended only for the cp shell command. Each request + * will return that we have a regular file with read, write and execute + * permissions for every one. The node index uses a global counter to support + * a remote to remote copy. This is not a very sophisticated method. + */ +static int rtems_ftpfs_fstat( + const rtems_filesystem_location_info_t *loc, + struct stat *st +) +{ + static unsigned ino = 0; + + /* FIXME */ + st->st_ino = ++ino; + st->st_dev = rtems_filesystem_make_dev_t(0xcc494cd6U, 0x1d970b4dU); + + st->st_mode = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO; + + return 0; +} + +static const rtems_filesystem_operations_table rtems_ftpfs_ops = { + .lock_h = rtems_filesystem_default_lock, + .unlock_h = rtems_filesystem_default_unlock, + .eval_path_h = rtems_ftpfs_eval_path, + .link_h = rtems_filesystem_default_link, + .are_nodes_equal_h = rtems_filesystem_default_are_nodes_equal, + .node_type_h = rtems_ftpfs_node_type, + .mknod_h = rtems_filesystem_default_mknod, + .rmnod_h = rtems_filesystem_default_rmnod, + .fchmod_h = rtems_filesystem_default_fchmod, + .chown_h = rtems_filesystem_default_chown, + .clonenod_h = rtems_filesystem_default_clonenode, + .freenod_h = rtems_ftpfs_free_node, + .mount_h = rtems_filesystem_default_mount, + .fsmount_me_h = rtems_ftpfs_initialize, + .unmount_h = rtems_filesystem_default_unmount, + .fsunmount_me_h = rtems_ftpfs_unmount_me, + .utime_h = rtems_filesystem_default_utime, + .symlink_h = rtems_filesystem_default_symlink, + .readlink_h = rtems_filesystem_default_readlink, + .rename_h = rtems_filesystem_default_rename, + .statvfs_h = rtems_filesystem_default_statvfs +}; + +static const rtems_filesystem_file_handlers_r rtems_ftpfs_handlers = { + .open_h = rtems_ftpfs_open, + .close_h = rtems_ftpfs_close, + .read_h = rtems_ftpfs_read, + .write_h = rtems_ftpfs_write, + .ioctl_h = rtems_filesystem_default_ioctl, + .lseek_h = rtems_filesystem_default_lseek, + .fstat_h = rtems_ftpfs_fstat, + .ftruncate_h = rtems_ftpfs_ftruncate, + .fsync_h = rtems_filesystem_default_fsync_or_fdatasync, + .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync, + .fcntl_h = rtems_filesystem_default_fcntl +}; + +static const rtems_filesystem_file_handlers_r rtems_ftpfs_root_handlers = { + .open_h = rtems_filesystem_default_open, + .close_h = rtems_filesystem_default_close, + .read_h = rtems_filesystem_default_read, + .write_h = rtems_filesystem_default_write, + .ioctl_h = rtems_ftpfs_ioctl, + .lseek_h = rtems_filesystem_default_lseek, + .fstat_h = rtems_filesystem_default_fstat, + .ftruncate_h = rtems_filesystem_default_ftruncate, + .fsync_h = rtems_filesystem_default_fsync_or_fdatasync, + .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync, + .fcntl_h = rtems_filesystem_default_fcntl +}; diff --git a/services/ftpfs/include/rtems/ftpfs.h b/services/ftpfs/include/rtems/ftpfs.h new file mode 100644 index 00000000..8b1de3ca --- /dev/null +++ b/services/ftpfs/include/rtems/ftpfs.h @@ -0,0 +1,160 @@ +/** + * @file + * + * @brief File Transfer Protocol file system (FTP client). + */ + +/* + * Copyright (c) 2009 + * embedded brains GmbH + * Obere Lagerstr. 30 + * D-82178 Puchheim + * Germany + * + * + * (c) Copyright 2002 + * Thomas Doerfler + * IMD Ingenieurbuero fuer Microcomputertechnik + * Herbststr. 8 + * 82178 Puchheim, Germany + * + * + * Modified by Sebastian Huber . + * + * 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.com/license/LICENSE. + */ + +#ifndef _RTEMS_FTPFS_H +#define _RTEMS_FTPFS_H + +#include +#include + +#include + +#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: + * /FTP/user:pw@@host/dir/file.txt. + * + * If the server is the default server specified in BOOTP, it can be ommitted: + * /FTP/user:pw/dir/file.txt. + * + * The user name will be used for the password if it is ommitted: + * /FTP/user@@host/dir/file.txt. + * + * 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 -- cgit v1.2.3