Avoid casting PacketElement to ICMPHeader until type is known.

This commit is contained in:
dmiller 2026-04-22 01:01:18 +00:00
parent 0742876cca
commit a98e14f4b6

View file

@ -1126,24 +1126,28 @@ bool PacketParser::is_response(PacketElement *sent, PacketElement *rcvd){
/* So far we've verified that the ICMP error contains an IP datagram that matches
* what we sent. Now, let's find the upper layer ICMP header (skip extension
* headers until we find ICMP) */
ICMPHeader *inner_icmp=(ICMPHeader *)iperror->getNextElement();
while(inner_icmp!=NULL){
if(inner_icmp->protocol_id()==HEADER_TYPE_ICMPv4 || inner_icmp->protocol_id()==HEADER_TYPE_ICMPv6 ){
iperror = dynamic_cast<NetworkLayerElement *>(iperror->getNextElement());
while(iperror!=NULL){
if(iperror->protocol_id()==HEADER_TYPE_ICMPv4 || iperror->protocol_id()==HEADER_TYPE_ICMPv6 ){
break;
}else{
inner_icmp=(ICMPHeader *)inner_icmp->getNextElement();
iperror = dynamic_cast<NetworkLayerElement *>(iperror->getNextElement());
}
}
if(inner_icmp==NULL)
if(iperror==NULL)
return false;
/* If we get here it means that we've found an ICMP header inside the
* ICMP error message that we received. First of all, check that the
* ICMP version matches what we sent. */
if(sent_layer4->protocol_id() != inner_icmp->protocol_id())
if(sent_layer4->protocol_id() != iperror->protocol_id())
return false;
/* Make sure ICMP type and code match */
ICMPHeader *inner_icmp = dynamic_cast<ICMPHeader *>(iperror);
if (inner_icmp == NULL)
return false;
if( ((ICMPHeader*)sent_layer4)->getType() != inner_icmp->getType() )
return false;
if( ((ICMPHeader*)sent_layer4)->getCode() != inner_icmp->getCode() )