2009-06-15 20:58:22 +02:00
|
|
|
/****************************************************************************
|
2020-01-31 09:44:28 +01:00
|
|
|
* net/socket/net_dup2.c
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01: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
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01: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.
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
2017-10-19 19:09:23 +02:00
|
|
|
#include <string.h>
|
2020-01-31 09:44:28 +01:00
|
|
|
#include <sched.h>
|
2009-06-15 20:58:22 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2014-07-05 00:38:51 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2017-10-19 19:09:23 +02:00
|
|
|
#include "inet/inet.h"
|
2017-08-29 18:53:04 +02:00
|
|
|
#include "tcp/tcp.h"
|
2014-06-29 01:25:18 +02:00
|
|
|
#include "socket/socket.h"
|
2009-06-15 20:58:22 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-01-19 23:02:56 +01:00
|
|
|
* Public Functions
|
2009-06-15 20:58:22 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-01-31 09:44:28 +01:00
|
|
|
* Name: psock_dup2
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-01-31 09:44:28 +01:00
|
|
|
* Performs the low level, common portion of net_dup() and net_dup2()
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
2017-08-29 20:27:58 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* psock1 - The existing socket that is being cloned.
|
2020-02-23 09:50:23 +01:00
|
|
|
* psock2 - A reference to an uninitialized socket structure allocated by
|
2017-08-29 20:27:58 +02:00
|
|
|
* the caller.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
2009-06-15 20:58:22 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-31 09:44:28 +01:00
|
|
|
int psock_dup2(FAR struct socket *psock1, FAR struct socket *psock2)
|
2009-06-15 20:58:22 +02:00
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Parts of this operation need to be atomic */
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2017-10-19 19:09:23 +02:00
|
|
|
/* Duplicate the relevant socket state (zeroing everything else) */
|
|
|
|
|
|
|
|
memset(psock2, 0, sizeof(struct socket));
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2015-01-22 14:50:31 +01:00
|
|
|
psock2->s_domain = psock1->s_domain; /* IP domain: PF_INET, PF_INET6, or PF_PACKET */
|
2009-06-15 20:58:22 +02:00
|
|
|
psock2->s_type = psock1->s_type; /* Protocol type: Only SOCK_STREAM or SOCK_DGRAM */
|
2017-07-12 23:07:32 +02:00
|
|
|
psock2->s_sockif = psock1->s_sockif; /* Socket interface */
|
2009-06-15 20:58:22 +02:00
|
|
|
psock2->s_flags = psock1->s_flags; /* See _SF_* definitions */
|
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
|
|
|
psock2->s_options = psock1->s_options; /* Selected socket options */
|
|
|
|
psock2->s_rcvtimeo = psock1->s_rcvtimeo; /* Receive timeout value (in deciseconds) */
|
|
|
|
psock2->s_sndtimeo = psock1->s_sndtimeo; /* Send timeout value (in deciseconds) */
|
2014-01-14 22:17:53 +01:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
|
|
|
psock2->s_linger = psock1->s_linger; /* Linger timeout value (in deciseconds) */
|
|
|
|
#endif
|
2009-06-15 20:58:22 +02:00
|
|
|
#endif
|
|
|
|
psock2->s_conn = psock1->s_conn; /* UDP or TCP connection structure */
|
|
|
|
|
2017-07-13 21:36:18 +02:00
|
|
|
/* Increment the reference count on the socket */
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2009-09-15 19:17:51 +02:00
|
|
|
psock2->s_crefs = 1; /* One reference on the new socket itself */
|
2009-06-15 21:50:06 +02:00
|
|
|
|
2017-07-13 21:36:18 +02:00
|
|
|
/* Increment the reference count on the underlying connection structure
|
|
|
|
* for this address family type.
|
|
|
|
*/
|
|
|
|
|
2020-05-03 21:07:01 +02:00
|
|
|
DEBUGASSERT(psock2->s_sockif != NULL &&
|
|
|
|
psock2->s_sockif->si_addref != NULL);
|
2017-07-13 21:36:18 +02:00
|
|
|
psock2->s_sockif->si_addref(psock2);
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2017-08-29 18:53:04 +02:00
|
|
|
#ifdef NET_TCP_HAVE_STACK
|
2017-10-19 19:09:23 +02:00
|
|
|
/* For connected socket types, it is necessary to also start the network
|
|
|
|
* monitor so that the newly cloned socket can receive a notification if
|
|
|
|
* the network connection is lost.
|
2017-08-29 18:53:04 +02:00
|
|
|
*/
|
|
|
|
|
2017-08-30 18:29:14 +02:00
|
|
|
if (psock2->s_type == SOCK_STREAM)
|
|
|
|
{
|
|
|
|
ret = tcp_start_monitor(psock2);
|
2017-10-19 19:09:23 +02:00
|
|
|
|
|
|
|
/* On failure, back out the reference count on the TCP connection
|
|
|
|
* structure. tcp_start_monitor() will fail only in the race condition
|
|
|
|
* where the TCP connection has been lost.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* There should be at least two reference counts on the connection
|
|
|
|
* structure: At least one from the original socket and the one
|
|
|
|
* from above where we incremented the reference count.
|
|
|
|
* inet_close() will handle all cases.
|
|
|
|
*
|
|
|
|
* NOTE: As a side-effect, inet_close()will also call
|
|
|
|
* tcp_stop_monitor() which could inform the loss of connection to
|
|
|
|
* all open sockets on the connection structure if the reference
|
|
|
|
* count decrements to zero.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
inet_close(psock2);
|
2018-11-27 14:40:55 +01:00
|
|
|
|
|
|
|
/* Then release our reference on the socket structure containing
|
|
|
|
* the connection.
|
|
|
|
*/
|
|
|
|
|
2018-11-27 14:50:09 +01:00
|
|
|
psock_release(psock2);
|
2017-10-19 19:09:23 +02:00
|
|
|
}
|
2017-08-30 18:29:14 +02:00
|
|
|
}
|
2017-08-29 18:53:04 +02:00
|
|
|
#endif
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2009-06-15 20:58:22 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-31 09:44:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_dup2
|
|
|
|
*
|
|
|
|
* Description:
|
2020-05-03 21:07:01 +02:00
|
|
|
* Clone a socket descriptor to an arbitrary descriptor number.
|
2020-01-31 09:44:28 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-05-03 21:07:01 +02:00
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
2020-01-31 09:44:28 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int net_dup2(int sockfd1, int sockfd2)
|
|
|
|
{
|
|
|
|
FAR struct socket *psock1;
|
|
|
|
FAR struct socket *psock2;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Lock the scheduler throughout the following */
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
|
|
|
/* Get the socket structures underly both descriptors */
|
|
|
|
|
|
|
|
psock1 = sockfd_socket(sockfd1);
|
|
|
|
psock2 = sockfd_socket(sockfd2);
|
|
|
|
|
|
|
|
/* Verify that the sockfd1 and sockfd2 both refer to valid socket
|
|
|
|
* descriptors and that sockfd2 corresponds to an allocated socket
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (psock1 == NULL || psock2 == NULL || psock1->s_crefs <= 0)
|
|
|
|
{
|
|
|
|
ret = -EBADF;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If sockfd2 also valid, allocated socket, then we will have to
|
|
|
|
* close it!
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (psock2->s_crefs > 0)
|
|
|
|
{
|
|
|
|
net_close(sockfd2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Duplicate the socket state */
|
|
|
|
|
|
|
|
ret = psock_dup2(psock1, psock2);
|
|
|
|
|
|
|
|
errout:
|
|
|
|
sched_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|