2008-11-16 19:48:29 +01:00
|
|
|
/****************************************************************************
|
2014-06-29 01:25:18 +02:00
|
|
|
* net/socket/net_poll.c
|
2008-11-16 19:48:29 +01:00
|
|
|
*
|
2015-01-30 13:08:26 +01:00
|
|
|
* Copyright (C) 2008-2009, 2011-2015 Gregory Nutt. All rights reserved.
|
2012-01-30 22:29:59 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2008-11-16 19:48:29 +01:00
|
|
|
*
|
|
|
|
* 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 <errno.h>
|
2008-11-18 23:14:25 +01:00
|
|
|
|
2014-06-29 02:36:09 +02:00
|
|
|
#include "tcp/tcp.h"
|
2015-01-30 13:08:26 +01:00
|
|
|
#include "udp/udp.h"
|
2015-01-30 14:09:25 +01:00
|
|
|
#include "local/local.h"
|
2014-06-29 01:25:18 +02:00
|
|
|
#include "socket/socket.h"
|
2017-03-31 16:58:14 +02:00
|
|
|
#include "usrsock/usrsock.h"
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2015-01-30 14:25:01 +01:00
|
|
|
#if defined(CONFIG_NET) && !defined(CONFIG_DISABLE_POLL)
|
|
|
|
|
2008-11-16 19:48:29 +01:00
|
|
|
/****************************************************************************
|
2008-11-18 18:30:30 +01:00
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 14:09:25 +01:00
|
|
|
/* Network polling can only be supported if poll support is provided by TCP,
|
|
|
|
* UDP, or LOCAL sockets.
|
2008-11-18 18:30:30 +01:00
|
|
|
*/
|
|
|
|
|
2015-01-30 14:09:25 +01:00
|
|
|
#undef HAVE_NET_POLL
|
2017-03-31 16:58:14 +02:00
|
|
|
#if defined(HAVE_TCP_POLL) || defined(HAVE_UDP_POLL) || \
|
|
|
|
defined(HAVE_LOCAL_POLL) || defined(CONFIG_NET_USRSOCK)
|
2015-01-30 14:09:25 +01:00
|
|
|
# define HAVE_NET_POLL 1
|
2008-11-18 18:30:30 +01:00
|
|
|
#endif
|
|
|
|
|
2015-01-30 14:09:25 +01:00
|
|
|
#ifdef HAVE_NET_POLL
|
|
|
|
|
2008-11-18 18:30:30 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 13:08:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Function: net_pollsetup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup to monitor events on one socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - The socket of interest
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int net_pollsetup(FAR struct socket *psock,
|
|
|
|
FAR struct pollfd *fds)
|
|
|
|
{
|
2015-01-30 14:09:25 +01:00
|
|
|
#if defined(HAVE_TCP_POLL) || defined(HAVE_LOCAL_POLL)
|
2015-01-30 13:08:26 +01:00
|
|
|
if (psock->s_type == SOCK_STREAM)
|
|
|
|
{
|
2015-01-30 14:09:25 +01:00
|
|
|
#ifdef HAVE_LOCAL_POLL
|
|
|
|
#ifdef HAVE_TCP_POLL
|
|
|
|
if (psock->s_domain == PF_LOCAL)
|
2015-01-30 13:08:26 +01:00
|
|
|
#endif
|
2015-01-30 14:09:25 +01:00
|
|
|
{
|
|
|
|
return local_pollsetup(psock, fds);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_LOCAL_POLL */
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-01-30 14:09:25 +01:00
|
|
|
#ifdef HAVE_TCP_POLL
|
|
|
|
#ifdef HAVE_LOCAL_POLL
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return tcp_pollsetup(psock, fds);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_TCP_POLL */
|
|
|
|
}
|
|
|
|
#endif /* HAVE_TCP_POLL || HAVE_LOCAL_POLL */
|
|
|
|
|
|
|
|
#if defined(HAVE_UDP_POLL) || defined(HAVE_LOCAL_POLL)
|
2015-01-30 13:08:26 +01:00
|
|
|
if (psock->s_type != SOCK_STREAM)
|
|
|
|
{
|
2015-01-30 14:09:25 +01:00
|
|
|
#ifdef HAVE_LOCAL_POLL
|
|
|
|
#ifdef HAVE_UDP_POLL
|
|
|
|
if (psock->s_domain == PF_LOCAL)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return local_pollsetup(psock, fds);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_LOCAL_POLL */
|
|
|
|
|
|
|
|
#ifdef HAVE_UDP_POLL
|
|
|
|
#ifdef HAVE_LOCAL_POLL
|
|
|
|
else
|
2015-01-30 13:08:26 +01:00
|
|
|
#endif
|
2015-01-30 14:09:25 +01:00
|
|
|
{
|
|
|
|
return udp_pollsetup(psock, fds);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_UDP_POLL */
|
|
|
|
}
|
|
|
|
#endif /* HAVE_UDP_POLL || HAVE_LOCAL_POLL */
|
2015-01-30 13:08:26 +01:00
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2008-11-18 18:30:30 +01:00
|
|
|
|
2015-01-30 13:08:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Function: net_pollteardown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Teardown monitoring of events on an socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - The TCP/IP socket of interest
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int net_pollteardown(FAR struct socket *psock,
|
|
|
|
FAR struct pollfd *fds)
|
|
|
|
{
|
2015-01-30 14:09:25 +01:00
|
|
|
#if defined(HAVE_TCP_POLL) || defined(HAVE_LOCAL_POLL)
|
2015-01-30 13:08:26 +01:00
|
|
|
if (psock->s_type == SOCK_STREAM)
|
|
|
|
{
|
2015-01-30 14:09:25 +01:00
|
|
|
#ifdef HAVE_LOCAL_POLL
|
|
|
|
#ifdef HAVE_TCP_POLL
|
|
|
|
if (psock->s_domain == PF_LOCAL)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return local_pollteardown(psock, fds);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_LOCAL_POLL */
|
|
|
|
|
|
|
|
#ifdef HAVE_TCP_POLL
|
|
|
|
#ifdef HAVE_LOCAL_POLL
|
|
|
|
else
|
2015-01-30 13:08:26 +01:00
|
|
|
#endif
|
2015-01-30 14:09:25 +01:00
|
|
|
{
|
|
|
|
return tcp_pollteardown(psock, fds);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_TCP_POLL */
|
|
|
|
}
|
|
|
|
#endif /* HAVE_TCP_POLL || HAVE_LOCAL_POLL */
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-01-30 14:09:25 +01:00
|
|
|
#if defined(HAVE_UDP_POLL) || defined(HAVE_LOCAL_POLL)
|
2015-01-30 13:08:26 +01:00
|
|
|
if (psock->s_type != SOCK_STREAM)
|
|
|
|
{
|
2015-01-30 14:09:25 +01:00
|
|
|
#ifdef HAVE_LOCAL_POLL
|
|
|
|
#ifdef HAVE_UDP_POLL
|
|
|
|
if (psock->s_domain == PF_LOCAL)
|
2015-01-30 13:08:26 +01:00
|
|
|
#endif
|
2015-01-30 14:09:25 +01:00
|
|
|
{
|
|
|
|
return local_pollteardown(psock, fds);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_LOCAL_POLL */
|
|
|
|
|
|
|
|
#ifdef HAVE_UDP_POLL
|
|
|
|
#ifdef HAVE_LOCAL_POLL
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return udp_pollteardown(psock, fds);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_UDP_POLL */
|
|
|
|
}
|
|
|
|
#endif /* HAVE_UDP_POLL || HAVE_LOCAL_POLL */
|
2015-01-30 13:08:26 +01:00
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2015-01-30 14:09:25 +01:00
|
|
|
#endif /* HAVE_NET_POLL */
|
2008-11-18 18:30:30 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
2008-11-16 19:48:29 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-08-16 18:24:06 +02:00
|
|
|
* Function: psock_poll
|
2008-11-16 19:48:29 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard poll() operation redirects operations on socket descriptors
|
|
|
|
* to this function.
|
|
|
|
*
|
2008-11-18 18:30:30 +01:00
|
|
|
* Input Parameters:
|
2013-01-24 20:19:38 +01:00
|
|
|
* psock - An instance of the internal socket structure.
|
2008-11-19 19:43:50 +01:00
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
2009-12-15 16:57:25 +01:00
|
|
|
* setup - true: Setup up the poll; false: Teardown the poll
|
2008-11-16 19:48:29 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2008-11-18 18:30:30 +01:00
|
|
|
* 0: Success; Negated errno on failure
|
2008-11-16 19:48:29 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-24 20:19:38 +01:00
|
|
|
int psock_poll(FAR struct socket *psock, FAR struct pollfd *fds, bool setup)
|
2008-11-16 19:48:29 +01:00
|
|
|
{
|
2015-01-30 14:09:25 +01:00
|
|
|
#ifndef HAVE_NET_POLL
|
|
|
|
return -ENOSYS;
|
|
|
|
#else
|
2008-11-18 18:30:30 +01:00
|
|
|
int ret;
|
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
#ifdef CONFIG_NET_USRSOCK
|
|
|
|
if (psock->s_type == SOCK_USRSOCK_TYPE)
|
|
|
|
{
|
|
|
|
/* Perform usrsock setup/teardown. */
|
|
|
|
|
|
|
|
return usrsock_poll(psock, fds, setup);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-18 18:30:30 +01:00
|
|
|
/* Check if we are setting up or tearing down the poll */
|
2013-05-23 15:16:46 +02:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
if (setup)
|
2008-11-18 18:30:30 +01:00
|
|
|
{
|
|
|
|
/* Perform the TCP/IP poll() setup */
|
|
|
|
|
2008-11-19 03:40:09 +01:00
|
|
|
ret = net_pollsetup(psock, fds);
|
2008-11-18 18:30:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Perform the TCP/IP poll() teardown */
|
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
ret = net_pollteardown(psock, fds);
|
2008-11-18 18:30:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-01-30 14:09:25 +01:00
|
|
|
#endif /* HAVE_NET_POLL */
|
2013-01-24 20:19:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Function: net_poll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard poll() operation redirects operations on socket descriptors
|
|
|
|
* to this function.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd - The socket descriptor of interest
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
* setup - true: Setup up the poll; false: Teardown the poll
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int net_poll(int sockfd, struct pollfd *fds, bool setup)
|
|
|
|
{
|
2015-01-30 14:09:25 +01:00
|
|
|
#ifndef HAVE_NET_POLL
|
2013-01-24 20:19:38 +01:00
|
|
|
return -ENOSYS;
|
|
|
|
#else
|
|
|
|
FAR struct socket *psock;
|
|
|
|
|
|
|
|
/* Get the underlying socket structure and verify that the sockfd
|
|
|
|
* corresponds to valid, allocated socket
|
|
|
|
*/
|
|
|
|
|
|
|
|
psock = sockfd_socket(sockfd);
|
|
|
|
if (!psock || psock->s_crefs <= 0)
|
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then let psock_poll() do the heavy lifting */
|
|
|
|
|
|
|
|
return psock_poll(psock, fds, setup);
|
2015-01-30 14:09:25 +01:00
|
|
|
#endif /* HAVE_NET_POLL */
|
2008-11-16 19:48:29 +01:00
|
|
|
}
|
|
|
|
|
2008-11-18 18:30:30 +01:00
|
|
|
#endif /* CONFIG_NET && !CONFIG_DISABLE_POLL */
|