Skip to content

Commit

Permalink
net/tcp: Call getrandom only when we have random device
Browse files Browse the repository at this point in the history
Fix link error because we only have getrandom function when we have
random devices.

Signed-off-by: Zhe Weng <[email protected]>
  • Loading branch information
wengzhe authored and xiaoxiang781216 committed Oct 19, 2023
1 parent 0f5086a commit 64ca5af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
14 changes: 7 additions & 7 deletions net/tcp/tcp_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,27 +579,27 @@ int tcp_selectport(uint8_t domain,
uint16_t portno)
{
static uint16_t g_last_tcp_port;
ssize_t ret;

/* Generate port base dynamically */

if (g_last_tcp_port == 0)
{
ret = getrandom(&g_last_tcp_port, sizeof(uint16_t), 0);
#if defined(CONFIG_DEV_URANDOM) || defined(CONFIG_DEV_RANDOM) || \
defined(CONFIG_CRYPTO_RANDOM_POOL)
ssize_t ret = getrandom(&g_last_tcp_port, sizeof(uint16_t), 0);
if (ret < 0)
{
ret = getrandom(&g_last_tcp_port, sizeof(uint16_t), GRND_RANDOM);
}

if (ret != sizeof(uint16_t))
#endif
{
g_last_tcp_port = clock_systime_ticks() % 32000;
}
else
{
g_last_tcp_port = g_last_tcp_port % 32000;
g_last_tcp_port = (uint16_t)clock_systime_ticks();
}

g_last_tcp_port = g_last_tcp_port % 32000;

if (g_last_tcp_port < 4096)
{
g_last_tcp_port += 4096;
Expand Down
15 changes: 7 additions & 8 deletions net/tcp/tcp_seqno.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ uint32_t tcp_addsequence(FAR uint8_t *seqno, uint16_t len)

void tcp_initsequence(FAR uint8_t *seqno)
{
int ret;

/* If g_tcpsequence is already initialized, just copy it */

if (g_tcpsequence == 0)
{
/* Get a random TCP sequence number */

ret = getrandom(&g_tcpsequence, sizeof(uint32_t), 0);
#if defined(CONFIG_DEV_URANDOM) || defined(CONFIG_DEV_RANDOM) || \
defined(CONFIG_CRYPTO_RANDOM_POOL)
int ret = getrandom(&g_tcpsequence, sizeof(uint32_t), 0);
if (ret < 0)
{
ret = getrandom(&g_tcpsequence, sizeof(uint32_t), GRND_RANDOM);
Expand All @@ -163,14 +163,13 @@ void tcp_initsequence(FAR uint8_t *seqno)
*/

if (ret != sizeof(uint32_t))
#endif
{
g_tcpsequence = clock_systime_ticks() % 2000000000;
}
else
{
g_tcpsequence = g_tcpsequence % 2000000000;
g_tcpsequence = (uint32_t)clock_systime_ticks();
}

g_tcpsequence = g_tcpsequence % 2000000000;

/* If the random value is "small" increase it */

if (g_tcpsequence < 1000000000)
Expand Down

0 comments on commit 64ca5af

Please sign in to comment.