net/inet: add support of FIONREAD

Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an 2021-06-07 18:06:44 +08:00 committed by Xiang Xiao
parent ebf89ff63b
commit eabe535de7
11 changed files with 309 additions and 19 deletions

View File

@ -195,15 +195,13 @@ struct sock_intf_s
CODE ssize_t (*si_recvmsg)(FAR struct socket *psock,
FAR struct msghdr *msg, int flags);
CODE int (*si_close)(FAR struct socket *psock);
CODE int (*si_ioctl)(FAR struct socket *psock, int cmd,
FAR void *arg, size_t arglen);
#ifdef CONFIG_NET_SENDFILE
CODE ssize_t (*si_sendfile)(FAR struct socket *psock,
FAR struct file *infile, FAR off_t *offset,
size_t count);
#endif
#ifdef CONFIG_NET_USRSOCK
CODE int (*si_ioctl)(FAR struct socket *psock, int cmd,
FAR void *arg, size_t arglen);
#endif
};
/* Each socket refers to a connection structure of type FAR void *. Each

View File

@ -624,9 +624,7 @@ int netdev_carrier_off(FAR struct net_driver_s *dev);
*
****************************************************************************/
#ifdef CONFIG_NET_USRSOCK
ssize_t net_ioctl_arglen(int cmd);
#endif
/****************************************************************************
* Name: net_chksum

View File

@ -76,6 +76,8 @@ static ssize_t inet_sendmsg(FAR struct socket *psock,
FAR struct msghdr *msg, int flags);
static ssize_t inet_recvmsg(FAR struct socket *psock,
FAR struct msghdr *msg, int flags);
static int inet_ioctl(FAR struct socket *psock, int cmd,
FAR void *arg, size_t arglen);
#ifdef CONFIG_NET_SENDFILE
static ssize_t inet_sendfile(FAR struct socket *psock,
FAR struct file *infile, FAR off_t *offset,
@ -100,7 +102,8 @@ static const struct sock_intf_s g_inet_sockif =
inet_poll, /* si_poll */
inet_sendmsg, /* si_sendmsg */
inet_recvmsg, /* si_recvmsg */
inet_close /* si_close */
inet_close, /* si_close */
inet_ioctl /* si_ioctl */
#ifdef CONFIG_NET_SENDFILE
,
inet_sendfile /* si_sendfile */
@ -1284,6 +1287,47 @@ static ssize_t inet_sendmsg(FAR struct socket *psock,
return ret;
}
/****************************************************************************
* Name: inet_ioctl
*
* Description:
* This function performs network device specific operations.
*
* Parameters:
* psock A reference to the socket structure of the socket
* cmd The ioctl command
* arg The argument of the ioctl cmd
* arglen The length of 'arg'
*
****************************************************************************/
static int inet_ioctl(FAR struct socket *psock, int cmd,
FAR void *arg, size_t arglen)
{
/* Verify that the sockfd corresponds to valid, allocated socket */
if (psock == NULL || psock->s_conn == NULL)
{
return -EBADF;
}
#if defined(CONFIG_NET_TCP) && !defined(CONFIG_NET_TCP_NO_STACK)
if (psock->s_type == SOCK_STREAM)
{
return tcp_ioctl(psock->s_conn, cmd, arg, arglen);
}
#endif
#if defined(CONFIG_NET_UDP) && defined(NET_UDP_HAVE_STACK)
if (psock->s_type == SOCK_DGRAM)
{
return udp_ioctl(psock->s_conn, cmd, arg, arglen);
}
#endif
return -EINVAL;
}
/****************************************************************************
* Name: inet_sendfile
*

View File

@ -34,6 +34,7 @@
#include <netinet/in.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/net/net.h>
#include <socket/socket.h>
@ -67,6 +68,8 @@ static int local_accept(FAR struct socket *psock,
static int local_poll(FAR struct socket *psock,
FAR struct pollfd *fds, bool setup);
static int local_close(FAR struct socket *psock);
static int local_ioctl(FAR struct socket *psock, int cmd,
FAR void *arg, size_t arglen);
/****************************************************************************
* Public Data
@ -86,7 +89,8 @@ const struct sock_intf_s g_local_sockif =
local_poll, /* si_poll */
local_sendmsg, /* si_sendmsg */
local_recvmsg, /* si_recvmsg */
local_close /* si_close */
local_close, /* si_close */
local_ioctl /* si_ioctl */
};
/****************************************************************************
@ -682,6 +686,48 @@ static int local_close(FAR struct socket *psock)
}
}
/****************************************************************************
* Name: local_ioctl
*
* Description:
* This function performs local device specific operations.
*
* Parameters:
* psock A reference to the socket structure of the socket
* cmd The ioctl command
* arg The argument of the ioctl cmd
* arglen The length of 'arg'
*
****************************************************************************/
static int local_ioctl(FAR struct socket *psock, int cmd,
FAR void *arg, size_t arglen)
{
FAR struct local_conn_s *conn;
int ret = OK;
conn = (FAR struct local_conn_s *)psock->s_conn;
switch (cmd)
{
case FIONREAD:
if (conn->lc_infile.f_inode != NULL)
{
ret = file_ioctl(&conn->lc_infile, cmd, arg);
}
else
{
ret = -ENOTCONN;
}
break;
default:
ret = -ENOTTY;
break;
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/

View File

@ -1454,7 +1454,7 @@ static int netdev_rt_ioctl(FAR struct socket *psock, int cmd,
#endif
/****************************************************************************
* Name: netdev_usrsock_ioctl
* Name: netdev_ioctl
*
* Description:
* Perform user private ioctl operations.
@ -1470,9 +1470,8 @@ static int netdev_rt_ioctl(FAR struct socket *psock, int cmd,
*
****************************************************************************/
#ifdef CONFIG_NET_USRSOCK
static int netdev_usrsock_ioctl(FAR struct socket *psock, int cmd,
unsigned long arg)
static int netdev_ioctl(FAR struct socket *psock, int cmd,
unsigned long arg)
{
if (psock->s_sockif && psock->s_sockif->si_ioctl)
{
@ -1491,7 +1490,6 @@ static int netdev_usrsock_ioctl(FAR struct socket *psock, int cmd,
return -ENOTTY;
}
}
#endif
/****************************************************************************
* Public Functions
@ -1512,11 +1510,13 @@ static int netdev_usrsock_ioctl(FAR struct socket *psock, int cmd,
*
****************************************************************************/
#ifdef CONFIG_NET_USRSOCK
ssize_t net_ioctl_arglen(int cmd)
{
switch (cmd)
{
case FIONREAD:
return sizeof(int);
case SIOCGIFADDR:
case SIOCSIFADDR:
case SIOCGIFDSTADDR:
@ -1607,7 +1607,6 @@ ssize_t net_ioctl_arglen(int cmd)
return -ENOTTY;
}
}
#endif
/****************************************************************************
* Name: psock_ioctl and psock_vioctl
@ -1654,12 +1653,10 @@ int psock_vioctl(FAR struct socket *psock, int cmd, va_list ap)
arg = va_arg(ap, unsigned long);
#ifdef CONFIG_NET_USRSOCK
/* Check for a USRSOCK ioctl command */
ret = netdev_usrsock_ioctl(psock, cmd, arg);
ret = netdev_ioctl(psock, cmd, arg);
if (ret == -ENOTTY)
#endif
{
/* Check for a standard network IOCTL command. */

View File

@ -53,7 +53,7 @@ endif
NET_CSRCS += tcp_conn.c tcp_seqno.c tcp_devpoll.c tcp_finddev.c tcp_timer.c
NET_CSRCS += tcp_send.c tcp_input.c tcp_appsend.c tcp_listen.c tcp_close.c
NET_CSRCS += tcp_monitor.c tcp_callback.c tcp_backlog.c tcp_ipselect.c
NET_CSRCS += tcp_recvwindow.c tcp_netpoll.c
NET_CSRCS += tcp_recvwindow.c tcp_netpoll.c tcp_ioctl.c
# TCP write buffering

View File

@ -1856,6 +1856,23 @@ int tcp_txdrain(FAR struct socket *psock, unsigned int timeout);
# define tcp_txdrain(conn, timeout) (0)
#endif
/****************************************************************************
* Name: tcp_ioctl
*
* Description:
* This function performs tcp specific ioctl() operations.
*
* Parameters:
* conn The TCP connection of interest
* cmd The ioctl command
* arg The argument of the ioctl cmd
* arglen The length of 'arg'
*
****************************************************************************/
int tcp_ioctl(FAR struct tcp_conn_s *conn, int cmd,
FAR void *arg, size_t arglen);
#ifdef __cplusplus
}
#endif

85
net/tcp/tcp_ioctl.c Normal file
View File

@ -0,0 +1,85 @@
/****************************************************************************
* net/tcp/tcp_ioctl.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <debug.h>
#include <errno.h>
#include <net/if.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/mm/iob.h>
#include <nuttx/net/net.h>
#include "tcp/tcp.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: tcp_ioctl
*
* Description:
* This function performs tcp specific ioctl() operations.
*
* Parameters:
* conn The TCP connection of interest
* cmd The ioctl command
* arg The argument of the ioctl cmd
* arglen The length of 'arg'
*
****************************************************************************/
int tcp_ioctl(FAR struct tcp_conn_s *conn,
int cmd, FAR void *arg, size_t arglen)
{
int ret = OK;
net_lock();
switch (cmd)
{
case FIONREAD:
if (conn->readahead != NULL)
{
*(FAR int *)((uintptr_t)arg) = conn->readahead->io_pktlen;
}
else
{
*(FAR int *)((uintptr_t)arg) = 0;
}
break;
default:
ret = -ENOTTY;
break;
}
net_unlock();
return ret;
}

View File

@ -48,6 +48,7 @@ endif
NET_CSRCS += udp_conn.c udp_devpoll.c udp_send.c udp_input.c udp_finddev.c
NET_CSRCS += udp_close.c udp_callback.c udp_ipselect.c udp_netpoll.c
NET_CSRCS += udp_ioctl.c
# UDP write buffering

View File

@ -869,6 +869,23 @@ int udp_txdrain(FAR struct socket *psock, unsigned int timeout);
# define udp_txdrain(conn, timeout) (0)
#endif
/****************************************************************************
* Name: udp_ioctl
*
* Description:
* This function performs udp specific ioctl() operations.
*
* Parameters:
* conn The TCP connection of interest
* cmd The ioctl command
* arg The argument of the ioctl cmd
* arglen The length of 'arg'
*
****************************************************************************/
int udp_ioctl(FAR struct udp_conn_s *conn,
int cmd, FAR void *arg, size_t arglen);
#undef EXTERN
#ifdef __cplusplus
}

87
net/udp/udp_ioctl.c Normal file
View File

@ -0,0 +1,87 @@
/****************************************************************************
* net/udp/udp_ioctl.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <debug.h>
#include <errno.h>
#include <net/if.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/mm/iob.h>
#include <nuttx/net/net.h>
#include "udp/udp.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: udp_ioctl
*
* Description:
* This function performs udp specific ioctl() operations.
*
* Parameters:
* conn The TCP connection of interest
* cmd The ioctl command
* arg The argument of the ioctl cmd
* arglen The length of 'arg'
*
****************************************************************************/
int udp_ioctl(FAR struct udp_conn_s *conn,
int cmd, FAR void *arg, size_t arglen)
{
FAR struct iob_s *iob;
int ret = OK;
net_lock();
switch (cmd)
{
case FIONREAD:
iob = iob_peek_queue(&conn->readahead);
if (iob)
{
*(FAR int *)((uintptr_t)arg) = iob->io_pktlen;
}
else
{
*(FAR int *)((uintptr_t)arg) = 0;
}
break;
default:
ret = -ENOTTY;
break;
}
net_unlock();
return ret;
}