hello everyone
  i have introduced a new packet checkloop in maodv but when i run a tcl script
  regarding maodv getting error
  format_aodv:invalid AODV packet type
  also i have done a little change in mact pkt. format.
  i m attaching two files related to it.
  please help me
  regards
  mahi

       
---------------------------------
 Check out what you're missing if you're not on Yahoo! Messenger 
/*
Copyright (c) 1997, 1998 Carnegie Mellon University.  All Rights
Reserved. 

Permission to use, copy, modify, and distribute this
software and its documentation is hereby granted (including for
commercial or for-profit use), provided that both the copyright notice and this 
permission notice appear in all copies of the software, derivative works, or 
modified versions, and any portions thereof, and that both notices appear in 
supporting documentation, and that credit is given to Carnegie Mellon 
University in all publications reporting on direct or indirect use of this code 
or its derivatives.

ALL CODE, SOFTWARE, PROTOCOLS, AND ARCHITECTURES DEVELOPED BY THE CMU
MONARCH PROJECT ARE EXPERIMENTAL AND ARE KNOWN TO HAVE BUGS, SOME OF
WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON PROVIDES THIS
SOFTWARE OR OTHER INTELLECTUAL PROPERTY IN ITS ``AS IS'' CONDITION,
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE OR
INTELLECTUAL PROPERTY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

Carnegie Mellon encourages (but does not require) users of this
software or intellectual property to return any improvements or
extensions that they make, and to grant Carnegie Mellon the rights to 
redistribute these changes without encumbrance.

The AODV code developed by the CMU/MONARCH group was optimized and tuned by 
Samir Das and Mahesh Marina, University of Cincinnati. The work was partially 
done in Sun Microsystems.
*/


#ifndef __aodv_packet_h__
#define __aodv_packet_h__

//#include <config.h>
//#include "aodv.h"
#define AODV_MAX_ERRORS 100


/* =====================================================================
   Packet Formats...
   ===================================================================== */
#define AODVTYPE_HELLO          0x01
#define AODVTYPE_RREQ           0x02
#define AODVTYPE_RREP           0x04
#define AODVTYPE_RERR           0x08
#define AODVTYPE_RREP_ACK       0x10

/*** added for multicast ***/
#define AODVTYPE_GRPH           0x06
#define AODVTYPE_MACT           0x05
//done by me 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define AODVTYPE_CHECKLOOP      0x12
/***************************/

/*** added for prediction in multicast***/
#define AODVTYPE_WARN           0x07
#define AODVTYPE_LPW    0x09
#define AODVTYPE_RPE    0x03
#define AODVTYPE_LINK_RREQ 0x11
/****************************/

/*
 * AODV Routing Protocol Header Macros
 */
#define HDR_AODV(p)             ((struct hdr_aodv*)hdr_aodv::access(p))
#define HDR_AODV_REQUEST(p)     ((struct hdr_aodv_request*)hdr_aodv::access(p))
#define HDR_AODV_REPLY(p)       ((struct hdr_aodv_reply*)hdr_aodv::access(p))
#define HDR_AODV_ERROR(p)       ((struct hdr_aodv_error*)hdr_aodv::access(p))
#define HDR_AODV_RREP_ACK(p)    ((struct hdr_aodv_rrep_ack*)hdr_aodv::access(p))

/*** added for multicast ***/
#define HDR_AODV_GRPH(p)        ((struct hdr_aodv_grph*)hdr_aodv::access(p))
#define HDR_AODV_MACT(p)        ((struct hdr_aodv_mact*)hdr_aodv::access(p))
#define HDR_AODV_CHECKLOOP(p)   ((struct 
hdr_aodv_checkloop*)hdr_aodv::access(p))
/***************************/

/*** added for prediction***/
#define HDR_AODV_WARN(p)    ((struct hdr_aodv_warn*)hdr_aodv::access(p))
#define HDR_AODV_REQUEST_LINK(p) ((struct hdr_aodv_request_link 
*)hdr_aodv::access(p))
#define HDR_AODV_RPE(p) ((struct hdr_aodv_rpe *)hdr_aodv::access(p))
#define HDR_AODV_LPW(p) ((struct hdr_aodv_lpw *)hdr_aodv::access(p))
/******************************************/


/*
 * General AODV Header - shared by all formats
 */
struct hdr_aodv {
        u_int8_t        ah_type;
        /*
        u_int8_t        ah_reserved[2];
        u_int8_t        ah_hopcount;
        */
                // Header access methods
        static int offset_; // required by PacketHeaderManager
        inline static int& offset() { return offset_; }
        inline static hdr_aodv* access(const Packet* p) {
                return (hdr_aodv*) p->access(offset_);
        }
};

/*** added for multicast ***/
#define RREQ_NO_FLAG 0x00
#define RREQ_J  0x01
#define RREQ_JR 0x03
#define RREQ_R  0x04
/**************************/

struct hdr_aodv_request {
        u_int8_t        rq_type;        // Packet Type

        /*** modified for multicast ***/
        //u_int8_t        reserved[2];
        u_int8_t        rq_flags;
        u_int8_t                reserved;
        /******************************/

        u_int8_t        rq_hop_count;   // Hop Count
        u_int32_t       rq_bcast_id;    // Broadcast ID

        nsaddr_t        rq_dst;         // Destination IP Address
        u_int32_t       rq_dst_seqno;   // Destination Sequence Number
        nsaddr_t        rq_src;         // Source IP Address
        u_int32_t       rq_src_seqno;   // Source Sequence Number

        double          rq_timestamp;   // when REQUEST sent;
                                        // used to compute route discovery 
latency

  // This define turns on gratuitous replies- see aodv.cc for implementation 
contributed by
  // Anant Utgikar, 09/16/02.
  //#define RREQ_GRAT_RREP      0x80

  inline int size() { 
  int sz = 0;
  /*
        sz = sizeof(u_int8_t)           // rq_type
             + 2*sizeof(u_int8_t)       // reserved
             + sizeof(u_int8_t)         // rq_hop_count
             + sizeof(double)           // rq_timestamp
             + sizeof(u_int32_t)        // rq_bcast_id
             + sizeof(nsaddr_t)         // rq_dst
             + sizeof(u_int32_t)        // rq_dst_seqno
             + sizeof(nsaddr_t)         // rq_src
             + sizeof(u_int32_t);       // rq_src_seqno
  */
        sz = 7*sizeof(u_int32_t);
        assert (sz >= 0);
        return sz;
  }
};

/*** added for multicast ***/
#define RREP_NO_FLAG 0x00
#define RREP_J  0x01
#define RREP_JR 0x03
#define RREP_R  0x04
/***************************/

struct hdr_aodv_reply {
        u_int8_t        rp_type;        // Packet Type

        /*** modified for multicast ***/
        //u_int8_t        reserved[2];
        u_int8_t        rp_flags;
        u_int8_t        reserved;
        /*******************************/

        u_int8_t        rp_hop_count;           // Hop Count
        nsaddr_t        rp_dst;                 // Destination IP Address
        u_int32_t       rp_dst_seqno;           // Destination Sequence Number
        nsaddr_t        rp_src;                 // Source IP Address
        double          rp_lifetime;            // Lifetime

        double          rp_timestamp;           // when corresponding REQ sent;
                                                // used to compute route 
discovery latency
                                                
  inline int size() { 
  int sz = 0;
  /*
        sz = sizeof(u_int8_t)           // rp_type
             + 2*sizeof(u_int8_t)       // rp_flags + reserved
             + sizeof(u_int8_t)         // rp_hop_count
             + sizeof(double)           // rp_timestamp
             + sizeof(nsaddr_t)         // rp_dst
             + sizeof(u_int32_t)        // rp_dst_seqno
             + sizeof(nsaddr_t)         // rp_src
             + sizeof(u_int32_t);       // rp_lifetime
  */
        sz = 6*sizeof(u_int32_t);
        assert (sz >= 0);
        return sz;
  }

};

struct hdr_aodv_error {
        u_int8_t        re_type;                // Type
        u_int8_t        reserved[2];            // Reserved
        u_int8_t        DestCount;                 // DestCount
        // List of Unreachable destination IP addresses and sequence numbers
        nsaddr_t        unreachable_dst[AODV_MAX_ERRORS];   
        u_int32_t       unreachable_dst_seqno[AODV_MAX_ERRORS];   

  inline int size() { 
  int sz = 0;
  /*
        sz = sizeof(u_int8_t)           // type
             + 2*sizeof(u_int8_t)       // reserved
             + sizeof(u_int8_t)         // length
             + length*sizeof(nsaddr_t); // unreachable destinations
  */
        sz = (DestCount*2 + 1)*sizeof(u_int32_t);
        assert(sz);
        return sz;
  }

};

struct hdr_aodv_rrep_ack {
        u_int8_t        rpack_type;
        u_int8_t        reserved;
};

/*** added for multicast ***/
#define MACT_J 0x01
#define MACT_P 0x02
#define MACT_GL 0x03

struct hdr_aodv_mact {
        u_int8_t        mact_type;
        u_int8_t        mact_flags;
        u_int8_t        reserved;
        u_int8_t        mact_hop_count;
        nsaddr_t        mact_grp_dst;
        nsaddr_t        mact_src;
        u_int32_t       mact_src_seqno;
        nsaddr_t        mact_joined_node;

        inline int size(){
                int sz = 5 * sizeof(u_int32_t);
                return sz;
        }
};

#define GRPH_U 0x01
#define GRPH_NO_FLAG 0x00
#define GRPH_M 0x02
struct hdr_aodv_grph {
        u_int8_t        gh_type;
        u_int8_t        gh_flags;
        u_int8_t        reserved;
        u_int8_t        gh_hop_count;
        nsaddr_t        gh_grp_leader_addr;
        nsaddr_t        gh_multi_grp_addr;
        u_int32_t       gh_grp_seqno;

        inline int size(){
                int sz = 4 * sizeof(u_int32_t);
                return sz;
        }
};

#define AODVTYPE_RREP_EXT 0x05
#define AODVTYPE_RREP_EXT_LEN 0x06
struct hdr_aodv_reply_ext{
        u_int8_t type;
    u_int8_t length;
    u_int16_t hops_grp_leader;
        nsaddr_t grp_leader_addr;

        inline int size(){
                int sz = 2 * sizeof(u_int32_t);
                return sz;
        }
};

#define AODVTYPE_RREQ_EXT 0x04
#define AODVTYPE_RREQ_EXT_LEN 0x02
struct hdr_aodv_request_ext {
        u_int8_t type;
        u_int8_t length;
        u_int16_t hop_count;

        inline int size(){
                int sz = 1 * sizeof(u_int32_t);
                return sz;
        }
};
//done by 
me//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct hdr_aodv_checkloop {
        nsaddr_t cl_reply_node;
        nsaddr_t cl_joined_node;
        nsaddr_t cl_grp_addr;

        inline int size(){
                int sz=3 * sizeof(u_int32_t);
                return sz;
        }
};
/************************/


/*** added for prediction ***/
#define WARN_U 0x01
#define WARN_D 0x02
struct hdr_aodv_warn {
        u_int8_t    wn_type;
        u_int8_t    wn_flags;
        u_int8_t    reserved;
        nsaddr_t    wn_grp;
        double      wn_expire;

        inline int size(){
                int sz = 3 * sizeof(u_int8_t) + 2 * sizeof(u_int32_t);
                return sz;
        }
};

struct hdr_aodv_lpw {
        u_int8_t rp_type;
        nsaddr_t rp_dst;
        double breakTime;

        inline int size(){
               int sz = 2 * sizeof(u_int32_t) + sizeof(u_int8_t);
               return sz;
        }
};

struct hdr_aodv_rpe {
        u_int8_t        rp_type;                // Type
        u_int8_t        reserved[2];            // Reserved
        u_int8_t        DestCount;                 // DestCount
        // List of destination IP addresses and sequence numbers
        
        double          breakTime;
        nsaddr_t        vulnerable_dst[AODV_MAX_ERRORS];   
        u_int32_t       vulnerable_dst_seqno[AODV_MAX_ERRORS];

  inline int size() {
        int sz = (DestCount*2 + 1)*sizeof(u_int32_t)+sizeof(double);
        assert(sz);
        return sz;
  }
};

struct hdr_aodv_request_link {
        u_int8_t        rq_type;                // Packet Type
        u_int8_t        rq_flags;
        u_int8_t        reserved;
        u_int8_t        rq_hop_count;           // Hop Count

        u_int32_t       rq_bcast_id;            // Broadcast ID

        nsaddr_t        rq_dst;                 // Destination IP Address
        u_int32_t       rq_dst_seqno;           // Destination Sequence Number
        nsaddr_t        rq_src;                 // Source IP Address
        u_int32_t       rq_src_seqno;           // Source Sequence Number

        double          rq_timestamp;           // when REQUEST sent

        nsaddr_t        from;
        nsaddr_t        to;

        inline int size(){
                int sz = 9 * sizeof(u_int32_t);
                return sz;
        }

};
/**********************/


// for size calculation of header-space reservation
union hdr_all_aodv {
  hdr_aodv          ah;
  hdr_aodv_request  rreq;
  hdr_aodv_reply    rrep;
  hdr_aodv_error    rerr;
  hdr_aodv_rrep_ack rrep_ack;

  /*** added for multicast ***/
  hdr_aodv_grph         grph;
  hdr_aodv_mact         mact;
//done by 
me//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  hdr_aodv_checkloop    cl;
  /***************************/

  /*** added for prediction ***/
  hdr_aodv_lpw  lpw;
  hdr_aodv_rpe  rep;
  hdr_aodv_request_link rq;
  hdr_aodv_warn     warn;
  /****************************/
};

#endif /* __aodv_packet_h__ */

/* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
 * Copyright (c) 1997 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the Computer Systems
 *      Engineering Group at Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Ported from CMU/Monarch's code, appropriate copyright applies.
 * nov'98 -Padma.
 *
 * $Header: /nfs/jade/vint/CVSROOT/ns-2/trace/cmu-trace.cc,v 1.72 2003/02/22 
03:53:35 buchheim Exp $
 */

#include <packet.h>
#include <ip.h>
#include <tcp.h>
#include <rtp.h>
#include <arp.h>
#include <dsr/hdr_sr.h> // DSR
#include <mac.h>
#include <mac-802_11.h>
#include <address.h>
#include <tora/tora_packet.h> //TORA
#include <imep/imep_spec.h>         // IMEP
#include <aodv/aodv_packet.h> //AODV
#include <cmu-trace.h>
#include <mobilenode.h>

#include "diffusion/diff_header.h" // DIFFUSION -- Chalermek


//#define LOG_POSITION

//extern char* pt_names[];

static class CMUTraceClass : public TclClass {
public:
        CMUTraceClass() : TclClass("CMUTrace") { }
        TclObject* create(int, const char*const* argv) {
                return (new CMUTrace(argv[4], *argv[5]));
        }
} cmutrace_class;



double CMUTrace::bradius = 0.0;
double CMUTrace::radius_scaling_factor_ = 0.0;
double CMUTrace::duration_scaling_factor_ = 0.0;


CMUTrace::CMUTrace(const char *s, char t) : Trace(t)
{
        bzero(tracename, sizeof(tracename));
        strncpy(tracename, s, MAX_ID_LEN);

        if(strcmp(tracename, "RTR") == 0) {
                tracetype = TR_ROUTER;
        }
        else if(strcmp(tracename, "TRP") == 0) {
                tracetype = TR_ROUTER;
        }
        else if(strcmp(tracename, "MAC") == 0) {
                tracetype = TR_MAC;
        }
        else if(strcmp(tracename, "IFQ") == 0) {
                tracetype = TR_IFQ;
        }
        else if(strcmp(tracename, "AGT") == 0) {
                tracetype = TR_AGENT;
        }
        else {
                fprintf(stderr, "CMU Trace Initialized with invalid type\n");
                exit(1);
        }

        assert(type_ == DROP || type_ == SEND || type_ == RECV);

        newtrace_ = 0;
        for (int i=0 ; i < MAX_NODE ; i++) 
                nodeColor[i] = 3 ;
        node_ = 0;

}

void
CMUTrace::format_mac(Packet *p, const char *why, int offset)
{
        struct hdr_cmn *ch = HDR_CMN(p);
        struct hdr_ip *ih = HDR_IP(p);
        struct hdr_mac802_11 *mh = HDR_MAC802_11(p);
        double x = 0.0, y = 0.0, z = 0.0;
       
        char op = (char) type_;
        Node* thisnode = Node::get_node_by_address(src_);
        double energy = -1;
        if (thisnode) {
            if (thisnode->energy_model()) {
                    energy = thisnode->energy_model()->energy();
            }
        }

        // hack the IP address to convert pkt format to hostid format
        // for now until port ids are removed from IP address. -Padma.

        int src = Address::instance().get_nodeaddr(ih->saddr());

        if(tracetype == TR_ROUTER && type_ == SEND) {
                if(src_ != src)
                        op = FWRD;
        }

        // use tagged format if appropriate
        if (pt_->tagged()) {
                int next_hop = -1 ;
                Node* nextnode = Node::get_node_by_address(ch->next_hop_);
                if (nextnode) next_hop = nextnode->nodeid(); 

                node_->getLoc(&x, &y, &z);

                if (op == DROP) op = 'd';
                if (op == SEND) op = '+';
                if (op == FWRD) op = 'h';

                sprintf(pt_->buffer() + offset,
                        "%c "TIME_FORMAT" -s %d -d %d -p %s -k %3s -i %d "
                        "-N:loc {%.2f %.2f %.2f} -N:en %f "
                        "-M:dur %x -M:s %x -M:d %x -M:t %x ",
                        op,                             // event type
                        Scheduler::instance().clock(),  // time
                        src_,                           // this node
                        next_hop,                       // next hop
                        packet_info.name(ch->ptype()),  // packet type
                        tracename,                      // trace level
                        ch->uid(),                      // event id
                        x, y, z,                        // location
                        energy,                         // energy
                        mh->dh_duration,                // MAC: duration
                        ETHER_ADDR(mh->dh_da),          // MAC: source
                        ETHER_ADDR(mh->dh_sa),          // MAC: destination
                        GET_ETHER_TYPE(mh->dh_body)     // MAC: type
                        );
                return;
        }


        // Use new ns trace format to replace the old cmu trace format)
        if (newtrace_) {
            
            node_->getLoc(&x, &y, &z);
            // consistence
            if ( op == DROP ) { op = 'd';}

                // basic trace infomation + basic exenstion

            sprintf(pt_->buffer() + offset,
                   "%c -t %.9f -Hs %d -Hd %d -Ni %d -Nx %.2f -Ny %.2f -Nz %.2f 
-Ne %f -Nl %3s -Nw %s ",
                    op,                       // event type
                    Scheduler::instance().clock(),  // time
                    src_,                           // this node
                    ch->next_hop_,                  // next hop
                    src_,                           // this node
                    x,                              // x coordinate
                    y,                              // y coordinate
                    z,                              // z coordinate
                    energy,                         // energy, -1 = not existing
                    tracename,                      // trace level
                    why);                            // reason

            // mac layer extension

            offset = strlen(pt_->buffer());

            sprintf(pt_->buffer() + offset, 
                    "-Ma %x -Md %x -Ms %x -Mt %x ",
                    mh->dh_duration,
                    ETHER_ADDR(mh->dh_da),
                    ETHER_ADDR(mh->dh_sa),
                    GET_ETHER_TYPE(mh->dh_body));
                    
             return;
        }


#ifdef LOG_POSITION
        double x = 0.0, y = 0.0, z = 0.0;
        node_->getLoc(&x, &y, &z);
#endif

        sprintf(pt_->buffer() + offset,
#ifdef LOG_POSITION
                "%c %.9f %d (%6.2f %6.2f) %3s %4s %d %s %d [%x %x %x %x] ",
#else
                "%c %.9f _%d_ %3s %4s %d %s %d [%x %x %x %x] ",
#endif
                op,
                Scheduler::instance().clock(),
                src_,                           // this node
#ifdef LOG_POSITION
                x,
                y,
#endif
                tracename,
                why,
                
                ch->uid(),                      // identifier for this event
                
                ((ch->ptype() == PT_MAC) ? (
                  (mh->dh_fc.fc_subtype == MAC_Subtype_RTS) ? "RTS"  :
                  (mh->dh_fc.fc_subtype == MAC_Subtype_CTS) ? "CTS"  :
                  (mh->dh_fc.fc_subtype == MAC_Subtype_ACK) ? "ACK"  :
                  "UNKN"
                  ) : packet_info.name(ch->ptype())),
                
                ch->size(),

                //*((u_int16_t*) &mh->dh_fc),
                mh->dh_duration,
                ETHER_ADDR(mh->dh_da),
                ETHER_ADDR(mh->dh_sa),
                GET_ETHER_TYPE(mh->dh_body));

        offset = strlen(pt_->buffer());

        if (thisnode) {
                if (thisnode->energy_model()) {
                        sprintf(pt_->buffer() + offset,
                                "[energy %f] ",
                                thisnode->energy_model()->energy());
                }
        }
}

void
CMUTrace::format_ip(Packet *p, int offset)
{
        struct hdr_cmn *ch = HDR_CMN(p);
        struct hdr_ip *ih = HDR_IP(p);
        
        // hack the IP address to convert pkt format to hostid format
        // for now until port ids are removed from IP address. -Padma.
        int src = Address::instance().get_nodeaddr(ih->saddr());
        int dst = Address::instance().get_nodeaddr(ih->daddr());

        if (pt_->tagged()) {
                sprintf(pt_->buffer() + offset,
                        "-IP:s %d -IP:sp %d -IP:d %d -IP:dp %d -p %s -e %d "
                        "-c %d -i %d -IP:ttl %d ",
                        src,                           // packet src
                        ih->sport(),                   // src port
                        dst,                           // packet dest
                        ih->dport(),                   // dst port
                        packet_info.name(ch->ptype()), // packet type
                        ch->size(),                    // packet size
                        ih->flowid(),                  // flow id
                        ch->uid(),                     // unique id
                        ih->ttl_                       // ttl
                        );
        } else if (newtrace_) {
            sprintf(pt_->buffer() + offset,
                    "-Is %d.%d -Id %d.%d -It %s -Il %d -If %d -Ii %d -Iv %d ",
                    src,                           // packet src
                    ih->sport(),                   // src port
                    dst,                           // packet dest
                    ih->dport(),                   // dst port
                    packet_info.name(ch->ptype()),  // packet type
                    ch->size(),                     // packet size
                    ih->flowid(),                   // flow id
                    ch->uid(),                      // unique id
                    ih->ttl_);                      // ttl
        } else {
            sprintf(pt_->buffer() + offset, "------- [%d:%d %d:%d %d %d] ",
                src, ih->sport(),
                dst, ih->dport(),
                ih->ttl_, (ch->next_hop_ < 0) ? 0 : ch->next_hop_);
        }
}

void
CMUTrace::format_arp(Packet *p, int offset)
{
        struct hdr_arp *ah = HDR_ARP(p);

        if (pt_->tagged()) {
            sprintf(pt_->buffer() + offset,
                    "-arp:op %s -arp:ms %d -arp:s %d -arp:md %d -arp:d %d ",
                    ah->arp_op == ARPOP_REQUEST ?  "REQUEST" : "REPLY",
                    ah->arp_sha,
                    ah->arp_spa,
                    ah->arp_tha,
                    ah->arp_tpa);
        } else if (newtrace_) {
            sprintf(pt_->buffer() + offset,
                    "-P arp -Po %s -Pms %d -Ps %d -Pmd %d -Pd %d ",
                    ah->arp_op == ARPOP_REQUEST ?  "REQUEST" : "REPLY",
                    ah->arp_sha,
                    ah->arp_spa,
                    ah->arp_tha,
                    ah->arp_tpa);
        } else {

            sprintf(pt_->buffer() + offset,
                "------- [%s %d/%d %d/%d]",
                ah->arp_op == ARPOP_REQUEST ?  "REQUEST" : "REPLY",
                ah->arp_sha,
                ah->arp_spa,
                ah->arp_tha,
                ah->arp_tpa);
        }
}

void
CMUTrace::format_dsr(Packet *p, int offset)
{
        hdr_sr *srh = hdr_sr::access(p);

        if (pt_->tagged()) {
            sprintf(pt_->buffer() + offset,
                    "-dsr:h %d -dsr:q %d -dsr:s %d -dsr:p %d -dsr:n %d "
                    "-dsr:l %d -dsr:e {%d %d} -dsr:w %d -dsr:m %d -dsr:c %d "
                    "-dsr:b {%d %d} ",
                    srh->num_addrs(),
                    srh->route_request(),
                    srh->rtreq_seq(),
                    srh->route_reply(),
                    srh->rtreq_seq(),
                    srh->route_reply_len(),
                    srh->reply_addrs()[0].addr,
                    srh->reply_addrs()[srh->route_reply_len()-1].addr,
                    srh->route_error(),
                    srh->num_route_errors(),
                    srh->down_links()[srh->num_route_errors() - 1].tell_addr,
                    srh->down_links()[srh->num_route_errors() - 1].from_addr,
                    srh->down_links()[srh->num_route_errors() - 1].to_addr);
            return;
        } else if (newtrace_) {
            sprintf(pt_->buffer() + offset, 
                "-P dsr -Ph %d -Pq %d -Ps %d -Pp %d -Pn %d -Pl %d -Pe %d->%d 
-Pw %d -Pm %d -Pc %d -Pb %d->%d ",
                    srh->num_addrs(),                   // how many nodes 
travered

                srh->route_request(),
                srh->rtreq_seq(),

                srh->route_reply(),
                srh->rtreq_seq(),
                srh->route_reply_len(),
                // the dest of the src route
                srh->reply_addrs()[0].addr,
                srh->reply_addrs()[srh->route_reply_len()-1].addr,

                srh->route_error(),
                srh->num_route_errors(),
                srh->down_links()[srh->num_route_errors() - 1].tell_addr,
                srh->down_links()[srh->num_route_errors() - 1].from_addr,
                srh->down_links()[srh->num_route_errors() - 1].to_addr);

           return;
        }
        sprintf(pt_->buffer() + offset, 
                "%d [%d %d] [%d %d %d %d->%d] [%d %d %d %d->%d]",
                srh->num_addrs(),

                srh->route_request(),
                srh->rtreq_seq(),

                srh->route_reply(),
                srh->rtreq_seq(),
                srh->route_reply_len(),
                // the dest of the src route
                srh->reply_addrs()[0].addr,
                srh->reply_addrs()[srh->route_reply_len()-1].addr,

                srh->route_error(),
                srh->num_route_errors(),
                srh->down_links()[srh->num_route_errors() - 1].tell_addr,
                srh->down_links()[srh->num_route_errors() - 1].from_addr,
                srh->down_links()[srh->num_route_errors() - 1].to_addr);
}

void
CMUTrace::format_msg(Packet *, int)
{
}

void
CMUTrace::format_tcp(Packet *p, int offset)
{
        struct hdr_cmn *ch = HDR_CMN(p);
        struct hdr_tcp *th = HDR_TCP(p);
        
        if (pt_->tagged()) {
            sprintf(pt_->buffer() + offset,
                    "-tcp:s %d -tcp:a %d -tcp:f %d -tcp:o %d ",
                    th->seqno_,
                    th->ackno_,
                    ch->num_forwards(),
                    ch->opt_num_forwards());
        } else if (newtrace_) {
            sprintf(pt_->buffer() + offset,
                "-Pn tcp -Ps %d -Pa %d -Pf %d -Po %d ",
                th->seqno_,
                th->ackno_,
                ch->num_forwards(),
                ch->opt_num_forwards());

        } else {
            sprintf(pt_->buffer() + offset,
                "[%d %d] %d %d",
                th->seqno_,
                th->ackno_,
                ch->num_forwards(),
                ch->opt_num_forwards());
        }
}

void
CMUTrace::format_rtp(Packet *p, int offset)
{
        struct hdr_cmn *ch = HDR_CMN(p);
        struct hdr_rtp *rh = HDR_RTP(p);
        struct hdr_ip *ih = HDR_IP(p);
        Node* thisnode = Node::get_node_by_address(src_);

        //hacking, needs to change later, 
        int dst = Address::instance().get_nodeaddr(ih->daddr());
        
        if (dst == src_){
                // I just received a cbr data packet
                if (thisnode->energy_model() && 
                    thisnode->energy_model()->powersavingflag()) {
                        
thisnode->energy_model()->set_node_state(EnergyModel::INROUTE);
                }
        }

        if (pt_->tagged()) {
                sprintf(pt_->buffer() + offset,
                        "-cbr:s %d -cbr:f %d -cbr:o %d ",
                        rh->seqno_,
                        ch->num_forwards(),
                        ch->opt_num_forwards());
        } else if (newtrace_) {
                sprintf(pt_->buffer() + offset,
                        "-Pn cbr -Pi %d -Pf %d -Po %d ",
                        rh->seqno_,
                        ch->num_forwards(),
                        ch->opt_num_forwards());
        } else {
                sprintf(pt_->buffer() + offset,
                        "[%d] %d %d",
                        rh->seqno_,
                        ch->num_forwards(),
                        ch->opt_num_forwards());
        }
}

void
CMUTrace::format_imep(Packet *p, int offset)
{
        struct hdr_imep *im = HDR_IMEP(p);

#define U_INT16_T(x)    *((u_int16_t*) &(x))

        if (pt_->tagged()) {
            sprintf(pt_->buffer() + offset,
                    "-imep:a %c -imep:h %c -imep:o %c -imep:l %04x ",
                    (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
                    (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
                    (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
                    U_INT16_T(im->imep_length));
        } else if (newtrace_) {
            sprintf(pt_->buffer() + offset,
                "-P imep -Pa %c -Ph %c -Po %c -Pl 0x%04x ] ",
                (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
                (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
                (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
                U_INT16_T(im->imep_length));
        } else {
            sprintf(pt_->buffer() + offset,
                "[%c %c %c 0x%04x] ",
                (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
                (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
                (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
                U_INT16_T(im->imep_length));
        }
#undef U_INT16_T
}


void
CMUTrace::format_tora(Packet *p, int offset)
{
        struct hdr_tora *th = HDR_TORA(p);
        struct hdr_tora_qry *qh = HDR_TORA_QRY(p);
        struct hdr_tora_upd *uh = HDR_TORA_UPD(p);
        struct hdr_tora_clr *ch = HDR_TORA_CLR(p);

        switch(th->th_type) {

        case TORATYPE_QRY:

                if (pt_->tagged()) {
                    sprintf(pt_->buffer() + offset,
                            "-tora:t %x -tora:d %d -tora:c QUERY",
                            qh->tq_type, qh->tq_dst);
                } else if (newtrace_) {
                    sprintf(pt_->buffer() + offset,
                        "-P tora -Pt 0x%x -Pd %d -Pc QUERY ",
                        qh->tq_type, qh->tq_dst);
                        
                } else {

                    sprintf(pt_->buffer() + offset, "[0x%x %d] (QUERY)",
                        qh->tq_type, qh->tq_dst);
                }
                break;

        case TORATYPE_UPD:

                if (pt_->tagged()) {
                    sprintf(pt_->buffer() + offset,
                            "-tora:t %x -tora:d %d -tora:a %f -tora:o %d "
                            "-tora:r %d -tora:e %d -tora:i %d -tora:c UPDATE",
                            uh->tu_type,
                            uh->tu_dst,
                            uh->tu_tau,
                            uh->tu_oid,
                            uh->tu_r,
                            uh->tu_delta,
                            uh->tu_id);
                } else if (newtrace_) {
                    sprintf(pt_->buffer() + offset,
                        "-P tora -Pt 0x%x -Pd %d (%f %d %d %d %d) -Pc UPDATE ",
                        uh->tu_type,
                        uh->tu_dst,
                        uh->tu_tau,
                        uh->tu_oid,
                        uh->tu_r,
                        uh->tu_delta,
                        uh->tu_id);
                } else {
                    sprintf(pt_->buffer() + offset,
                        "-Pt 0x%x -Pd %d -Pa %f -Po %d -Pr %d -Pe %d -Pi %d -Pc 
UPDATE ",
                        uh->tu_type,
                        uh->tu_dst,
                        uh->tu_tau,
                        uh->tu_oid,
                        uh->tu_r,
                        uh->tu_delta,
                        uh->tu_id);
                }
                break;

        case TORATYPE_CLR:
                if (pt_->tagged()) {
                    sprintf(pt_->buffer() + offset,
                            "-tora:t %x -tora:d %d -tora:a %f -tora:o %d "
                            "-tora:c CLEAR ",
                            ch->tc_type,
                            ch->tc_dst,
                            ch->tc_tau,
                            ch->tc_oid);
                } else if (newtrace_) {
                    sprintf(pt_->buffer() + offset, 
                        "-P tora -Pt 0x%x -Pd %d -Pa %f -Po %d -Pc CLEAR ",
                        ch->tc_type,
                        ch->tc_dst,
                        ch->tc_tau,
                        ch->tc_oid);
                } else {
                    sprintf(pt_->buffer() + offset, "[0x%x %d %f %d] (CLEAR)",
                        ch->tc_type,
                        ch->tc_dst,
                        ch->tc_tau,
                        ch->tc_oid);
                }
                break;
        }
}

void
CMUTrace::format_aodv(Packet *p, int offset)
{
        struct hdr_aodv *ah = HDR_AODV(p);
        struct hdr_aodv_request *rq = HDR_AODV_REQUEST(p);
        struct hdr_aodv_reply *rp = HDR_AODV_REPLY(p);

        /*** added for multicast ***/
        struct hdr_aodv_grph *gh = HDR_AODV_GRPH(p);
        struct hdr_aodv_mact *mact = HDR_AODV_MACT(p);
        struct hdr_aodv_warn *warn = HDR_AODV_WARN(p);
//done by 
me//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        struct hdr_aodv_checkloop *cl=HDR_AODV_CHECKLOOP(p);
        /****************************/

        switch(ah->ah_type) {
        case AODVTYPE_RREQ:

        /*** added for prediction for unicast ***/
        case AODVTYPE_LINK_RREQ:
        /****************************************/

                if (pt_->tagged()) {
                    sprintf(pt_->buffer() + offset,
                            "-aodv:t %x -aodv:h %d -aodv:b %d -aodv:d %d "
                            "-aodv:ds %d -aodv:s %d -aodv:ss %d "
                            "-aodv:c REQUEST ",
                            rq->rq_type,
                            rq->rq_hop_count,
                            rq->rq_bcast_id,
                            rq->rq_dst,
                            rq->rq_dst_seqno,
                            rq->rq_src,
                            rq->rq_src_seqno);
                } else if (newtrace_) {

                    sprintf(pt_->buffer() + offset,
                        "-P aodv -Pt 0x%x -Ph %d -Pb %d -Pd %d -Pds %d -Ps %d 
-Pss %d -Pc REQUEST ",
                        rq->rq_type,
                        rq->rq_hop_count,
                        rq->rq_bcast_id,
                        rq->rq_dst,
                        rq->rq_dst_seqno,
                        rq->rq_src,
                        rq->rq_src_seqno);


                } else {

                    sprintf(pt_->buffer() + offset,
                        "[0x%x %d %d [%d %d] [%d %d]] (REQUEST)",
                        rq->rq_type,
                        rq->rq_hop_count,
                        rq->rq_bcast_id,
                        rq->rq_dst,
                        rq->rq_dst_seqno,
                        rq->rq_src,
                        rq->rq_src_seqno);
                }
                break;

        case AODVTYPE_RREP:
        case AODVTYPE_HELLO:
        case AODVTYPE_RERR:

        /*** added for prediction for unicast ***/
        case AODVTYPE_LPW:
        case AODVTYPE_RPE:
        /****************************************/
                
                if (pt_->tagged()) {
                    sprintf(pt_->buffer() + offset,
                            "-aodv:t %x -aodv:h %d -aodv:d %d -adov:ds %d "
                            "-aodv:l %f -aodv:c %s ",
                            rp->rp_type,
                            rp->rp_hop_count,
                            rp->rp_dst,
                            rp->rp_dst_seqno,
                            rp->rp_lifetime,
                        /*** modified for prediction for unicast ***/
                        rp->rp_type == AODVTYPE_RREP ? "REPLY" :
                        (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
                        (rp->rp_type == AODVTYPE_HELLO ? "HELLO" :
                        (rp->rp_type == AODVTYPE_LPW ? "LINK PREDICTION 
WARNING" :
                        "ROUTE PREDICTION ERROR"))));
                            //rp->rp_type == AODVTYPE_RREP ? "REPLY" :
                            //(rp->rp_type == AODVTYPE_RERR ? "ERROR" :
                             //"HELLO"));
                        /**********************************************/

                } else if (newtrace_) {
                        
                        sprintf(pt_->buffer() + offset,
                            "-P aodv -Pt 0x%x -Ph %d -Pd %d -Pds %d -Pl %f -Pc 
%s ",
                                rp->rp_type,
                                rp->rp_hop_count,
                                rp->rp_dst,
                                rp->rp_dst_seqno,
                                rp->rp_lifetime,
                        /*** modified for prediction for unicast ***/
                        rp->rp_type == AODVTYPE_RREP ? "REPLY" :
                        (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
                        (rp->rp_type == AODVTYPE_HELLO ? "HELLO" :
                        (rp->rp_type == AODVTYPE_LPW ? "LINK PREDICTION 
WARNING" :
                        "ROUTE PREDICTION ERROR"))));
                            //rp->rp_type == AODVTYPE_RREP ? "REPLY" :
                            //(rp->rp_type == AODVTYPE_RERR ? "ERROR" :
                             //"HELLO"));
                        /**********************************************/

                } else {
                        
                        sprintf(pt_->buffer() + offset,
                                "[0x%x %d [%d %d] %f] (%s)",
                                rp->rp_type,
                                rp->rp_hop_count,
                                rp->rp_dst,
                                rp->rp_dst_seqno,
                                rp->rp_lifetime,
                        /*** modified for prediction for unicast ***/
                        rp->rp_type == AODVTYPE_RREP ? "REPLY" :
                        (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
                        (rp->rp_type == AODVTYPE_HELLO ? "HELLO" :
                        (rp->rp_type == AODVTYPE_LPW ? "LINK PREDICTION 
WARNING" :
                        "ROUTE PREDICTION ERROR"))));
                            //rp->rp_type == AODVTYPE_RREP ? "REPLY" :
                            //(rp->rp_type == AODVTYPE_RERR ? "ERROR" :
                             //"HELLO"));
                        /**********************************************/
                }
                break;

        /*** added for multicast ***/
        case AODVTYPE_GRPH:
            sprintf(pt_->buffer() + offset,
                "[0x%x 0x%x %d %d [%d %d]] (GROUP HELLO)",
                gh->gh_type, gh->gh_flags, gh->gh_hop_count, 
gh->gh_grp_leader_addr,
                gh->gh_multi_grp_addr, gh->gh_grp_seqno);
            break;

        case AODVTYPE_MACT:
            sprintf(pt_->buffer() + offset,
                "[0x%x 0x%x %d %d [%d %d] %d] (MACT)",
                mact->mact_type, mact->mact_flags, mact->mact_hop_count, 
mact->mact_grp_dst,
                mact->mact_src, mact->mact_src_seqno,mact->mact_joined_node);
            break;

        case AODVTYPE_WARN:
            sprintf(pt_->buffer() + offset,
                "[0x%x 0x%x %f %d(WARN)",
                warn->wn_type, warn->wn_flags, warn->wn_expire, warn->wn_grp);
            break;
//done by 
me//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        case AODVTYPE_CHECKLOOP:
            sprintf(pt_->buffer() + offset,
                "[%d %d %d] 
(CHECKLOOP)",cl->cl_reply_node,cl->cl_joined_node,cl->cl_grp_addr);
            break; 
                
        /****************************/
        default:
#ifdef WIN32
                fprintf(stderr,
                        "CMUTrace::format_aodv: invalid AODV packet type\n");
#else
                fprintf(stderr,
                        "%s: invalid AODV packet type\n", __FUNCTION__);
#endif
                abort();
        }
}

void
CMUTrace::nam_format(Packet *p, int offset)
{
        Node* srcnode = 0 ;
        Node* dstnode = 0 ;
        Node* nextnode = 0 ;
        struct hdr_cmn *ch = HDR_CMN(p);
        struct hdr_ip *ih = HDR_IP(p);
        char op = (char) type_;
        char colors[32];
        int next_hop = -1 ;

        int dst = Address::instance().get_nodeaddr(ih->daddr());

        nextnode = Node::get_node_by_address(ch->next_hop_);
        if (nextnode) next_hop = nextnode->nodeid(); 

        srcnode = Node::get_node_by_address(src_);
        dstnode = Node::get_node_by_address(ch->next_hop_);

        double distance = 0;

        if ((srcnode) && (dstnode)) {
           MobileNode* tmnode = (MobileNode*)srcnode;
           MobileNode* rmnode = (MobileNode*)dstnode;

           distance = tmnode->propdelay(rmnode) * 300000000 ;
        }

        double energy = -1;
        double initenergy = -1;

        //default value for changing node color with respect to energy depletion
        double l1 = 0.5; 
        double l2 = 0.2;
        
        if (srcnode) {
            if (srcnode->energy_model()) {
                    energy = srcnode->energy_model()->energy();
                    initenergy = srcnode->energy_model()->initialenergy();
                    l1 = srcnode->energy_model()->level1();
                    l2 = srcnode->energy_model()->level2();
            }
        }

        int energyLevel = 0 ;
        double energyLeft = (double)(energy/initenergy) ;

        if ((energyLeft <= 1 ) && (energyLeft >= l1 )) energyLevel = 3; 
        if ((energyLeft >= l2 ) && (energyLeft < l1 )) energyLevel = 2; 
        if ((energyLeft > 0 ) && (energyLeft < l2 )) energyLevel = 1;   

        if (energyLevel == 0) 
                strcpy(colors,"-c black -o red");
        else if (energyLevel == 1) 
                strcpy(colors,"-c red -o yellow");
        else if (energyLevel == 2) 
                strcpy(colors,"-c yellow -o green");
        else if (energyLevel == 3) 
                strcpy(colors,"-c green -o black");

        // A simple hack for scadds demo (fernandez's visit) -- Chalermek
        int pkt_color = 0;
        if (ch->ptype()==PT_DIFF) {
                hdr_cdiff *dfh= HDR_CDIFF(p);
                if (dfh->mess_type != DATA) {
                        pkt_color = 1;
                }
        }
        // convert to nam format 
        if (op == 's') op = 'h' ;
        if (op == 'D') op = 'd' ;
        if (op == 'h') {
           sprintf(pt_->nbuffer(),
                "+ -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s ",
                Scheduler::instance().clock(),
                src_,                           // this node
                next_hop,
                packet_info.name(ch->ptype()),
                ch->size(),
                pkt_color,   
                ch->uid(),
                tracename);

           offset = strlen(pt_->nbuffer());
           pt_->namdump();
           sprintf(pt_->nbuffer() ,
                "- -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",
                Scheduler::instance().clock(),
                src_,                           // this node
                next_hop,
                packet_info.name(ch->ptype()),
                ch->size(),
                pkt_color,
                ch->uid(),
                tracename);

           offset = strlen(pt_->nbuffer());
           pt_->namdump();
        }

        // if nodes are too far from each other
        // nam won't dump SEND event 'cuz it's
        // gonna be dropped later anyway
        // this value 250 is pre-calculated by using 
        // two-ray ground refelction model with fixed
        // transmission power 3.652e-10
//      if ((type_ == SEND)  && (distance > 250 )) return ;

        if(tracetype == TR_ROUTER && type_ == RECV && dst != -1 ) return ;
        if(type_ == RECV && dst == -1 )dst = src_ ; //broadcasting event

        if (energy != -1) { //energy model being turned on
           if (nodeColor[src_] != energyLevel ) { //only dump it when node  
               sprintf(pt_->nbuffer() ,                    //color change
                  "n -t %.9f -s %d -S COLOR %s",
                   Scheduler::instance().clock(),
                   src_,                           // this node
                   colors);
               offset = strlen(pt_->nbuffer());
               pt_->namdump();
               nodeColor[src_] = energyLevel ;
            }   
        }


        // don't generate an "r" event for a broadcast packet
        // as it was already generated when the "h" event was written
        if (next_hop == -1 && op == 'r')
                return;
        
        sprintf(pt_->nbuffer(),
                "%c -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",
                op,
                Scheduler::instance().clock(),
                src_,                           // this node
                next_hop,
                packet_info.name(ch->ptype()),
                ch->size(),
                pkt_color,
                ch->uid(),
                tracename);

        if (next_hop == -1 && op == 'h') {
                // print extra fields for broadcast packets

                // bradius is calculated assuming 2-ray ground reflectlon
                // model using default settings of Phy/WirelessPhy and
                // Antenna/OmniAntenna
                if (bradius == 0.0) calculate_broadcast_parameters();

                double radius = bradius*radius_scaling_factor_; 

                // duration is calculated based on the radius and
                // the speed of light (299792458 m/s)
                double duration = 
(bradius/299792458.0)*duration_scaling_factor_;


                sprintf(pt_->nbuffer() + strlen(pt_->nbuffer()),
                        " -R %.2f -D %.2f",
                        radius,
                        duration);

                // schedule "r" event
                Tcl& tcl = Tcl::instance();
                tcl.evalf("[Simulator instance] at %f {[Simulator instance] 
puts-nam-traceall {r -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s -R 
%.2f -D %.2f}}",
                        Scheduler::instance().clock() + duration,
                        Scheduler::instance().clock() + duration,
                        src_,
                        next_hop,
                        packet_info.name(ch->ptype()),
                        ch->size(),
                        pkt_color,
                        ch->uid(),
                        tracename,
                        radius,
                        duration);
        }

        offset = strlen(pt_->nbuffer());
        pt_->namdump();
}

void CMUTrace::format(Packet* p, const char *why)
{
        hdr_cmn *ch = HDR_CMN(p);
        int offset = 0;

        /*
         * Log the MAC Header
         */
        format_mac(p, why, offset);

        if (pt_->namchannel()) 
                nam_format(p, offset);
        offset = strlen(pt_->buffer());
        switch(ch->ptype()) {
        case PT_MAC:
                break;
        case PT_ARP:
                format_arp(p, offset);
                break;
        default:
                format_ip(p, offset);
                offset = strlen(pt_->buffer());
                switch(ch->ptype()) {
                case PT_AODV:
                        format_aodv(p, offset);
                        break;
                case PT_TORA:
                        format_tora(p, offset);
                        break;
                case PT_IMEP:
                        format_imep(p, offset);
                        break;
                case PT_DSR:
                        format_dsr(p, offset);
                        break;
                case PT_MESSAGE:
                case PT_UDP:
                        format_msg(p, offset);
                        break;
                case PT_TCP:
                case PT_ACK:
                        format_tcp(p, offset);
                        break;
                case PT_CBR:
                        format_rtp(p, offset);
                        break;
                case PT_DIFF:
                        break;
                case PT_GAF:
                case PT_PING:
                        break;
                default:
                        fprintf(stderr, "%s - invalid packet type (%s).\n",
                                __PRETTY_FUNCTION__, 
packet_info.name(ch->ptype()));
                        exit(1);
                }
        }
}

int
CMUTrace::command(int argc, const char*const* argv)
{
        
        if(argc == 3) {
                if(strcmp(argv[1], "node") == 0) {
                        node_ = (MobileNode*) TclObject::lookup(argv[2]);
                        if(node_ == 0)
                                return TCL_ERROR;
                        return TCL_OK;
                }
                if (strcmp(argv[1], "newtrace") == 0) {
                        newtrace_ = atoi(argv[2]);
                        return TCL_OK;
                }
        }
        return Trace::command(argc, argv);
}

/*ARGSUSED*/
void
CMUTrace::recv(Packet *p, Handler *h)
{
        if (!node_energy()) {
                Packet::free(p);
                return;
        }
        assert(initialized());
        /*
         * Agent Trace "stamp" the packet with the optimal route on
         * sending.
         */
        if (tracetype == TR_AGENT && type_ == SEND) {
                God::instance()->stampPacket(p);
        }
#if 0
        /*
         * When the originator of a packet drops the packet, it may or may
         * not have been stamped by GOD.  Stamp it before logging the
         * information.
         */
        if(src_ == src && type_ == DROP) {
                God::instance()->stampPacket(p);
        }
#endif
        format(p, "---");
        pt_->dump();
        //namdump();
        if(target_ == 0)
                Packet::free(p);
        else
                send(p, h);
}

void
CMUTrace::recv(Packet *p, const char* why)
{
        assert(initialized() && type_ == DROP);
        if (!node_energy()) {
                Packet::free(p);
                return;
        }
#if 0
        /*
         * When the originator of a packet drops the packet, it may or may
         * not have been stamped by GOD.  Stamp it before logging the
         * information.
         */
        if(src_ == ih->saddr()) {
                God::instance()->stampPacket(p);
        }
#endif
        format(p, why);
        pt_->dump();
        //namdump();
        Packet::free(p);
}

int CMUTrace::node_energy()
{
        Node* thisnode = Node::get_node_by_address(src_);
        double energy = 1;
        if (thisnode) {
                if (thisnode->energy_model()) {
                        energy = thisnode->energy_model()->energy();
                }
        } 
        if (energy > 0) return 1;
        return 0;
}

void CMUTrace::calculate_broadcast_parameters() {
        // Calculate the maximum distance at which a packet can be received
        // based on the two-ray reflection model using the current default
        // values for Phy/WirelessPhy and Antenna/OmniAntenna.

        double P_t, P_r, G_t, G_r, h, L;
        Tcl& tcl = Tcl::instance();

        tcl.evalc("Phy/WirelessPhy set Pt_");
        P_t = atof(tcl.result());
        tcl.evalc("Phy/WirelessPhy set RXThresh_");
        P_r = atof(tcl.result());
        tcl.evalc("Phy/WirelessPhy set L_");
        L = atof(tcl.result());
        tcl.evalc("Antenna/OmniAntenna set Gt_");
        G_t = atof(tcl.result());
        tcl.evalc("Antenna/OmniAntenna set Gr_");
        G_r = atof(tcl.result());
        tcl.evalc("Antenna/OmniAntenna set Z_");
        h = atof(tcl.result());
        bradius = pow(P_t*G_r*G_t*pow(h,4.0)/(P_r*L), 0.25);

        // Also get the scaling factors
        tcl.evalc("CMUTrace set radius_scaling_factor_");
        radius_scaling_factor_ = atof(tcl.result());
        tcl.evalc("CMUTrace set duration_scaling_factor_");
        duration_scaling_factor_ = atof(tcl.result());
}



Reply via email to