Search code examples
ctcplwip

TCP Server on LwIP raw API - question about tcp_close and accept callback


I'm using this simple echo-server as an example. It creates a listening connection, receives a packet, sends it back and then closes the connection.

In the initialization function, accept callback is registered in lwip like this:

void
echo_init(void)
{
    echo_pcb = tcp_new();
    ...
    echo_pcb = tcp_listen(echo_pcb);
    tcp_accept(echo_pcb, echo_accept);

Connection is closed by the server after each echo session, like this:

void
echo_close(struct tcp_pcb *tpcb, struct echo_state *es)
{
  tcp_arg(tpcb, NULL);
  tcp_sent(tpcb, NULL);
  tcp_recv(tpcb, NULL);
  tcp_err(tpcb, NULL);
  tcp_poll(tpcb, NULL, 0);

  if (es != NULL)
  {
    mem_free(es);
  }  
  tcp_close(tpcb);

Documentation says that tcp_close will free pcb structure. All of the callbacks that are used for tcp server are registered with this structure.

But when client sends new packet and starts a new connection, accept callback is called! Even though tcp_accept(echo_pcb, echo_accept); (i.e. callback registration) is done only once in the init function and that echo_pcb structure is already freed after tcp_close.

So I'm confused. Why all the other callbacks are registered multiple times but accept is registered only once? Is it okay to do it like this?


Solution

  • Okay, so according to this answer to the same question on the lwip mailing list, that is correct behaviour. tcp_accept registers callback for a port and it won't be unregistered when tcp_close is called.