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
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-04-28 18:17:03 +02:00
|
|
|
* 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
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
2020-04-28 18:17:03 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
2020-04-28 18:17:03 +02:00
|
|
|
* 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.
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
2021-02-23 11:04:13 +01:00
|
|
|
#include <string.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
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input 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 */
|
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
if (psock == NULL)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
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
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
if (psock->s_conn != NULL)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
2022-02-08 06:18:01 +01:00
|
|
|
FAR struct socket_conn_s *conn = psock->s_conn;
|
|
|
|
|
2020-04-28 18:17:03 +02:00
|
|
|
/* Assume that the socket close operation will be successful. Save
|
|
|
|
* the current flags and mark the socket uninitialized. This avoids
|
|
|
|
* race conditions in the SMP case. We save the flags as a type
|
|
|
|
* unsigned int in case the size of s_flags changes in the future
|
|
|
|
* (currently uint8_t).
|
|
|
|
*/
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
unsigned int saveflags = conn->s_flags;
|
2020-04-28 18:17:03 +02:00
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
conn->s_flags &= ~_SF_INITD;
|
2020-04-28 18:17:03 +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
|
|
|
|
2020-04-28 18:17:03 +02:00
|
|
|
DEBUGASSERT(psock->s_sockif != NULL &&
|
|
|
|
psock->s_sockif->si_close != NULL);
|
|
|
|
|
2017-07-13 20:15:15 +02:00
|
|
|
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)
|
|
|
|
{
|
2020-04-28 18:17:03 +02:00
|
|
|
/* No.. restore the socket flags */
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
conn->s_flags = saveflags;
|
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
|
|
|
}
|
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
/* The socket will not persist... reset it */
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
memset(psock, 0, sizeof(*psock));
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
return OK;
|
2009-09-16 22:29:00 +02:00
|
|
|
}
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
#endif /* CONFIG_NET */
|