2009-06-16 02:58:22 +08:00
|
|
|
/****************************************************************************
|
2020-01-31 16:44:28 +08:00
|
|
|
* net/socket/net_dup2.c
|
2009-06-16 02:58:22 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08: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-16 02:58:22 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-16 02:58:22 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08: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-16 02:58:22 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
2017-10-20 01:09:23 +08:00
|
|
|
#include <string.h>
|
2020-01-31 16:44:28 +08:00
|
|
|
#include <sched.h>
|
2021-05-14 10:03:23 +08:00
|
|
|
#include <assert.h>
|
2009-06-16 02:58:22 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2014-07-05 06:38:51 +08:00
|
|
|
#include <nuttx/net/net.h>
|
2021-03-18 23:30:40 +08:00
|
|
|
#include <nuttx/net/tcp.h>
|
2009-06-16 02:58:22 +08:00
|
|
|
|
2017-10-20 01:09:23 +08:00
|
|
|
#include "inet/inet.h"
|
2017-08-30 00:53:04 +08:00
|
|
|
#include "tcp/tcp.h"
|
2014-06-29 07:25:18 +08:00
|
|
|
#include "socket/socket.h"
|
2009-06-16 02:58:22 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-01-20 06:02:56 +08:00
|
|
|
* Public Functions
|
2009-06-16 02:58:22 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-01-31 16:44:28 +08:00
|
|
|
* Name: psock_dup2
|
2009-06-16 02:58:22 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2021-02-23 18:04:13 +08:00
|
|
|
* Performs the low level, common portion of dup
|
2009-06-16 02:58:22 +08:00
|
|
|
*
|
2017-08-30 02:27:58 +08:00
|
|
|
* Input Parameters:
|
|
|
|
* psock1 - The existing socket that is being cloned.
|
2020-02-23 16:50:23 +08:00
|
|
|
* psock2 - A reference to an uninitialized socket structure allocated by
|
2017-08-30 02:27:58 +08:00
|
|
|
* the caller.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
2009-06-16 02:58:22 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-31 16:44:28 +08:00
|
|
|
int psock_dup2(FAR struct socket *psock1, FAR struct socket *psock2)
|
2009-06-16 02:58:22 +08:00
|
|
|
{
|
|
|
|
/* Parts of this operation need to be atomic */
|
|
|
|
|
2016-12-04 06:28:19 +08:00
|
|
|
net_lock();
|
2009-06-16 02:58:22 +08:00
|
|
|
|
2017-10-20 01:09:23 +08:00
|
|
|
/* Duplicate the relevant socket state (zeroing everything else) */
|
|
|
|
|
|
|
|
memset(psock2, 0, sizeof(struct socket));
|
2009-06-16 02:58:22 +08:00
|
|
|
|
2015-01-22 21:50:31 +08:00
|
|
|
psock2->s_domain = psock1->s_domain; /* IP domain: PF_INET, PF_INET6, or PF_PACKET */
|
2009-06-16 02:58:22 +08:00
|
|
|
psock2->s_type = psock1->s_type; /* Protocol type: Only SOCK_STREAM or SOCK_DGRAM */
|
2017-07-13 05:07:32 +08:00
|
|
|
psock2->s_sockif = psock1->s_sockif; /* Socket interface */
|
2009-06-16 02:58:22 +08:00
|
|
|
psock2->s_conn = psock1->s_conn; /* UDP or TCP connection structure */
|
|
|
|
|
2017-07-14 03:36:18 +08:00
|
|
|
/* Increment the reference count on the underlying connection structure
|
|
|
|
* for this address family type.
|
|
|
|
*/
|
|
|
|
|
2020-05-04 03:07:01 +08:00
|
|
|
DEBUGASSERT(psock2->s_sockif != NULL &&
|
|
|
|
psock2->s_sockif->si_addref != NULL);
|
2017-07-14 03:36:18 +08:00
|
|
|
psock2->s_sockif->si_addref(psock2);
|
2009-06-16 02:58:22 +08:00
|
|
|
|
2016-12-04 06:28:19 +08:00
|
|
|
net_unlock();
|
2021-10-20 18:19:27 +08:00
|
|
|
|
|
|
|
return OK;
|
2009-06-16 02:58:22 +08:00
|
|
|
}
|