2007-09-03 20:34:44 +00:00
|
|
|
/****************************************************************************
|
2014-06-28 17:25:18 -06:00
|
|
|
* net/socket/net_close.c
|
2007-09-03 20:34:44 +00:00
|
|
|
*
|
2017-09-30 10:41:21 -06:00
|
|
|
* Copyright (C) 2007-2017 Gregory Nutt. All rights reserved.
|
2012-01-30 21:29:59 +00:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-09-03 20:34:44 +00: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 15:57:25 +00:00
|
|
|
#include <stdint.h>
|
2014-01-13 11:58:45 -06:00
|
|
|
#include <stdbool.h>
|
2007-09-03 20:34:44 +00:00
|
|
|
#include <errno.h>
|
2007-11-24 13:02:03 +00:00
|
|
|
#include <debug.h>
|
2014-07-21 18:46:47 -06:00
|
|
|
#include <assert.h>
|
2007-11-24 13:02:03 +00:00
|
|
|
|
2014-07-04 16:38:51 -06:00
|
|
|
#include <nuttx/net/net.h>
|
2007-09-03 20:34:44 +00:00
|
|
|
|
2014-07-04 16:38:51 -06:00
|
|
|
#include "socket/socket.h"
|
2014-01-13 11:58:45 -06:00
|
|
|
|
2017-07-13 12:15:15 -06:00
|
|
|
#ifdef CONFIG_NET
|
2015-01-24 15:19:50 -06:00
|
|
|
|
2007-11-24 13:02:03 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
2007-09-03 20:34:44 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-21 16:33:14 -06:00
|
|
|
* Name: psock_close
|
2007-09-03 20:34:44 +00:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-30 21:29:59 +00:00
|
|
|
* Performs the close operation on a socket instance
|
2007-09-03 20:34:44 +00:00
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2009-09-16 20:29:00 +00:00
|
|
|
* psock Socket instance
|
2007-09-03 20:34:44 +00:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-09-30 10:41:21 -06:00
|
|
|
* Returns zero (OK) on success. On failure, it returns a negated errno
|
|
|
|
* value to indicate the nature of the error.
|
2007-09-03 20:34:44 +00:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-30 21:29:59 +00:00
|
|
|
int psock_close(FAR struct socket *psock)
|
2007-09-03 20:34:44 +00:00
|
|
|
{
|
2017-07-13 12:15:15 -06:00
|
|
|
int ret;
|
2007-09-03 20:34:44 +00:00
|
|
|
|
|
|
|
/* Verify that the sockfd corresponds to valid, allocated socket */
|
|
|
|
|
|
|
|
if (!psock || psock->s_crefs <= 0)
|
|
|
|
{
|
2017-09-30 10:41:21 -06:00
|
|
|
return -EBADF;
|
2007-09-03 20:34:44 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 09:31:44 -06:00
|
|
|
/* We perform the close operation only if this is the last count on
|
2015-05-28 08:23:51 -06:00
|
|
|
* the socket. (actually, I think the socket crefs only takes the values
|
|
|
|
* 0 and 1 right now).
|
2016-01-22 15:59:15 -06:00
|
|
|
*
|
|
|
|
* It is possible for a psock to have no connection, e.g. a TCP socket
|
|
|
|
* waiting in accept.
|
2009-06-15 18:58:22 +00:00
|
|
|
*/
|
2007-09-03 20:34:44 +00:00
|
|
|
|
2016-01-22 15:59:15 -06:00
|
|
|
if (psock->s_crefs <= 1 && psock->s_conn != NULL)
|
2007-09-03 20:34:44 +00:00
|
|
|
{
|
2017-07-13 12:15:15 -06:00
|
|
|
/* Let the address family's close() method handle the operation */
|
2015-01-24 15:19:50 -06:00
|
|
|
|
2017-07-13 12:15:15 -06:00
|
|
|
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_close != NULL);
|
|
|
|
ret = psock->s_sockif->si_close(psock);
|
2015-01-24 15:19:50 -06:00
|
|
|
|
2017-07-13 12:15:15 -06:00
|
|
|
/* Was the close successful */
|
2015-01-24 15:19:50 -06:00
|
|
|
|
2017-07-13 12:15:15 -06:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-09-30 10:41:21 -06:00
|
|
|
return ret;
|
2009-06-15 18:58:22 +00:00
|
|
|
}
|
2007-09-03 20:34:44 +00:00
|
|
|
}
|
|
|
|
|
2009-06-15 18:58:22 +00:00
|
|
|
/* Then release our reference on the socket structure containing the connection */
|
2007-09-03 20:34:44 +00:00
|
|
|
|
2018-11-27 07:50:09 -06:00
|
|
|
psock_release(psock);
|
2007-09-03 20:34:44 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2009-09-16 20:29:00 +00:00
|
|
|
/****************************************************************************
|
2017-04-21 16:33:14 -06:00
|
|
|
* Name: net_close
|
2009-09-16 20:29:00 +00:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Performs the close operation on socket descriptors
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2009-09-16 20:29:00 +00:00
|
|
|
* sockfd Socket descriptor of socket
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-09-30 10:41:21 -06:00
|
|
|
* Returns zero (OK) on success. On failure, it returns a negated errno
|
|
|
|
* value to indicate the nature of the error.
|
2009-09-16 20:29:00 +00:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int net_close(int sockfd)
|
|
|
|
{
|
2012-01-30 21:29:59 +00:00
|
|
|
return psock_close(sockfd_socket(sockfd));
|
2009-09-16 20:29:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-03 20:34:44 +00:00
|
|
|
#endif /* CONFIG_NET */
|