How to write data in payload ?

1,322 views
Skip to first unread message

Ali Marandi

unread,
Jun 12, 2011, 5:44:10 AM6/12/11
to ns-3-users
Hello,

I have a very basic question. I have created a packet as follows:

uint32_t payloadSize = 1000;
Ptr<Packet> p = Create<Packet> (payloadSize);

Then I have added a 25 bytes header to the above packet (My purpose
was to write 25 bytes in payload). Now when I look in the pcap file,
the packet size is 1025. If the proper way of writing in payload is
to serialize whatever you want to write in payload in the header, so
why the 25 bytes is added (prepended) to the payload ??. What should I
do for writing in the 1000 bytes payload ??

Thank you in advance,
Ali.

phuchong kheawchaoom

unread,
Jun 13, 2011, 12:54:49 AM6/13/11
to ns-3-...@googlegroups.com
stringstream msg;

msg << " your pay load";
Ptr<Packet> p = Create<Packet> ((uint8_t*) msg.str().c_str(), msg.str().length());


--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To post to this group, send email to ns-3-...@googlegroups.com.
To unsubscribe from this group, send email to ns-3-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ns-3-users?hl=en.


Ali Marandi

unread,
Jun 13, 2011, 2:22:17 AM6/13/11
to ns-3-users
Thank you Phuchong, for your reply.
Actually I have seen this approach in one of tutorials. Assume I want
to write data in the payload after I create my packet, what should I
do ?. Is it possible to write some other data type in the payload or
we should convert any type to string and then write it in payload ?.
How can we edit whatever we wrote before in the payload ?.

Best Regards,
Ali.

---
On Jun 13, 8:54 am, phuchong kheawchaoom <phuchon...@gmail.com> wrote:
> stringstream msg;
>
> msg << " your pay load";
> Ptr<Packet> p = Create<Packet> ((uint8_t*) msg.str().c_str(),
> msg.str().length());
>

Andrea Ranieri

unread,
Jun 13, 2011, 2:58:41 AM6/13/11
to ns-3-...@googlegroups.com
On Mon, Jun 13, 2011 at 08:22, Ali Marandi <marand...@gmail.com> wrote:
> Thank you Phuchong, for your reply.
> Actually I have seen this approach in one of tutorials. Assume I want
> to write data in the payload after I create my packet, what should I
> do ?. Is it possible to write some other data type in the payload or
> we should convert any type to string and then write it in payload ?.
> How can we edit whatever we wrote before in the payload ?.

I think that the standard way is to re-read the packet payload with
Packet::CopyData (uint8_t *buffer, uint32_t size) and then write it
back to a new packet as you did before. The non-standard way,
conversely, is to use uint8_t const * Packet::PeekData (void) to get a
pointer to the internal buffer of the packet and then directly write
into it. Less clean but you avoid a deep-copy operation.

Regards
Andrea

Ali Marandi

unread,
Jun 13, 2011, 3:28:16 AM6/13/11
to ns-3-users


On Jun 13, 10:58 am, Andrea Ranieri <andre...@gmail.com> wrote:
> I think that the standard way is to re-read the packet payload with
> Packet::CopyData (uint8_t *buffer, uint32_t size) and then write it
> back to a new packet as you did before. The non-standard way,
> conversely, is to use uint8_t const * Packet::PeekData (void) to get a
> pointer to the internal buffer of the packet and then directly write
> into it. Less clean but you avoid a deep-copy operation.

Many thanks Andrea :-).
Is it possible to write other data types in the payload (uint32_t,
uint64_t, etc...) or we should convert any data type to string and
then write it in payload ?. Can you please give me an example ?

Thanks,
Ali

Andrea Ranieri

unread,
Jun 13, 2011, 4:09:10 AM6/13/11
to ns-3-...@googlegroups.com
On Mon, Jun 13, 2011 at 09:28, Ali Marandi <marand...@gmail.com> wrote:
>
> Many thanks Andrea :-).

Glad to be able to help :)

> Is it possible to write other data types in the payload (uint32_t,
> uint64_t, etc...) or we should convert any data type to string and
> then write it in payload ?. Can you please give me an example ?

I strongly suggest to you to implement your own "application packet"
class with its serialize and deserialize methods, and derive from it
further packet classes as needed. For example, as base class I use an
"overlay packet" such as below.

class OvlPacket
{
public:

OvlPacket ();
OvlPacket (const OvlPacket &a);
virtual ~OvlPacket ();

virtual OvlPacket & operator = (const OvlPacket &ref);

virtual void serialize (std::string &str) const;
virtual bool deserialize (const std::string &str);

virtual void print () const;

private:

void initState ();

static const std::string sep;

std::string header;
std::string id;
std::string ipaddr;
std::string counter;
std::string time;
};

I used strings for my datatypes because I'm a very lazy programmer,
but using the proper datatype and convert them to string in the
serialize method, should be the right thing to do.

Hope this helps
Andrea

Reply all
Reply to author
Forward
0 new messages