next up previous contents index
Next: 11.1.1 Adding a New Up: 11. Packet Headers and Previous: 11. Packet Headers and

   
11.1 A Protocol-Specific Packet Header

Protocol developers will often wish to provide a specific header type to be used in packets. Doing so allows a new protocol implementation to avoid overloading already-existing header fields. We consider a simplified version of RTP as an example. The RTP header will require a sequence number fields and a source identifier field. The following classes create the needed header (see rtp.h and rtp.cc):

{\rm From rtp.h:}
        /* rtp packet.  For now, just have srcid + seqno. */
        struct hdr_rtp { 
                u_int32_t srcid_;
                int seqno_;
                /* per-field member functions  */
                u_int32_t& srcid() { return (srcid_); }
                int& seqno() { return (seqno_); }

                /* Packet header access functions */
                static int offset_;
                inline static int& offset() { return offset_; }
                inline static hdr_rtp* access(Packet* p) {
                        return (hdr_rtp*) p-\>access(offset_);
                }
        };

{\rm From rtp.cc:}

        class RTPAgent: public Agent {
                \ldots
                int off_rtp_;
        };

        class RTPHeaderClass : public PacketHeaderClass {
        public: 
                RTPHeaderClass() : PacketHeaderClass("PacketHeader/RTP",
                                                     sizeof(hdr_rtp)) {
                        bind_offset(&hdr_rtp::offset_);
                }
        } class_rtphdr;

        void RTPAgent::sendpkt()
        {
                Packet* p = allocpkt();
                hdr_rtp *rh = hdr_rtp::access(p);
                lastpkttime_ = Scheduler::instance().clock();

                /* Fill in srcid_ and seqno */
                rh-\>seqno() = seqno_++;
                rh-\>srcid() = session_-\>srcid();
                target_-\>recv(p, 0);
        }

        RTPAgent::RTPAgent()
                : session_(0), lastpkttime_(-1e6)
        {
                type_ = PT_RTP;
                bind("seqno_", &seqno_);
        }
The first structure, hdr_rtp, defines the layout of the RTP packet header (in terms of words and their placement): which fields are needed and how big they are. This structure definition is only used by the compiler to compute byte offsets of fields; no objects of this structure type are ever directly allocated. The structure also provides member functions which in turn provide a layer of data hiding for objects wishing to read or modify header fields of packets. Note that the static class variable offset_ is used to find the byte offset at which the rtp header is located in an arbitrary packet. Two methods are provided to utilize this variable to access this header in any packet: offset() and access(). The latter is what most users should choose to access this particular header in a packet; the former is used by the packet header management class and should seldom be used. For example, to access the RTP packet header in a packet pointed by p, one simply says hdr_rtp::access(p). The actual binding of offset_ to the position of this header in a packet is done by routines inside tcl/lib/ns-packet.tcl and packet.cc.

IMPORTANT: Notice that this is completely different from the original method to access a packet header, which requires that an integer variable, off_hdrname_, be defined for any packet header that one needs to access. This is now an obsolete method; its usage is tricky and its misuse can be very difficult to detect.

The static object class_rtphdr of RTPHeaderClass../ns-2/rtp.cc is used to provide linkage to OTcl when the RTP header is enabled at configuration time. When the simulator executes, this static object calls the PacketHeaderClass constructor with arguments "PacketHeader/RTP" and sizeof(hdr_rtp). This causes the size of the RTP header to be stored and made available to the packet header manager at configuration time (see below, Section 11.2.4). Notice that bind_offset() MUST be called in the constructor of this class, so that the packet header manager knows where to store the offset for this particular packet header.

The sample member function []sendpkt method of RTPAgent creates a new packet to send by calling []allocpkt, which handles assignment of all the network-layer packet header fields (in this case, IP). Headers other than IP are handled separately. In this case, the agent uses the RTPHeader defined above. The Packet::access member function returns the address of the first byte in a buffer used to hold header information (see below). Its return value is cast as a pointer to the header of interest, after which member functions of the RTPHeader object are used to access individual fields.



 
next up previous contents index
Next: 11.1.1 Adding a New Up: 11. Packet Headers and Previous: 11. Packet Headers and

2000-08-24