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>
|
2021-05-14 04:03:23 +02:00
|
|
|
#include <assert.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>
|
2021-03-18 16:30:40 +01:00
|
|
|
#include <nuttx/net/tcp.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:
|
2021-02-23 11:04:13 +01:00
|
|
|
* Performs the low level, common portion of dup
|
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
|
|
|
{
|
|
|
|
/* 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_conn = psock1->s_conn; /* UDP or TCP connection structure */
|
|
|
|
|
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
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2021-10-20 12:19:27 +02:00
|
|
|
|
|
|
|
return OK;
|
2009-06-15 20:58:22 +02:00
|
|
|
}
|