From f5d0fc74476e61ab991845063551408956606396 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 27 Apr 2018 08:36:25 -0600 Subject: [PATCH] fs/vfs and net/udp: Repartition logic of commit 0c963449d6e8f5f8b2dfe96e9de3116633a3749a, moving into net/udp. Add support to handle connected UDP sockets. The correct 'optimal blksize' to return is the MSS. The MTU is always too big. --- fs/vfs/fs_fstat.c | 24 ++--- include/nuttx/net/net.h | 26 ++++- include/nuttx/net/netconfig.h | 6 +- net/socket/Make.defs | 3 +- net/socket/net_fstat.c | 190 ++++++++++++++++++++++++++++++++++ 5 files changed, 227 insertions(+), 22 deletions(-) create mode 100644 net/socket/net_fstat.c diff --git a/fs/vfs/fs_fstat.c b/fs/vfs/fs_fstat.c index 64d96f9b75..5b357f25e0 100644 --- a/fs/vfs/fs_fstat.c +++ b/fs/vfs/fs_fstat.c @@ -84,28 +84,18 @@ int fstat(int fd, FAR struct stat *buf) if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS) { -#if defined(CONFIG_NET_TCP) && CONFIG_NSOCKET_DESCRIPTORS > 0 - if (sockfd_socket(fd) == NULL) +#if CONFIG_NSOCKET_DESCRIPTORS > 0 + /* Let the networking logic handle the fstat() */ + + ret = net_fstat(fd, buf); + if (ret < 0) { - ret = -EBADF; goto errout; } - else - { - memset(buf, 0, sizeof(struct stat)); - buf->st_mode = S_IFSOCK; -#ifdef CONFIG_NET_ETHERNET - /* REVISIT: Ideally, we would get the MTU from the device that - * serves the connection (assuming the socket is connected). - */ - - buf->st_blksize = CONFIG_NET_ETH_MTU; -#endif - return OK; - } + return OK; #else - /* No networking... it is a bad descriptor in any event */ + /* No networking... it is just a bad descriptor */ ret = -EBADF; goto errout; diff --git a/include/nuttx/net/net.h b/include/nuttx/net/net.h index 677984753c..9aee838305 100644 --- a/include/nuttx/net/net.h +++ b/include/nuttx/net/net.h @@ -1,7 +1,8 @@ /**************************************************************************** * include/nuttx/net/net.h * - * Copyright (C) 2007, 2009-2014, 2016-2017 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009-2014, 2016-2018 Gregory Nutt. All rights + * reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -551,6 +552,7 @@ int psock_close(FAR struct socket *psock); ****************************************************************************/ struct sockaddr; /* Forward reference. Defined in nuttx/include/sys/socket.h */ + int psock_bind(FAR struct socket *psock, FAR const struct sockaddr *addr, socklen_t addrlen); @@ -1113,6 +1115,7 @@ int netdev_ioctl(int sockfd, int cmd, unsigned long arg); #ifndef CONFIG_DISABLE_POLL struct pollfd; /* Forward reference -- see poll.h */ + int psock_poll(FAR struct socket *psock, struct pollfd *fds, bool setup); #endif @@ -1136,6 +1139,7 @@ int psock_poll(FAR struct socket *psock, struct pollfd *fds, bool setup); #ifndef CONFIG_DISABLE_POLL struct pollfd; /* Forward reference -- see poll.h */ + int net_poll(int sockfd, struct pollfd *fds, bool setup); #endif @@ -1173,6 +1177,26 @@ int net_dupsd(int sockfd, int minsd); int net_dupsd2(int sockfd1, int sockfd2); +/**************************************************************************** + * Name: net_fstat + * + * Description: + * Performs fstat operations on socket + * + * Input Parameters: + * sockfd - Socket descriptor of the socket to operate on + * bug - Caller-provided location in which to return the fstat data + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * any failure to indicate the nature of the failure. + * + ****************************************************************************/ + +struct stat; /* Forward reference. See sys/stat.h */ + +int net_fstat(int sockfd, FAR struct stat *buf); + /**************************************************************************** * Name: net_clone * diff --git a/include/nuttx/net/netconfig.h b/include/nuttx/net/netconfig.h index f290f03986..491b920c0e 100644 --- a/include/nuttx/net/netconfig.h +++ b/include/nuttx/net/netconfig.h @@ -76,7 +76,7 @@ #define __IPv4_HDRLEN 20 /* Must match IPv4_HDRLEN in include/nuttx/net/ip.h */ #define __IPv6_HDRLEN 40 /* Must match IPv4_HDRLEN in include/nuttx/net/ip.h */ -#define __UDP_HDRLEN 8 /* Must match UDP_HDRLEN in include/nuttx/net/dup.h */ +#define __UDP_HDRLEN 8 /* Must match UDP_HDRLEN in include/nuttx/net/udp.h */ #define __TCP_HDRLEN 20 /* Must match TCP_HDRLEN in include/nuttx/net/tcp.h */ /* REVISIT: Not really a constant */ @@ -321,8 +321,8 @@ # define __TUN_MIN_UDP_MSS(h) MIN(TUN_UDP_MSS(h),__SLIP_MIN_UDP_MSS(h)) # define __TUN_MAX_UDP_MSS(h) MAX(TUN_UDP_MSS(h),__SLIP_MAX_UDP_MSS(h)) #else -# define __TUN_MIN_UDP_MSS(h) __SLIP_MIN_UDP_MSS(h) -# define __TUN_MAX_UDP_MSS(h) __SLIP_MAX_UDP_MSS(h) +# define __TUN_MIN_UDP_MSS(h) __SLIP_MIN_UDP_MSS(h) +# define __TUN_MAX_UDP_MSS(h) __SLIP_MAX_UDP_MSS(h) #endif #ifdef CONFIG_NET_IPv4 diff --git a/net/socket/Make.defs b/net/socket/Make.defs index c38dbe736f..d007e66e3b 100644 --- a/net/socket/Make.defs +++ b/net/socket/Make.defs @@ -1,7 +1,7 @@ ############################################################################ # net/socket/Make.defs # -# Copyright (C) 2014-2015, 2017 Gregory Nutt. All rights reserved. +# Copyright (C) 2014-2015, 2017, 2018 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -38,6 +38,7 @@ SOCK_CSRCS += bind.c connect.c getsockname.c recv.c recvfrom.c send.c SOCK_CSRCS += sendto.c socket.c net_sockets.c net_close.c net_dupsd.c SOCK_CSRCS += net_dupsd2.c net_sockif.c net_clone.c net_poll.c net_vfcntl.c +SOCK_CSRCS += net_fstat.c # TCP/IP support diff --git a/net/socket/net_fstat.c b/net/socket/net_fstat.c new file mode 100644 index 0000000000..73d784650f --- /dev/null +++ b/net/socket/net_fstat.c @@ -0,0 +1,190 @@ +/**************************************************************************** + * net/socket/net_fstat.c + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include +#include + +#include +#include +#include + +#include "tcp/tcp.h" +#include "udp/udp.h" +#include "socket/socket.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: net_fstat + * + * Description: + * Performs fstat operations on socket + * + * Input Parameters: + * sockfd - Socket descriptor of the socket to operate on + * bug - Caller-provided location in which to return the fstat data + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * any failure to indicate the nature of the failure. + * + ****************************************************************************/ + +int net_fstat(int sockfd, FAR struct stat *buf) +{ + FAR struct socket *psock; + int ret = OK; + + /* Get the underlying socket structure */ + + psock = sockfd_socket(sockfd); + if (psock == NULL) + { + /* sockfd does not refer to a valid, open socket */ + + return -EBADF; + } + + /* Return fstat data. The st_mode and st_blksize fields are the only + * fields set for socket descriptors. The st_mode field is set to a value + * that indicates the descriptor is a socket descriptor and the st_blksize + * field is set to an optimal value determined by the system. The optimal + * packet size is the MSS. + */ + + memset(buf, 0, sizeof(struct stat)); + buf->st_mode = S_IFSOCK; + + /* The socket must be open and in a connected state in order to get the + * MSS. There may be multiple networks served by different network + * devices, each supporting a different MSS. + */ + + if (psock->s_conn == NULL || !_SS_ISCONNECTED(psock->s_flags)) + { + /* Not connected.. Return an optimal blocksize of zero (or, perhaps, + * even an error?) + * + * REVISIT: The concept of connected only applies to TCP and UDP + * sockets. Other sockets, such raw radio sockets, have no such + * concept. + */ + + nwarn("WARNING: Socket not connected\n"); + return OK; + } + + /* We are only prepared to handle the MSS of connected TCP/IP and UDP + * sockets here. + */ + + switch (psock->s_type) + { +#if defined(NET_TCP_HAVE_STACK) + case SOCK_STREAM: + { + FAR struct tcp_conn_s *conn = (FAR struct tcp_conn_s *)psock->s_conn; + + /* For TCP, the MSS is a dynamic value that maintained in the + * connection structure. + */ + + buf->st_blksize = conn->mss; + } + break; +#endif + +#if defined(NET_UDP_HAVE_STACK) + case SOCK_DGRAM: + { + FAR struct udp_conn_s *conn = (FAR struct udp_conn_s *)psock->s_conn; + FAR struct net_driver_s *dev; + uint16_t iplen; + + /* For a connected UDP socket, we have do do a little more work: + * + * MSS = MTU - LL_HDRLEN - UDP_HDRLEN - IP_HDRLEN + * + * We need to have the device that services the connection in order + * to get the MTU and LL_HDRLEN: + */ + + dev = udp_find_raddr_device(conn); + if (dev == NULL) + { + /* This should never happen except perhaps in some rare race + * condition. If the UDP socket is connected, then the device + * service the network that it is connected to should always + * exist. + */ + + nerr("ERROR: Could not find network device\n"); + ret = -ENODEV; + } + else + { + /* We need the length of the IP header */ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + iplen = (conn->domain == PF_INET) ? IPv4_HDRLEN : IPv6_HDRLEN +#elif defined(CONFIG_NET_IPv4) + iplen = IPv4_HDRLEN; +#else + iplen = IPv6_HDRLEN; +#endif + /* Now we can calculate the MSS */ + + buf->st_blksize = UDP_MSS(dev, iplen); + } + } + break; +#endif + default: + nwarn("WARNING: Unhandled socket type: %u\n", psock->s_type); + break; + } + + return ret; +}