networkingheadersimulationns-3

How to get source/destination IP address of a packet in NS3 when I am using MacTx TraceSource for PointToPointNetDevice?


I am trying to simulate a fattree network in NS3. I have UdpServerHelper and UdpClientHelper to generate traffic between two hosts. Then I call

TraceConnectWithoutContext ("MacTx", MakeCallback (&SinkMethod)) 

on one of my NetDeviceContainer nodes and my SinkMethod (Ptr<const Packet> pkt) is successfully called. I can use pkt->GetSize () to see the packet size but although I took a considerable amount of time browsing for the solution, I could not figure out how I can see the source and destination addresses of the packet since the header seems to be empty.


Solution

  • The Headers on the Packets can either be Peek()ed or Remove()d. But, you can only the Peek() the top-most header, here a link-layer header, eg. PppHeader.

    To access higher-layer headers, the preceding header must be removed first. Something along these lines should do the trick:

    static void
    RxDrop(Ptr<const Packet> packet) {
        /* 
         * Need to copy packet since headers need to be removed
         * to be inspected. Alternatively, remove the headers,
         * and add them back.
         */
        Ptr<Packet> copy = packet->Copy();
    
        // Headers must be removed in the order they're present.
        PppHeader pppHeader;
        copy->RemoveHeader(pppHeader);
        Ipv4Header ipHeader;
        copy->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;
    }