Get Source and Destination IP Addresses in Phy MonitorSnifferRx Trace source method

27 views
Skip to first unread message

Hassam Mughal

unread,
Mar 11, 2020, 12:31:19 PM3/11/20
to ns-3-users
Greetings everyone!

I am trying to get the source and destination IP address of the packet in my Phy MonitorSnifferRx trace by using the following method;
Ipv4Address GetSourceAddress(Ptr<const Packet> p)
{
       
Ipv4Address src;
       
// To get a header from Ptr<Packet> packet first, copy the packet
       
Ptr<Packet> q = p->Copy();
       
// Use indicator to search the packet
       
PacketMetadata::ItemIterator metadataIterator = q->BeginItem();
       
PacketMetadata::Item item;
       
while (metadataIterator.HasNext())
       
{
                item
= metadataIterator.Next();
                NS_LOG_FUNCTION
("item name: " << item.tid.GetName());
               
// If we want to have a dsdv header
               
if(item.tid.GetName() == "ns3::ArpHeader")
               
{
                       
Callback<ObjectBase *> constr = item.tid.GetConstructor();
                        NS_ASSERT
(!constr.IsNull());
                       
// Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
                       
ObjectBase *instance = constr();
                        NS_ASSERT
(instance != 0);
                       
ArpHeader* arpHeader = dynamic_cast<ArpHeader*>(instance);
                        NS_ASSERT
(arpHeader != 0);
                        arpHeader
->Deserialize(item.current);
                        src
= arpHeader->GetSourceIpv4Address();
               
}
       
}
 
return src;
}Ipv4Address GetDestAddress(Ptr<const Packet> p)
{
       
Ipv4Address dest;
       
// To get a header from Ptr<Packet> packet first, copy the packet
       
Ptr<Packet> q = p->Copy();
       
// Use indicator to search the packet
       
PacketMetadata::ItemIterator metadataIterator = q->BeginItem();
       
PacketMetadata::Item item;
       
while (metadataIterator.HasNext())
       
{
                item
= metadataIterator.Next();
                NS_LOG_FUNCTION
("item name: " << item.tid.GetName());
               
// If we want to have a dsdv header
               
if(item.tid.GetName() == "ns3::ArpHeader")
               
{
                       
Callback<ObjectBase *> constr = item.tid.GetConstructor();
                        NS_ASSERT
(!constr.IsNull());
                       
// Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
                       
ObjectBase *instance = constr();
                        NS_ASSERT
(instance != 0);
                       
ArpHeader* arpHeader = dynamic_cast<ArpHeader*>(instance);
                        NS_ASSERT
(arpHeader != 0);
                        arpHeader
->Deserialize(item.current);
                        dest
= arpHeader->GetDestinationIpv4Address();
               
}
       
}
 
return dest;
}
And in this trace source method, I am calling them.

void
Rx (std::string context, Ptr <const Packet> packet, uint16_t channelFreqMhz,  WifiTxVector txVector, MpduInfo aMpdu, SignalNoiseDbm signalNoise){
       
//context will include info about the source of this event. Use string manipulation if you want to extract info.
       
//std::cout << BOLD_CODE <<  context << END_CODE << std::endl;
       
//Print the info.
       
Packet::EnablePrinting();
               
Packet::EnableChecking();
        packet
->Print(std::cout << "\nTime: " << Simulator::Now().GetSeconds() << "s, Packet Details are: ");
       
Ipv4Address src_ip = GetSourceAddress(packet);
       
Ipv4Address des_ip = GetDestAddress(packet);    if(packet->GetSize() >= 500){
                counterAppRX
++;
       
} else {
                counterRX
++;
       
}
       
//Packet Received from: " << src_ip << ", Destination address in Packet: " << des_ip<< ",       NS_LOG_DEBUG(" Packet Received from: " << src_ip << ", Destination address in Packet: " << des_ip<< ", Size = " << packet->GetSize()
                         
<< " Freq = "<<channelFreqMhz
                         
<< " Mode = " << txVector.GetMode()
                         
<< " ReceptionDataRate = " << txVector.GetMode().GetDataRate(txVector)
                         
<< " RX Counter: " << counterRX << "\t RX APP Counter: " << counterAppRX);
        m_rxDataRate
= txVector.GetMode().GetDataRate(txVector)/1000000;        //We can also examine the WifiMacHeader
       
WifiMacHeader hdr;
       
if (packet->PeekHeader(hdr))
       
{
                NS_LOG_DEBUG
("\tDestination MAC : " << hdr.GetAddr1() << "\tSource MAC : " << hdr.GetAddr2());
       
}
}



However, I am receiving the following error in my log file as attached.
assert failed. cond="uid <= m_information.size () && uid != 0", file=../src/core/model/type-id.cc, line=458
Any guidance or help in this regard will be highly appreciated. 
Thanks 
log.out
Reply all
Reply to author
Forward
0 new messages