0

I am using stm32f4+lwip 2.0.3 version on my application, I was using IWDG timer on my code, I detect when I receive lots of Modbus-TCP packets with different connections. I get reset every time. Than I face with the below code blocks cause that. In the tcp_in.c file on the lwip library.

My master sender send different Modbus TCP requests different ports but when the before communication ports closed.

I found the problem caused this line

if (pcb->remote_port == tcphdr->src &&
            pcb->local_port == tcphdr->dest &&

because sometimes pcb->remote_port not equals to tcphdr->src. So I stuck between for loops so IWDG timer triggered.

I get this ouputs when I debug:

tcp_input: active pcb->state != CLOSED
tcp_input: active pcb->state != TIME-WAIT
tcp_input: active pcb->state != LISTEN
State: 9 63226 502 63236 502
tcp_input: active pcb->state != CLOSED
tcp_input: active pcb->state != TIME-WAIT
tcp_input: active pcb->state != LISTEN
State: 9 63226 502 63236 502
tcp_input: active pcb->state != CLOSED
tcp_input: active pcb->state != TIME-WAIT
tcp_input: active pcb->state != LISTEN

port 63226 pbc->remote_port and this is the last successfull modbus communication port but the sender side, send different packets. As you can see the wireshark :

https://www.hizliresim.com/m3pkg8y

Why this happened ? why the before port does not close successfuly ?

Below codes in tcp_in.c and in the tcp_input() function.

   for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
        LWIP_ASSERT("tcp_input: active pcb->state != CLOSED", pcb->state != CLOSED);
        LWIP_ASSERT("tcp_input: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
        LWIP_ASSERT("tcp_input: active pcb->state != LISTEN", pcb->state != LISTEN);
        
        SerialPrint("State: %d %d %d %d %d\n",pcb->state, pcb->remote_port,pcb->local_port,tcphdr->src,tcphdr->dest);
        if (pcb->remote_port == tcphdr->src &&
            pcb->local_port == tcphdr->dest &&
            ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()) &&
            ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
          /* Move this PCB to the front of the list so that subsequent
             lookups will be faster (we exploit locality in TCP segment
             arrivals). */
          LWIP_ASSERT("tcp_input: pcb->next != pcb (before cache)", pcb->next != pcb);
          if (prev != NULL) {
            prev->next = pcb->next;
            pcb->next = tcp_active_pcbs;
            tcp_active_pcbs = pcb;
          } else {
            TCP_STATS_INC(tcp.cachehit);
          }
          LWIP_ASSERT("tcp_input: pcb->next != pcb (after cache)", pcb->next != pcb);
          break;
        }
        prev = pcb;
      }
gogogo
  • 283
  • 1
  • 12

0 Answers0