xiaoxiang781216 commented on code in PR #19456:
URL: https://github.com/apache/nuttx/pull/19456#discussion_r3603826956


##########
arch/sim/src/sim/posix/sim_hostusrsock.c:
##########
@@ -327,20 +429,37 @@ int host_usrsock_connect(int sockfd,
                          const struct nuttx_sockaddr *addr,
                          nuttx_socklen_t addrlen)
 {
-  struct sockaddr naddr;
+  struct sockaddr_storage naddr;
   socklen_t naddrlen;
   int ret;
 
-  sockaddr_to_native(addr, addrlen, &naddr, &naddrlen);
+  ret = sockaddr_to_native(addr, addrlen, &naddr, &naddrlen);
+  if (ret < 0)
+    {
+      return ret;
+    }
 
-  sock_nonblock(sockfd, false);
-  ret = connect(sockfd, &naddr, naddrlen);
-  sock_nonblock(sockfd, true);
+  ret = connect(sockfd, (struct sockaddr *)&naddr, naddrlen);
   if (ret < 0)
     {
+      if (errno == EISCONN)
+        {
+          host_usrsock_clear_fd(sockfd, &g_active_write_fds);

Review Comment:
   why not set fd



##########
net/usrsock/usrsock_sockif.c:
##########
@@ -103,9 +103,9 @@ static int usrsock_sockif_setup(FAR struct socket *psock)
   int ret;
 
   if (psock->s_domain != PF_INET && psock->s_domain != PF_INET6 &&
-      psock->s_domain != PF_NETLINK)
+      psock->s_domain != PF_NETLINK && psock->s_domain != PF_LOCAL)

Review Comment:
   should we prefer nuttx local socket if the current defconfig enable it



##########
arch/sim/src/sim/posix/sim_hostusrsock.c:
##########
@@ -92,26 +94,93 @@ static void host_usrsock_set_fd(int fd, fd_set *fds)
     }
 }
 
-static void sockaddr_to_native(const struct nuttx_sockaddr *addr,
-                               const nuttx_socklen_t addrlen,
-                               struct sockaddr *naddr,
-                               socklen_t *naddrlen)
+static int sockaddr_to_native(const struct nuttx_sockaddr *addr,
+                              const nuttx_socklen_t addrlen,
+                              struct sockaddr_storage *naddr,
+                              socklen_t *naddrlen)
 {
-  naddr->sa_family = addr->sa_family;
-  memcpy(naddr->sa_data, addr->sa_data, sizeof(naddr->sa_data));
+  if (addr == NULL || naddr == NULL || naddrlen == NULL)
+    {
+      return -EINVAL;
+    }
+
+  memset(naddr, 0, sizeof(*naddr));
+
+  if (addr->sa_family == NUTTX_AF_LOCAL)
+    {
+      const struct nuttx_sockaddr_un *un =
+        (const struct nuttx_sockaddr_un *)addr;
+      struct sockaddr_un *native = (struct sockaddr_un *)naddr;
+      size_t pathlen;
 
+      if (addrlen < offsetof(struct nuttx_sockaddr_un, sun_path) + 1)
+        {
+          return -EINVAL;
+        }
+
+      pathlen = strnlen(un->sun_path, sizeof(un->sun_path));
+      if (pathlen >= sizeof(native->sun_path))
+        {
+          return -ENAMETOOLONG;
+        }
+
+      native->sun_family = AF_UNIX;
+      memcpy(native->sun_path, un->sun_path, pathlen + 1);
+      *naddrlen = offsetof(struct sockaddr_un, sun_path) + pathlen + 1;
+
+      return 0;
+    }
+
+  if (addrlen > sizeof(*naddr))
+    {
+      return -ENOSPC;
+    }
+
+  memcpy(naddr, addr, addrlen);
   *naddrlen = addrlen;
+
+  return 0;
 }
 
-static void sockaddr_to_nuttx(const struct sockaddr *naddr,
-                              const socklen_t naddrlen,
-                              struct nuttx_sockaddr *addr,
-                              nuttx_socklen_t *addrlen)
+static int sockaddr_to_nuttx(const struct sockaddr *naddr,

Review Comment:
   change sockaddr to sockaddr_storage and remove the cast in caller



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to