This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 8a1a2210ff1bfd62485788a652a8f47495cddc71 Author: yintao <[email protected]> AuthorDate: Fri Mar 27 17:36:19 2026 +0800 net/rpmsg: refuse a connection when no server is listening Each listening socket registered its own rpmsg callback that matched only its name. The announcement of a client that connects to a name nobody listens on matched no callback, so the rpmsg core kept it in its bind list and connect() blocked until a server appeared or the send timeout expired, where a unix domain socket fails at once. Register one callback for the announcements of all rpmsg stream clients at net_initialize() time and keep the listening sockets in a list. If no listening socket matches, answer with a NS_DESTROY: the client's ns_unbind callback marks the connection unbound and wakes connect(), which then fails with ECONNRESET, as it does when the peer goes away during the handshake. Datagram announcements are left to the bind list as before: both ends of a datagram socket create an endpoint with the same name, and the first announcement has to wait there for the other end. A stream client is recognized by the ":<id>" suffix of its endpoint name. rpmsg_socket_ns_bind() holds the list lock while it uses the server it found, so that close() cannot free that server under it, as the per socket callback used to guarantee through the rpmsg core. Signed-off-by: mazhuang <[email protected]> Signed-off-by: Bowen Wang <[email protected]> --- net/net_initialize.c | 5 ++ net/rpmsg/rpmsg.h | 14 +++++ net/rpmsg/rpmsg_sockif.c | 150 +++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 144 insertions(+), 25 deletions(-) diff --git a/net/net_initialize.c b/net/net_initialize.c index c4792faf359..e404eed2dfe 100644 --- a/net/net_initialize.c +++ b/net/net_initialize.c @@ -48,6 +48,7 @@ #include "route/route.h" #include "usrsock/usrsock.h" #include "vsock/vsock.h" +#include "rpmsg/rpmsg.h" /**************************************************************************** * Public Functions @@ -113,6 +114,10 @@ void net_initialize(void) #ifdef CONFIG_NET_VSOCK vsock_initialize(); #endif + +#ifdef CONFIG_NET_RPMSG + rpmsg_socket_initialize(); +#endif } #endif /* CONFIG_NET */ diff --git a/net/rpmsg/rpmsg.h b/net/rpmsg/rpmsg.h index 37fa858778b..e55cfacda1d 100644 --- a/net/rpmsg/rpmsg.h +++ b/net/rpmsg/rpmsg.h @@ -43,6 +43,20 @@ extern "C" extern const struct sock_intf_s g_rpmsg_sockif; +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: rpmsg_socket_initialize + * + * Description: + * Initialize the rpmsg socket support. Called once by net_initialize(). + * + ****************************************************************************/ + +void rpmsg_socket_initialize(void); + #ifdef __cplusplus } #endif diff --git a/net/rpmsg/rpmsg_sockif.c b/net/rpmsg/rpmsg_sockif.c index 88be1bdc870..0b1cc354d50 100644 --- a/net/rpmsg/rpmsg_sockif.c +++ b/net/rpmsg/rpmsg_sockif.c @@ -41,6 +41,7 @@ #include <nuttx/rpmsg/rpmsg.h> #include <nuttx/mutex.h> #include <nuttx/poll.h> +#include <nuttx/queue.h> #include <nuttx/sched.h> #include <nuttx/semaphore.h> #include <nuttx/fs/ioctl.h> @@ -131,6 +132,10 @@ struct rpmsg_socket_conn_s FAR struct rpmsg_socket_conn_s *pendlist; FAR struct rpmsg_socket_conn_s *pendnext; + /* Entry in g_rpmsg_socket_server while the socket is listening */ + + struct dq_entry_s snode; + /* server listen-scoket listening: backlog > 0; * others: backlog = 0; * pending is current number of pending connections; @@ -247,6 +252,13 @@ const struct sock_intf_s g_rpmsg_sockif = static uint32_t g_rpmsg_id; +/* Listening sockets. rpmsg_socket_ns_bind() holds the lock while it uses + * the server it found, so that close() cannot free that server meanwhile. + */ + +static mutex_t g_rpmsg_socket_lock = NXMUTEX_INITIALIZER; +static dq_queue_t g_rpmsg_socket_server; + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -696,31 +708,74 @@ static bool rpmsg_socket_ns_match(FAR struct rpmsg_device *rdev, FAR void *priv, FAR const char *name, uint32_t dest) { - FAR struct rpmsg_socket_conn_s *server = priv; + /* Only a stream client names its endpoint with a ":<id>" suffix. The + * two ends of a datagram socket use the same name, and the first + * announcement has to wait in the rpmsg bind list until the other end + * creates its endpoint, so leave those alone. + */ + + return strncmp(name, RPMSG_SOCKET_NAME_PREFIX, + RPMSG_SOCKET_NAME_PREFIX_LEN) == 0 && + strchr(name + RPMSG_SOCKET_NAME_PREFIX_LEN, ':') != NULL; +} + +/**************************************************************************** + * Name: rpmsg_socket_find_server + * + * Assumptions: + * g_rpmsg_socket_lock is held by the caller. + * + ****************************************************************************/ + +static FAR struct rpmsg_socket_conn_s * +rpmsg_socket_find_server(FAR struct rpmsg_device *rdev, + FAR const char *name) +{ + FAR struct rpmsg_socket_conn_s *server; char namebuf[RPMSG_NAME_SIZE]; - rpmsg_socket_format_name(server, namebuf); - if (strncmp(name, namebuf, strlen(namebuf))) + dq_for_every_entry(&g_rpmsg_socket_server, server, + struct rpmsg_socket_conn_s, snode) { - return false; - } + rpmsg_socket_format_name(server, namebuf); + if (strncmp(name, namebuf, strlen(namebuf))) + { + continue; + } - if (server->rpaddr.rp_cpu[0] && - strcmp(server->rpaddr.rp_cpu, rpmsg_get_cpuname(rdev))) - { - /* Bind specific CPU, then only listen that CPU */ + if (server->rpaddr.rp_cpu[0] && + strcmp(server->rpaddr.rp_cpu, rpmsg_get_cpuname(rdev))) + { + /* Bind specific CPU, then only listen that CPU */ - return false; + continue; + } + + return server; } - return true; + return NULL; } -static void rpmsg_socket_ns_bind(FAR struct rpmsg_device *rdev, - FAR void *priv, FAR const char *name, - uint32_t dest) +static void rpmsg_socket_ns_destroy(FAR struct rpmsg_device *rdev, + FAR const char *name, + uint32_t dest) +{ + struct rpmsg_ns_msg ns_msg; + + strlcpy(ns_msg.name, name, RPMSG_NAME_SIZE); + ns_msg.addr = dest; + ns_msg.flags = RPMSG_NS_DESTROY; + + rpmsg_send_offchannel_raw(&rdev->ns_ept, rdev->ns_ept.addr, + RPMSG_NS_EPT_ADDR, &ns_msg, sizeof(ns_msg), + true); +} + +static void rpmsg_socket_ns_accept(FAR struct rpmsg_socket_conn_s *server, + FAR struct rpmsg_device *rdev, + FAR const char *name, uint32_t dest) { - FAR struct rpmsg_socket_conn_s *server = priv; FAR struct rpmsg_socket_conn_s *new; int ret; @@ -824,6 +879,30 @@ errout_with_pending: rpmsg_socket_free(new); } +static void rpmsg_socket_ns_bind(FAR struct rpmsg_device *rdev, + FAR void *priv, FAR const char *name, + uint32_t dest) +{ + FAR struct rpmsg_socket_conn_s *server; + + nxmutex_lock(&g_rpmsg_socket_lock); + server = rpmsg_socket_find_server(rdev, name); + if (server != NULL) + { + rpmsg_socket_ns_accept(server, rdev, name, dest); + } + else + { + /* Nobody listens on this name: refuse the connection, so that the + * client fails with ECONNREFUSED instead of waiting for a listener. + */ + + rpmsg_socket_ns_destroy(rdev, name, dest); + } + + nxmutex_unlock(&g_rpmsg_socket_lock); +} + static int rpmsg_socket_getaddr(FAR struct rpmsg_socket_conn_s *conn, FAR struct sockaddr *addr, FAR socklen_t *addrlen) @@ -936,12 +1015,15 @@ static int rpmsg_socket_listen(FAR struct socket *psock, int backlog) return -EINVAL; } + nxmutex_lock(&g_rpmsg_socket_lock); + if (server->backlog == 0) + { + dq_addlast(&server->snode, &g_rpmsg_socket_server); + } + server->backlog = backlog; - return rpmsg_register_callback(server, - NULL, - NULL, - rpmsg_socket_ns_match, - rpmsg_socket_ns_bind); + nxmutex_unlock(&g_rpmsg_socket_lock); + return 0; } static int rpmsg_socket_connect_internal(FAR struct socket *psock) @@ -1590,11 +1672,9 @@ static int rpmsg_socket_close(FAR struct socket *psock) { FAR struct rpmsg_socket_conn_s *pend; - rpmsg_unregister_callback(conn, - NULL, - NULL, - rpmsg_socket_ns_match, - rpmsg_socket_ns_bind); + nxmutex_lock(&g_rpmsg_socket_lock); + dq_rem(&conn->snode, &g_rpmsg_socket_server); + nxmutex_unlock(&g_rpmsg_socket_lock); /* Detach any connection still waiting for its SYNC: they hold a * pointer to this listening socket which is about to be freed. @@ -1860,3 +1940,23 @@ static int rpmsg_socket_setsockopt(FAR struct socket *psock, int level, } #endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rpmsg_socket_initialize + * + * Description: + * Catch the name service announcements of all rpmsg socket clients, so + * that a connection to a name nobody listens on is refused at once. + * + ****************************************************************************/ + +void rpmsg_socket_initialize(void) +{ + rpmsg_register_callback(NULL, NULL, NULL, + rpmsg_socket_ns_match, + rpmsg_socket_ns_bind); +}
