From a66d130b1aaf55606d9107df9ac5681ebf5ac15f Mon Sep 17 00:00:00 2001
From: Chen Wen <chenwen@espressif.com>
Date: Fri, 9 Nov 2018 14:42:14 +0800
Subject: [PATCH] feat: make lwip allocate a new local TCP port is random

---
 components/lwip/lwip/src/core/tcp.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/components/lwip/lwip/src/core/tcp.c b/components/lwip/lwip/src/core/tcp.c
index 1ba69ace..c8d4f980 100644
--- a/components/lwip/lwip/src/core/tcp.c
+++ b/components/lwip/lwip/src/core/tcp.c
@@ -62,6 +62,10 @@
 
 #include <string.h>
 
+#if ESP_LWIP
+#include "arch/sys_arch.h"
+#endif
+
 #ifdef LWIP_HOOK_FILENAME
 #include LWIP_HOOK_FILENAME
 #endif
@@ -847,9 +851,13 @@ tcp_new_port(void)
   struct tcp_pcb *pcb;
 
 again:
+#if ESP_LWIP
+  tcp_port = (u16_t)(esp_random() % (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START)) + TCP_LOCAL_PORT_RANGE_START;
+#else
   if (tcp_port++ == TCP_LOCAL_PORT_RANGE_END) {
     tcp_port = TCP_LOCAL_PORT_RANGE_START;
   }
+#endif
   /* Check all PCB lists. */
   for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
     for (pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {