Print out source and destination IP address of a packet

749 views
Skip to first unread message

stornian

unread,
Aug 8, 2018, 5:18:15 AM8/8/18
to ns-3-users

Dear fellow ns3 users,

I am using the UdpEchoClient and UdpEchoServer applications and wanted to modify the NS_LOG_INFOs slightly, so that instead of “At time t, a packet with x bytes was send/received from/to y” it prints the details of the IP header (e.g. source and destination IP) as well

So I added the following lines to the send and receive method of my client, thinking they should do the trick (based on https://www.nsnam.org/wiki/HOWTO_extract_the_IP_src/dst_address_from_a_packet )

Ptr<Packet> copy = p->Copy ();

Ipv4Header iph;

copy->PeekHeader (iph);

NS_LOG_INFO ("This is the IP header:" << iph);

 

If I run myfirst.cc using this slightly modified UdpEcho client and server, the header that is printed does not provide anything useful:


I tried doing the same with the original packet (instead of using the copy) and tried PeekHeader instead of Remove, but the result was always the same. Can someone explain why the header looks like this?

From this post https://groups.google.com/forum/#!topic/ns-3-users/EtiGaGvh3YQ I found that this address is shown if the address is not initialized.

But why should the packet not be initialized in my case? The packets travel successfully from client to server, how should that be possible if they were empty. I did not change anything else in the code of the UpdEcho applications, and I know if I create pcap files, the packets I see are correct.


I am a beginner with ns3 and I feel like I must have misunderstood something here, and even though I have worked through the tutorial in detail and have searched the ns3 forum for a while now, I can’t resolve the issue on my own. So if anyone could explain my mistake to me, or at least point me to the right direction in order to understand I would be very grateful.

I considered using Ns3::Packet::Print(std::istream &os) const instead, but it only prints “Payload (size=1024)” and I am not sure if this method can even provide source and destination IP.


Best Regards

stornian

unread,
Aug 20, 2018, 12:42:58 PM8/20/18
to ns-3-users
Ok hi everyone,

since I could not find a solution to why the IP header is empty I ended up doing the following to print out the IP of the node where the application is currently executed:

this->GetNode()->GetObject<Ipv4>()->GetAddress(1,0).GetLocal();

I added this to the NS_LOG_INFO that is already there in the UDP echo application, so now I have both sender and receiver IP adress as console output

jared...@gmail.com

unread,
Aug 21, 2018, 1:52:44 AM8/21/18
to ns-3-users
Stornian,

I don't know for sure, but my guess is that the ip header is not making it to that point in the simulated stack.  The UdpEchoClient (or Server) is more of an application, which might be seeing a packet without the headers.  I looked for a few minutes and found that the packet seems to travel up to the app through the function in UdpL4Protocol containing this line https://www.nsnam.org/doxygen/udp-l4-protocol_8cc_source.html#l00366.  That might explain why you can't see the header info.

Good luck,
Jared.
Reply all
Reply to author
Forward
0 new messages