You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/07/05 15:06:31 UTC

[GitHub] [incubator-nuttx] anchao opened a new pull request #4071: net/local: add AF_[UNIX|LOCAL] socketpair support

anchao opened a new pull request #4071:
URL: https://github.com/apache/incubator-nuttx/pull/4071


   ## Summary
   
   net/local: add AF_[UNIX|LOCAL] socketpair support
   
   ## Impact
   
   net/local: add AF_[UNIX|LOCAL] socketpair support
   
   ## Testing
   
   socketpair wirth AF_UNIX and AF_LOCAL 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] yamt commented on a change in pull request #4071: net/local: add AF_[UNIX|LOCAL] socketpair support

Posted by GitBox <gi...@apache.org>.
yamt commented on a change in pull request #4071:
URL: https://github.com/apache/incubator-nuttx/pull/4071#discussion_r664927211



##########
File path: net/socket/socketpair.c
##########
@@ -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: psock_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:

Review comment:
       i guess psock_close is necessary for sockets without descriptors.

##########
File path: net/socket/socketpair.c
##########
@@ -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: psock_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;

Review comment:
       doesn't this cause double-free of psock for which you did nx_close above?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] anchao commented on a change in pull request #4071: net/local: add AF_[UNIX|LOCAL] socketpair support

Posted by GitBox <gi...@apache.org>.
anchao commented on a change in pull request #4071:
URL: https://github.com/apache/incubator-nuttx/pull/4071#discussion_r664541919



##########
File path: net/local/local_sockif.c
##########
@@ -728,6 +731,106 @@ static int local_ioctl(FAR struct socket *psock, int cmd,
   return ret;
 }
 
+/****************************************************************************
+ * Name: local_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 local_socketpair(FAR struct socket *psocks[2])
+{
+#if defined(CONFIG_NET_LOCAL_STREAM) || defined(CONFIG_NET_LOCAL_DGRAM)
+  FAR struct local_conn_s *conns[2];
+#ifdef CONFIG_NET_LOCAL_STREAM
+  bool nonblock;
+  int ret;
+#endif /* CONFIG_NET_LOCAL_STREAM */
+  int i;
+
+  for (i = 0; i < 2; i++)
+    {
+      conns[i] = psocks[i]->s_conn;
+      snprintf(conns[i]->lc_path,
+               sizeof(conns[i]->lc_path), "%p", psocks[0]);
+
+      conns[i]->lc_proto = psocks[i]->s_type;
+      conns[i]->lc_type  = LOCAL_TYPE_PATHNAME;
+      conns[i]->lc_state = LOCAL_STATE_BOUND;
+    }
+
+#ifdef CONFIG_NET_LOCAL_DGRAM
+#ifdef CONFIG_NET_LOCAL_STREAM

Review comment:
       if define the UDP without TCP, remove this ifdef will caused compile warning "no return value"




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] anchao commented on a change in pull request #4071: net/local: add AF_[UNIX|LOCAL] socketpair support

Posted by GitBox <gi...@apache.org>.
anchao commented on a change in pull request #4071:
URL: https://github.com/apache/incubator-nuttx/pull/4071#discussion_r664539949



##########
File path: net/inet/inet_sockif.c
##########
@@ -1328,6 +1342,147 @@ 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])
+{
+#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP)
+  FAR struct socket *pserver = NULL;
+  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 defined(CONFIG_NET_TCP)
+  FAR struct socket server;
+
+  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;
+    }
+#endif /* CONFIG_NET_TCP */
+
+#if defined(CONFIG_NET_UDP)

Review comment:
       TCP / UDP is a completely independent scope. which can be enabled separately, so it is impossible to add else to above scope




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #4071: net/local: add AF_[UNIX|LOCAL] socketpair support

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #4071:
URL: https://github.com/apache/incubator-nuttx/pull/4071#discussion_r664276081



##########
File path: net/local/local_sockif.c
##########
@@ -728,6 +731,106 @@ static int local_ioctl(FAR struct socket *psock, int cmd,
   return ret;
 }
 
+/****************************************************************************
+ * Name: local_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 local_socketpair(FAR struct socket *psocks[2])
+{
+#if defined(CONFIG_NET_LOCAL_STREAM) || defined(CONFIG_NET_LOCAL_DGRAM)
+  FAR struct local_conn_s *conns[2];
+#ifdef CONFIG_NET_LOCAL_STREAM
+  bool nonblock;
+  int ret;
+#endif /* CONFIG_NET_LOCAL_STREAM */
+  int i;
+
+  for (i = 0; i < 2; i++)
+    {
+      conns[i] = psocks[i]->s_conn;
+      snprintf(conns[i]->lc_path,
+               sizeof(conns[i]->lc_path), "%p", psocks[0]);
+
+      conns[i]->lc_proto = psocks[i]->s_type;
+      conns[i]->lc_type  = LOCAL_TYPE_PATHNAME;
+      conns[i]->lc_state = LOCAL_STATE_BOUND;
+    }
+
+#ifdef CONFIG_NET_LOCAL_DGRAM
+#ifdef CONFIG_NET_LOCAL_STREAM
+  if (psocks[0]->s_type == SOCK_DGRAM)
+#endif /* CONFIG_NET_LOCAL_STREAM */
+    {
+      return OK;
+    }
+#endif /* CONFIG_NET_LOCAL_DGRAM */
+
+#ifdef CONFIG_NET_LOCAL_STREAM

Review comment:
       #else

##########
File path: net/inet/inet_sockif.c
##########
@@ -1328,6 +1342,147 @@ 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])
+{
+#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP)
+  FAR struct socket *pserver = NULL;
+  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 defined(CONFIG_NET_TCP)
+  FAR struct socket server;
+
+  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;
+    }
+#endif /* CONFIG_NET_TCP */
+
+#if defined(CONFIG_NET_UDP)

Review comment:
       else
   #endif
     {
   

##########
File path: net/inet/inet_sockif.c
##########
@@ -1328,6 +1342,147 @@ 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])
+{
+#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP)
+  FAR struct socket *pserver = NULL;
+  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 defined(CONFIG_NET_TCP)
+  FAR struct socket server;

Review comment:
       move to the begin of function

##########
File path: net/local/local_sockif.c
##########
@@ -728,6 +731,106 @@ static int local_ioctl(FAR struct socket *psock, int cmd,
   return ret;
 }
 
+/****************************************************************************
+ * Name: local_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 local_socketpair(FAR struct socket *psocks[2])
+{
+#if defined(CONFIG_NET_LOCAL_STREAM) || defined(CONFIG_NET_LOCAL_DGRAM)
+  FAR struct local_conn_s *conns[2];
+#ifdef CONFIG_NET_LOCAL_STREAM
+  bool nonblock;
+  int ret;
+#endif /* CONFIG_NET_LOCAL_STREAM */
+  int i;
+
+  for (i = 0; i < 2; i++)
+    {
+      conns[i] = psocks[i]->s_conn;
+      snprintf(conns[i]->lc_path,
+               sizeof(conns[i]->lc_path), "%p", psocks[0]);
+
+      conns[i]->lc_proto = psocks[i]->s_type;
+      conns[i]->lc_type  = LOCAL_TYPE_PATHNAME;
+      conns[i]->lc_state = LOCAL_STATE_BOUND;
+    }
+
+#ifdef CONFIG_NET_LOCAL_DGRAM
+#ifdef CONFIG_NET_LOCAL_STREAM

Review comment:
       remove?

##########
File path: net/inet/inet_sockif.c
##########
@@ -1328,6 +1342,147 @@ 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])
+{
+#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP)
+  FAR struct socket *pserver = NULL;
+  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 defined(CONFIG_NET_TCP)
+  FAR struct socket server;
+
+  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;
+    }
+#endif /* CONFIG_NET_TCP */
+
+#if defined(CONFIG_NET_UDP)
+  if (psocks[0]->s_type == SOCK_DGRAM)
+    {
+      pserver = psocks[1];
+    }
+#endif /* CONFIG_NET_UDP */
+
+  ret = psock_bind(pserver, &addr[1].addr, len);
+  if (ret < 0)
+    {
+      goto errout;
+    }
+
+  psock_getsockname(pserver, &addr[1].addr, &len);
+
+#if defined(CONFIG_NET_UDP)
+  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;
+        }
+    }
+#endif /* CONFIG_NET_UDP */
+
+#if defined(CONFIG_NET_TCP)

Review comment:
       else
     {
   #endif

##########
File path: net/local/local_sockif.c
##########
@@ -728,6 +731,106 @@ static int local_ioctl(FAR struct socket *psock, int cmd,
   return ret;
 }
 
+/****************************************************************************
+ * Name: local_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 local_socketpair(FAR struct socket *psocks[2])
+{
+#if defined(CONFIG_NET_LOCAL_STREAM) || defined(CONFIG_NET_LOCAL_DGRAM)
+  FAR struct local_conn_s *conns[2];
+#ifdef CONFIG_NET_LOCAL_STREAM
+  bool nonblock;
+  int ret;
+#endif /* CONFIG_NET_LOCAL_STREAM */
+  int i;
+
+  for (i = 0; i < 2; i++)
+    {
+      conns[i] = psocks[i]->s_conn;
+      snprintf(conns[i]->lc_path,
+               sizeof(conns[i]->lc_path), "%p", psocks[0]);

Review comment:
       socketpair%p

##########
File path: net/socket/socketpair.c
##########
@@ -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

Review comment:
       psock_socketpair




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #4071: net/local: add AF_[UNIX|LOCAL] socketpair support

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #4071:
URL: https://github.com/apache/incubator-nuttx/pull/4071#discussion_r664276081



##########
File path: net/local/local_sockif.c
##########
@@ -728,6 +731,106 @@ static int local_ioctl(FAR struct socket *psock, int cmd,
   return ret;
 }
 
+/****************************************************************************
+ * Name: local_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 local_socketpair(FAR struct socket *psocks[2])
+{
+#if defined(CONFIG_NET_LOCAL_STREAM) || defined(CONFIG_NET_LOCAL_DGRAM)
+  FAR struct local_conn_s *conns[2];
+#ifdef CONFIG_NET_LOCAL_STREAM
+  bool nonblock;
+  int ret;
+#endif /* CONFIG_NET_LOCAL_STREAM */
+  int i;
+
+  for (i = 0; i < 2; i++)
+    {
+      conns[i] = psocks[i]->s_conn;
+      snprintf(conns[i]->lc_path,
+               sizeof(conns[i]->lc_path), "%p", psocks[0]);
+
+      conns[i]->lc_proto = psocks[i]->s_type;
+      conns[i]->lc_type  = LOCAL_TYPE_PATHNAME;
+      conns[i]->lc_state = LOCAL_STATE_BOUND;
+    }
+
+#ifdef CONFIG_NET_LOCAL_DGRAM
+#ifdef CONFIG_NET_LOCAL_STREAM
+  if (psocks[0]->s_type == SOCK_DGRAM)
+#endif /* CONFIG_NET_LOCAL_STREAM */
+    {
+      return OK;
+    }
+#endif /* CONFIG_NET_LOCAL_DGRAM */
+
+#ifdef CONFIG_NET_LOCAL_STREAM

Review comment:
       #else

##########
File path: net/inet/inet_sockif.c
##########
@@ -1328,6 +1342,147 @@ 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])
+{
+#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP)
+  FAR struct socket *pserver = NULL;
+  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 defined(CONFIG_NET_TCP)
+  FAR struct socket server;
+
+  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;
+    }
+#endif /* CONFIG_NET_TCP */
+
+#if defined(CONFIG_NET_UDP)

Review comment:
       else
   #endif
     {
   

##########
File path: net/inet/inet_sockif.c
##########
@@ -1328,6 +1342,147 @@ 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])
+{
+#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP)
+  FAR struct socket *pserver = NULL;
+  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 defined(CONFIG_NET_TCP)
+  FAR struct socket server;

Review comment:
       move to the begin of function

##########
File path: net/local/local_sockif.c
##########
@@ -728,6 +731,106 @@ static int local_ioctl(FAR struct socket *psock, int cmd,
   return ret;
 }
 
+/****************************************************************************
+ * Name: local_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 local_socketpair(FAR struct socket *psocks[2])
+{
+#if defined(CONFIG_NET_LOCAL_STREAM) || defined(CONFIG_NET_LOCAL_DGRAM)
+  FAR struct local_conn_s *conns[2];
+#ifdef CONFIG_NET_LOCAL_STREAM
+  bool nonblock;
+  int ret;
+#endif /* CONFIG_NET_LOCAL_STREAM */
+  int i;
+
+  for (i = 0; i < 2; i++)
+    {
+      conns[i] = psocks[i]->s_conn;
+      snprintf(conns[i]->lc_path,
+               sizeof(conns[i]->lc_path), "%p", psocks[0]);
+
+      conns[i]->lc_proto = psocks[i]->s_type;
+      conns[i]->lc_type  = LOCAL_TYPE_PATHNAME;
+      conns[i]->lc_state = LOCAL_STATE_BOUND;
+    }
+
+#ifdef CONFIG_NET_LOCAL_DGRAM
+#ifdef CONFIG_NET_LOCAL_STREAM

Review comment:
       remove?

##########
File path: net/inet/inet_sockif.c
##########
@@ -1328,6 +1342,147 @@ 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])
+{
+#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP)
+  FAR struct socket *pserver = NULL;
+  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 defined(CONFIG_NET_TCP)
+  FAR struct socket server;
+
+  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;
+    }
+#endif /* CONFIG_NET_TCP */
+
+#if defined(CONFIG_NET_UDP)
+  if (psocks[0]->s_type == SOCK_DGRAM)
+    {
+      pserver = psocks[1];
+    }
+#endif /* CONFIG_NET_UDP */
+
+  ret = psock_bind(pserver, &addr[1].addr, len);
+  if (ret < 0)
+    {
+      goto errout;
+    }
+
+  psock_getsockname(pserver, &addr[1].addr, &len);
+
+#if defined(CONFIG_NET_UDP)
+  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;
+        }
+    }
+#endif /* CONFIG_NET_UDP */
+
+#if defined(CONFIG_NET_TCP)

Review comment:
       else
     {
   #endif

##########
File path: net/local/local_sockif.c
##########
@@ -728,6 +731,106 @@ static int local_ioctl(FAR struct socket *psock, int cmd,
   return ret;
 }
 
+/****************************************************************************
+ * Name: local_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 local_socketpair(FAR struct socket *psocks[2])
+{
+#if defined(CONFIG_NET_LOCAL_STREAM) || defined(CONFIG_NET_LOCAL_DGRAM)
+  FAR struct local_conn_s *conns[2];
+#ifdef CONFIG_NET_LOCAL_STREAM
+  bool nonblock;
+  int ret;
+#endif /* CONFIG_NET_LOCAL_STREAM */
+  int i;
+
+  for (i = 0; i < 2; i++)
+    {
+      conns[i] = psocks[i]->s_conn;
+      snprintf(conns[i]->lc_path,
+               sizeof(conns[i]->lc_path), "%p", psocks[0]);

Review comment:
       socketpair%p

##########
File path: net/socket/socketpair.c
##########
@@ -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

Review comment:
       psock_socketpair




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] anchao commented on a change in pull request #4071: net/local: add AF_[UNIX|LOCAL] socketpair support

Posted by GitBox <gi...@apache.org>.
anchao commented on a change in pull request #4071:
URL: https://github.com/apache/incubator-nuttx/pull/4071#discussion_r664540152



##########
File path: net/local/local_sockif.c
##########
@@ -728,6 +731,106 @@ static int local_ioctl(FAR struct socket *psock, int cmd,
   return ret;
 }
 
+/****************************************************************************
+ * Name: local_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 local_socketpair(FAR struct socket *psocks[2])
+{
+#if defined(CONFIG_NET_LOCAL_STREAM) || defined(CONFIG_NET_LOCAL_DGRAM)
+  FAR struct local_conn_s *conns[2];
+#ifdef CONFIG_NET_LOCAL_STREAM
+  bool nonblock;
+  int ret;
+#endif /* CONFIG_NET_LOCAL_STREAM */
+  int i;
+
+  for (i = 0; i < 2; i++)
+    {
+      conns[i] = psocks[i]->s_conn;
+      snprintf(conns[i]->lc_path,
+               sizeof(conns[i]->lc_path), "%p", psocks[0]);

Review comment:
       Done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] anchao commented on a change in pull request #4071: net/local: add AF_[UNIX|LOCAL] socketpair support

Posted by GitBox <gi...@apache.org>.
anchao commented on a change in pull request #4071:
URL: https://github.com/apache/incubator-nuttx/pull/4071#discussion_r664998555



##########
File path: net/socket/socketpair.c
##########
@@ -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: psock_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;

Review comment:
       Done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 merged pull request #4071: net/local: add AF_[UNIX|LOCAL] socketpair support

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 merged pull request #4071:
URL: https://github.com/apache/incubator-nuttx/pull/4071


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org