How to Add New Routing Protocol in NS2
Writing routing protocol is fairly easy in NS2, but for beginners it seems very difficult. Therefore, if you are new to NS2 and want to write your own routing protocol, I would strongly recommend to revise AODV source code. Because, I believe AODV source code is straightforward and fairly easy to understand due to the simplicity of the AODV protocol.
Before you begin reading this post, I assume that you have already installed NS2 on Linux. I have used version 2.34, which is current release. If you have not installed yet, DOWNLOAD HERE and INSTALL. Okey, simple requirements to write your own routing protocol
- NS2 installed
- You should know how to program in C/C++.
- Optionally, shell scripting and perl.
Let's start with creating directory of routing protocol. Goto the "$NS_ROOT/ ns-2.34/". Create directory named as wfrp, we call it WSN Flooding-based Routing Protocol in which sink nodes periodically send a beacon message and other nodes construct route towards the sink nodes. Then nodes report to sink node every certain period using UDP protocol. Direct Diffusion may be an example of such protocol, but what we are writing is simpler and has more functionalities.
mkdir wfrp
In the directory we create three files : wrfp.cc, wrfp.h, wrfp_packet.h. Download and put these files in wfrp directory. I will not explain the code here, and if you don't understand just leave comment I will try to answer.
Now, we are going to modify following files. Therefore it is better you backup these files before you start adding new protocol, so that you can easily go back.
- $NS_ROOT/Makefile
- $NS_ROOT/queue/priqueue.cc
- $NS_ROOT/common/packet.h
- $NS_ROOT/trace/cmu-trace.h
- $NS_ROOT/trace/cmu-trace.cc
- $NS_ROOT/tcl/lib/ns-packet.tcl
- $NS_ROOT/tcl/lib/ns-lib.tcl
- $NS_ROOT/tcl/lib/ns-agent.tcl
- $NS_ROOT/tcl/lib/ns-mobilenode.tcl
Let's start with ~/ns-allinone-2.34/ns-2.34/Makefile just add following lien at 269
wfrp/wfrp.o \
Add following lines to ~/ns-allinone-2.34/ns-2.34/queue/priqueue.cc from line 93.
// WFRP patch case PT_WFRP:
To define new routing protocol packet type we have to modify ~/ns-allinone-2.34/ns-2.34/common/packet.h file. We change PT_NTYPE to 63, and for our protocol PT_WFRP = 62. If you have already installed another routing protocol. Just make sure PT_NTYPE is last, and protocol number is ordered sequentially. From line 85 changes would be :
// WFRP packet static const packet_t PT_WFRP = 62; // insert new packet types here static packet_t PT_NTYPE = 63; // This MUST be the LAST one
We make following code change at line 254 of ~/ns-allinone-2.34/ns-2.34/common/packet.h. The code is used that the packet is routing protocol packet and has high priority.
type == PT_AODV || type == PT_WFRP)
And at line 390 of the same file
// WFRP patch name_[PT_WFRP] = "WFRP";
Now we will make NS2 trace our simulation and write it to *something*.tr, in order to do that we have to modify cmu-trace.h and cmu-trace.cc.
To add trace function we add following line to ~/ns-allinone-2.34/ns-2.34/trace/cmu-trace.h at line 163:
void format_wfrp(Packet *p, int offset);
~/ns-allinone-2.34/ns-2.34/trace/cmu-trace.cc must be added following code at line 1071
// WFRP patch
void
CMUTrace::format_wfrp(Packet *p, int offset)
{
struct hdr_wfrp *wh = HDR_WFRP(p);
struct hdr_wfrp_beacon *wb = HDR_WFRP_BEACON(p);
struct hdr_wfrp_error *we = HDR_WFRP_ERROR(p);
switch(wh->pkt_type) {
case WFRP_BEACON:
if (pt_->tagged()) {
sprintf(pt_->buffer() + offset,
"-wfrp:t %x -wfrp:h %d -wfrp:b %d -wfrp:s %d "
"-wfrp:px %d -wfrp:py %d -wfrp:ts %f "
"-wfrp:c BEACON ",
wb->pkt_type,
wb->beacon_hops,
wb->beacon_id,
wb->beacon_src,
wb->beacon_posx,
wb->beacon_posy,
wb->timestamp);
} else if (newtrace_) {
sprintf(pt_->buffer() + offset,
"-P wfrp -Pt 0x%x -Ph %d -Pb %d -Ps %d -Ppx %d -Ppy %d -Pts %f -Pc BEACON ",
wb->pkt_type,
wb->beacon_hops,
wb->beacon_id,
wb->beacon_src,
wb->beacon_posx,
wb->beacon_posy,
wb->timestamp);
} else {
sprintf(pt_->buffer() + offset,
"[0x%x %d %d [%d %d] [%d %f]] (BEACON)",
wb->pkt_type,
wb->beacon_hops,
wb->beacon_id,
wb->beacon_src,
wb->beacon_posx,
wb->beacon_posy,
wb->timestamp);
}
break;
case WFRP_ERROR:
// TODO: need to add code
break;
default:
#ifdef WIN32
fprintf(stderr,
"CMUTrace::format_wfrp: invalid WFRP packet type\n");
#else
fprintf(stderr,
"%s: invalid WFRP packet type\n", __FUNCTION__);
#endif
abort();
}
}
Now we will modify tcl files to create routing agent. First we define protocol name to use in tcl file. It would done by modifying ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-packet.tcl @ line 172
# WFRP patch WFRP
Now we set routing agent by modifying ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-lib.tcl @ line 633
WFRP {
set ragent [$self create-wfrp-agent $node]
}
From line 860 of the same file following code should be added.
Simulator instproc create-wfrp-agent { node } {
# Create WFRP routing agent
set ragent [new Agent/WFRP [$node node-addr]]
$self at 0.0 "$ragent start"
$node set ragent_ $ragent
return $ragent
}
Now we will set port numbers of routing agent. sport is source port, dport is destination port. Modify ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-agent.tcl line 202
Agent/WFRP instproc init args {
$self next $args
}
Agent/WFRP set sport_ 0
Agent/WFRP set dport_ 0
Frankly speaking I have no idea why I have to add following things. But I believe it should be done according to some tutorial : ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-mobilenode.tcl line 201
# Special processing for WFRP
set wfrponly [string first "WFRP" [$agent info class]]
if {$wfrponly != -1 } {
$agent if-queue [$self set ifq_(0)] ;# ifq between LL and MAC
}
We are done. got to ~/ns-allinone-2.34/ns-2.34/ directory and do
make clean make
When the compile is finished, you can test using wfrp_802_15_4.tcl file as :
ns wfrp_802_15_4.tcl
In this test the NODE 0 is sink node, starts sending beacon 1 second after simulation i started, and NODE 10 is reporting node. It starts sending report over CBR/UDP at 5.0 seconds (after simulation is started). Report interval is 2 seconds.
To remove debugging WFRP, uncomment #define DEBUG (line 36 of wfrp.cc & re-make it).
Simple method to analyze NS2 Trace file
Today I am going to show a simple perl code to analyze NS2 trace file as an example of AODV routing protocol. As you know when you run simulation, NS2 generates a trace file like sometrace.tr. It will give a lot of information about your simulation result. Not knowing how to analyze this file it is useless to run NS2 simulator. In this topic we will learn how to compute delivery ratio and message overhead.
First go to your home directory and create bin directory there. We will create trace file here so that we can access it from anywhere we want.
cd ~ mkdir bin cd bin
Download analyze.pl file, which is attached to the post, to the bin directory. I will explain main points of the code. Following code opens a file to write simulation results.
$ofile="simulation_result.csv"; open OUT, ">$ofile" or die "$0 cannot open output file $ofile: $!";
Usually in trace file each line is started with some letter like r, s, D, N. Each of the letters has meaning. For detailed meaning of the letter refer to the NS Manual Page . And following perl code extracts lines which start with "s", which means sent packets. It maybe : control packets (AODV), data packets (cbr). We are only interested in packets those are sent by routers (RTR). If you enable MAC trace, the packets sent or received by MAC layer is also shown.
if (/^s/){
if (/^s.*AODV/) {
$aodvSent++;
if (/^s.*REQUEST/) {
$aodvSendRequest++;
}
elsif (/^s.*REPLY/) {
$aodvSendReply++;
}
}
elsif (/^s.*AGT/) {
$dataSent++;
}
}
REQUEST - AODV Route Request (RREQ) packets
REPLY - AODV Route Reply (RREP) packets;
AGT - Packets those are sent by agent such as cbr, udp, tcp;
And following code counts packet received by each function.
elsif (/^r/){
if (/^r.*AODV/) {
$aodvRecv++;
if (/^r.*REQUEST/) {
$aodvRecvRequest++;
}
elsif (/^r.*REPLY/) {
$aodvRecvReply++;
}
}
elsif (/^r.*AGT/) {
$dataRecv++;
}
}
Finally packets which are dropped are counted using following code :
elsif (/^D/) {
if (/^D.*AODV/) {
if (/^D.*REQUEST/) {
$aodvDropRequest++;
}
elsif (/^D.*REPLY/) {
$aodvDropReply++;
}
}
if (/^D.*RTR/) {
$routerDrop++;
}
}
Now we will analyze example file. In this post I have written about simulating WSN with AODV protocol, download it and do following. ( I am assuming you have already put analyze.pl file into your bin directory). Here is full source code to the analyze file : analyze.pl. More trace analyzer code is available in the this archive.
ns aodv_802_15_4.tcl cat trace-aodv-802-15-4.tr | analyze.pl
The result would something like this :

Adding Malicious Node to AODV
Many people have asked me how to implement malicious drop in AODV. I have decided to write simple code for adding malicious node in AODV ( or in any routing protocol).
First you need to modify aodv.cc and aodv.h files. In aodv.h after
/* The Routing Agent */
class AODV: public Agent {
...
/*
* History management
*/
double PerHopTime(aodv_rt_entry *rt);
...
add following line
bool malicious;
With this variable we are trying to define if the node is malicious or not. In aodv.cc after
/*
Constructor
*/
AODV::AODV(nsaddr_t id) : Agent(PT_AODV), btimer(this), htimer(this), ntimer(this), rtimer(this), lrtimer(this), rqueue() {
index = id;
seqno = 2;
bid = 1;
...
add following line
malicious = false;
The above code is needed to initialize, and all nodes are initially not malicious. Then we will write a code to catch which node is set as malicious. In aodv.cc after
if(argc == 2) {
Tcl& tcl = Tcl::instance();
if(strncasecmp(argv[1], "id", 2) == 0) {
tcl.resultf("%d", index);
return TCL_OK;
}
add following line
if(strcmp(argv[1], "hacker") == 0) {
malicious = true;
return TCL_OK;
}
Now we will do some work in TCL to set a malicious node. Using script in my post , we add following line to set node 5 as malicious node.
$ns at 0.0 "[$mnode_(5) set ragent_] hacker"
You may add this line after
for {set i 0} {$i < $val(nn)} { incr i } {
$ns initial_node_pos $mnode_($i) 10
}
...
Alright, we have set malicious node but we did not tell malicious node what to do. As it is known, rt_resolve(Packet *p) function is used to select next hop node when routing data packets. So, we tell malicious node just drop any packet when it receives. To do that after
/*
Route Handling Functions
*/
void
AODV::rt_resolve(Packet *p) {
struct hdr_cmn *ch = HDR_CMN(p);
struct hdr_ip *ih = HDR_IP(p);
aodv_rt_entry *rt;
...
We add a few lines
// if I am malicious node
if (malicious == true ) {
drop(p, DROP_RTR_ROUTE_LOOP);
// DROP_RTR_ROUTE_LOOP is added for no reason.
}
And implementing malicious node is done. I hope the post will be helpful to design your secure routing protocol.
P.S. Guys please don't ask me c/c++ questions, check your book first
Re-gaining Root Access after Cupcake Install
The Official HTC website has been updated to offer instructions on how users can flash their Android Dev Phones with a factory Android platform system image and associated radio images.
With these system image files, Android dev phone users can:
Keep their Dev Phone up to date with the latest Android system images Test their application on multiple Android platform versions, to ensure compatibility
Restore a corrupted device to a factory state The Android 1.5 radio image, system image, and recovery image can all be downloaded here
After doing this I LOST root access, but I have found out that people easiest way to re-gaining root access. Follow These Steps :
adb shell su mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system cd /system/bin cat sh > su chmod 4755 su
*reboot*
Connect G1 to Ad-hoc network
This guide will help you to connect your G1 to laptop's Ad-hoc wireless network.
Requirements:
1. Rooted G1 phone.
2. Terminal emulator. (You can download it here or from Android market)
3. Busybox for android. (download)
Steps:
1. Install Terminal emulator
I assume that most of you guys already have this since it is essential for root access.
For those who don't have this installed you will have to make sure that you have root access first.
The easyest way to install this application is to download it from Android market, but if you don't have access to internet from your phone, you could install it over ADB or some other way.
2. Install busybox for android
Open Terminal emulator and type following commands:
Become super user:
su
Make new directory for busybox and navigate to it:
mkdir /data/busybox cd /data/busybox
Now you should copy your downloaded busybox file to this new directory. You could do this simply by connecting your phone to the computer over usb cable and copy file to the sdcard or you could push it using ADB service directly to our directory. If you wish to do this over ADB you should do the following.
a) ADB is part of Android SDK and you can freely download it for Windows, Linux or even Mac, just google it. Your downloaded busybox file must be in the same folder with adb or you can specify the different path in adb command if you have it somewhere else. To push the file to the phone using ADB you could type the following command on your computer (offcourse you must first navigate to the folder in which is your Android SDK and then to the subfolder tools):
adb push busybox /data/busybox/busybox
b) The other way is to copy the file to your sdcard and then move it to our folder with cat command, since we don't have cp command to copy the file. To do this you must type the following commands on your phone assuming we didn't leave Terminal emulator and we are still in /data/busybox/ directory.
cat /sdcard/busybox > ./busybox
Now that you have it in your folder you should do the following:
Make busybox binary executable
chmod 755 ./busybox
Execute following command to install busybox:
./busybox --install
Make all the installed tools in folder executable like you did with the first one:
chmod 755 *
Last step is to export it to PATH to make it usable at all time.
export PATH=/data/busybox:$PATH
3. Now you can modify two important files to make your phone able to connect to Ad-hoc
First gain root access with su command like before:
su
Remount /system partition as read-write with the following command:
mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system
Navigate to /system/etc/wifi/
cd /system/etc/wifi
Now you must edit tiwlan.ini file in current directory. I did this with VI editor which is part of busybox. You must know some basic things about VI editor befor you do this. VI operates in two modes. When you open editor you are in command mode and you can't edit your file yet.
To edit file type:
vi tiwlan.ini
First you must navigate trough file with your trackball. Find the following line:
WiFiAdhoc = 0
This is the line we need to edit. To enter edit mode press A on your phone's keyboard. Now move the cursor with trackball to the end off this line. Replace 0 with 1. You must add two more lines in order this to work. To add new line press enter key and then type:
dot11DesiredSSID = HTCG1 dot11DesiredBSSType = 0
Replace HTCG1 with whatever you like. This is SSID of your wireless Ad-hoc network.
Now you must exit back again to command mode. This is done with escape key, and since our keyboard doesn't have escape key we must click down trackball and simultaniously press 1. This is the same as you pressed escape on regular computer keyboard. In command mode again we can't edit the file but we can type commands. Command for saving file is 'w' and for exiting editor is 'q'. Every command is typed after ':' so our command to save file and exit editor will be:
:wq
The next file we need to edit is /data/misc/wifi/wpa_supplicant.conf
Edit it the same way as previous file and add these lines:
network={
ssid="HTCG1"
key_mgmt=NONE
mode=1
}
Save and exit
'mode = 1' indicate that this is Ad-hoc network
4. Make Ad-hoc network on your laptop or computer.
I assume you know how to do that and I won't explain it because you can google it. Only thing to know when you do this is that you have to set SSID for the network same as the SSID in two files you edited and that you must make open network without wep or other security. Yet if you decide to have protected network you must also edit wpa_supplicant.conf file and modify it. Again google it if you want to know more.
5. Finally connect
If you configured correctly your computer wireless network and selected it to connect, you should then disable and enable wireless in your mobile phone settings. If SSID is broadcasted you should see your network and signal strenght in network list. Connect.
