From: Angus Gratton Date: Thu, 20 Jul 2017 00:04:20 +0000 (+1000) Subject: lwip: Use hardware RNG for LWIP_RAND(), use LWIP_RAND() for random port assignment X-Git-Tag: v3.1-dev~302^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=73b3ae8a2d9e51cdcec825ec94b321ef04a20a2b;p=esp-idf lwip: Use hardware RNG for LWIP_RAND(), use LWIP_RAND() for random port assignment --- diff --git a/components/lwip/core/tcp.c b/components/lwip/core/tcp.c index 3ad5903d9e..1f915128a4 100755 --- a/components/lwip/core/tcp.c +++ b/components/lwip/core/tcp.c @@ -696,14 +696,12 @@ tcp_new_port(void) again: #if ESP_RANDOM_TCP_PORT - tcp_port = system_get_time(); - if (tcp_port < 0) - tcp_port = LWIP_RAND() - tcp_port; - tcp_port %= TCP_LOCAL_PORT_RANGE_START; + tcp_port = abs(LWIP_RAND()) % (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START); + tcp_port += TCP_LOCAL_PORT_RANGE_START; #else - if (tcp_port++ == TCP_LOCAL_PORT_RANGE_END) { - tcp_port = TCP_LOCAL_PORT_RANGE_START; - } + if (tcp_port++ == TCP_LOCAL_PORT_RANGE_END) { + tcp_port = TCP_LOCAL_PORT_RANGE_START; + } #endif /* Check all PCB lists. */ diff --git a/components/lwip/include/lwip/port/arch/sys_arch.h b/components/lwip/include/lwip/port/arch/sys_arch.h index 8d30ef171d..716fd9fa53 100644 --- a/components/lwip/include/lwip/port/arch/sys_arch.h +++ b/components/lwip/include/lwip/port/arch/sys_arch.h @@ -67,7 +67,6 @@ typedef struct sys_mbox_s { #define sys_sem_valid( x ) ( ( ( *x ) == NULL) ? pdFALSE : pdTRUE ) #define sys_sem_set_invalid( x ) ( ( *x ) = NULL ) -uint32_t system_get_time(void); void sys_delay_ms(uint32_t ms); sys_sem_t* sys_thread_sem_init(void); void sys_thread_sem_deinit(void); diff --git a/components/lwip/include/lwip/port/lwipopts.h b/components/lwip/include/lwip/port/lwipopts.h index 49c2174bf3..7bc4021e5a 100644 --- a/components/lwip/include/lwip/port/lwipopts.h +++ b/components/lwip/include/lwip/port/lwipopts.h @@ -37,6 +37,7 @@ #include #include #include "esp_task.h" +#include "esp_system.h" #include "sdkconfig.h" /* Enable all Espressif-only options */ @@ -65,7 +66,7 @@ */ #define SMEMCPY(dst,src,len) memcpy(dst,src,len) -#define LWIP_RAND rand +#define LWIP_RAND esp_random /* ------------------------------------