2007-09-03 22:34:44 +02:00
|
|
|
/****************************************************************************
|
2014-06-29 01:25:18 +02:00
|
|
|
* net/socket/net_close.c
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
2015-01-16 19:30:18 +01:00
|
|
|
* Copyright (C) 2007-2015 Gregory Nutt. All rights reserved.
|
2012-01-30 22:29:59 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-09-03 22:34:44 +02: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>
|
|
|
|
#ifdef CONFIG_NET
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2009-12-15 16:57:25 +01:00
|
|
|
#include <stdint.h>
|
2014-01-13 18:58:45 +01:00
|
|
|
#include <stdbool.h>
|
2007-09-03 22:34:44 +02:00
|
|
|
#include <errno.h>
|
2007-11-24 14:02:03 +01:00
|
|
|
#include <debug.h>
|
2014-07-22 02:46:47 +02:00
|
|
|
#include <assert.h>
|
2007-11-24 14:02:03 +01:00
|
|
|
|
|
|
|
#include <arch/irq.h>
|
2014-07-05 00:38:51 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/tcp.h>
|
|
|
|
#include <nuttx/net/udp.h>
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2014-01-13 18:58:45 +01:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
|
|
|
# include <nuttx/clock.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-27 17:56:45 +02:00
|
|
|
#include "netdev/netdev.h"
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-06-29 02:36:09 +02:00
|
|
|
#include "tcp/tcp.h"
|
2014-07-05 22:40:29 +02:00
|
|
|
#include "udp/udp.h"
|
2014-06-25 18:34:52 +02:00
|
|
|
#include "pkt/pkt.h"
|
2015-01-24 22:19:50 +01:00
|
|
|
#include "local/local.h"
|
2014-07-05 00:38:51 +02:00
|
|
|
#include "socket/socket.h"
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2014-01-13 18:58:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/****************************************************************************
|
2007-11-24 14:02:03 +01:00
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-13 18:58:45 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
|
|
|
struct tcp_close_s
|
|
|
|
{
|
2014-06-29 20:59:34 +02:00
|
|
|
FAR struct devif_callback_s *cl_cb; /* Reference to TCP callback instance */
|
2014-01-13 18:58:45 +01:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2014-06-29 20:59:34 +02:00
|
|
|
FAR struct socket *cl_psock; /* Reference to the TCP socket */
|
|
|
|
sem_t cl_sem; /* Signals disconnect completion */
|
|
|
|
int cl_result; /* The result of the close */
|
2016-01-21 18:54:26 +01:00
|
|
|
systime_t cl_start; /* Time close started (in ticks) */
|
2014-01-13 18:58:45 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2007-11-24 14:02:03 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-13 18:58:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Function: close_timeout
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check for a timeout on a lingering close.
|
|
|
|
*
|
|
|
|
* Parameters:
|
2014-01-13 19:04:01 +01:00
|
|
|
* pstate - close state structure
|
2014-01-13 18:58:45 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* TRUE:timeout FALSE:no timeout
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Running at the interrupt level
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-07 20:35:24 +02:00
|
|
|
#if defined(CONFIG_NET_TCP) && defined(CONFIG_NET_SOLINGER)
|
2014-01-13 18:58:45 +01:00
|
|
|
static inline int close_timeout(FAR struct tcp_close_s *pstate)
|
|
|
|
{
|
|
|
|
FAR struct socket *psock = 0;
|
|
|
|
|
|
|
|
/* Make sure that we are performing a lingering close */
|
|
|
|
|
|
|
|
if (pstate)
|
|
|
|
{
|
2015-10-08 23:10:04 +02:00
|
|
|
/* Yes Check for a timeout configured via setsockopts(SO_LINGER).
|
|
|
|
* If none... we well let the send wait forever.
|
|
|
|
*/
|
2014-01-13 18:58:45 +01:00
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
psock = pstate->cl_psock;
|
|
|
|
if (psock && psock->s_linger != 0)
|
|
|
|
{
|
|
|
|
/* Check if the configured timeout has elapsed */
|
2014-01-13 18:58:45 +01:00
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
return net_timeo(pstate->cl_start, psock->s_linger);
|
|
|
|
}
|
2014-01-13 18:58:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No timeout */
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-08-07 20:35:24 +02:00
|
|
|
#endif /* CONFIG_NET_SOCKOPTS && CONFIG_NET_SOLINGER */
|
2014-01-13 18:58:45 +01:00
|
|
|
|
2007-11-24 14:02:03 +01:00
|
|
|
/****************************************************************************
|
2012-01-31 21:32:49 +01:00
|
|
|
* Function: netclose_interrupt
|
2007-11-24 14:02:03 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-31 21:32:49 +01:00
|
|
|
* Handle uIP callback events.
|
2007-11-24 14:02:03 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* conn - uIP TCP connection structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called from normal user-level logic
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP
|
2014-06-28 00:48:12 +02:00
|
|
|
static uint16_t netclose_interrupt(FAR struct net_driver_s *dev,
|
2013-10-06 17:48:54 +02:00
|
|
|
FAR void *pvconn, FAR void *pvpriv,
|
|
|
|
uint16_t flags)
|
2007-11-24 14:02:03 +01:00
|
|
|
{
|
2014-01-13 18:58:45 +01:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_close_s *pstate = (FAR struct tcp_close_s *)pvpriv;
|
2014-01-13 18:58:45 +01:00
|
|
|
#endif
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *conn = (FAR struct tcp_conn_s *)pvconn;
|
2007-11-24 14:02:03 +01:00
|
|
|
|
2013-10-06 17:48:54 +02:00
|
|
|
DEBUGASSERT(conn != NULL);
|
2007-11-24 14:02:03 +01:00
|
|
|
|
2013-10-17 17:13:31 +02:00
|
|
|
nllvdbg("conn: %p flags: %04x\n", conn, flags);
|
2013-10-06 17:48:54 +02:00
|
|
|
|
2015-05-30 17:12:27 +02:00
|
|
|
/* TCP_DISCONN_EVENTS:
|
|
|
|
* TCP_CLOSE: The remote host has closed the connection
|
|
|
|
* TCP_ABORT: The remote host has aborted the connection
|
|
|
|
* TCP_TIMEDOUT: The remote did not respond, the connection timed out
|
|
|
|
* NETDEV_DOWN: The network device went down
|
2013-10-06 17:48:54 +02:00
|
|
|
*/
|
|
|
|
|
2015-05-30 17:12:27 +02:00
|
|
|
if ((flags & TCP_DISCONN_EVENTS) != 0)
|
2007-11-24 14:02:03 +01:00
|
|
|
{
|
2014-01-13 18:58:45 +01:00
|
|
|
/* The disconnection is complete */
|
2007-11-24 14:02:03 +01:00
|
|
|
|
2014-01-13 18:58:45 +01:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2015-10-08 23:10:04 +02:00
|
|
|
/* pstate non-NULL means that we are performing a LINGERing close. */
|
2014-01-13 18:58:45 +01:00
|
|
|
|
|
|
|
if (pstate)
|
|
|
|
{
|
2014-01-14 16:43:59 +01:00
|
|
|
/* Wake up the waiting thread with a successful result */
|
|
|
|
|
2014-01-13 18:58:45 +01:00
|
|
|
pstate->cl_result = OK;
|
|
|
|
goto end_wait;
|
|
|
|
}
|
2013-10-08 17:14:27 +02:00
|
|
|
|
2014-01-13 18:58:45 +01:00
|
|
|
/* Otherwise, nothing is waiting on the close event and we can perform
|
|
|
|
* the completion actions here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Free connection resources */
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_free(conn);
|
2014-01-13 18:58:45 +01:00
|
|
|
|
|
|
|
/* Stop further callbacks */
|
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 20:35:24 +02:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2014-01-13 18:58:45 +01:00
|
|
|
/* Check for a timeout. */
|
|
|
|
|
|
|
|
else if (pstate && close_timeout(pstate))
|
|
|
|
{
|
2014-01-14 16:43:59 +01:00
|
|
|
/* Yes.. Wake up the waiting thread and report the timeout */
|
2013-10-08 17:14:27 +02:00
|
|
|
|
2014-01-13 18:58:45 +01:00
|
|
|
nlldbg("CLOSE timeout\n");
|
|
|
|
pstate->cl_result = -ETIMEDOUT;
|
|
|
|
goto end_wait;
|
2013-10-06 17:48:54 +02:00
|
|
|
}
|
2014-01-13 18:58:45 +01:00
|
|
|
|
2014-08-07 20:35:24 +02:00
|
|
|
#endif /* CONFIG_NET_SOLINGER */
|
2014-01-14 00:11:01 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
2014-01-13 18:58:45 +01:00
|
|
|
/* Check if all outstanding bytes have been ACKed */
|
|
|
|
|
2016-01-22 22:58:02 +01:00
|
|
|
else if (conn->unacked != 0 || !sq_empty(&conn->write_q))
|
2014-01-13 18:58:45 +01:00
|
|
|
{
|
|
|
|
/* No... we are still waiting for ACKs. Drop any received data, but
|
2014-07-07 01:22:02 +02:00
|
|
|
* do not yet report TCP_CLOSE in the response.
|
2014-01-13 18:58:45 +01:00
|
|
|
*/
|
|
|
|
|
2014-04-13 22:32:20 +02:00
|
|
|
dev->d_len = 0;
|
2014-07-07 01:22:02 +02:00
|
|
|
flags = (flags & ~TCP_NEWDATA);
|
2014-01-13 18:58:45 +01:00
|
|
|
}
|
|
|
|
|
2014-01-14 00:11:01 +01:00
|
|
|
#endif /* CONFIG_NET_TCP_WRITE_BUFFERS */
|
2014-01-13 18:58:45 +01:00
|
|
|
|
2013-10-06 17:48:54 +02:00
|
|
|
else
|
|
|
|
{
|
2014-07-07 01:22:02 +02:00
|
|
|
/* Drop data received in this state and make sure that TCP_CLOSE
|
2013-10-06 17:48:54 +02:00
|
|
|
* is set in the response
|
|
|
|
*/
|
|
|
|
|
|
|
|
dev->d_len = 0;
|
2014-07-07 01:22:02 +02:00
|
|
|
flags = (flags & ~TCP_NEWDATA) | TCP_CLOSE;
|
2007-11-24 14:02:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
2014-01-13 18:58:45 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
|
|
|
end_wait:
|
|
|
|
pstate->cl_cb->flags = 0;
|
|
|
|
pstate->cl_cb->priv = NULL;
|
|
|
|
pstate->cl_cb->event = NULL;
|
|
|
|
sem_post(&pstate->cl_sem);
|
|
|
|
|
|
|
|
nllvdbg("Resuming\n");
|
|
|
|
return 0;
|
|
|
|
#endif
|
2007-11-24 14:02:03 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_TCP */
|
|
|
|
|
2015-01-17 15:33:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Function: netclose_txnotify
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Notify the appropriate device driver that we are have data ready to
|
|
|
|
* be send (TCP)
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* psock - Socket state structure
|
|
|
|
* conn - The TCP connection structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-18 00:00:19 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-17 15:33:14 +01:00
|
|
|
static inline void netclose_txnotify(FAR struct socket *psock,
|
|
|
|
FAR struct tcp_conn_s *conn)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
/* If both IPv4 and IPv6 support are enabled, then we will need to select
|
|
|
|
* the device driver using the appropriate IP domain.
|
|
|
|
*/
|
|
|
|
|
2015-01-17 16:27:05 +01:00
|
|
|
if (psock->s_domain == PF_INET)
|
2015-01-17 15:33:14 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Notify the device driver that send data is available */
|
|
|
|
|
2015-02-10 01:15:34 +01:00
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
2015-01-17 15:33:14 +01:00
|
|
|
netdev_ipv4_txnotify(conn->u.ipv4.laddr, conn->u.ipv4.raddr);
|
|
|
|
#else
|
|
|
|
netdev_ipv4_txnotify(conn->u.ipv4.raddr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2015-01-17 16:27:05 +01:00
|
|
|
else /* if (psock->s_domain == PF_INET6) */
|
2015-01-17 15:33:14 +01:00
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
{
|
|
|
|
/* Notify the device driver that send data is available */
|
|
|
|
|
2015-01-17 16:27:05 +01:00
|
|
|
DEBUGASSERT(psock->s_domain == PF_INET6);
|
2015-02-10 01:15:34 +01:00
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
2015-01-17 15:33:14 +01:00
|
|
|
netdev_ipv6_txnotify(conn->u.ipv6.laddr, conn->u.ipv6.raddr);
|
|
|
|
#else
|
|
|
|
netdev_ipv6_txnotify(conn->u.ipv6.raddr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
}
|
2015-01-18 00:00:19 +01:00
|
|
|
#endif /* CONFIG_NET_TCP */
|
2015-01-17 15:33:14 +01:00
|
|
|
|
2007-11-24 14:02:03 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Function: netclose_disconnect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Break any current TCP connection
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* conn - uIP TCP connection structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called from normal user-level logic
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP
|
2014-01-13 18:58:45 +01:00
|
|
|
static inline int netclose_disconnect(FAR struct socket *psock)
|
2007-11-24 14:02:03 +01:00
|
|
|
{
|
2014-01-13 18:58:45 +01:00
|
|
|
struct tcp_close_s state;
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *conn;
|
2014-06-26 22:23:21 +02:00
|
|
|
net_lock_t flags;
|
2014-01-13 18:58:45 +01:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
|
|
|
bool linger;
|
|
|
|
#endif
|
|
|
|
int ret = OK;
|
2007-11-24 14:02:03 +01:00
|
|
|
|
|
|
|
/* Interrupts are disabled here to avoid race conditions */
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
flags = net_lock();
|
2014-06-25 02:12:49 +02:00
|
|
|
conn = (FAR struct tcp_conn_s *)psock->s_conn;
|
2007-11-24 14:02:03 +01:00
|
|
|
|
2014-01-15 01:19:43 +01:00
|
|
|
/* If we have a semi-permanent write buffer callback in place, then
|
|
|
|
* release it now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
if (psock->s_sndcb)
|
|
|
|
{
|
|
|
|
psock->s_sndcb = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-22 22:58:02 +01:00
|
|
|
DEBUGASSERT(conn != NULL);
|
2007-11-24 14:02:03 +01:00
|
|
|
|
2013-10-08 17:14:27 +02:00
|
|
|
/* Check for the case where the host beat us and disconnected first */
|
2007-11-24 14:02:03 +01:00
|
|
|
|
2014-07-07 00:10:26 +02:00
|
|
|
if (conn->tcpstateflags == TCP_ESTABLISHED &&
|
2014-06-29 02:36:09 +02:00
|
|
|
(state.cl_cb = tcp_callback_alloc(conn)) != NULL)
|
2013-10-08 17:14:27 +02:00
|
|
|
{
|
|
|
|
/* Set up to receive TCP data event callbacks */
|
2007-11-28 16:25:09 +01:00
|
|
|
|
2015-05-30 17:12:27 +02:00
|
|
|
state.cl_cb->flags = (TCP_NEWDATA | TCP_POLL | TCP_DISCONN_EVENTS);
|
2014-01-13 18:58:45 +01:00
|
|
|
state.cl_cb->event = netclose_interrupt;
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
|
|
|
/* Check for a lingering close */
|
|
|
|
|
|
|
|
linger = _SO_GETOPT(psock->s_options, SO_LINGER);
|
|
|
|
|
|
|
|
/* Has a lingering close been requested */
|
|
|
|
|
|
|
|
if (linger)
|
|
|
|
{
|
|
|
|
/* A non-NULL value of the priv field means that lingering is
|
|
|
|
* enabled.
|
|
|
|
*/
|
|
|
|
|
2014-01-13 19:04:01 +01:00
|
|
|
state.cl_cb->priv = (FAR void *)&state;
|
2014-01-13 18:58:45 +01:00
|
|
|
|
|
|
|
/* Set up for the lingering wait */
|
|
|
|
|
|
|
|
state.cl_psock = psock;
|
|
|
|
state.cl_result = -EBUSY;
|
|
|
|
sem_init(&state.cl_sem, 0, 0);
|
|
|
|
|
|
|
|
/* Record the time that we started the wait (in ticks) */
|
|
|
|
|
|
|
|
state.cl_start = clock_systimer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* CONFIG_NET_SOLINGER */
|
|
|
|
|
|
|
|
{
|
|
|
|
/* We will close immediately. The NULL priv field signals this */
|
|
|
|
|
|
|
|
state.cl_cb->priv = NULL;
|
2007-11-28 16:25:09 +01:00
|
|
|
|
2014-01-13 18:58:45 +01:00
|
|
|
/* No further references on the connection */
|
|
|
|
|
|
|
|
conn->crefs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify the device driver of the availability of TX data */
|
2007-11-24 14:02:03 +01:00
|
|
|
|
2015-01-17 15:33:14 +01:00
|
|
|
netclose_txnotify(psock, conn);
|
2014-01-13 18:58:45 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
|
|
|
/* Wait only if we are lingering */
|
|
|
|
|
|
|
|
if (linger)
|
|
|
|
{
|
|
|
|
/* Wait for the disconnect event */
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
(void)net_lockedwait(&state.cl_sem);
|
2014-01-13 18:58:45 +01:00
|
|
|
|
|
|
|
/* We are now disconnected */
|
|
|
|
|
|
|
|
sem_destroy(&state.cl_sem);
|
2014-06-29 02:36:09 +02:00
|
|
|
tcp_callback_free(conn, state.cl_cb);
|
2014-01-13 18:58:45 +01:00
|
|
|
|
|
|
|
/* Free the connection */
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
conn->crefs = 0; /* No more references on the connection */
|
|
|
|
tcp_free(conn); /* Free uIP resources */
|
2014-01-13 18:58:45 +01:00
|
|
|
|
|
|
|
/* Get the result of the close */
|
|
|
|
|
|
|
|
ret = state.cl_result;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_SOLINGER */
|
2013-10-08 17:14:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_free(conn);
|
2007-11-24 14:02:03 +01:00
|
|
|
}
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
net_unlock(flags);
|
2014-01-13 18:58:45 +01:00
|
|
|
return ret;
|
2007-11-24 14:02:03 +01:00
|
|
|
}
|
2014-01-13 18:58:45 +01:00
|
|
|
#endif /* CONFIG_NET_TCP */
|
2007-11-24 14:02:03 +01:00
|
|
|
|
2015-01-24 22:19:50 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Function: local_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Performs the close operation on a local socket instance
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* psock Socket instance
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; -1 on error with errno set appropriately.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_LOCAL
|
|
|
|
static void local_close(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
|
|
|
|
|
|
|
/* Is this the last reference to the connection structure (there could
|
|
|
|
* be more if the socket was dup'ed).
|
|
|
|
*/
|
|
|
|
|
2015-01-25 14:36:16 +01:00
|
|
|
if (conn->lc_crefs <= 1)
|
2015-01-24 22:19:50 +01:00
|
|
|
{
|
2015-01-25 14:36:16 +01:00
|
|
|
conn->lc_crefs = 0;
|
2015-01-25 19:37:40 +01:00
|
|
|
local_release(conn);
|
2015-01-24 22:19:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. Just decrement the reference count */
|
|
|
|
|
2015-01-25 14:36:16 +01:00
|
|
|
conn->lc_crefs--;
|
2015-01-24 22:19:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_LOCAL */
|
|
|
|
|
2007-11-24 14:02:03 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
2007-09-03 22:34:44 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-30 22:29:59 +01:00
|
|
|
* Function: psock_close
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-30 22:29:59 +01:00
|
|
|
* Performs the close operation on a socket instance
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
2009-09-16 22:29:00 +02:00
|
|
|
* psock Socket instance
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; -1 on error with errno set appropriately.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-30 22:29:59 +01:00
|
|
|
int psock_close(FAR struct socket *psock)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* Verify that the sockfd corresponds to valid, allocated socket */
|
|
|
|
|
|
|
|
if (!psock || psock->s_crefs <= 0)
|
|
|
|
{
|
|
|
|
err = EBADF;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2015-05-28 16:23:51 +02:00
|
|
|
/* We perform the uIP close operation only if this is the last count on
|
|
|
|
* the socket. (actually, I think the socket crefs only takes the values
|
|
|
|
* 0 and 1 right now).
|
2016-01-22 22:59:15 +01:00
|
|
|
*
|
|
|
|
* It is possible for a psock to have no connection, e.g. a TCP socket
|
|
|
|
* waiting in accept.
|
2009-06-15 20:58:22 +02:00
|
|
|
*/
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2016-01-22 22:59:15 +01:00
|
|
|
if (psock->s_crefs <= 1 && psock->s_conn != NULL)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
2009-06-15 20:58:22 +02:00
|
|
|
/* Perform uIP side of the close depending on the protocol type */
|
|
|
|
|
|
|
|
switch (psock->s_type)
|
2007-11-22 00:29:14 +01:00
|
|
|
{
|
2015-01-31 14:58:51 +01:00
|
|
|
#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_LOCAL_STREAM)
|
2009-06-15 20:58:22 +02:00
|
|
|
case SOCK_STREAM:
|
|
|
|
{
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2015-01-24 22:19:50 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
|
|
|
if (psock->s_domain == PF_LOCAL)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Release our reference to the local connection structure */
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2015-01-24 22:19:50 +01:00
|
|
|
local_close(psock);
|
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_LOCAL_STREAM */
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2015-01-24 22:19:50 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2015-01-24 22:19:50 +01:00
|
|
|
else
|
|
|
|
#endif
|
2009-06-15 20:58:22 +02:00
|
|
|
{
|
2015-01-24 22:19:50 +01:00
|
|
|
FAR struct tcp_conn_s *conn = psock->s_conn;
|
|
|
|
|
|
|
|
/* Is this the last reference to the connection structure
|
|
|
|
* (there could be more if the socket was dup'ed).
|
|
|
|
*/
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2015-01-24 22:19:50 +01:00
|
|
|
if (conn->crefs <= 1)
|
2014-01-13 18:58:45 +01:00
|
|
|
{
|
2015-01-24 22:19:50 +01:00
|
|
|
/* Yes... then perform the disconnection now */
|
|
|
|
|
|
|
|
tcp_unlisten(conn); /* No longer accepting connections */
|
|
|
|
conn->crefs = 0; /* Discard our reference to the connection */
|
|
|
|
|
|
|
|
/* Break any current connections */
|
|
|
|
|
|
|
|
err = netclose_disconnect(psock);
|
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
/* This would normally occur only if there is a
|
|
|
|
* timeout from a lingering close.
|
|
|
|
*/
|
2014-01-13 18:58:45 +01:00
|
|
|
|
2015-01-24 22:19:50 +01:00
|
|
|
goto errout_with_psock;
|
|
|
|
}
|
2015-05-30 19:49:55 +02:00
|
|
|
|
|
|
|
/* Stop the network monitor */
|
|
|
|
|
|
|
|
net_stopmonitor(conn);
|
2014-01-13 18:58:45 +01:00
|
|
|
}
|
2015-01-24 22:19:50 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. Just decrement the reference count */
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2015-01-24 22:19:50 +01:00
|
|
|
conn->crefs--;
|
|
|
|
}
|
2009-06-15 20:58:22 +02:00
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_TCP || CONFIG_NET_LOCAL_STREAM */
|
2009-06-15 20:58:22 +02:00
|
|
|
}
|
|
|
|
break;
|
2007-11-22 19:36:46 +01:00
|
|
|
#endif
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#if defined(CONFIG_NET_UDP) || defined(CONFIG_NET_LOCAL_DGRAM)
|
2009-06-15 20:58:22 +02:00
|
|
|
case SOCK_DGRAM:
|
|
|
|
{
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2015-01-24 22:19:50 +01:00
|
|
|
#ifdef CONFIG_NET_UDP
|
|
|
|
if (psock->s_domain == PF_LOCAL)
|
|
|
|
#endif
|
2009-06-15 20:58:22 +02:00
|
|
|
{
|
2015-01-24 22:19:50 +01:00
|
|
|
/* Release our reference to the local connection structure */
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2015-01-24 22:19:50 +01:00
|
|
|
local_close(psock);
|
2009-06-15 20:58:22 +02:00
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
2015-01-24 22:19:50 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_UDP
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2009-06-15 20:58:22 +02:00
|
|
|
else
|
2015-01-24 22:19:50 +01:00
|
|
|
#endif
|
2009-06-15 20:58:22 +02:00
|
|
|
{
|
2015-01-24 22:19:50 +01:00
|
|
|
FAR struct udp_conn_s *conn = psock->s_conn;
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2015-01-24 22:19:50 +01:00
|
|
|
/* Is this the last reference to the connection structure
|
|
|
|
* (there could be more if the socket was dup'ed).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (conn->crefs <= 1)
|
|
|
|
{
|
|
|
|
/* Yes... free the connection structure */
|
|
|
|
|
|
|
|
conn->crefs = 0;
|
|
|
|
udp_free(psock->s_conn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. Just decrement the reference count */
|
|
|
|
|
|
|
|
conn->crefs--;
|
|
|
|
}
|
2009-06-15 20:58:22 +02:00
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_UDP || CONFIG_NET_LOCAL_DGRAM */
|
2009-06-15 20:58:22 +02:00
|
|
|
}
|
|
|
|
break;
|
2007-09-03 22:34:44 +02:00
|
|
|
#endif
|
2007-11-22 19:36:46 +01:00
|
|
|
|
2015-01-25 14:36:16 +01:00
|
|
|
#ifdef CONFIG_NET_PKT
|
|
|
|
case SOCK_RAW:
|
|
|
|
{
|
|
|
|
FAR struct pkt_conn_s *conn = psock->s_conn;
|
|
|
|
|
|
|
|
/* Is this the last reference to the connection structure (there
|
|
|
|
* could be more if the socket was dup'ed).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (conn->crefs <= 1)
|
|
|
|
{
|
|
|
|
/* Yes... free the connection structure */
|
|
|
|
|
|
|
|
conn->crefs = 0; /* No more references on the connection */
|
|
|
|
pkt_free(psock->s_conn); /* Free uIP resources */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. Just decrement the reference count */
|
|
|
|
|
|
|
|
conn->crefs--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2009-06-15 20:58:22 +02:00
|
|
|
default:
|
|
|
|
err = EBADF;
|
|
|
|
goto errout;
|
|
|
|
}
|
2007-09-03 22:34:44 +02:00
|
|
|
}
|
|
|
|
|
2009-06-15 20:58:22 +02:00
|
|
|
/* Then release our reference on the socket structure containing the connection */
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2009-09-16 22:29:00 +02:00
|
|
|
sock_release(psock);
|
2007-09-03 22:34:44 +02:00
|
|
|
return OK;
|
|
|
|
|
2014-01-13 18:58:45 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
|
|
|
errout_with_psock:
|
|
|
|
sock_release(psock);
|
|
|
|
#endif
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
errout:
|
2014-01-13 18:58:45 +01:00
|
|
|
set_errno(err);
|
2007-09-03 22:34:44 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2009-09-16 22:29:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Function: net_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Performs the close operation on socket descriptors
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* sockfd Socket descriptor of socket
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; -1 on error with errno set appropriately.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int net_close(int sockfd)
|
|
|
|
{
|
2012-01-30 22:29:59 +01:00
|
|
|
return psock_close(sockfd_socket(sockfd));
|
2009-09-16 22:29:00 +02:00
|
|
|
}
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
#endif /* CONFIG_NET */
|