casaroli commented on code in PR #21418:
URL: https://github.com/apache/nuttx/pull/21418#discussion_r4469343466
##########
net/local/local_sockif.c:
##########
@@ -822,9 +855,70 @@ static int local_connect(FAR struct socket *psock,
#ifdef CONFIG_NET_LOCAL_DGRAM
case SOCK_DGRAM:
{
- /* Perform the datagram connection logic */
+ FAR struct local_conn_s *conn = psock->s_conn;
+ FAR const struct sockaddr_un *unaddr =
+ (FAR const struct sockaddr_un *)addr;
+
+ if (addr->sa_family == AF_UNSPEC)
+ {
+ memset(&conn->lc_daddr, 0, sizeof(conn->lc_daddr));
+ if (conn->lc_state == LOCAL_STATE_CONNECTED)
+ {
+ conn->lc_state = conn->lc_type == LOCAL_TYPE_UNNAMED
+ ? LOCAL_STATE_UNBOUND
+ : LOCAL_STATE_BOUND;
+ }
+
+ return OK;
+ }
+
+ /* Validate the destination before recording it, so that
+ * connect() reports a bad name instead of letting every later
+ * send() fail. POSIX allows a connectionless connect() to
+ * check the address.
+ */
+
+ if (conn->lc_peer == NULL)
Review Comment:
connect() with only `sun_family` (addrlen 2) returns 0, and an empty name
matches any unbound socket.
Fix: return -EINVAL when `addrlen <= sizeof(sa_family_t) + 1`, as
`psock_local_bind()` does.
##########
net/local/local_sockif.c:
##########
@@ -822,9 +855,70 @@ static int local_connect(FAR struct socket *psock,
#ifdef CONFIG_NET_LOCAL_DGRAM
case SOCK_DGRAM:
{
- /* Perform the datagram connection logic */
+ FAR struct local_conn_s *conn = psock->s_conn;
+ FAR const struct sockaddr_un *unaddr =
+ (FAR const struct sockaddr_un *)addr;
+
+ if (addr->sa_family == AF_UNSPEC)
+ {
+ memset(&conn->lc_daddr, 0, sizeof(conn->lc_daddr));
+ if (conn->lc_state == LOCAL_STATE_CONNECTED)
+ {
+ conn->lc_state = conn->lc_type == LOCAL_TYPE_UNNAMED
+ ? LOCAL_STATE_UNBOUND
+ : LOCAL_STATE_BOUND;
+ }
+
+ return OK;
+ }
+
+ /* Validate the destination before recording it, so that
+ * connect() reports a bad name instead of letting every later
+ * send() fail. POSIX allows a connectionless connect() to
+ * check the address.
+ */
+
+ if (conn->lc_peer == NULL)
+ {
+ FAR struct local_conn_s *server;
+
+ local_lock();
+ server = local_findconn(conn, unaddr);
+ local_unlock();
+
+ if (server == NULL)
+ {
+ FAR const char *path =
+ &unaddr->sun_path[unaddr->sun_path[0] == '\0' ? 1 : 0];
+ struct stat buf;
+
+ /* No socket of that name. A name that exists in the
+ * file system but is not one of our sockets is a
+ * connection refused, anything else does not exist.
+ */
- return -ENOSYS;
+ if (stat(path, &buf) == 0)
Review Comment:
`stat()` is the user API and sets errno.
Fix: `nx_stat(path, &buf, 1)`, as `psock_local_connect()` does.
##########
net/local/local_sendmsg.c:
##########
@@ -286,11 +202,13 @@ static ssize_t local_sendto(FAR struct socket *psock,
/* Verify that this is not a connected peer socket. It need not be
* bound, however. If unbound, recvfrom will see this as a nameless
- * connection.
+ * connection. DGRAM connected state is allowed since connect() only
+ * sets the default peer address without establishing a FIFO link.
*/
if (conn->lc_state != LOCAL_STATE_UNBOUND &&
- conn->lc_state != LOCAL_STATE_BOUND)
+ conn->lc_state != LOCAL_STATE_BOUND &&
+ conn->lc_state != LOCAL_STATE_CONNECTED)
Review Comment:
sendto() with an address on a datagram socket pair now hits an assert. A
pair is LOCAL_STATE_CONNECTED, so this check lets it through, and its
`lc_outfile` is open, so the DEBUGASSERT at line 262 fails with
CONFIG_DEBUG_ASSERTIONS: "Assertion failed : at file:
local/local_sendmsg.c:262" on qemu-armv8a:nsh. Master returns EISCONN.
Fix: return -EISCONN here when `conn->lc_peer != NULL`.
--
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]