From 40c6355d61213544d2b9eede7d97b84defd60a93 Mon Sep 17 00:00:00 2001
From: jiangheng <jinag12138@gmail.com>
Date: Sat, 25 Nov 2023 14:56:50 +0800
Subject: [PATCH] Expand backlog size from u8_t to u16_t

---
 src/api/api_lib.c                      | 2 +-
 src/api/sockets.c                      | 6 +++---
 src/apps/altcp_tls/altcp_tls_mbedtls.c | 2 +-
 src/apps/http/altcp_proxyconnect.c     | 2 +-
 src/core/altcp.c                       | 2 +-
 src/core/altcp_tcp.c                   | 2 +-
 src/core/init.c                        | 4 ++--
 src/core/tcp.c                         | 4 ++--
 src/include/lwip/altcp.h               | 2 +-
 src/include/lwip/api.h                 | 2 +-
 src/include/lwip/priv/altcp_priv.h     | 2 +-
 src/include/lwip/priv/api_msg.h        | 2 +-
 src/include/lwip/tcp.h                 | 8 ++++----
 13 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/src/api/api_lib.c b/src/api/api_lib.c
index 60678f88..cad06dd1 100644
--- a/src/api/api_lib.c
+++ b/src/api/api_lib.c
@@ -431,7 +431,7 @@ netconn_disconnect(struct netconn *conn)
  *         don't return any error (yet?))
  */
 err_t
-netconn_listen_with_backlog(struct netconn *conn, u8_t backlog)
+netconn_listen_with_backlog(struct netconn *conn, u16_t backlog)
 {
 #if LWIP_TCP
   API_MSG_VAR_DECLARE(msg);
diff --git a/src/api/sockets.c b/src/api/sockets.c
index ddf37ab7..1819301f 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -928,10 +928,10 @@ lwip_listen(int s, int backlog)
     return -1;
   }
 
-  /* limit the "backlog" parameter to fit in an u8_t */
-  backlog = LWIP_MIN(LWIP_MAX(backlog, 0), 0xff);
+  /* limit the "backlog" parameter to fit in an u16_t */
+  backlog = LWIP_MIN(LWIP_MAX(backlog, 0), 0xffff);
 
-  err = netconn_listen_with_backlog(sock->conn, (u8_t)backlog);
+  err = netconn_listen_with_backlog(sock->conn, (u16_t)backlog);
 
   if (err != ERR_OK) {
     LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err));
diff --git a/src/apps/altcp_tls/altcp_tls_mbedtls.c b/src/apps/altcp_tls/altcp_tls_mbedtls.c
index 9e619126..caa29ce0 100644
--- a/src/apps/altcp_tls/altcp_tls_mbedtls.c
+++ b/src/apps/altcp_tls/altcp_tls_mbedtls.c
@@ -1120,7 +1120,7 @@ altcp_mbedtls_connect(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t por
 }
 
 static struct altcp_pcb *
-altcp_mbedtls_listen(struct altcp_pcb *conn, u8_t backlog, err_t *err)
+altcp_mbedtls_listen(struct altcp_pcb *conn, u16_t backlog, err_t *err)
 {
   struct altcp_pcb *lpcb;
   if (conn == NULL) {
diff --git a/src/apps/http/altcp_proxyconnect.c b/src/apps/http/altcp_proxyconnect.c
index 3d5e7e83..1ae1c76a 100644
--- a/src/apps/http/altcp_proxyconnect.c
+++ b/src/apps/http/altcp_proxyconnect.c
@@ -481,7 +481,7 @@ altcp_proxyconnect_connect(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_
 }
 
 static struct altcp_pcb *
-altcp_proxyconnect_listen(struct altcp_pcb *conn, u8_t backlog, err_t *err)
+altcp_proxyconnect_listen(struct altcp_pcb *conn, u16_t backlog, err_t *err)
 {
   LWIP_UNUSED_ARG(conn);
   LWIP_UNUSED_ARG(backlog);
diff --git a/src/core/altcp.c b/src/core/altcp.c
index 9644e82d..959cdfb3 100644
--- a/src/core/altcp.c
+++ b/src/core/altcp.c
@@ -326,7 +326,7 @@ altcp_connect(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port, altcp
  * @see tcp_listen_with_backlog_and_err()
  */
 struct altcp_pcb *
-altcp_listen_with_backlog_and_err(struct altcp_pcb *conn, u8_t backlog, err_t *err)
+altcp_listen_with_backlog_and_err(struct altcp_pcb *conn, u16_t backlog, err_t *err)
 {
   if (conn && conn->fns && conn->fns->listen) {
     return conn->fns->listen(conn, backlog, err);
diff --git a/src/core/altcp_tcp.c b/src/core/altcp_tcp.c
index 4f21b703..f5a48079 100644
--- a/src/core/altcp_tcp.c
+++ b/src/core/altcp_tcp.c
@@ -282,7 +282,7 @@ altcp_tcp_connect(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port, a
 }
 
 static struct altcp_pcb *
-altcp_tcp_listen(struct altcp_pcb *conn, u8_t backlog, err_t *err)
+altcp_tcp_listen(struct altcp_pcb *conn, u16_t backlog, err_t *err)
 {
   struct tcp_pcb *pcb;
   struct tcp_pcb *lpcb;
diff --git a/src/core/init.c b/src/core/init.c
index b906eff4..6c4d073b 100644
--- a/src/core/init.c
+++ b/src/core/init.c
@@ -160,8 +160,8 @@ PACK_STRUCT_END
 #if (LWIP_TCP && ((TCP_MAXRTX > 12) || (TCP_SYNMAXRTX > 12)))
 #error "If you want to use TCP, TCP_MAXRTX and TCP_SYNMAXRTX must less or equal to 12 (due to tcp_backoff table), so, you have to reduce them in your lwipopts.h"
 #endif
-#if (LWIP_TCP && TCP_LISTEN_BACKLOG && ((TCP_DEFAULT_LISTEN_BACKLOG < 0) || (TCP_DEFAULT_LISTEN_BACKLOG > 0xff)))
-#error "If you want to use TCP backlog, TCP_DEFAULT_LISTEN_BACKLOG must fit into an u8_t"
+#if (LWIP_TCP && TCP_LISTEN_BACKLOG && ((TCP_DEFAULT_LISTEN_BACKLOG < 0) || (TCP_DEFAULT_LISTEN_BACKLOG > 0xffff)))
+#error "If you want to use TCP backlog, TCP_DEFAULT_LISTEN_BACKLOG must fit into an u16_t"
 #endif
 #if (LWIP_TCP && LWIP_TCP_SACK_OUT && !TCP_QUEUE_OOSEQ)
 #error "To use LWIP_TCP_SACK_OUT, TCP_QUEUE_OOSEQ needs to be enabled"
diff --git a/src/core/tcp.c b/src/core/tcp.c
index ea95ffee..42fb27de 100644
--- a/src/core/tcp.c
+++ b/src/core/tcp.c
@@ -823,7 +823,7 @@ tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
  *             tpcb = tcp_listen_with_backlog(tpcb, backlog);
  */
 struct tcp_pcb *
-tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
+tcp_listen_with_backlog(struct tcp_pcb *pcb, u16_t backlog)
 {
   LWIP_ASSERT_CORE_LOCKED();
   return tcp_listen_with_backlog_and_err(pcb, backlog, NULL);
@@ -846,7 +846,7 @@ tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
  *             tpcb = tcp_listen_with_backlog_and_err(tpcb, backlog, &err);
  */
 struct tcp_pcb *
-tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u8_t backlog, err_t *err)
+tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u16_t backlog, err_t *err)
 {
   struct tcp_pcb_listen *lpcb = NULL;
   err_t res;
diff --git a/src/include/lwip/altcp.h b/src/include/lwip/altcp.h
index 11acaff3..620dfeb5 100644
--- a/src/include/lwip/altcp.h
+++ b/src/include/lwip/altcp.h
@@ -105,7 +105,7 @@ err_t altcp_bind(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port);
 err_t altcp_connect(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port, altcp_connected_fn connected);
 
 /* return conn for source code compatibility to tcp callback API only */
-struct altcp_pcb *altcp_listen_with_backlog_and_err(struct altcp_pcb *conn, u8_t backlog, err_t *err);
+struct altcp_pcb *altcp_listen_with_backlog_and_err(struct altcp_pcb *conn, u16_t backlog, err_t *err);
 #define altcp_listen_with_backlog(conn, backlog) altcp_listen_with_backlog_and_err(conn, backlog, NULL)
 /** @ingroup altcp */
 #define altcp_listen(conn) altcp_listen_with_backlog_and_err(conn, TCP_DEFAULT_LISTEN_BACKLOG, NULL)
diff --git a/src/include/lwip/api.h b/src/include/lwip/api.h
index be8c22aa..f980268e 100644
--- a/src/include/lwip/api.h
+++ b/src/include/lwip/api.h
@@ -327,7 +327,7 @@ err_t   netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port);
 err_t   netconn_bind_if(struct netconn *conn, u8_t if_idx);
 err_t   netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port);
 err_t   netconn_disconnect (struct netconn *conn);
-err_t   netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
+err_t   netconn_listen_with_backlog(struct netconn *conn, u16_t backlog);
 /** @ingroup netconn_tcp */
 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
 err_t   netconn_accept(struct netconn *conn, struct netconn **new_conn);
diff --git a/src/include/lwip/priv/altcp_priv.h b/src/include/lwip/priv/altcp_priv.h
index 50e6b970..6ecc2ba5 100644
--- a/src/include/lwip/priv/altcp_priv.h
+++ b/src/include/lwip/priv/altcp_priv.h
@@ -61,7 +61,7 @@ typedef void (*altcp_recved_fn)(struct altcp_pcb *conn, u16_t len);
 typedef err_t (*altcp_bind_fn)(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port);
 typedef err_t (*altcp_connect_fn)(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port, altcp_connected_fn connected);
 
-typedef struct altcp_pcb *(*altcp_listen_fn)(struct altcp_pcb *conn, u8_t backlog, err_t *err);
+typedef struct altcp_pcb *(*altcp_listen_fn)(struct altcp_pcb *conn, u16_t backlog, err_t *err);
 
 typedef void  (*altcp_abort_fn)(struct altcp_pcb *conn);
 typedef err_t (*altcp_close_fn)(struct altcp_pcb *conn);
diff --git a/src/include/lwip/priv/api_msg.h b/src/include/lwip/priv/api_msg.h
index 9e8ffc9e..b36f00ab 100644
--- a/src/include/lwip/priv/api_msg.h
+++ b/src/include/lwip/priv/api_msg.h
@@ -145,7 +145,7 @@ struct api_msg {
 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
 #if TCP_LISTEN_BACKLOG
     struct {
-      u8_t backlog;
+      u16_t backlog;
     } lb;
 #endif /* TCP_LISTEN_BACKLOG */
   } msg;
diff --git a/src/include/lwip/tcp.h b/src/include/lwip/tcp.h
index 3991fd6e..01afedbd 100644
--- a/src/include/lwip/tcp.h
+++ b/src/include/lwip/tcp.h
@@ -232,8 +232,8 @@ struct tcp_pcb_listen {
 #endif /* LWIP_CALLBACK_API */
 
 #if TCP_LISTEN_BACKLOG
-  u8_t backlog;
-  u8_t accepts_pending;
+  u16_t backlog;
+  u16_t accepts_pending;
 #endif /* TCP_LISTEN_BACKLOG */
 };
 
@@ -461,8 +461,8 @@ void             tcp_bind_netif(struct tcp_pcb *pcb, const struct netif *netif);
 err_t            tcp_connect (struct tcp_pcb *pcb, const ip_addr_t *ipaddr,
                               u16_t port, tcp_connected_fn connected);
 
-struct tcp_pcb * tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u8_t backlog, err_t *err);
-struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
+struct tcp_pcb * tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u16_t backlog, err_t *err);
+struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u16_t backlog);
 /** @ingroup tcp_raw */
 #define          tcp_listen(pcb) tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
 
-- 
2.27.0

