Hi All, Our website moved to a new URL. Please bookmark the new link: https://ns2bloggers.blogspot.com/

Saturday 3 May 2014

IMPLEMENTING A NEW PROTOCOL

Tutorial to implement protoname in ns2.35


This is an Implementation tutorial of new manet(Mobile Ad-hoc NETworks) unicast protocol name protoname.

List of files to be modified

  • ~/ns-allinone-2.35/ns-2.35/common/packet.h
  • ~/ns-allinone-2.35/ns-2.35/trace/cmu-trace.h
  • ~/ns-allinone-2.35/ns-2.35/trace/cmu-trace.cc
  • ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-packet.tcl
  • ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-default.tcl 
  • ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-lib.tcl
  • ~/ns-allinone-2.35/ns-2.35/queue/priqueue.cc

 
** Note: Text in red is the part that is to be added or modified in given file and location and purple colour denotes the terminal commands.

** Note: mod stands for modification.

** Note: Line number is approx and is provided for your convenience. Please check the methods below and above before inserting.

Step 1:

download protoname.rar

http://www.cs.nccu.edu.tw/~g10031/protoname.rar

Extract the files and place them in ~/ns-allinone-2.35/ns-2.35/protoname directory(create the directory if not present)

Step 2:

gedit ~/ns-allinone-2.35/ns-2.35/common/packet.h (mods at 3 places)

  • a.  its somewhere near line 200
static const packet_t PT_DCCP = 63;
static const packet_t PT_DCCP_REQ = 64;
static const packet_t PT_DCCP_RESP = 65;
static const packet_t PT_DCCP_ACK = 66;
static const packet_t PT_DCCP_DATA = 67;
static const packet_t PT_DCCP_DATAACK = 68;
static const packet_t PT_DCCP_CLOSE  = 69;
static const packet_t PT_DCCP_CLOSEREQ = 70;
static const packet_t PT_DCCP_RESET = 71;

        // M-DART packets
static const packet_t PT_MDART = 72;

        // insert new packet types here

static const packet_t PT_PROTONAME = 73;

static packet_t       PT_NTYPE = 74; // This MUST be the LAST one


  • b. near line 250

static bool data_packet(packet_t type) {
return ( (type) == PT_TCP || \
        (type) == PT_TELNET || \
        (type) == PT_CBR || \
        (type) == PT_AUDIO || \
        (type) == PT_VIDEO || \
        (type) == PT_ACK || \
        (type) == PT_SCTP || \
        (type) == PT_SCTP_APP1 || \
        (type) == PT_HDLC || \  //(remember this mod also)
(type) == PT_PROTONAME \ 
       );

  • c.      near line 422
static void initName()
{
                .....
                name_[PT_DCCP_REQ]="DCCP_Request";
name_[PT_DCCP_RESP]="DCCP_Response";
name_[PT_DCCP_ACK]="DCCP_Ack";
name_[PT_DCCP_DATA]="DCCP_Data";
name_[PT_DCCP_DATAACK]="DCCP_DataAck";
name_[PT_DCCP_CLOSE]="DCCP_Close";
name_[PT_DCCP_CLOSEREQ]="DCCP_CloseReq";
name_[PT_DCCP_RESET]="DCCP_Reset";
name_[PT_PROTONAME]="protoname"; 
name_[PT_NTYPE]= "undefined";
}


save and close.

Step 3:

gedit ~/ns-allinone-2.35/ns-2.35/trace/cmu-trace.h (mod at 1 place)

  • a. Near line 164.

class CMUTrace: public Trace {
public:
CMUTrace (const char * s, char t);
...
private:
char tracename [MAX_ID_LEN + 1];
int nodeColor [MAX_NODE];
...
void format_tora (Packet * p, int offset);
void format_imep (Packet * p, int offset);
void format_aodv (Packet * p, int offset);
/ / ----------------------------------------------
void format_protoname (Packet *p, int offset);
/ / ----------------------------------------------
void format_aomdv (Packet * p, int offset);
void format_mdart (Packet * p, int offset);

/ / This holds all the tracers added at run-time
static PacketTracer * pktTrc_;

};
# Endif / * __ cmu_trace__ * /

save and close.

Step 4:

gedit ~/ns-allinone-2.35/ns-2.35/trace/cmu-trace.cc (mods at 3 places)

  • a. Add this line at start

#include <protoname/protoname_pkt.h>

  • b. Near line 1168
void
CMUTrace :: format_mdart (Packet * p, int offset) {
.........
........
........
}

void
CMUTrace::format_protoname(Packet *p, int offset)
{
struct hdr_protoname_pkt* ph = HDR_PROTONAME_PKT(p);
if (pt_->tagged())
{
sprintf(pt_->buffer() + offset, "-protoname:o %d -protoname:s %d -protoname:l %d ", ph->pkt_src(), ph->pkt_seq_num(), ph->pkt_len());
}
else if (newtrace_)
{
sprintf(pt_->buffer() + offset, "-P protoname -Po %d -Ps %d -Pl %d ", ph->pkt_src(), ph->pkt_seq_num(), ph->pkt_len());
}
else
{
sprintf(pt_->buffer() + offset, "[protoname %d %d %d] ", ph->pkt_src(), ph->pkt_seq_num(), ph->pkt_len());
}
}


  • c. Near line 1477
void CMUTrace :: format (Packet * p, const char * why)
{...
switch (ch-> ptype ()) {
case PT_MAC:
...
case PT_GAF:
case PT_PING:
break;
/ / --------------------------------------------
case PT_PROTONAME:
format_protoname(p, offset);
break;
/ / --------------------------------------------
default:
...
}

save and close.

Step 5:

gedit ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-packet.tcl  (mod at 1 place)

  • a. Near line 172

# Mobility, Ad-Hoc Networks, Sensor Nets:
    AODV     # routing protocol for ad-hoc networks
    Protoname # new routing protocol for ad-hoc networks
    Diffusion     # diffusion/diffusion.cc
    IMEP     # Internet MANET Encapsulation Protocol, for ad-hoc

Save and close.

Step 6:

gedit ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-default.tcl  (mod at 1 place)

  • a. Add at the last.

# Defaults defined for Protoname
Agent/Protoname set accessible_var_ true


save and close.
Step 7:

gedit ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-lib.tcl  (mod at 2 places)

  • a. Near line 671

switch-exact $ routingAgent_ {
...
ManualRtg {
set ragent [$ self create-manual-rtg-agent $
}
# / ------------------------------------------------ -
Protoname 
{
         set ragent [$self create-protoname-agent $node]

}
# / ------------------------------------------------ -
default {
...
}


  • b. Near line 2278

Simulator instproc create-omnimcast-agent {node} {
...
}
# / ------------------------------------------------ -----
Simulator instproc create-protoname-agent {node} {
    # Create Protoname routing agent
    set ragent [new Agent/Protoname [$node node-addr]]
    $self at 0.0 "$ragent start"
    $node set ragent_ $ragent
    return $ragent
}

# / ------------------------------------------------ -----
# XXX These are very simulation-specific methods, why should they belon
Simulator instproc put-in-list {agent} {
...
}


Step 8:

gedit ~/ns-allinone-2.35/ns-2.35/queue/priqueue.cc (mod at 1 place)

  • a. Near line 95.
void
PriQueue :: recv (Packet * p, Handler * h)
{
struct hdr_cmn * ch = HDR_CMN (p);

if (Prefer_Routing_Protocols) {
switch (ch-> ptype ()) {
...
case PT_MDART:
/ / --------------------------------------------
case PT_PROTONAME:
/ / --------------------------------------------
recvHighPriority (p, h);
break;
default:
Queue :: recv (p, h);
}
}
else {
Queue :: recv (p, h);
}
}

save and close.

Step 9:

gedit ~/ns-allinone-2.35/ns-2.35/Makefile (mod at 1 place)

  • a. Near line 336

OBJ_CC = \
tools / random.o tools / rng.o tools / ranvar.o common / misc.o common /
...
wpan/p802_15_4trace.o wpan/p802_15_4transac.o \
apps / pbc.o \
# / / ----------------------------------------------- -
protoname/protoname.o protoname/protoname_rtable.o \
# / / ----------------------------------------------- -
$ (OBJ_STL)

save and close.

Step 10:


build it now, changes done ( run these in terminal in ~/ns-allinone-2.35/ns-2.35 directory )

  • a. make clean
  • b. touch common/packet.cc
  • c. make
  • d. sudo make install

(if you are getting some errors check the spaces in the editing you did above)


Step 11:


gedit ~/ns-allinone-2.35/ns-2.35/test.tcl

copy and paste

set ns [new Simulator]
$ns node-config -Routing protoname   
set nf [open out.nam w]     
$ns namtrace-all $nf       
set nd [open out.tr w]       
$ns trace-all $nd             
  proc finish {} {
          global ns nf  nd
          $ns flush-trace 
          close $nf       
          close $nd       
          exec nam out.nam &
          exit 0
   }

for {set i 0} {$i < 7} {incr i} {set n($i) [$ns node] }
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
set udp0 [new Agent/UDP]   
$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR] 
$cbr0 set packetSize_ 500     
$cbr0 set interval_ 0.005      
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0
$ns connect $udp0 $null0 
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n(1) $n(2) 
$ns rtmodel-at 2.0 up $n(1) $n(2)   
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run


save and close

ns ~/ns-allinone-2.35/ns-2.35/test.tcl

and boom you are getting the nam (if not sorry troubleshoot once by going through the tutorial again as I am getting the outputs completely and so should you)


The whole credit of this work goes to Francisco J. Ros and Pedro M. Ruiz for their beautiful work and excellent explanation they provide.

3 comments :

  1. Dear NS2 user:
    when I gave make it shows the following error:
    trace/cmu-trace.cc: In member function ‘void CMUTrace::format_mpolsr(Packet*, int)’:
    trace/cmu-trace.cc:1175:46: error: ‘HDR_MPOLSR_PKT’ was not declared in this scope
    struct hdr_mpolsr_pkt* ph = HDR_MPOLSR_PKT(p);
    ^
    trace/cmu-trace.cc:1180:5: error: invalid use of incomplete type ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    ph->pkt_src(),
    ^
    trace/cmu-trace.cc:1175:9: note: forward declaration of ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    struct hdr_mpolsr_pkt* ph = HDR_MPOLSR_PKT(p);
    ^
    trace/cmu-trace.cc:1181:5: error: invalid use of incomplete type ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    ph->pkt_seq_num(),
    ^
    trace/cmu-trace.cc:1175:9: note: forward declaration of ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    struct hdr_mpolsr_pkt* ph = HDR_MPOLSR_PKT(p);
    ^
    trace/cmu-trace.cc:1182:5: error: invalid use of incomplete type ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    ph->pkt_len());
    ^
    trace/cmu-trace.cc:1175:9: note: forward declaration of ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    struct hdr_mpolsr_pkt* ph = HDR_MPOLSR_PKT(p);
    ^
    trace/cmu-trace.cc:1188:5: error: invalid use of incomplete type ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    ph->pkt_src(),
    ^
    trace/cmu-trace.cc:1175:9: note: forward declaration of ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    struct hdr_mpolsr_pkt* ph = HDR_MPOLSR_PKT(p);
    trace/cmu-trace.cc:1189:5: error: invalid use of incomplete type ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    ph->pkt_seq_num(),
    ^
    trace/cmu-trace.cc:1175:9: note: forward declaration of ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    struct hdr_mpolsr_pkt* ph = HDR_MPOLSR_PKT(p);
    ^
    trace/cmu-trace.cc:1190:5: error: invalid use of incomplete type ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    ph->pkt_len());
    ^
    trace/cmu-trace.cc:1175:9: note: forward declaration of ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    struct hdr_mpolsr_pkt* ph = HDR_MPOLSR_PKT(p);
    ^
    trace/cmu-trace.cc:1196:5: error: invalid use of incomplete type ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    ph->pkt_src(),
    ^
    trace/cmu-trace.cc:1175:9: note: forward declaration of ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    struct hdr_mpolsr_pkt* ph = HDR_MPOLSR_PKT(p);
    ^
    trace/cmu-trace.cc:1197:5: error: invalid use of incomplete type ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    ph->pkt_seq_num(),
    ^
    trace/cmu-trace.cc:1175:9: note: forward declaration of ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    struct hdr_mpolsr_pkt* ph = HDR_MPOLSR_PKT(p);
    ^
    trace/cmu-trace.cc:1198:5: error: invalid use of incomplete type ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    ph->pkt_len());
    ^
    trace/cmu-trace.cc:1175:9: note: forward declaration of ‘struct CMUTrace::format_mpolsr(Packet*, int)::hdr_mpolsr_pkt’
    struct hdr_mpolsr_pkt* ph = HDR_MPOLSR_PKT(p);
    ^
    trace/cmu-trace.cc: In member function ‘void CMUTrace::format(Packet*, const char*)’:
    trace/cmu-trace.cc:1458:8: error: ‘PT_AOMDV’ was not declared in this scope
    case PT_AOMDV:
    ^
    Makefile:93: recipe for target 'trace/cmu-trace.o' failed
    make: *** [trace/cmu-trace.o] Error 1

    Help me please. Thank you in advance.

    ReplyDelete
  2. Hello sir,
    Can you suggest any innovative ideas to change the existing aodv unicasting protocol.

    ReplyDelete
  3. Hello sir,
    Can you suggest any innovative ideas to modify the existing aodv unicasting protocol.
    Or aodv multicasting protocol. Thank you

    ReplyDelete