Understanding the packet header of packet in NS2.

Source: Internet
Author: User
All the packets in NS2. when we need to create a new packet, we call the packet: alloc method. Let's take a look at what packet: alloc has done:

Inline packet * packet: alloc () {packet * P = free _; If (P! = 0) {assert (p-> fflag _ = false); free _ = p-> next _; Assert (p-> data _ = 0 ); p-> uid _ = 0; P-> time _ = 0;} else {P = new packet; P-> bits _ = new unsigned char [hdrlen _]; if (P = 0 | p-> bits _ = 0) Abort ();} Init (p); // initialize bits _ [] (hdr_mn (p )) -> next_hop _ =-2; //-1 reserved for ip_broadcast (hdr_cen (p)-> last_hop _ =-2; //-1 reserved for ip_broadcast p-> fflag _ = true; (hdr_cen (p)-> directi On () = hdr_mn: down;/* setting all ction of Pkts to be downward as default; until Channel Changes it to + 1 (upward) */p-> next _ = 0; Return (p);} The problem occurred. Check this line for p-> bits _ = new unsigned char [hdrlen _]. where does hdrlen _ come from and where is it initialized? A search in the C ++ code found such a statement: packetheadermanager () {BIND ("hdrlen _", & Packet: hdrlen _);} the original packet :: hdrlen _ is bound to the OTCL class packetheadermanager. Continue searching in the Tcl file and find such a function: packetheadermanager instproc allochdr Cl {set size [$ Cl set hdrlen _] $ self instvar hdrlen _ set ns_align 8 # Round Up to nearest ns_align bytes # (needed on iSCSI/Solaris) set incr [expr ($ size + ($ NS_ALIGN-1 ))&~ ($ NS_ALIGN-1)] set base $ hdrlen _ incr hdrlen _ $ incr return $ base} Haha, the original packetheadermanager each added a Baotou definition, the length of this header is added up, it's no wonder that the document in NS2. it says you want to delete unnecessary header definitions !! Let's see who calls allochdr? Simulator instproc create_packetformat {}{ packetheadermanager instvar tab _ set PM [New packetheadermanager] foreach cl [packetheader info subclass] {if [info exists tab _ ($ Cl)] {set off [$ PM allochdr $ Cl] $ Cl offset $ off }}$ Self set packetmanager _ $ pm} is clear. When creating a simulator, it will add up the headers of all started data packets, so the document also says that the addition and deletion of the headers should be done before the simulator is created. The reason is here. Next, how do I access data in a specified packet header? In NS2. each different data packet must inherit a subclass from the C ++ class packetheaderclass and call the packetheaderclass: bind_offset method in the constructor to see what this function has done: inline void bind_offset (int * off) {offset _ = off;} stores an offset pointer. Generally, this offset is stored in HDR *: Offset. Packetheaderclass also implements a method called offset to read and set the offset. Int packetheaderclass: method (int ac, const char * const * AV) {TCL & TCL = TCL: instance (); int argc = ac-2; const char * const * argv = Av + 2; If (argc = 3) {If (strcmp (argv [1], "offset") = 0) {If (offset _) {* offset _ = atoi (argv [2]); Return tcl_ OK;} TCL. resultf ("Warning: cannot set Offset _ for % s", classname _); Return tcl_ OK ;}} else if (argc = 2) {If (strcmp (argv [1], "offset") = 0) {If (o Ffset _) {TCL. resultf ("% d", * offset _); Return tcl_ OK ;}} return tclclass: method (AC, AV) ;}when OTCL calls "offset? Size ?" The specified value is saved to the HDR *: Offset _ variable. Let's take a look at the above create_packetformat method. In this line: $ Cl offset $ off, we call the Offset Method of packetheaderclass to set the offset of this packet, in this way, we can access the specified header Through the header pointer and offset.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.