net/socketpair: move socketpair implement into socket internal

Change-Id: Ibf84671a1d59122ee5a3c5161048ea4f00356b1c
Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an
2021-06-08 22:15:42 +08:00
parent cf9baa0178
commit 55c032d061
6 changed files with 344 additions and 189 deletions
+139 -1
View File
@@ -32,6 +32,7 @@
#include <debug.h>
#include <nuttx/net/net.h>
#include <nuttx/net/tcp.h>
#include <nuttx/kmalloc.h>
#include "tcp/tcp.h"
@@ -44,6 +45,17 @@
#ifdef HAVE_INET_SOCKETS
/****************************************************************************
* Private Type Definitions
****************************************************************************/
union sockaddr_u
{
struct sockaddr addr;
struct sockaddr_in inaddr;
struct sockaddr_in6 in6addr;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
@@ -78,6 +90,7 @@ static ssize_t inet_recvmsg(FAR struct socket *psock,
FAR struct msghdr *msg, int flags);
static int inet_ioctl(FAR struct socket *psock, int cmd,
FAR void *arg, size_t arglen);
static int inet_socketpair(FAR struct socket *psocks[2]);
#ifdef CONFIG_NET_SENDFILE
static ssize_t inet_sendfile(FAR struct socket *psock,
FAR struct file *infile, FAR off_t *offset,
@@ -103,7 +116,8 @@ static const struct sock_intf_s g_inet_sockif =
inet_sendmsg, /* si_sendmsg */
inet_recvmsg, /* si_recvmsg */
inet_close, /* si_close */
inet_ioctl /* si_ioctl */
inet_ioctl, /* si_ioctl */
inet_socketpair /* si_socketpair */
#ifdef CONFIG_NET_SENDFILE
,
inet_sendfile /* si_sendfile */
@@ -1328,6 +1342,130 @@ static int inet_ioctl(FAR struct socket *psock, int cmd,
return -EINVAL;
}
/****************************************************************************
* Name: inet_socketpair
*
* Description:
* Create a pair of connected sockets between psocks[2]
*
* Parameters:
* psocks A reference to the socket structure of the socket pair
*
****************************************************************************/
static int inet_socketpair(FAR struct socket *psocks[2])
{
FAR struct socket *pserver;
FAR struct socket server;
union sockaddr_u addr[2];
socklen_t len;
int ret;
/* Set the sock address to localhost */
#ifdef CONFIG_NET_IPv6
if (psocks[0]->s_domain == AF_INET6)
{
struct in6_addr init_sin6_addr = IN6ADDR_LOOPBACK_INIT;
len = sizeof(addr[0].in6addr);
memset(&addr[0], 0, len);
addr[0].in6addr.sin6_family = psocks[0]->s_domain;
addr[0].in6addr.sin6_addr = init_sin6_addr;
}
else
#endif
{
len = sizeof(addr[0].inaddr);
memset(&addr[0], 0, len);
addr[0].inaddr.sin_family = psocks[0]->s_domain;
addr[0].inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
}
memcpy(&addr[1], &addr[0], len);
ret = psock_bind(psocks[0], &addr[0].addr, len);
if (ret < 0)
{
return ret;
}
psock_getsockname(psocks[0], &addr[0].addr, &len);
/* For SOCK_STREAM, Use proxy service handle to make temporary
* pserver process, psocks[1] will be replaced with a new accept handle
*/
if (psocks[0]->s_type == SOCK_STREAM)
{
ret = psock_socket(psocks[1]->s_domain, psocks[1]->s_type,
psocks[1]->s_proto, &server);
if (ret < 0)
{
return ret;
}
pserver = &server;
}
else
{
pserver = psocks[1];
}
ret = psock_bind(pserver, &addr[1].addr, len);
if (ret < 0)
{
goto errout;
}
psock_getsockname(pserver, &addr[1].addr, &len);
if (psocks[0]->s_type == SOCK_DGRAM)
{
ret = psock_connect(psocks[0], &addr[1].addr, len);
if (ret < 0)
{
goto errout;
}
ret = psock_connect(pserver, &addr[0].addr, len);
if (ret < 0)
{
goto errout;
}
}
else
{
ret = psock_listen(pserver, 2);
if (ret < 0)
{
goto errout;
}
ret = psock_connect(psocks[0], &addr[1].addr, len);
if (ret < 0)
{
goto errout;
}
/* Release the resource of psocks[1], accept will replace
* this handle
*/
psock_close(psocks[1]);
ret = psock_accept(pserver, &addr[1].addr, &len, psocks[1]);
}
errout:
if (pserver->s_type == SOCK_STREAM)
{
psock_close(pserver);
}
return ret;
}
/****************************************************************************
* Name: inet_sendfile
*
+1 -1
View File
@@ -22,7 +22,7 @@
SOCK_CSRCS += bind.c connect.c getsockname.c getpeername.c
SOCK_CSRCS += recv.c recvfrom.c send.c sendto.c
SOCK_CSRCS += socket.c net_close.c
SOCK_CSRCS += socket.c socketpair.c net_close.c
SOCK_CSRCS += recvmsg.c sendmsg.c
SOCK_CSRCS += net_dup2.c net_sockif.c net_poll.c net_vfcntl.c
SOCK_CSRCS += net_fstat.c
+181
View File
@@ -0,0 +1,181 @@
/****************************************************************************
* net/socket/socketpair.c
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <nuttx/kmalloc.h>
#include <nuttx/net/net.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: socketpair
*
* Description:
* Create an unbound pair of connected sockets in a specified domain, of a
* specified type, under the protocol optionally specified by the protocol
* argument. The two sockets shall be identical. The file descriptors used
* in referencing the created sockets shall be returned in
* sv[0] and sv[1].
*
* Input Parameters:
* domain - (see sys/socket.h)
* type - (see sys/socket.h)
* protocol - (see sys/socket.h)
* psocks - The array to catch the pair descriptors
*
****************************************************************************/
int psock_socketpair(int domain, int type, int protocol,
FAR struct socket *psocks[2])
{
int ret;
/* Initialize the socket structure */
ret = psock_socket(domain, type, protocol, psocks[0]);
if (ret < 0)
{
return ret;
}
if (psocks[0]->s_sockif->si_socketpair == NULL)
{
ret = -EAFNOSUPPORT;
goto errsock;
}
ret = psock_socket(domain, type, protocol, psocks[1]);
if (ret < 0)
{
goto errsock;
}
/* Perform socketpair process */
ret = psocks[0]->s_sockif->si_socketpair(psocks);
if (ret == 0)
{
return ret;
}
psock_close(psocks[1]);
errsock:
psock_close(psocks[0]);
return ret;
}
/****************************************************************************
* Name: socketpair
*
* Description:
* Create an unbound pair of connected sockets in a specified domain, of a
* specified type, under the protocol optionally specified by the protocol
* argument. The two sockets shall be identical. The file descriptors used
* in referencing the created sockets shall be returned in
* sv[0] and sv[1].
*
* Input Parameters:
* domain - (see sys/socket.h)
* type - (see sys/socket.h)
* protocol - (see sys/socket.h)
* sv[2] - The user provided array in which to catch the pair
descriptors
*
****************************************************************************/
int socketpair(int domain, int type, int protocol, int sv[2])
{
FAR struct socket *psocks[2];
int oflags = O_RDWR;
int i = 0;
int ret;
if (sv == NULL)
{
ret = -EINVAL;
goto errout;
}
for (i = 0; i < 2; i++)
{
psocks[i] = kmm_zalloc(sizeof(*psocks[i]));
if (psocks[i] == NULL)
{
ret = -ENOMEM;
goto errout_with_alloc;
}
}
ret = psock_socketpair(domain, type, protocol, psocks);
if (ret < 0)
{
goto errout_with_alloc;
}
if (type & SOCK_CLOEXEC)
{
oflags |= O_CLOEXEC;
}
/* Allocate a socket descriptor */
for (i = 0; i < 2; i++)
{
sv[i] = sockfd_allocate(psocks[i], oflags);
if (sv[i] < 0)
{
ret = sv[i];
goto errout_with_psock;
}
}
return OK;
errout_with_psock:
while (i-- > 0)
{
nx_close(sv[i]);
}
i = 2;
errout_with_alloc:
while (i-- > 0)
{
kmm_free(psocks[i]);
}
errout:
set_errno(-ret);
return ERROR;
}