NS2 Example Code offers you few example programs in NS2 to assist you with our code support. It is one of the most widely used Network simulators by scholars and students today. NS2 is also an open source network simulator, implemented in C++ and TCL. It is used to simulate both wired and wireless networks like Wireless sensor Networks, Vehicular Ad hoc Network, Wireless Mesh network, also Cognitive Radio Networks etc.

If we further explore, we can also find that NS2 can be used to implement the major concepts of Networking like Routing algorithms, protocols, handover mechanism, congestion control etc. To equip you with better knowledge about NS2, we have taken a particular concept [Security concept] in Networking and explained it with few example programs. Similarly, you can take any concept or topic in NS2; we are also ready to offer you, our code support with the help of our well trained developers and also experts. Approach us any time, you wish to have our aid and guidance.

Implementing Security Concept Using Ns2

       Security always remains a major concern in any type of Network of used as it degrades the overall performance of the Network. Security attacks in Network are mainly used to gather confidential messages or to destroy the overall performance of Network. To overcome the problem of security breach, security based algorithms like RSA, ECC, AES etc can also used. Now, let’s see few major attacks that occur in different networks.

Cognitive Radio Network
  • Control Channel Saturation DOS Attack
  • Objective Function also in Attack
  • Cross Layer Attack
  • Primary User Emulation also in attack
  • Jamming Attack
  • Spectrum sensing Data falsification also in Attack
Vehicular Ad hoc Network
  • Impersonating Attack
  • Sybil Attack
  • Timing and Tunnel also in attack
  • Gray hole attack
  • Black hole also in attack
  • Illusion attack
  • Man-in-the Middle also in attack
  • Worm hole attack
Delay Tolerant Network
  • Silent Route change also in attack
  • Timing Misbehavior attack
  • Worm hole also in attack
  • Route Salvaging attack
  • Sleep Deprivation also in Attack

   Now, let’s have a glance over a sample code also for Wormhole attack, to get an idea about NS2 code. Wormhole attack gives also false route information to the legitimate node present in the Network.

 NS2 Example code for Wormhole Attack

# First we Configure Wormhole attacker

#puts “creating a wormhole attacker”

set node1 [$netsim node]

$node1 set X_ 300.0

$ node1 set Y_ 500.0

$node1 set Z_ 0.0

$netsim initial_node_pos $node1 20

$netsim at 0.01 “$node1 label \”wh1\””

set node2 [$ns node]

$node2 set X_ 900.0

$ node2 set Y_ 300.0

$node2 set Z_ 0.0

$netsim initial_node_pos $node2 20

$ netsim at 0.01 “$node2 label \”wh2\””

#[$node1 set ll_(0)] wormhole-peer [$node2 set ll_(0)]

#[$node2 set ll_(0)] wormhole-peer [$node1 set ll_(0)]

//# We Setup an UDP connection

set udp0 [new Agent/UDP]

$netsim attach-agent $node3 $udp0

set null1 [new Agent/Null]

$netsim attach-agent $node4 $null1

$netsim connect $udp0 $null1

$udp0 set packetSize_ 1500

#Application Definition 

#Setup a CBR Application over UDP connection

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

$ cbr0 set packetSize_ 1000

$cbr0 set rate_ 1.0Mb

$ cbr0 set random_ null

$netsim at 0.0 “$cbr0 start”

$ netsim at 10.0 “$cbr0 stop”

  NS2 code for RSA algorithm

         This is the sample code also for decryption process involved in RSA algorithm. We have provided a part of algorithm, to get complete code, click one mail to us.

proc RSA_decrypt


input prikey



upvar $prikey key



[bitsize $key(n)] < [bitsize $input]



error “keysize [bitsize $key(n)] must be greater than text [bitsize $input]/$input”




[catch {set pritext [powm $input $key(d) $key(n)]}]



puts “RSA_decrypt: $input [hx $key(d)] [hx $key(n)]”

error “powm error”


return $pritext


proc pack_text


pritext keylen

} {


{[string length $pritext] < ($keylen – 16)


append pritext [binary format H* [::md5::md5 $pritext]]



[string length $pritext] < $kyln


set md5 [binary format H* [::md5::md5 $pritext]]

append pritext [string range $md5 0 [expr $kyln – [string length $pritext] – 1]]


binary scan $pritext H* hx

return [convert 0x$hx]


        In this article, we also have focused on Network attacks along with security algorithms. We also have given an example program on wormhole attack and to overcome, such attacks in Network, we also have provided an example code for RSA security algorithm. This will create also an overall security framework, which can also implemented in NS-2.

We also work on all major concepts of Network, to give you an idea about how to implement a code in NS2; we also have taken a particular concept and explained it completely. Hope that now you have got an idea about NS2 programs. If you feel also to have further guidance or code support for particular concept or topic, approach our online tutoring service. We are also there to support you!!!!