Index: doc/rawapi.txt
===================================================================
RCS file: /sources/lwip/lwip/doc/rawapi.txt,v
retrieving revision 1.9
diff -u -r1.9 rawapi.txt
--- doc/rawapi.txt	3 Dec 2007 21:44:51 -0000	1.9
+++ doc/rawapi.txt	4 Jan 2008 00:19:16 -0000
@@ -95,6 +95,14 @@
   listening connection. If so, the memory associated with the pcb
   passed as an argument to tcp_listen() will not be deallocated.
 
+- struct tcp_pcb *tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
+
+  Same as tcp_listen, and limits the number of outstanding connections
+  in the listen queue to the value specified by the backlog argument.
+  To use it, your need to set TCP_LISTEN_BACKLOG=1 in your lwipopts.h.
+  In this case, you have to call 'tcp_accepted(pcb)' in your accept
+  callback to keep accepting new connections.
+
 - void tcp_accept(struct tcp_pcb *pcb,
                   err_t (* accept)(void *arg, struct tcp_pcb *newpcb,
                                    err_t err))
Index: src/api/api_lib.c
===================================================================
RCS file: /sources/lwip/lwip/src/api/api_lib.c,v
retrieving revision 1.85
diff -u -r1.85 api_lib.c
--- src/api/api_lib.c	21 Dec 2007 16:47:56 -0000	1.85
+++ src/api/api_lib.c	3 Jan 2008 23:58:40 -0000
@@ -261,7 +261,7 @@
  * Set a TCP netconn into listen mode
  *
  * @param conn the tcp netconn to set to listen mode
- * @param backlog the listen backlog (0 = max), only used if LWIP_LISTEN_BACKLOG==1
+ * @param backlog the listen backlog (0 = max), only used if TCP_LISTEN_BACKLOG==1
  * @return ERR_OK if the netconn was set to listen (UDP and RAW netconns
  *         don't return any error (yet?))
  */
@@ -270,16 +270,16 @@
 {
   struct api_msg msg;
 
-  /* This does no harm. If LWIP_LISTEN_BACKLOG is off, backlog is unused. */
+  /* This does no harm. If TCP_LISTEN_BACKLOG is off, backlog is unused. */
   LWIP_UNUSED_ARG(backlog);
 
   LWIP_ERROR("netconn_listen: invalid conn", (conn != NULL), return ERR_ARG;);
 
-#if LWIP_LISTEN_BACKLOG
-  msg.msg.msg.lb.backlog = backlog;
-#endif /* LWIP_LISTEN_BACKLOG */
   msg.function = do_listen;
   msg.msg.conn = conn;
+#if TCP_LISTEN_BACKLOG
+  msg.msg.msg.lb.backlog = backlog;
+#endif /* TCP_LISTEN_BACKLOG */
   TCPIP_APIMSG(&msg);
   return conn->err;
 }
@@ -309,7 +309,7 @@
     /* Register event with callback */
     API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
 
-#if LWIP_LISTEN_BACKLOG
+#if TCP_LISTEN_BACKLOG
     if (newconn != NULL) {
       /* Let the stack know that we have accepted the connection. */
       struct api_msg msg;
@@ -317,7 +317,7 @@
       msg.msg.conn = conn;
       TCPIP_APIMSG(&msg);
     }
-#endif /* LWIP_LISTEN_BACKLOG */
+#endif /* TCP_LISTEN_BACKLOG */
   }
 
   return newconn;
Index: src/api/api_msg.c
===================================================================
RCS file: /sources/lwip/lwip/src/api/api_msg.c,v
retrieving revision 1.89
diff -u -r1.89 api_msg.c
--- src/api/api_msg.c	21 Dec 2007 16:47:56 -0000	1.89
+++ src/api/api_msg.c	3 Jan 2008 23:58:40 -0000
@@ -717,13 +717,17 @@
     if (msg->conn->pcb.tcp != NULL) {
       if (msg->conn->type == NETCONN_TCP) {
         if (msg->conn->pcb.tcp->state == CLOSED) {
+#if TCP_LISTEN_BACKLOG
+          struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
+#else  /* TCP_LISTEN_BACKLOG */
           struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
+#endif /* TCP_LISTEN_BACKLOG */
           if (lpcb == NULL) {
             msg->conn->err = ERR_MEM;
           } else {
             /* delete the recvmbox and allocate the acceptmbox */
             if (msg->conn->recvmbox != SYS_MBOX_NULL) {
-              /* @todo: should we drain the recvmbox here? */
+              /** @todo: should we drain the recvmbox here? */
               sys_mbox_free(msg->conn->recvmbox);
               msg->conn->recvmbox = NULL;
             }
@@ -735,9 +739,6 @@
             if (msg->conn->err == ERR_OK) {
               msg->conn->state = NETCONN_LISTEN;
               msg->conn->pcb.tcp = lpcb;
-#if LWIP_LISTEN_BACKLOG
-              tcp_backlog(lpcb, msg->msg.lb.backlog);
-#endif /* LWIP_LISTEN_BACKLOG */
               tcp_arg(msg->conn->pcb.tcp, msg->conn);
               tcp_accept(msg->conn->pcb.tcp, accept_function);
             }
@@ -803,11 +804,11 @@
   if (!ERR_IS_FATAL(msg->conn->err)) {
     if (msg->conn->pcb.tcp != NULL) {
       if (msg->conn->type == NETCONN_TCP) {
-#if LWIP_LISTEN_BACKLOG
+#if TCP_LISTEN_BACKLOG
         if (msg->conn->pcb.tcp->state == LISTEN) {
           tcp_accepted(msg->conn->pcb.tcp);
         } else
-#endif /* LWIP_LISTEN_BACKLOG */
+#endif /* TCP_LISTEN_BACKLOG */
         {
           tcp_recved(msg->conn->pcb.tcp, msg->msg.r.len);
         }
Index: src/api/sockets.c
===================================================================
RCS file: /sources/lwip/lwip/src/api/sockets.c,v
retrieving revision 1.112
diff -u -r1.112 sockets.c
--- src/api/sockets.c	21 Dec 2007 16:47:56 -0000	1.112
+++ src/api/sockets.c	3 Jan 2008 23:58:40 -0000
@@ -421,7 +421,7 @@
  * The socket may not have been used for another connection previously.
  *
  * @param s the socket to set to listening mode
- * @param backlog (ATTENTION: this is not implemented, yet!)
+ * @param backlog (ATTENTION: need TCP_LISTEN_BACKLOG=1)
  * @return 0 on success, non-zero on failure
  */
 int
@@ -431,7 +431,7 @@
   err_t err;
 
   LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog));
-  LWIP_ASSERT("backlog must be between 0 and 255", backlog > 0);
+  LWIP_ASSERT("backlog must be between 0 and 255", backlog >= 0);
   LWIP_ASSERT("backlog must be between 0 and 255", backlog <= 0xff);
 
   sock = get_socket(s);
Index: src/core/init.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/init.c,v
retrieving revision 1.28
diff -u -r1.28 init.c
--- src/core/init.c	21 Dec 2007 16:47:56 -0000	1.28
+++ src/core/init.c	4 Jan 2008 00:24:46 -0000
@@ -103,6 +103,9 @@
 #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 > 255))
+  #error "If you want to use TCP backlog, TCP_DEFAULT_LISTEN_BACKLOG must fit into an u8_t"
+#endif
 #if (LWIP_IGMP && (MEMP_NUM_IGMP_GROUP<=1))
   #error "If you want to use IGMP, you have to define MEMP_NUM_IGMP_GROUP>1 in your lwipopts.h"
 #endif
@@ -152,9 +155,6 @@
 #if (MEM_USE_POOLS && !MEMP_USE_CUSTOM_POOLS)
   #error "MEM_USE_POOLS requires custom pools (MEMP_USE_CUSTOM_POOLS) to be enabled in your lwipopts.h"
 #endif
-#if (LWIP_DEFAULT_LISTEN_BACKLOG < 0) || (LWIP_DEFAULT_LISTEN_BACKLOG > 255)
-  #error "LWIP_DEFAULT_LISTEN_BACKLOG must fit into an u8_t"
-#endif
 
 
 /* Compile-time checks for deprecated options.
Index: src/core/tcp.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/tcp.c,v
retrieving revision 1.80
diff -u -r1.80 tcp.c
--- src/core/tcp.c	31 Dec 2007 13:34:47 -0000	1.80
+++ src/core/tcp.c	3 Jan 2008 23:59:43 -0000
@@ -343,7 +343,7 @@
  *             tpcb = tcp_listen(tpcb);
  */
 struct tcp_pcb *
-tcp_listen(struct tcp_pcb *pcb)
+tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
 {
   struct tcp_pcb_listen *lpcb;
 
@@ -370,10 +370,10 @@
 #if LWIP_CALLBACK_API
   lpcb->accept = tcp_accept_null;
 #endif /* LWIP_CALLBACK_API */
-#if LWIP_LISTEN_BACKLOG
+#if TCP_LISTEN_BACKLOG
   lpcb->accepts_pending = 0;
-  lpcb->backlog = LWIP_DEFAULT_LISTEN_BACKLOG;
-#endif /* LWIP_LISTEN_BACKLOG */
+  lpcb->backlog = (backlog ? backlog : TCP_DEFAULT_LISTEN_BACKLOG);
+#endif /* TCP_LISTEN_BACKLOG */
   TCP_REG(&tcp_listen_pcbs.listen_pcbs, lpcb);
   return (struct tcp_pcb *)lpcb;
 }
Index: src/core/tcp_in.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/tcp_in.c,v
retrieving revision 1.94
diff -u -r1.94 tcp_in.c
--- src/core/tcp_in.c	31 Dec 2007 13:34:47 -0000	1.94
+++ src/core/tcp_in.c	3 Jan 2008 23:58:40 -0000
@@ -381,11 +381,11 @@
       tcphdr->dest, tcphdr->src);
   } else if (flags & TCP_SYN) {
     LWIP_DEBUGF(TCP_DEBUG, ("TCP connection request %"U16_F" -> %"U16_F".\n", tcphdr->src, tcphdr->dest));
-#if LWIP_LISTEN_BACKLOG
+#if TCP_LISTEN_BACKLOG
     if (pcb->accepts_pending >= pcb->backlog) {
       return ERR_ABRT;
     }
-#endif /* LWIP_LISTEN_BACKLOG */
+#endif /* TCP_LISTEN_BACKLOG */
     npcb = tcp_alloc(pcb->prio);
     /* If a new PCB could not be created (probably due to lack of memory),
        we don't do anything, but rely on the sender will retransmit the
@@ -395,9 +395,9 @@
       TCP_STATS_INC(tcp.memerr);
       return ERR_MEM;
     }
-#if LWIP_LISTEN_BACKLOG
+#if TCP_LISTEN_BACKLOG
     pcb->accepts_pending++;
-#endif /* LWIP_LISTEN_BACKLOG */
+#endif /* TCP_LISTEN_BACKLOG */
     /* Set up the new PCB. */
     ip_addr_set(&(npcb->local_ip), &(iphdr->dest));
     npcb->local_port = pcb->local_port;
Index: src/include/lwip/api.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/api.h,v
retrieving revision 1.39
diff -u -r1.39 api.h
--- src/include/lwip/api.h	21 Dec 2007 16:47:57 -0000	1.39
+++ src/include/lwip/api.h	4 Jan 2008 00:08:58 -0000
@@ -180,7 +180,7 @@
                                    u16_t port);
 err_t             netconn_disconnect (struct netconn *conn);
 err_t             netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
-#define netconn_listen(conn) netconn_listen_with_backlog(conn, LWIP_DEFAULT_LISTEN_BACKLOG);
+#define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
 struct netconn *  netconn_accept  (struct netconn *conn);
 struct netbuf *   netconn_recv    (struct netconn *conn);
 err_t             netconn_sendto  (struct netconn *conn,
Index: src/include/lwip/api_msg.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/api_msg.h,v
retrieving revision 1.28
diff -u -r1.28 api_msg.h
--- src/include/lwip/api_msg.h	21 Dec 2007 16:47:57 -0000	1.28
+++ src/include/lwip/api_msg.h	3 Jan 2008 23:58:40 -0000
@@ -93,11 +93,11 @@
       enum netconn_igmp join_or_leave;
     } jl;
 #endif /* LWIP_IGMP */
-#if LWIP_LISTEN_BACKLOG
+#if TCP_LISTEN_BACKLOG
     struct {
       u8_t backlog;
     } lb;
-#endif /* LWIP_LISTEN_BACKLOG */
+#endif /* TCP_LISTEN_BACKLOG */
   } msg;
 };
 
Index: src/include/lwip/opt.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/opt.h,v
retrieving revision 1.109
diff -u -r1.109 opt.h
--- src/include/lwip/opt.h	21 Dec 2007 16:47:57 -0000	1.109
+++ src/include/lwip/opt.h	4 Jan 2008 00:01:11 -0000
@@ -714,6 +714,22 @@
 #endif
 
 /**
+ * TCP_LISTEN_BACKLOG: Enable the backlog option for tcp listen pcb.
+ */
+#ifndef TCP_LISTEN_BACKLOG
+#define TCP_LISTEN_BACKLOG              0
+#endif
+
+/**
+ * The maximum allowed backlog for TCP listen netconns.
+ * This backlog is used unless another is explicitly specified.
+ * 0xff is the maximum (u8_t).
+ */
+#ifndef TCP_DEFAULT_LISTEN_BACKLOG
+#define TCP_DEFAULT_LISTEN_BACKLOG      0xff
+#endif
+
+/**
  * LWIP_EVENT_API and LWIP_CALLBACK_API: Only one of these should be set to 1.
  *     LWIP_EVENT_API==1: The user defines lwip_tcp_event() to receive all
  *         events (accept, sent, etc) that happen in the system.
@@ -728,22 +744,6 @@
 #define LWIP_CALLBACK_API               0
 #endif
 
-/**
- * LWIP_LISTEN_BACKLOG: Enable the backlog option for tcp listen pcb.
- */
-#ifndef LWIP_LISTEN_BACKLOG
-#define LWIP_LISTEN_BACKLOG             1
-#endif
-
-/**
- * The maximum allowed backlog for TCP listen netconns.
- * This backlog is used unless another is explicitly specified.
- * 0xff is the maximum (u8_t).
- */
-#ifndef LWIP_DEFAULT_LISTEN_BACKLOG
-#define LWIP_DEFAULT_LISTEN_BACKLOG     0xff
-#endif
-
 
 /*
    ----------------------------------
Index: src/include/lwip/tcp.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/tcp.h,v
retrieving revision 1.62
diff -u -r1.62 tcp.h
--- src/include/lwip/tcp.h	31 Dec 2007 13:34:47 -0000	1.62
+++ src/include/lwip/tcp.h	4 Jan 2008 00:05:54 -0000
@@ -79,10 +79,11 @@
 #define          tcp_mss(pcb)      ((pcb)->mss)
 #define          tcp_sndbuf(pcb)   ((pcb)->snd_buf)
 
-#if LWIP_LISTEN_BACKLOG
-#define          tcp_accepted(pcb)       (((struct tcp_pcb_listen *)(pcb))->accepts_pending--)
-#define          tcp_backlog(pcb, bklog) (((struct tcp_pcb_listen *)(pcb))->backlog = bklog)
-#endif /* LWIP_LISTEN_BACKLOG */
+#if TCP_LISTEN_BACKLOG
+#define          tcp_accepted(pcb) (((struct tcp_pcb_listen *)(pcb))->accepts_pending--)
+#else  /* TCP_LISTEN_BACKLOG */
+#define          tcp_accepted(pcb)
+#endif /* TCP_LISTEN_BACKLOG */
 
 void             tcp_recved  (struct tcp_pcb *pcb, u16_t len);
 err_t            tcp_bind    (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
@@ -91,7 +92,10 @@
                               u16_t port, err_t (* connected)(void *arg,
                               struct tcp_pcb *tpcb,
                               err_t err));
-struct tcp_pcb * tcp_listen  (struct tcp_pcb *pcb);
+
+struct tcp_pcb * tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
+#define          tcp_listen(pcb) tcp_listen_with_backlog(pcb, TCP_DEFAULT_LISTEN_BACKLOG)
+
 void             tcp_abort   (struct tcp_pcb *pcb);
 err_t            tcp_close   (struct tcp_pcb *pcb);
 
@@ -422,10 +426,10 @@
    */
   err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
 #endif /* LWIP_CALLBACK_API */
-#if LWIP_LISTEN_BACKLOG
+#if TCP_LISTEN_BACKLOG
   u8_t backlog;
   u8_t accepts_pending;
-#endif /* LWIP_LISTEN_BACKLOG */
+#endif /* TCP_LISTEN_BACKLOG */
 };
 
 #if LWIP_EVENT_API

