Tutorial to implement protoname in ns2.35


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

The whole credit goes to Francisco J. Ros and Pedro M. Ruiz for their beautiful work and excellent explanation they provide. The link to their work is below. It contains a PDF file also which explains you in detail and I recommend you to go through it once.

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

(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)


Comments

Shital said…
Excellent!!! First time I saw somewhat working tip on NS2!!!
Aime Ebonga said…
Got this error

trace/cmu-trace.cc:1176: error: stray '\302' inprogram
trace/cmu-trace.cc:1176: error: stray '\240' inprogram
trace/cmu-trace.cc:1176: error: stray '\302' inprogram
trace/cmu-trace.cc:1176: error: stray '\240' inprogram
...
trace/cmu-trace.cc:1183: error: stray '\302' inprogram
trace/cmu-trace.cc:1183: error: stray '\240' inprogram
Anand Kumar said…
check your cmu.trace.cc file... its a syntactic error.. see you inserted the changes in the right place with right syntax... I did my successfully not in just one attempt... errors come but you need to remove them ... be vigilant ... cheers!!!
Aime Ebonga said…
Thank you I solved that but still got this error

trace/cmu-trace.cc:1120: error: stray _FUNCTION_ not declared

How do I include the FUNCTION?
Frank Wu said…
Greate tutorial! I have followed the orignal tutorial by Francisco J. Ros and Pedro M. Ruiz, and made some modifications to successfully compile it, after which I read your post and used the test script. I was wondering what "protoname" does exactly? I didn't know how to test it until I saw your post. So there must be something I didn't quite understand. Thanks :)
chandan kumar said…
how can i use SPIN routing protocol in wireless sensor network in ns2, i have SPIN code but don't know how to implement it....
hrishikesh said…
can you tell me what protoname does exactly ??
Anand Kumar said…
protoname does not implement any extra ordinary features , it just teaches you basics of creating a user defined protocol and how to add it to ns and use that protocol.

You can read the .pdf file in the protoname folder. It explains everything in detail. first it pings the neighbours to locate them and then find the path.
Anand Kumar said…
read ns-manual . They have taught how to implement your own protocol (if SPIN is a user defined one).
And you can use the above tutorial as reference.
Anand Kumar said…
And ya its a new manet unicast protocol (manet stands for Mobile Adhoc NETworks)
Anand Kumar said…
Have updated the tutorial today with some formatting details and extra information. Hope you like it. :-)
eiei khin said…
Hello..... nice to meet everyone...... I am working the project of black hole attack in AODV protcol. I want to know how can I add new packet type in AODV protocol. I write alarm packet type in aodv protocol such as Hello packet. I declare this packet in aodv_packet.h and then I write the send and receive fun in aodv.h. I use by calling this fun in aodv.cc. When I am compiling Ns-2, there is no error. But, when I run TCL script, there is error such as Invalid AODV pcket format. So, Anybody help me. How can I add alarm packet in aodv protocol??????
deepika said…
hey i m getting dis error
common/tclAppInit.o: In function `Tcl_AppInit':
tclAppInit.cc:(.text+0xec): undefined reference to `et_ns_ptypes'
collect2: error: ld returned 1 exit status
make: *** [ns] Error 1
plz help
Anand Kumar said…
re-check the changes you made.... especially in first 3 steps
Qone Nugget said…
thnx for the tutorial :)! i'm super new to ns2.. but following your instructions i got it working!
but i was curious of step 11.. where exactly is the new implemented protocol being used?
is it this line? $ns node-config -Routing protoname
Anand Kumar said…
trace your program control... use debugging tools
Anand Kumar said…
yup... try using AODV in place of protoname or DSDV...
Unknown said…
excelent tutorial!!
by the way i need a more simple agent to implement on my system
i saw the ping agent is appropriate to my system
do you have a success tutorial in implement new ping agent (because in ns 2.35 already exist a ping agent)
i will change some part of the ping agent, therefore the first thing that i need to do is to imitate the ping agent.
some tutorial i have try but no one is succes like your tutorial
would you be kind to give me information about this..
thanks
Unknown said…
protoname/protoname.cc: In member function ‘void Protoname::recv_protoname_pkt(Packet*)’:
protoname/protoname.cc:112:17: warning: unused variable ‘ih’ [-Wunused-variable]
protoname/protoname.cc:113:28: warning: unused variable ‘ph’ [-Wunused-variable]
protoname/protoname.cc: In member function ‘void Protoname::forward_data(Packet*)’:
protoname/protoname.cc:175:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
make: *** No rule to make target `protoname/protoname/protoname_rtable.o', needed by `ns'. Stop.
Amitha said…
Hello, i got the same error....My mistake was in the protoname. My file name was Protoname.cc. So I couldn't make protoname.o. After I corrected inside the Makefile as protoname/Protoname.o protoname/protoname_rtable.o instead of protoname/protoname.o protoname/protoname_rtable.o , the error is gone.. so i think , your error is in the filename(check for capital letters).
Unknown said…
Can you post a sample wireless tcl file for using protoname protocol?
Unknown said…
I got a error while make but I am able to run test.tcl file successfully.
I want to add anthocnet protocol in NS-2.35.I am not able to know where I need to make the required changes.Can you please suggest some tutorial for it or can help me with it?
Thank you.
Unknown said…
I have implemented my own routing protocol for adhoc network but i am getting empty nam and trace files.. can you tell me what my problem can be? thanks
Unknown said…
i want to add SAODV protocol in ns-2.35
so how to add this in ns-2.35 ???
i have source code of SAODV with all files..
plz help me to add this protocol
mail id: vivektank201299@gmail.com
Anonymous said…
HI. I want to add Bcast_flood protocol in ns-2.35.I followed all yours steps but i have this problem now.
trace/cmu-trace.cc:60:41: fatal error: BCAST_FLOOD/Bcast_flood_pkt.h: No such file or directory
compilation terminated.
make: *** [trace/cmu-trace.o] Error 1
Thank you
Anand Kumar said…
mistake is in step 2.. first try to execute it for protoname... once successful you will get better idea and places where common mistakes are done
Anonymous said…
I have done all yours instrucions in the step 2 and i don't have errors in the packet.h file but if i write make in the terminal i get the same errors .thank you a lot
Unknown said…
This comment has been removed by the author.
Unknown said…
Hello.I want to add a new protocol in ns-2.35 .But i get this error when i do make in termina
make: *** No rule to make target `trace/cmu-trace.o', needed by `ns'. Stop.
Thenx
Unknown said…
I have recompiled the new protocol in ns2, but it works for old protocol.I don't know how to modify the tcl script in ns2. Please, reply as quickly as possible.It will be very helpful for me to implement my project.
Unknown said…
I have recompiled the new protocol in ns2, but it works for old protocol.I don't know how to modify the tcl script in ns2. Please, reply as quickly as possible.It will be very helpful for me to implement my project.
Anand Kumar said…
In step number 11. I have provided you the tcl script for this protocol where line 2 defines the protocol for our script
Unknown said…
hi,

if i want to add a new field for the CBR packet, which file should i change it?

Thx
Anand Kumar said…
you need to override the existing CBR packet for that.. and later update the necessary build files for successful compilation. Use the above given .pdf file. It explains in detail about each step. U have to refer the packet section of the document mainly and its bindings to the project.
Unknown said…
It works for your example but i need to use protoname in wireless topology i try it with my example but it didn't work can you give me a simple test tcl file ?
Unknown said…
can u please tell me what is Protoname? can i as consider a any routing protocol or else ?
Anonymous said…
Is this steps are same for all protocol modifications or only routing protocols?
TA said…
I'm tring to add security protocol in ns 2 and i found security.cc, security.h and security.tcl and i followed the step found in the link "https://www.nsnam.com/2015/03/security-protocol-packet-in-ns2.html" and in the terminal i type ./ configure and then type make after this the out put give me this error "common/tclAppInit.o: In function `Tcl_AppInit':
tclAppInit.cc:(.text+0xdf): undefined reference to `et_ns_ptypes'
collect2: error: ld returned 1 exit status
Makefile:433: recipe for target 'ns' failed
make: *** [ns] Error 1" i don't know what the problem is any one pleas help me.
Maya said…
Hello every I want to do modification in TCP reno protocol what don't know how to do and what to do kindly help me?
Elektro Garage said…
ERR
{command 'ns' not found but can be installed with:
sudo apt get install ns2}

I followed the steps carefully but when I checked by command the terminal reply me [install by----- sudo apt get ns2] but nam is working here ....
but when I run sudo apt get install ns2 then it shows working by checking ns & enter in the terminal...... here I am confused that from where the DSR protocol is working unzipped installed or by command installed ns2 ....
I want to modify the DSR protocol ... where I can implement my modification?

Popular posts from this blog

Installing ns-2.35 on Mountain Lion, Mavericks, Yosemite and El Capitan (OS X 10.8/10.9/10.10/10.11)

Installing ns-2.35 in Ubuntu 12.04, 12.10, 13.04, 13.10 and 14.04