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 <wangchen41@xiaomi.com>
This commit is contained in:
wangchen 2023-08-03 14:30:38 +08:00 committed by Xiang Xiao
parent d528d2c410
commit 5c798dd297

View File

@ -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