From 5c798dd2971f63eafa3190378e709ec4c60b7ead Mon Sep 17 00:00:00 2001 From: wangchen Date: Thu, 3 Aug 2023 14:30:38 +0800 Subject: [PATCH] inet/inet_sockif.c:In tcp protocol, Add random ports during the listening phase, if no ports are bound In tcp protocol, if no ports are bound, Add random ports during the listening phase libuvtestcase: TEST_IMPL(tcp_listen_without_bind) { int r; uv_tcp_t server; r = uv_tcp_init(uv_default_loop(), &server); ASSERT(r == 0); r = uv_listen((uv_stream_t*)&server, 128, NULL); ASSERT(r == 0); MAKE_VALGRIND_HAPPY(); return 0; } Signed-off-by: wangchen --- net/inet/inet_sockif.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/net/inet/inet_sockif.c b/net/inet/inet_sockif.c index 213cbd00db..a45c083a0d 100644 --- a/net/inet/inet_sockif.c +++ b/net/inet/inet_sockif.c @@ -1137,9 +1137,37 @@ static int inet_listen(FAR struct socket *psock, int backlog) #ifdef NET_TCP_HAVE_STACK conn = psock->s_conn; - if (conn->lport <= 0) + if (conn->lport == 0) { - return -EOPNOTSUPP; +#ifdef CONFIG_NET_IPv4 +#ifdef CONFIG_NET_IPv6 + if (conn->domain == PF_INET) +#endif + { + /* Select a port that is unique for this IPv4 local address + * (network order). + */ + + conn->lport = tcp_selectport(PF_INET, + (FAR const union ip_addr_u *) + &conn->u.ipv4.laddr, 0); + } +#endif /* CONFIG_NET_IPv4 */ + +#ifdef CONFIG_NET_IPv6 +#ifdef CONFIG_NET_IPv4 + else +#endif + { + /* Select a port that is unique for this IPv6 local address + * (network order). + */ + + conn->lport = tcp_selectport(PF_INET6, + (FAR const union ip_addr_u *) + conn->u.ipv6.laddr, 0); + } +#endif /* CONFIG_NET_IPv6 */ } #ifdef CONFIG_NET_TCPBACKLOG