Hi, how to get the right header from packet?

1,839 views
Skip to first unread message

Jin L

unread,
Aug 19, 2016, 12:06:01 PM8/19/16
to ns-3-users
Hi, I'm new to use NS-3 Simulator. And I've met some problems that I can't solve. So here is the problem:

I create a small topology of network with some nodes, and I install for every node a sink using SinkApplication and a source using OnOffApplication. For time being, the source from every node will send packets to every other nodes using TCP. My target is to trace the packet, from which node flow the packet to another nodes. So from my trace output I can get the information that packet did come from some nodes to another nodes, but when I try to extract header from the packets, the ip address is uninitialized address 102.102.102.102. But when I enable the Print header function, I do get metadata of header and tell from which ip flows to which ip. So my problem is why I can't get correct information from Peek header function just as Print header?


The Trace Source looks like this:
AsciiTraceHelper ascii;
Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("simulation_received");
std::ostringstream oss;
oss<<"/NodeList/*/DeviceList/*/$ns3::PointToPointNetDevice/PhyRxEnd";
Config::Connect(oss.str(),MakeBoundCallback(&PacketReceived,stream));

And I hook with this function to get Output

static void
PacketReceived (Ptr<OutputStreamWrapper> stream, std::string context, Ptr< const Packet > p)
{
Ipv4Header header;
p->PeekHeader(header);

p->Print(*stream->GetStream());
*stream->GetStream()<<std::endl;

*stream->GetStream ()<<"Sent from "<<header.GetSource()<<" Received by "<<header.GetDestination()<<" at "<<Simulator::Now().GetSeconds()<<std::endl;
}


So, from print header I do get what i need(but with a lot of other information that I don't need), but from PeekHeader, I only get 102.102.102.102. So what's the problem with my code?

The output I get is like this:
ns3::PppHeader (Point-to-Point Protocol: IP (0x0021)) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 0 protocol 6 offset (bytes) 0 flags [none] length: 56 10.0.147.2 > 10.0.105.1) ns3::TcpHeader (49153 > 9 [SYN] Seq=0 Ack=0 Win=32768 ns3::TcpOptionWinScale(2) ns3::TcpOptionTS(2000;0) ns3::TcpOptionEnd(EOL))
Sent from 102.102.102.102 Received by 102.102.102.102 at 2.00229

Konstantinos

unread,
Aug 19, 2016, 12:19:02 PM8/19/16
to ns-3-users
Hi,

The packet you are using in your trace is at Phy layer.
There is also MAC header (the PPP header as reported in your output) and after that you can find the IP header you try to read.

So, when you try to Peek the IP header, you are actually reading the contents from the PPP header which does not initialize the IP header correctly (hence the 102.102.102.102 which is the default value). 
You need to remove the PPP header and then read the IP.

Jin L

unread,
Aug 19, 2016, 1:58:35 PM8/19/16
to ns-3-users
Hi, thank you for your reply. Now I get at least a little bit clue what's going on. But I still have some questions on it.

1. If I want to directly get the ip header, which layer/which component should I observe(as trace source)?

2. I know that there is different headers for different layers, but I don't know how they represent themselves in NS3. Like PPP header, Ipv4Header, some other headers, etc.  How can I get information of available headers in NS3? Is there any literature or website has introduction about it? I usually look at Doxygen, but I can only get what functions I have in one particular header(only if I knew or guess right about the header name) but not the "big picture" of those headers.


Konstantinos於 2016年8月19日星期五 UTC+2下午6時19分02秒寫道:

Konstantinos

unread,
Aug 19, 2016, 4:09:25 PM8/19/16
to ns-3-users
See replies inline


On Friday, August 19, 2016 at 6:58:35 PM UTC+1, Jin L wrote:
Hi, thank you for your reply. Now I get at least a little bit clue what's going on. But I still have some questions on it.

1. If I want to directly get the ip header, which layer/which component should I observe(as trace source)?


At the network layer, e.g. Ipv4L3Protocol traces.
 
2. I know that there is different headers for different layers, but I don't know how they represent themselves in NS3. Like PPP header, Ipv4Header, some other headers, etc.  How can I get information of available headers in NS3? Is there any literature or website has introduction about it? I usually look at Doxygen, but I can only get what functions I have in one particular header(only if I knew or guess right about the header name) but not the "big picture" of those headers.


The ns-3 manual/tutorial should be a good starting point. For example the Internet section https://www.nsnam.org/docs/models/html/internet-models.html 
The implementation follows the real implementation of standards, which means that you have headers for transport (TCP/UDP), network (IP) and MAC layers (depends on NetDevice). For general knowledge about that, any good communications book (e.g. Computer Networks by Tanenbaum).

yli...@asu.edu

unread,
Sep 2, 2017, 3:19:49 AM9/2/17
to ns-3-users
Hi Konstantinos,

Is there anyway that I can use Python to read the packet header? I tried the Print method, the system gave me the error message "AttributeError: 'network.Packet' object has no attribute 'Print' "

Thanks!
Reply all
Reply to author
Forward
Message has been deleted
0 new messages