Printing Source and Destination IP address from Packet

131 views
Skip to first unread message

azeez

unread,
Feb 5, 2024, 5:47:42 AM2/5/24
to ns-3-users
Hi all,

I am currently working on printing source and destination IP address from packet header.
I have a few questions and would appreciate your insights:
  1. What is the difference between PppHeader & Ipv4Header?
  2. How to retrieve the IP addresses of source and destination?
  3. Why when I am using the following code I receive (102.102.102.102)? And how to fix it to print the actual IP addresses?

Here is the snippet of my code:
void
MonitorSniffRx(Ptr<const Packet> packet,
               uint16_t channelFreqMhz,
               WifiTxVector txVector,
               MpduInfo aMpdu,
               SignalNoiseDbm signalNoise,
               uint16_t staId)
{
   packet->Print(std::cout);    
    Ptr<Packet> copy_of_packet = packet->Copy();
    Ipv4Header ipHeader;
   
    copy_of_packet->RemoveHeader(ipHeader);

    std::cout << "Source IP: ";
    ipHeader.GetSource().Print(std::cout);
    std::cout << std::endl;

    std::cout << "Destination IP: ";
    ipHeader.GetDestination().Print(std::cout);
    std::cout << std::endl;
}


Thanks in advance!

Tommaso Pecorella

unread,
Feb 5, 2024, 8:54:43 AM2/5/24
to ns-3-users
The PppHeader is a header used in PointToPoint networks /typically) by the PPP protocol, which is a L2.5 protocol (i.e., a protocol to adapt the underlying network to IP).
PPP was used (again, typically) in dial-up networks or point-to-point networks, and it provides various features. See https://datatracker.ietf.org/doc/html/rfc1661

The IP header is... the IP header.

If the network is IP and it also uses PPP, in the packet you'll find first the PPP header, then the IP header.

Your code doesn't work because in ns-3 you have to remove the headers like in a stack. You can't remove a header before removing the headers in front of it. Since, most likely, your function is being called by a NetDevice callback, you'll have to remove the right headers before arriving at the IP one. Which headers... no idea.

Now, if there are no errors in the code, RemoveHeader should return zero if the header wasn't removed, for example because it's not there. However there are caveats - some headers might be mistaken for others. Hence, it's always a good idea to start with what YOU know about the packet, and proceed to a manual deserialisation, but I guess that this is beyond what you want to do.

About why you have 102.102.102.102, that's the IP address that is returned when the header is not removed correctly: it reverts the address to its default value (which we decided it's 102.102.102.102 to indicate a non-initialised address).

How you fix it?
That's the neat part: you don't (easily). The easiest way (but also the slower) is to leverage the packet printing:
- Use Packet::EnablePrinting () at the beginning of the script
- Print the packet to a std::string
- Parse the string

Beware: it's slow as hell.
Reply all
Reply to author
Forward
0 new messages