summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/examples/README.txt4
-rw-r--r--apps/include/ftpc.h9
-rw-r--r--apps/netutils/ftpc/ftpc_connect.c8
-rwxr-xr-xnuttx/configs/olimex-lpc1766stk/ftpc/defconfig2
-rw-r--r--nuttx/fs/Makefile21
-rw-r--r--nuttx/fs/fs_fdopen.c119
-rw-r--r--nuttx/include/nuttx/net.h8
-rw-r--r--nuttx/net/Makefile31
-rw-r--r--nuttx/net/connect.c7
-rw-r--r--nuttx/net/net_checksd.c88
-rw-r--r--nuttx/net/send.c2
11 files changed, 253 insertions, 46 deletions
diff --git a/apps/examples/README.txt b/apps/examples/README.txt
index f14037e72..2a15dac5d 100644
--- a/apps/examples/README.txt
+++ b/apps/examples/README.txt
@@ -63,6 +63,10 @@ examples/ftpc
where xx.xx.xx.xx is the IP address of the FTP server and pp is an
optional port number.
+ NOTE: The ftpc task uses the system console for input/output. It will
+ not work from NSH over a telnet NSH connection (Well, it will work you
+ just won't be able to access the command line).
+
examples/hello
^^^^^^^^^^^^^^
diff --git a/apps/include/ftpc.h b/apps/include/ftpc.h
index 211edbad4..0676a7040 100644
--- a/apps/include/ftpc.h
+++ b/apps/include/ftpc.h
@@ -112,7 +112,14 @@
typedef FAR void *SESSION;
-/* This structure provides information to connect to a host FTP server */
+/* This structure provides information to connect to a host FTP server.
+ *
+ * addr - The IPv4 address of the FTP server (or the proxy) for the FTP
+ * server.
+ * port - The port number on the FTP server to connect to. This is usually
+ * port 21 for FTP. You may set this value to zero to let FTPC
+ * select the default port number for you (CONFIG_FTP_DEFPORT).
+ */
struct ftpc_connect_s
{
diff --git a/apps/netutils/ftpc/ftpc_connect.c b/apps/netutils/ftpc/ftpc_connect.c
index a61ca2d02..ff19af324 100644
--- a/apps/netutils/ftpc/ftpc_connect.c
+++ b/apps/netutils/ftpc/ftpc_connect.c
@@ -98,7 +98,7 @@ SESSION ftpc_connect(FAR struct ftpc_connect_s *server)
/* Initialize the session structure */
session->addr.s_addr = server->addr.s_addr;
- session->port = server->port;
+ session->port = server->port ? server->port : CONFIG_FTP_DEFPORT;
session->pid = getpid();
/* Create up a timer to prevent hangs */
@@ -165,9 +165,7 @@ int ftpc_reconnect(FAR struct ftpc_session_s *session)
#ifdef CONFIG_DEBUG
tmp = inet_ntoa(session->addr);
- ndbg("Connecting to server address %s:%d\n",
- tmp, ntohl(session->port));
- free(tmp);
+ ndbg("Connecting to server address %s:%d\n", tmp, ntohl(session->port));
#endif
addr.sin_family = AF_INET;
@@ -214,10 +212,8 @@ int ftpc_reconnect(FAR struct ftpc_session_s *session)
ndbg("Connected\n");
tmp = inet_ntoa(session->cmd.raddr.sin_addr);
ndbg(" Remote address: %s:%d\n", tmp, ntohl(session->cmd.raddr.sin_port));
- free(tmp);
tmp = inet_ntoa(session->cmd.laddr.sin_addr);
ndbg(" Local address: %s:d\n", tmp, ntohl(session->cmd.laddr.sin_port));
- free(tmp);
#endif
return OK;
diff --git a/nuttx/configs/olimex-lpc1766stk/ftpc/defconfig b/nuttx/configs/olimex-lpc1766stk/ftpc/defconfig
index 80e6ef504..8e2a75668 100755
--- a/nuttx/configs/olimex-lpc1766stk/ftpc/defconfig
+++ b/nuttx/configs/olimex-lpc1766stk/ftpc/defconfig
@@ -336,7 +336,7 @@ CONFIG_START_DAY=23
CONFIG_GREGORIAN_TIME=n
CONFIG_JULIAN_TIME=n
CONFIG_DEV_CONSOLE=y
-CONFIG_DEV_LOWCONSOLE=y
+CONFIG_DEV_LOWCONSOLE=n
CONFIG_MUTEX_TYPES=n
CONFIG_PRIORITY_INHERITANCE=n
CONFIG_SEM_PREALLOCHOLDERS=0
diff --git a/nuttx/fs/Makefile b/nuttx/fs/Makefile
index 45ff2db79..f42ebcaaf 100644
--- a/nuttx/fs/Makefile
+++ b/nuttx/fs/Makefile
@@ -39,11 +39,28 @@ ASRCS =
AOBJS = $(ASRCS:.S=$(OBJEXT))
CSRCS =
+
+# If there are no file descriptors configured, then a small part of the
+# logic in this directory may still apply to socket descriptors
+
ifeq ($(CONFIG_NFILE_DESCRIPTORS),0)
ifneq ($(CONFIG_NSOCKET_DESCRIPTORS),0)
+
+# Socket descriptor support
+
CSRCS += fs_close.c fs_read.c fs_write.c fs_ioctl.c fs_poll.c fs_select.c
endif
+
+# Support for network access using streams
+
+ifneq ($(CONFIG_NFILE_STREAMS),0)
+CSRCS += fs_fdopen.c
+endif
+
else
+
+# Common file/socket descriptor support
+
CSRCS += fs_open.c fs_close.c fs_read.c fs_write.c fs_ioctl.c \
fs_poll.c fs_select.c fs_lseek.c fs_dup.c fs_filedup.c \
fs_dup2.c fs_fcntl.c fs_filedup2.c fs_opendir.c fs_closedir.c \
@@ -56,10 +73,14 @@ CSRCS += fs_registerblockdriver.c fs_unregisterblockdriver.c \
include mmap/Make.defs
+# Stream support
+
ifneq ($(CONFIG_NFILE_STREAMS),0)
CSRCS += fs_fdopen.c
endif
+# Additional files required is mount-able file systems are supported
+
ifneq ($(CONFIG_DISABLE_MOUNTPOINT),y)
CSRCS += fs_mount.c fs_umount.c fs_fsync.c fs_unlink.c fs_rename.c \
fs_mkdir.c fs_rmdir.c
diff --git a/nuttx/fs/fs_fdopen.c b/nuttx/fs/fs_fdopen.c
index 9445e97fa..329e4dd84 100644
--- a/nuttx/fs/fs_fdopen.c
+++ b/nuttx/fs/fs_fdopen.c
@@ -47,24 +47,79 @@
#include <nuttx/kmalloc.h>
#include <nuttx/fs.h>
+#include <nuttx/net.h>
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
+ * Name: fs_checkfd
+ *
+ * Description:
+ * Check if the file descriptor is valid for the provided TCB and if it
+ * supports the requested access.
+ *
+ ****************************************************************************/
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+static inline int fs_checkfd(FAR _TCB *tcb, int fd, int oflags)
+{
+ FAR struct filelist *flist;
+ FAR struct inode *inode;
+
+ /* Get the file list from the TCB */
+
+ flist = tcb->filelist;
+
+ /* Get the inode associated with the file descriptor. This should
+ * normally be the case if fd >= 0. But not in the case where the
+ * called attempts to explictly stdin with fdopen(0) but stdin has
+ * been closed.
+ */
+
+ inode = flist->fl_files[fd].f_inode;
+ if (!inode)
+ {
+ /* No inode -- descriptor does not correspond to an open file */
+
+ return -ENOENT;
+ }
+
+ /* Make sure that the inode supports the requested access. In
+ * the case of fdopen, we are not actually creating the file -- in
+ * particular w and w+ do not truncate the file and any files have
+ * already been created.
+ */
+
+ if (inode_checkflags(inode, oflags) != OK)
+ {
+ /* Cannot support the requested access */
+
+ return -EACCES;
+ }
+
+ /* Looks good to me */
+
+ return OK;
+}
+#endif
+
+/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: fs_fdopen
+ *
+ * Description:
+ * This function does the core operations for fopen and fdopen.
+ *
****************************************************************************/
FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR _TCB *tcb)
{
- FAR struct filelist *flist;
FAR struct streamlist *slist;
- FAR struct inode *inode;
FAR FILE *stream;
int err = OK;
int ret;
@@ -88,37 +143,55 @@ FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR _TCB *tcb)
tcb = sched_self();
}
- /* Get the file and stream list from the TCB */
+ /* Verify that this is a valid file/socket descriptor and that the
+ * requested access can be support.
+ *
+ * Is this fd in the range of valid file descriptors? Socket descriptors
+ * lie in a different range.
+ */
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+ if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
+#endif
+ {
+ /* No.. If networking is enabled then this might be a socket
+ * descriptor.
+ */
- flist = tcb->filelist;
- slist = tcb->streams;
+#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
+ ret = net_checksd;
+#else
+ /* No networking... it is just a bad descriptor */
- /* Get the inode associated with the file descriptor. This should
- * normally be the case if fd >= 0. But not in the case where the
- * called attempts to explictly stdin with fdopen(0) but stdin has
- * been closed.
- */
-
- inode = flist->fl_files[fd].f_inode;
- if (!inode)
+ err = EBADF;
+ return ERROR;
+#endif
+ }
+
+ /* The descriptor is in a valid range to file descriptor... do the read */
+
+#if CONFIG_NFILE_DESCRIPTORS > 0
+ else
{
- err = ENOENT;
- goto errout;
+ ret = fs_checkfd(tcb, fd, oflags);
}
+#endif
- /* Make sure that the inode supports the requested access. In
- * the case of fdopen, we are not actually creating the file -- in
- * particular w and w+ do not truncate the file and any files have
- * already been created.
- */
+ /* Do we have a good descriptor of some sort? */
- if (inode_checkflags(inode, oflags) != OK)
+ if (ret < 0)
{
- err = EACCES;
+ /* No... return the reported error */
+
+ err = -ret;
goto errout;
}
- /* Find an unallocated FILE structure */
+ /* Get the stream list from the TCB */
+
+ slist = tcb->streams;
+
+ /* Find an unallocated FILE structure in the stream list */
ret = sem_wait(&slist->sl_sem);
if (ret != OK)
diff --git a/nuttx/include/nuttx/net.h b/nuttx/include/nuttx/net.h
index 62b2e1673..1d514e888 100644
--- a/nuttx/include/nuttx/net.h
+++ b/nuttx/include/nuttx/net.h
@@ -131,6 +131,14 @@ extern "C" {
#define EXTERN extern
#endif
+/* net_checksd.c *************************************************************/
+/* Check if the socket descriptor is valid for the provided TCB and if it
+ * supports the requested access.
+ */
+
+typedef struct _TCB _TCB; /* Forward reference */
+EXTERN int net_checksd(FAR _TCB *tcb, int fd, int oflags);
+
/* net_sockets.c *************************************************************/
/* There interfaces are called only from OS scheduling and iniialization logic
* under sched/
diff --git a/nuttx/net/Makefile b/nuttx/net/Makefile
index d252b036f..94af9e0d2 100644
--- a/nuttx/net/Makefile
+++ b/nuttx/net/Makefile
@@ -36,27 +36,42 @@
-include $(TOPDIR)/Make.defs
ifeq ($(CONFIG_NET),y)
-SOCK_ASRCS =
-SOCK_CSRCS = bind.c connect.c getsockname.c recv.c recvfrom.c socket.c \
+
+# Basic networking support
+
+SOCK_ASRCS =
+SOCK_CSRCS = bind.c connect.c getsockname.c recv.c recvfrom.c socket.c \
sendto.c net_sockets.c net_close.c net_dup.c net_dup2.c net_clone.c \
net_vfcntl.c
+# TCP/IP support
+
ifeq ($(CONFIG_NET_TCP),y)
-SOCK_CSRCS += send.c listen.c accept.c
+SOCK_CSRCS += send.c listen.c accept.c
endif
+# Socket options
+
ifeq ($(CONFIG_NET_SOCKOPTS),y)
-SOCK_CSRCS += setsockopt.c getsockopt.c
+SOCK_CSRCS += setsockopt.c getsockopt.c
ifneq ($(CONFIG_DISABLE_CLOCK),y)
-SOCK_CSRCS += net_timeo.c net_dsec2timeval.c net_timeval2dsec.c
+SOCK_CSRCS += net_timeo.c net_dsec2timeval.c net_timeval2dsec.c
ifneq ($(CONFIG_NET_SLIP),y)
-SOCK_CSRCS += net_arptimer.c
+SOCK_CSRCS += net_arptimer.c
endif
endif
endif
-NETDEV_ASRCS =
-NETDEV_CSRCS = netdev_register.c netdev_ioctl.c net_poll.c netdev_txnotify.c \
+# Support for network access using streams
+
+ifneq ($(CONFIG_NFILE_STREAMS),0)
+SOCK_CSRCS += net_checksd.c
+endif
+
+# Support for operations on network devices
+
+NETDEV_ASRCS =
+NETDEV_CSRCS = netdev_register.c netdev_ioctl.c net_poll.c netdev_txnotify.c \
netdev_findbyname.c netdev_findbyaddr.c netdev_count.c \
netdev_foreach.c netdev_unregister.c
diff --git a/nuttx/net/connect.c b/nuttx/net/connect.c
index ae4518ad0..f6006e3e6 100644
--- a/nuttx/net/connect.c
+++ b/nuttx/net/connect.c
@@ -372,12 +372,7 @@ static inline int tcp_connect(FAR struct socket *psock, const struct sockaddr_in
if (ret < 0)
{
- int err = errno;
- if (err >= 0)
- {
- err = ENOSYS;
- }
- ret = -err;
+ ret = -errno;
}
else
{
diff --git a/nuttx/net/net_checksd.c b/nuttx/net/net_checksd.c
new file mode 100644
index 000000000..76d30fbee
--- /dev/null
+++ b/nuttx/net/net_checksd.c
@@ -0,0 +1,88 @@
+/****************************************************************************
+ * net/net_checksd.c
+ *
+ * Copyright (C) 2011 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * 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. Neither the name NuttX 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 COPYRIGHT HOLDERS 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
+ * COPYRIGHT OWNER 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/socket.h>
+
+#include <sched.h>
+#include <errno.h>
+#include <debug.h>
+
+#include "net_internal.h"
+
+/****************************************************************************
+ * Global Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: net_checksd
+ *
+ * Description:
+ * Check if the socket descriptor is valid for the provided TCB and if it
+ * supports the requested access. This trivial operation is part of the
+ * fdopen() operation when the fdopen() is performed on a socket descriptor.
+ * It simply performs some sanity checking before permitting the socket
+ * descriptor to be wrapped as a C FILE stream.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_NET) && CONFIG_NFILE_DESCRIPTORS > 0
+int net_checksd(FAR _TCB *tcb, int sd, int oflags)
+{
+ FAR struct socket *psock = sockfd_socket(sd);
+
+ /* Verify that the sockfd corresponds to valid, allocated socket */
+
+ if (!psock || psock->s_crefs <= 0)
+ {
+ nvdbg("No valid socket for sd: %d\n", sd);
+ return -EBADF;
+ }
+
+ /* NOTE: We permit the socket FD to be "wrapped" in a stream as
+ * soon as the socket descriptor is created by socket(). Therefore
+ * (1) we don't care if the socket is connected yet, and (2) there
+ * are no access restrictions that can be enforced yet.
+ */
+
+ return OK;
+}
+#endif /* CONIG_NET && ONFIG_NFILE_DESCRIPTORS > 0 */
+
diff --git a/nuttx/net/send.c b/nuttx/net/send.c
index ffa689996..0b2f3739c 100644
--- a/nuttx/net/send.c
+++ b/nuttx/net/send.c
@@ -415,7 +415,7 @@ ssize_t send(int sockfd, const void *buf, size_t len, int flags)
goto errout;
}
- /* If this is a connected socket, then return ENOTCONN */
+ /* If this is an un-connected socket, then return ENOTCONN */
if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(psock->s_flags))
{