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
|
|
|
*
|
2017-09-30 18:41:21 +02:00
|
|
|
* Copyright (C) 2007-2017 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>
|
|
|
|
|
|
|
|
#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
|
|
|
|
2014-07-05 00:38:51 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2014-07-05 00:38:51 +02:00
|
|
|
#include "socket/socket.h"
|
2014-01-13 18:58:45 +01:00
|
|
|
|
2017-07-13 20:15:15 +02:00
|
|
|
#ifdef CONFIG_NET
|
2015-01-24 22:19:50 +01:00
|
|
|
|
2007-11-24 14:02:03 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
2007-09-03 22:34:44 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: 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:
|
2017-09-30 18:41:21 +02:00
|
|
|
* Returns zero (OK) on success. On failure, it returns a negated errno
|
|
|
|
* value to indicate the nature of the error.
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-30 22:29:59 +01:00
|
|
|
int psock_close(FAR struct socket *psock)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
2017-07-13 20:15:15 +02:00
|
|
|
int ret;
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* Verify that the sockfd corresponds to valid, allocated socket */
|
|
|
|
|
|
|
|
if (!psock || psock->s_crefs <= 0)
|
|
|
|
{
|
2017-09-30 18:41:21 +02:00
|
|
|
return -EBADF;
|
2007-09-03 22:34:44 +02:00
|
|
|
}
|
|
|
|
|
2016-05-30 17:31:44 +02:00
|
|
|
/* We perform the close operation only if this is the last count on
|
2015-05-28 16:23:51 +02:00
|
|
|
* 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
|
|
|
{
|
2017-07-13 20:15:15 +02:00
|
|
|
/* Let the address family's close() method handle the operation */
|
2015-01-24 22:19:50 +01:00
|
|
|
|
2017-07-13 20:15:15 +02:00
|
|
|
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_close != NULL);
|
|
|
|
ret = psock->s_sockif->si_close(psock);
|
2015-01-24 22:19:50 +01:00
|
|
|
|
2017-07-13 20:15:15 +02:00
|
|
|
/* Was the close successful */
|
2015-01-24 22:19:50 +01:00
|
|
|
|
2017-07-13 20:15:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-09-30 18:41:21 +02:00
|
|
|
return ret;
|
2009-06-15 20:58:22 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2009-09-16 22:29:00 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_close
|
2009-09-16 22:29:00 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Performs the close operation on socket descriptors
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* sockfd Socket descriptor of socket
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-09-30 18:41:21 +02:00
|
|
|
* Returns zero (OK) on success. On failure, it returns a negated errno
|
|
|
|
* value to indicate the nature of the error.
|
2009-09-16 22:29:00 +02:00
|
|
|
*
|
|
|
|
* 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 */
|