user3243499
user3243499

Reputation: 3151

cPacket causing simulation crash in OMNet++

I have defined 2 cPackets msg files as:

packet MyLinkFrame {
    int f1;
    int f2;
}

and

packet IPv4ARPTotf {
    string SourceIP;
    string DestinationIP;
}

Then in my simulation, one network node receives the linklayer frame MyLinkFrame is received which encapsulates IPv4ARPTotf.

Then, on the receiving network node I create a new ARP Reply packet of IPv4ARPTotf type as follows:

if(check_for_validity(static_cast<IPv4ARPTotf *>(MyLinkFrame->decapsulate())))
{ 
    if (MyLinkFrame->getFrameType() == 0) {
       tempIPARPRequest = static_cast<IPv4ARPTotf *>(MyLinkFrame->decapsulate());
       tempIPARPReply = new IPv4ARPTotf("IPv4ARPReply");
       tempIPARPReply->setSourceIP(tempIPARPRequest->getDestinationIP());
       tempIPARPReply->setDestinationIP(tempIPARPRequest->getSourceIP());
    } 
} 

Now it is crashing for lines inside the inner if block.

Upvotes: 1

Views: 132

Answers (1)

Utkal Sinha
Utkal Sinha

Reputation: 1041

You are decapsulating the received frame two times.

Once in the function call of check_for_validity() and another inside the if block whenever the if condition passes. So inside the if block when you try to decapsulate() it again then a null pointer is returned. Hence, when you try to access parameters tempIPARPRequest->getDestinationIP() of this null object you get an exception and your simulation crashes.

An easy fix would be to pass a duplicate object to your check_for_validity() function and optionally delete the dulicated msg inside that function.

Sample modification of your supplied code:

if(check_for_validity(static_cast<IPv4ARPTotf *>(MyLinkFrame->dup()->decapsulate())))
{ 
    if (MyLinkFrame->getFrameType() == 0) {
       tempIPARPRequest = static_cast<IPv4ARPTotf *>(MyLinkFrame->decapsulate());
       tempIPARPReply = new IPv4ARPTotf("IPv4ARPReply");
       tempIPARPReply->setSourceIP(tempIPARPRequest->getDestinationIP());
       tempIPARPReply->setDestinationIP(tempIPARPRequest->getSourceIP());
    } 
}

Upvotes: 3

Related Questions