Read Data in packet (buffer) at Ipv4L3Protocol

408 views
Skip to first unread message

Shital Kothari

unread,
Jul 30, 2016, 7:18:16 AM7/30/16
to ns-3-users
Hello NS3 Users,

I used SetFill method of UdpEchoClientHelper to Send "Hello World" along with packet,

UdpEchoClientHelper client (Address(i1.GetAddress (0)), UDPport);
apps
= client.Install (SourceNode);
apps
.Start (Seconds (0.5));
apps
.Stop (Seconds (10.0));
client
.SetFill (apps.Get (0), "Hello World");

I tried to read same data from IPV4-L3-Protocol:: Receive function,

uint8_t *buffer = new uint8_t[packet->GetSize ()];
packet->CopyData(buffer, packet->GetSize ());
// std::string s = std::string((char*)buffer);
char temp[packet->GetSize()];
memcpy(temp,buffer,packet->GetSize());
NS_LOG_LOGIC("Received Data:"<<temp);

It shows output:

Received Data:E

1. How can I differentiate packets? Packets having data added by me and other control packets for routing protocol?
2. How can I use data written in packet at Application layer, in Ipv4L3Protocol/ RoutingProtocol?

Thanking You,
---
Shital Kothari

Tommaso Pecorella

unread,
Jul 30, 2016, 10:17:35 AM7/30/16
to ns-3-users
Hi,

most probably you forgot to remove an header. Moreover, remember that the stuff you write in the packet payload is not null-terminated (unless you specify it).
About the second question, it is too generic to provide an answer.

Cheers,

T.

Shital Kothari

unread,
Jul 30, 2016, 11:29:34 AM7/30/16
to ns-3-users
Hello Tommaso,

Thank you for reply! Your reply helped me lot! 
I appended '\0' to stream as follows,

      UdpEchoClientHelper client (Address(i1.GetAddress (0)), UDPport);

     
//client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
     
// client.SetAttribute ("Interval", TimeValue (interPacketInterval));
      client
.SetAttribute ("PacketSize", UintegerValue (packetSize));



      apps
= client.Install (SourceNode);
      apps
.Start (Seconds (0.5));
      apps
.Stop (Seconds (10.0));



      std
::ostringstream msg; msg << "Hello World!" << '\0';


      client
.SetFill (apps.Get (0), (uint8_t*) msg.str().c_str(), msg.str().length(),msg.str().length());

I really forgotten to remove an header. But, I can't find best location to put this code, 
If I put this code in IpvL3Protocol::Receive function then it will try to extract data out of every packet it receives.
Please help me to relocate following LOC:

  packet->RemoveHeader (ipHeader);

  packet
->RemoveAllPacketTags();

  uint8_t
*buffer = new uint8_t[packet->GetSize ()];
  packet
->CopyData(buffer, packet->GetSize ());
 
// std::string s = std::string((char*)buffer);
 
char temp[packet->GetSize()];
  memcpy
(temp,buffer,packet->GetSize());
  NS_LOG_LOGIC
("Received Data:"<<temp);

Thanking You,
---
Shital Kothari

Konstantinos

unread,
Jul 31, 2016, 4:41:54 AM7/31/16
to ns-3-users
Hi Shital,

There is a major problem in you code, related to the order of the method calling.
Particularly when you set-up the SetFill().

You have:

UdpEchoClientHelper client (Address(i1.GetAddress (0)), UDPport);
apps 
= client.Install (SourceNode);
apps
.Start (Seconds (0.5));
apps
.Stop (Seconds (10.0));
client
.SetFill (apps.Get (0), "Hello World");

But the application (client) is installed on the node on the second line. Anything related to the 'client' after calling Install() will not have an impact.
So, try moving the SetFill before the Install() and reply back with if that solves the problem.

As an easy way to verify what you have on the packet (headers, content etc) you can call Print() and see the output. Just remember to enable metadata printing.

Regards,
K.

Shital Kothari

unread,
Jul 31, 2016, 5:32:27 AM7/31/16
to ns-3-users
Hello Konstantinos,

I tried the same, but it gives error:

UdpEchoClientApplication:SetFill(0, Hello World!, 13, 13)


Program received signal SIGSEGV, Segmentation fault.
0x05f7121d in ns3::UdpEchoClient::SetFill (this=0x0, fill=
   
0x8136614 "Hello World!", fillSize=13, dataSize=13)
    at
../src/applications/model/udp-echo-client.cc:234
234   if (dataSize != m_dataSize)
Missing separate debuginfos, use: debuginfo-install atk-1.30.0-1.fc13.i686 cairo-1.8.10-1.fc13.i686 expat-2.0.1-10.fc13.i686 fontconfig-2.8.0-1.fc13.i686 freetype-2.3.11-3.fc13.i686 glib2-2.24.1-1.fc13.i686 glibc-2.12-1.i686 gsl-1.13-2.fc13.i686 gtk2-2.20.1-1.fc13.i686 libX11-1.3.1-3.fc13.i686 libXau-1.0.5-1.fc12.i686 libXcomposite-0.4.1-2.fc13.i686 libXcursor-1.1.10-4.fc13.i686 libXdamage-1.1.2-2.fc13.i686 libXext-1.1-2.fc13.i686 libXfixes-4.0.4-2.fc13.i686 libXi-1.3-2.fc13.i686 libXinerama-1.1-2.fc13.i686 libXrandr-1.3.0-5.fc13.i686 libXrender-0.9.5-1.fc13.i686 libgcc-4.4.5-2.fc13.i686 libpng-1.2.43-1.fc13.i686 libselinux-2.0.90-5.fc13.i686 libstdc++-4.4.5-2.fc13.i686 libxcb-1.5-1.fc13.i686 libxml2-2.7.7-2.fc13.i686 pango-1.28.0-1.fc13.i686 pixman-0.18.0-1.fc13.i686 sqlite-3.6.22-1.fc13.i686 zlib-1.2.3-23.fc12.i686

I think,

apps.get(0) will be null if we use SetFill before install.

---

Konstantinos

unread,
Jul 31, 2016, 6:56:16 AM7/31/16
to ns-3-users
My suggestion is just to move the call few lines above like this

original 

UdpEchoClientHelper client (Address(i1.GetAddress (0)), UDPport);
apps 
= client.Install (SourceNode);
apps
.Start (Seconds (0.5));
apps
.Stop (Seconds (10.0));
client
.SetFill (apps.Get (0), "Hello World");

modified

UdpEchoClientHelper client (Address(i1.GetAddress (0)), UDPport);
client.SetFill (apps.Get (0), "Hello World");
apps = client.Install (SourceNode);
apps
.Start (Seconds (0.5));
apps
.Stop (Seconds (10.0));


Shital Kothari

unread,
Jul 31, 2016, 7:08:37 AM7/31/16
to ns-3-users
After running modified code,

apps.Get(0) remains null [at second line of code]

&
Throws segmentation fault.

at third line [in modified] it will initialize apps after calling Install method.

Shital Kothari

unread,
Jul 31, 2016, 7:53:38 AM7/31/16
to ns-3-users
Thank you NS3 Users!!!!

I forgotten to remove UDP Header also...

Solution to Insert Data Along with Packet ->

  UdpEchoClientHelper client (Address(i1.GetAddress (0)), UDPport);
     
//client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
     
// client.SetAttribute ("Interval", TimeValue (interPacketInterval));


      std
::ostringstream msg; msg << "Hello World!" << '\0';
      client
.SetAttribute ("PacketSize", UintegerValue (msg.str().length()));
     
// Ptr packet = Create<Packet> (, );


      apps
= client.Install (SourceNode);
      apps
.Start (Seconds (0.5));
      apps
.Stop (Seconds (10.0));


     client
.SetFill (apps.Get (0), (uint8_t*) msg.str().c_str(), msg.str().length(), msg.str().length());



& in Ipv4-l3-Protocol.cc 

I written following code in Send function [case 3]

UdpHeader udpHeader;
packet
->RemoveHeader(udpHeader);

packet
->EnablePrinting();
packet
->Print(std::cout);

uint8_t
*buffer = new uint8_t[packet->GetSize ()];
packet
->CopyData(buffer, packet->GetSize ());

std
::string s = std::string((char*)buffer);
char temp[packet->GetSize()];
memcpy
(temp,buffer,packet->GetSize());
NS_LOG_LOGIC
("Received Data:"<<temp);

NS_LOG_LOGIC
("Printing Packet case3");
packet
->AddHeader(udpHeader);
SendRealOut (route, packet->Copy (), ipHeader);

Thank you Tommaso & Konstantinos for giving me right directions & spending your precious time to resolve my query!
---
Shital Kothari
Reply all
Reply to author
Forward
0 new messages