Agentuino - A lightweight SNMP Agent

I'm afraid I need a little more help with the SNMP library and possibly the underlying Ethernet library...

It's been a while but I've now come back to troubleshooting / debugging my weather station sketch. The problem is that occasionally a received packet gets stuck and is being processes over and over again. I'm not sure what that packet is but it's definitely not an SNMP packet.

What I have done is I added lots and lost of serial port output to the sketch and I keep a machine running to log that output to a file. I have attached both the sketch and the part of the log file that shows the sketch running into the problem.

The problem starts at line 2170 in the log.txt. What is happening is that (for some reason) the Arduino receives an unknown packet from the same IP that usually sends the SNMP GET requests. Since it is not a valid SNMP packet, the following if statement is not true (from line 200 in the sketch):

  // check if valid packet
  if ( (pdu.type == SNMP_PDU_GET || pdu.type == SNMP_PDU_GET_NEXT || pdu.type == SNMP_PDU_SET) 
        && pdu.error == SNMP_ERR_NO_ERROR && api_status == SNMP_API_STAT_SUCCESS ) 
    {

So the sketch jumps to the following else statement (line 505)

  else 
  // packet not valid, send GENERAL_ERROR response. Required, otherwise the invalid packet 
  // will get stuck in the buffer and processed over and over again
  
  {
    Serial << "Unknown Packet!!" << endl;
    Serial << "PDU Type: " << pdu.type << " PDU Error: " << pdu.error << " API status: "<< api_status << endl; 
    Serial << "from IP: " << Agentuino.g_dstIP(0) << "." << Agentuino.g_dstIP(1) << "." << Agentuino.g_dstIP(2) << "." << Agentuino.g_dstIP(3) << endl;   
    pdu.type = SNMP_PDU_RESPONSE;
    pdu.error = SNMP_ERR_GEN_ERROR;
//    Agentuino.responsePdu(&pdu);
    Serial << "Sent 'GENERAL_ERROR' response" << endl;
  }

So far so good. However, the next time and all subsequent times I call Agentuino.listen() that same packet is still in the buffer. No further received packets are being processed.

The question is, why does this packet stay in the buffer? Is there a way to flush the buffer from my sketch (or from Agentuino) if an unknown packet is being received? I've been through all the components of the Ethernet library (Udp, Ethernet, W5100, socket) and also the Agentuino library to figure out where and how the packet buffer is released but I couldn't find it.

BTW, the method Agentuino.g_dstIP() is one I added to the Agentuino library in order to get the IP address where the packet came from in an attempt to determine what the nature of the offending packets is.

Weather_Station.pde (20.4 KB)

log.txt (51.4 KB)