Two tcp pcbs listening on same IP address and port

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Two tcp pcbs listening on same IP address and port

Dado Admir
Hi all,

We're using lwip on an embedded device with two Ethernet ports and two tcp server applications running on it. The applications are bound to specific Ethernet port, and this is done by using tcp_bind_netif function. They are also bound to local ip addresses (address assigned to netif at the moment) and local port by using tcp_bind function. This setup works fine in all cases except when we have two same ip addresses assigned to two netifs. In this case, we are getting ERR_USE when trying to bind the second tcp to <IP address, port> pair (SO_REUSE option is enabled).

We managed to get the code working by adding few lines to the lwip tcp code in parts where there are checks for address/port reuse (tcp_bind, tcp_listen and tcp_input functions). We added checks for assigned netif index for every tcp pcb.

For example, in function tcp_listen_with_backlog, the condition is changed from:

      if ((lpcb->local_port == pcb->local_port) &&
          ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) {
        /* this address/port is already used */
      if ((lpcb->local_port == pcb->local_port) &&
          ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip) &&
          (lpcb->netif_idx == pcb->netif_idx)) {
        /* this address/port is already used */

Similar changes are in the other two functions. 

The idea is to make listening tcp defined by <IP address, port,  netif_idx> instead of only <IP address, port>.  This would be valid only for pcbs on which tcp_bind_netif was previously called. 

The questions are, would this change make sense for lwip and does it violate any TCP rules? Same questions for UDP. 

We're using lwip version 2.1.2.

Thank you

lwip-devel mailing list
[hidden email]