In most cases, NS2 extracts information from data payload and stores the information into packet header. This idea removes the necessity to process data payload at runtime. For example, instead of counting the number of bits in a packet, NS2 stores packet size in variable hdr_cmn::size_, and accesses this variable at runtime
Given a pointer to a Packet object p, the following statement sets the packet size to be "my_size"
hdr_cmn* hdr = hdr_cmn::access(p);
hdr->size() = my_size;
For more information about Packets, see Chapter 8 in the following book from Springer:
T. Issaraiyakul and E. Hossain, "Introduction to Network Simulator NS2", Springer 2008.
http://www.springer.com/engineering/signals/book/978-0-387-71759-3
You may also find the following website useful:
http://www.ece.ubc.ca/~teerawat/NS2.htm