how to add a cosum tag or header to a udp packet in packet client application?

1,667 views
Skip to first unread message

Hosein

unread,
Apr 8, 2014, 2:05:22 PM4/8/14
to ns-3-...@googlegroups.com
Hi, I install udp client application on my nodes (I am simulating Leana-simple-epc.cc example). I want to include node id and and the current position of the sender to the udp packet. as I understand, I should write a custom class and add my own tag or header to packet, now I have some question:
1- where should I add the tag or header to packets? should I change the source file of udp-client.cc and in the UdpClient::Send section which the packet is created, add the tag or header?
2- I found two class MyTag (the example "/ns-3-dev/src/network/examples/main- packet-tag.cc") and MyHeader (http://www.nsnam.org/support/faq/miscellaneous/) if I want to use them, where should I put them? is it right to put them in my simulation file before main and also in the first of udp-client.cc?

Konstantinos

unread,
Apr 8, 2014, 2:31:09 PM4/8/14
to ns-3-...@googlegroups.com


On Tuesday, April 8, 2014 7:05:22 PM UTC+1, Hosein wrote:
Hi, I install udp client application on my nodes (I am simulating Leana-simple-epc.cc example). I want to include node id and and the current position of the sender to the udp packet. as I understand, I should write a custom class and add my own tag or header to packet, now I have some question:
1- where should I add the tag or header to packets? should I change the source file of udp-client.cc and in the UdpClient::Send section which the packet is created, add the tag or header?

Yes, or create a new application if you don't want to mess with existing code.
 
2- I found two class MyTag (the example "/ns-3-dev/src/network/examples/main- packet-tag.cc") and MyHeader (http://www.nsnam.org/support/faq/miscellaneous/) if I want to use them, where should I put them? is it right to put them in my simulation file before main and also in the first of udp-client.cc?

The definition of the class (either tag or header) should be done only once, otherwise the compiler will complain. So, I would suggest to create two new files (one for .cc and one for .h) for your class and put them perhaps in the applications/model folder together with your application. Then you need to update the wsript file of that module (applications) in order to compile them. 
 

Hosein

unread,
Apr 8, 2014, 5:05:28 PM4/8/14
to ns-3-...@googlegroups.com
Thank you very much konstantinos
I created (mytag.cc and mytag.h) as you said and add these line to udp-client.cc in UdpClient::Send (void):
  Ptr <Node> node = GetNode ();
  MyTag tag;
  tag.SetSimpleValue (node->GetId());
  p->AddPacketTag (tag);
I successfully compiled the program,  at receiver I used these lines to achieve the tag but the result is not the same as the number I put in the tag at receiver!
  MyTag tag;
  aCopy->RemovePacketTag (tag);
  std::cout << tag.GetSimpleValue () << std::endl;
can u help me?

Konstantinos

unread,
Apr 8, 2014, 5:26:50 PM4/8/14
to ns-3-...@googlegroups.com
The code you have provided is not adequate to get results.
What is aCopy and how do you get it?

Hosein

unread,
Apr 8, 2014, 5:34:27 PM4/8/14
to ns-3-...@googlegroups.com
sorry about that,
I used this function to set Callback :
static void SetRxCallBack(Ptr<Node> nodeId)
{

      std::ostringstream ossRx;
      NS_LOG_INFO( "Set call back for destination " << nodeId->GetId());
      ossRx <<"/NodeList/"<< nodeId->GetId()<< "/ApplicationList/0" <<"/$ns3::PacketSink/Rx";
      Config::Connect(ossRx.str(),MakeCallback(&Rx));
}

which callback function Rx:
void Rx(std::string context,Ptr<const Packet> rxPacket,const Address &nodeAddress)
{
    Ptr<Packet> aCopy = rxPacket->Copy ();

    MyTag tag;
    aCopy->RemovePacketTag (tag);
    std::cout << tag.GetSimpleValue () << std::endl;
}

Konstantinos

unread,
Apr 8, 2014, 6:02:18 PM4/8/14
to ns-3-...@googlegroups.com
Hi, 

And what is the problem? Do you get an output? If yes, then the callback is working.
If the output is not correct, the a) problem in serialize/deserialize or b) just problem in representation since it is a unsigned integer (try to cast it into integer and check if the output is correct).

Hosein

unread,
Apr 8, 2014, 6:34:19 PM4/8/14
to ns-3-...@googlegroups.com
thanks again,
yes I get output and callback is working. but the output is not the same as in the sender, I used this to cast to integer (I am not sure it is the right way to cast to integer!) but the number I get is different than the number I put in the tag at sender!
  int xx= (int) tag.GetSimpleValue ();
so if the problem is due to serialize/deserialize, how can I fix it?
It is worth to mention that I used the
 p->RemovePacketTag (tag);
 std::cout << tag.GetSimpleValue() <<std::endl;
at sender and got the correct result, (then I commented these line)

Konstantinos

unread,
Apr 8, 2014, 6:38:39 PM4/8/14
to ns-3-...@googlegroups.com
If you have "removed" the tag at the sender, then how do you expect to read it at the receiver?

Try the PacketPeekTag instead, which does not removes it.

Hosein

unread,
Apr 8, 2014, 7:19:24 PM4/8/14
to ns-3-...@googlegroups.com
sorry I think I explain badly and you misunderstand, I removed the tag and print it at sender just  to check whether the tag contains the right number or not, then I commented (clean) that lines which remove and print the tag in the sender.
I used these line at receiver (in call back function):
aCopy->PeekPacketTag (tag);
aCopy->PrintPacketTags (std::cout);

and I got this:
address=02-06-01:00:00:02:01:c0
address=02-06-01:00:00:02:02:c0
address=02-06-01:00:00:02:01:c0
address=02-06-01:00:00:02:02:c0
address=02-06-01:00:00:02:01:c0
address=02-06-01:00:00:02:02:c0
address=02-06-01:00:00:02:01:c0
address=02-06-01:00:00:02:02:c0
..

Konstantinos

unread,
Apr 8, 2014, 7:52:27 PM4/8/14
to ns-3-...@googlegroups.com
Try to Peek the tag on the RxPacket, not the copy, in case the PacketTags are not copied properly.
If still this does not work, then you need to send us the files (attach the code) of your Tag.

Hosein

unread,
Apr 8, 2014, 8:03:32 PM4/8/14
to ns-3-...@googlegroups.com
Thank you very much for your time,
I tried with rxpacket too and it does not match the sender tag.
I attached my code (which is modified version of lena-simple-epc.cc) and mytag.cc, mytag.h and modified udp-client.cc
thanks again
mytag.cc
mytag.h
udp-client.cc
lena-simple-epc-modified.cc

Konstantinos

unread,
Apr 9, 2014, 4:37:12 AM4/9/14
to ns-3-...@googlegroups.com
If you have said from the beginning that you are working on LTE scenario, then the solution would be simpler!!!
This is due to RLC operations, the packet tag get's lost. You should use ByteTag.

To read it, use 
bool FindFirstMatchingByteTag (Tag &tag) const
 Finds the first tag matching the parameter Tag type. More...

Hosein

unread,
Apr 9, 2014, 9:44:48 AM4/9/14
to ns-3-...@googlegroups.com
thank you so much konstantinos
I did mention in my first post that I am using (lena-simple-epc.cc);
anyway, I used ByteTag and problem has been solved, I appreciated you for your kindly help.
one other question that if you can advise me, my purpose to calculate the difference between the position of the sender at the sending and reception time. so I want to attach (with tag or header) two information to packet:
1- current postion of the sender (at the transmission time)
2- node id of the sender to get the position of the sender at receiver.
with MyTag, I could send the node id, now for sending the position, can I send position with a header? if yes how? because position is a vector and each x and y is double value;
Thanks again

Konstantinos

unread,
Apr 9, 2014, 10:26:41 AM4/9/14
to ns-3-...@googlegroups.com
Hi,

If you want to use Tags, you can (de-)serialized double values (the x and y of the position). 

If you want to use header, you have to 'convert' it into uint (only unsigned integers should be used for header)

Hosein

unread,
Apr 9, 2014, 11:52:26 AM4/9/14
to ns-3-...@googlegroups.com
Thank you very very much and sorry to waste your valuable time!I wish I could compensate your kindly help;
If I want to use tag to send double values, should I change the MyTag code? (MyTag::Serialize (TagBuffer i) and MyTag::Deserialize (TagBuffer i)) e.g.  i.WriteU8 (m_simpleValue); will change to i.WriteDouble(m_simpleValue); ? if not can you explain what should I do? if yes:
1- can I add 2-3 tag from same type (MyTag) to the packet? and how can I read them at receiver?
2- should I define a variable from TagBuffer class to put the data? I tried to define it like TagBuffer tagbuffer; but I got this error:
../scratch/lena-simple-epc-itterator3.cc: In function ‘void Rx1(std::string, ns3::Ptr<const ns3::Packet>, const ns3::Address&)’:
../scratch/lena-simple-epc-itterator3.cc:104:13: error: no matching function for call to ‘ns3::TagBuffer::TagBuffer()’
   TagBuffer tagbuffer;
             ^
../scratch/lena-simple-epc-itterator3.cc:104:13: note: candidates are:
In file included from ./ns3/address.h:28:0,
                 from ./ns3/net-device.h:29,
                 from ./ns3/lte-helper.h:27,
                 from ../scratch/lena-simple-epc-itterator3.cc:21:
./ns3/tag-buffer.h:54:3: note: ns3::TagBuffer::TagBuffer(uint8_t*, uint8_t*)
   TagBuffer (uint8_t *start, uint8_t *end);
   ^
./ns3/tag-buffer.h:54:3: note:   candidate expects 2 arguments, 0 provided
./ns3/tag-buffer.h:51:7: note: ns3::TagBuffer::TagBuffer(const ns3::TagBuffer&)
 class TagBuffer
       ^
./ns3/tag-buffer.h:51:7: note:   candidate expects 1 argument, 0 provided

Thanks a lot

Konstantinos

unread,
Apr 9, 2014, 12:09:06 PM4/9/14
to ns-3-...@googlegroups.com
Yes, you should change your MyTag code.

1) Add a private variable, other than simpleValue, to hold the position information. e.g. Vector m_simplePosition;
2) Create corresponding methods to Get/Set position similar to the SimpleVariable
3) Update Serialiaze/Deserialize methods to write/read from the TagBuffer not only the 8-bits of simpleValue but also the X,Y of your position.
4) Update GetSerializedSize method which returns the number of bytes written/read from the buffer. Now it is 1, because you only write 1 byte (8bits). Should be increased to account for all the bytes.
5) Update any other methods e.g. Print to print the values.


For (3)

void MyTag::Serialize (TagBuffer i) const
{
  i
.WriteU8 (m_simpleValue);
  i
.WriteDouble (m_simplePosition.x); // this writes the X-coordinate
  i
.WriteDouble (m_simplePosition.y); // this writes the Y-coordinate
}

void MyTag::Deserialize (TagBuffer i)
{
 
double tmp_x, tmp_y;
  m_simpleValue
= i.ReadU8 ();
  tmp_x
= i.ReadDouble(); // this reads the X-coordinate in a tmp variable
  tmp_y
= i.ReadDouble(); // this reads the Y-coordinate in a tmp variable
// now we need to use these tmp variables to update the m_simplePosition
  m_simplePosition
= Vector(tmp_x, tmp_y, 0); // I assumed that you only have x,y so z=0.
}


I do not know how many nodes you want to simulate, but the simpleValue is an 8-bit, which means only 2^8 = 256 different numbers. If you have more nodes, then you need to update it. 


On Wednesday, April 9, 2014 4:52:26 PM UTC+1, Hosein wrote:
Thank you very very much and sorry to waste your valuable time!I wish I could compensate your kindly help;
If I want to use tag to send double values, should I change the MyTag code? (MyTag::Serialize (TagBuffer i) and MyTag::Deserialize (TagBuffer i)) e.g.  i.WriteU8 (m_simpleValue); will change to i.WriteDouble(m_simpleValue); ? if not can you explain what should I do? if yes:
1- can I add 2-3 tag from same type (MyTag) to the packet? and how can I read them at receiver?

You could but it is not advisable. The compiler will not complain but you would have hard time identifying which tag is what. 
 
2- should I define a variable from TagBuffer class to put the data? I tried to define it like TagBuffer tagbuffer;

No need for that. See (3) above.

Hosein

unread,
Apr 9, 2014, 1:24:43 PM4/9/14
to ns-3-...@googlegroups.com
thank you so much for your complete answer, I did what you said and update all methods in mytag and now I can successfully receive the tag with a vector value and uint8,(I send an arbitrary vector not the position in order to check tag works)  but I encounter another problem, when I want to get position of the node in udp-client.cc, to add in the tag. I add these line in UdpClient::Send (void) to get position of the node to add to the tag:
  Ptr<MobilityModel> Ueposition = node->GetObject<MobilityModel> ();
  Vector Position = Ueposition->GetPosition ();
  tag.SetPositionValue(Position);

but I got this error:
./libns3-dev-applications-debug.so: undefined reference to `ns3::MobilityModel::GetTypeId()'
./libns3-dev-applications-debug.so: undefined reference to `ns3::MobilityModel::GetPosition() const'
./libns3-dev-applications-debug.so: undefined reference to `typeinfo for ns3::MobilityModel'
collect2: error: ld returned 1 exit status
./libns3-dev-applications-debug.so: undefined reference to `ns3::MobilityModel::GetTypeId()'
./libns3-dev-applications-debug.so: undefined reference to `ns3::MobilityModel::GetPosition() const'
./libns3-dev-applications-debug.so: undefined reference to `typeinfo for ns3::MobilityModel'
collect2: error: ld returned 1 exit status
./libns3-dev-applications-debug.so: undefined reference to `ns3::MobilityModel::GetTypeId()'
./libns3-dev-applications-debug.so: undefined reference to `ns3::MobilityModel::GetPosition() const'
./libns3-dev-applications-debug.so: undefined reference to `typeinfo for ns3::MobilityModel'
collect2: error: ld returned 1 exit status


I attached my udp-client.cc,
udp-client.cc

Konstantinos

unread,
Apr 9, 2014, 1:50:01 PM4/9/14
to ns-3-...@googlegroups.com
You have to add a dependency in the applications module for the mobility model. 
It is explained here when you create a new module how you define your wscript file. 

Hosein

unread,
Apr 9, 2014, 2:15:26 PM4/9/14
to ns-3-...@googlegroups.com
If I understand right, I should only change this line in wscript of application module:
module = bld.create_ns3_module('applications', ['internet', 'config-store','stats'])
to this:
module = bld.create_ns3_module('applications', ['internet', 'config-store','stats','mobility'])
but when I did that, I got this error:
terminated with signal SIGSEGV. Run it under a debugger to get more information (./waf --run <program> --command-template="gdb --args %s <args>").
and also when I tried ./test.py some of the test has been crashed (and don't PASS)

Konstantinos

unread,
Apr 9, 2014, 3:02:38 PM4/9/14
to ns-3-...@googlegroups.com
Yes, that is correct. Only that line.
The compiler error is dismissed and now you have a run-time error. 

Most probably you are trying to access something from a null pointer. 
Reply all
Reply to author
Forward
0 new messages