Getting a packet's IP header

2,579 views
Skip to first unread message

Mike

unread,
Apr 1, 2013, 8:01:07 AM4/1/13
to ns-3-...@googlegroups.com
Hi all!

Given a packet p, I would like to get the IP (Ipv4) address of its source.
I tried simply printing the packet, but it looks like extracting the ip address from there won't be easy.
I also tried getting the packet header first and I saw a possible solution from http://polythinking.wordpress.com/2012/05/30/ns-3-network-simulator-how-to-find-a-specific-header-in-packet-in-ns-3/, but I just got errors with it starting at line 23. It says that " 'Ipv4Header' was not declared in this scope". I have no idea why. :(

Can someone please help me out? Thanks!

Konstantinos

unread,
Apr 1, 2013, 8:35:15 AM4/1/13
to ns-3-...@googlegroups.com
Hi,

In order to get the IP header (or any header) you have to make sure that the header is still there. 
If you are accessing the packet at the application layer you won't be able to extract it. 
If it is at PHY or MAC layer, you have to first remove the PHY and MAC headers respectively to get the IP header.

For the error you mention, have you included the appropriate header files? #include "ns3/internet-module.h" should work.

Regards,
Konstantinos 

Mike

unread,
Apr 1, 2013, 9:57:59 AM4/1/13
to ns-3-...@googlegroups.com
Hi, Konstantinos! Thank you very much for your reply.

Yes, it turns out I that the internet module wasn't included. Regarding the packet header, I'm  trying to get the IP header when a point-to-point net device either receives the packet, or before it sends the packet. The way I understand it, the headers are still there at those points. Please correct me if I'm wrong. 

Thanks again!

Konstantinos

unread,
Apr 1, 2013, 10:17:24 AM4/1/13
to ns-3-...@googlegroups.com
Yes, at the NetDevice (which basically implements Layer 2) the packet has the IP header.

Mike

unread,
Apr 1, 2013, 11:40:35 AM4/1/13
to ns-3-...@googlegroups.com
Hooray! Thanks again! I finally made it work! :)

zvi rad

unread,
May 7, 2013, 12:58:22 AM5/7/13
to ns-3-...@googlegroups.com

zvi rad

unread,
May 7, 2013, 12:58:35 AM5/7/13
to ns-3-...@googlegroups.com
Hi Mike and Konstantinos,
 
I am happy if you can share you code.
I am tryng to look at the queuing time of a packet in the queue. I generate TCP packets and look at the packet at the transmitting queue. Here, the packet encapsulate
the tcp ip and probabely the framing infromation.
I accutally would like to compute the FlowID that consists of the source and destination  adress and thier ports.
 
It would be great if you can instruct me how to do so.
 
Thanks,
 
Zvi

Mike

unread,
May 7, 2013, 2:24:12 AM5/7/13
to ns-3-...@googlegroups.com
Hi!

I'm sorry but I'm not really sure how to get what you want. But just in case you would also want to look at a packet's IP header, then here's a link to my code: https://www.dropbox.com/s/h2c0zzzkleekp1o/my-point-to-point-net-device.cc (see GetPacketSource method starting at line 237).

Hope this helps!

Regards,
Mike

Harini Venkat

unread,
Oct 28, 2013, 2:07:53 PM10/28/13
to ns-3-...@googlegroups.com
Hi Konstantinos,

Is there any way of accessing the packets at the application layer??

Regards,
Harini V.

Konstantinos

unread,
Oct 28, 2013, 3:24:32 PM10/28/13
to ns-3-...@googlegroups.com
What do you mean accessing at application layer? (a) Is the packet destined for that node, or (b) do you want to access a packet that is not destined for that node?

Either way, you can do it. 
For the first case, you can use one of the source applications and have access to the packet that is received
For the second case, you can use a trace source at either L2/L3 to call a function at application layer. 

lius...@gmail.com

unread,
Feb 27, 2014, 7:31:09 AM2/27/14
to ns-3-...@googlegroups.com
Hi Konstantinos, i am a newcomer on  learning ns3, recently i'm coding for getting tcp header of a packet ,and then read information in the head.But i found that in packet.cc i can't find the getheader 
function ,so i write a funtion in this file ,however ,when i build it ,it's wrong ,what can i do ,can you give me some advice ,i will appreciate it vrey much.

Tommaso Pecorella

unread,
Feb 27, 2014, 1:42:17 PM2/27/14
to ns-3-...@googlegroups.com
Hi,

the right place isn't in packet.cc. You do not need to modify it, and you shouldn't.

My strong suggestion is: before modifying the code, you have to understand it. Headers are not simply a bunch of bytes in a buffer, they're metadata dynamically attached to the packet.
Moreover, the header may be there or not, according to the layer.

Check the manual and the AddHeader / RemoveHeader function:

T.
Reply all
Reply to author
Forward
0 new messages