You are on page 1of 65

INDEX

Serial Number Aim Date Remarks


1. Experiment 1: Introduction of various 25 March 2021
tools for Adhoc and Sensor Networks
Protocols.
2. Experiment 2: Introduction and 1 April 2021
Installation of Network Simulator 2
(NS2).
3. Experiment 3: Implementation of wired 8 April 2021
node connection in Network Simulator
2(NS2).
4. Experiment 4: Implementation of 15 April 2021
AODV (Adhoc on demand distance
vector routing protocol) in NS2.

5. Experiment 5: Implementation of 22 April 2021


DSDV (Destination Sequence Distance
Vector) in NS2.
6. Experiment 6: Implementation of DSR 29 April 2021
(Dynamic Source Routing) in NS2.

7. Experiment 7: Implementation of 6 May 2021


FAMA- NCS protocol in NS2.

8. Experiment 8: Implementation of 13 May 2021


synchronous protocol in NS2.

9. Content Beyond Syllabus 1: 20 May 2021


Implementation of CTS and RTS
approach in NS2.
10. Content Beyond Syllabus 2: Black 27 May 2021
Hole Attacks in Network Simulator
NS2.
Experiment 3
● Aim: Implementation of wired node connection in Network Simulator
2(NS2).
● Software Used: Network Simulator 2(NS2)
● Code:
# Filename: test1.tcl
#——-Event scheduler object creation——–#
set ns [new Simulator]
#———-creating trace objects—————-#
set nt [open test1.tr w]
$ns trace-all $nt
#———-creating nam objects—————-#
set nf [open test1.nam w]
$ns namtrace-all $nf
#———-Setting color ID—————-#
$ns color 1 darkmagenta
$ns color 2 yellow
$ns color 3 blue
$ns color 4 green
$ns color 5 black
#———- Creating Network—————-#
set totalNodes 3
for {set i 0} {$i < $totalNodes} {incr i} {
set node_($i) [$ns node]
}
set server 0
set router 1
set client 2
#———- Creating Duplex Link—————-#
$ns duplex-link $node_($server) $node_($router) 2Mb 50ms
DropTail
$ns duplex-link $node_($router) $node_($client) 2Mb 50ms
DropTail
$ns duplex-link-op $node_($server) $node_($router) orient right
$ns duplex-link-op $node_($router) $node_($client) orient right
#————Labelling—————-#
$ns at 0.0 "$node_($server) label Server"
$ns at 0.0 "$node_($router) label Router"
$ns at 0.0 "$node_($client) label Client"
$ns at 0.0 "$node_($server) color blue"
$ns at 0.0 "$node_($client) color blue"
$node_($server) shape hexagon
$node_($client) shape hexagon
#———finish procedure——–# proc
finish {} {
global ns nf nt
$ns flush-trace
close $nf close
$nt
puts "running nam…"
exec nam test1.nam &
exit 0
}
#Calling finish procedure
$ns at 10.0 "finish"
$ns run
#——— Execution ——–#
ns test2.tcl
● Output :
● Result: The code to implement wired node connection in Network
Simulator 2(NS2) was successfully completed.
Experiment 4
● Aim: Implementation of AODV (Adhoc on demand distance vector
routing protocol) in NS2.
● Software Used: Network Simulator 2(NS2)
● Code:
#Filename: sample15.tcl
#**************AODV ROUTING
PROTOCOL***************************#
# Simulator Instance Creation
set ns [new Simulator]
#Fixing the co-ordinate of simutaion area
set val(x) 500
set val(y) 500
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation
model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 10 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
set val(x) 500 ;# X dimension of topography
set val(y) 500 ;# Y dimension of topography
set val(stop) 10.0 ;# time of simulation end
# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# general operational descriptor- storing the hop details in
the network
create-god $val(nn)
# configure the nodes
$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON
# Node Creation
for {set i 0} {$i < 10} {incr i} {
set node_($i) [$ns node]
$node_($i) color black
}
for {set i 0} {$i < 10} {incr i} {
$node_($i) set X_ [expr rand()*$val(x)]
$node_($i) set Y_ [expr rand()*$val(y)]
$node_($i) set Z_ 0
}
#******************************Defining Communication Between
node0 and all nodes ****************************8
for {set i 1} {$i < 10} {incr i} {
# Defining a transport agent for sending
set udp [new Agent/UDP]
# Attaching transport agent to sender node
$ns attach-agent $node_($i) $udp
# Defining a transport agent for receiving
set null [new Agent/Null]
# Attaching transport agent to receiver node
$ns attach-agent $node_(0) $null
#Connecting sending and receiving transport agents
$ns connect $udp $null
#Defining Application instance
set cbr [new Application/Traffic/CBR]
# Attaching transport agent to application agent
$cbr attach-agent $udp
#Packet size in bytes and interval in seconds definition
$cbr set packetSize_ 512
$cbr set interval_ 0.1
# data packet generation starting time
$ns at 1.0 "$cbr start"
# data packet generation ending time
#$ns at 6.0 "$cbr stop"
}
int hdr_aodv::offset_;
static class aodvHeaderClass : public PacketHeaderClass {
public: aodvHeaderClass() :
PacketHeaderClass("PacketHeader/aodv",

sizeof(hdr_all_aodv)) {
bind_offset(&hdr_aodv::offset_);
}
} class_rtProtoaodv_hdr;

static class aodvclass : public TclClass {


public:
aodvclass() : TclClass("Agent/aodv") {}
TclObject* create(int argc, const char*const* argv)
{ assert(argc == 5);
return (new aodv((nsaddr_t)
Address::instance().str2addr(argv[4])));
}
} class_rtProtoaodv;

int
aodv::command(int argc, const char*const* argv) {
if(argc == 2) {
Tcl& tcl = Tcl::instance();

if(strncasecmp(argv[1], "id", 2) == 0) {
tcl.resultf("%d", index);
return TCL_OK;
}
if (strncasecmp(argv[1], "dump-table", 10) == 0) {
printf("Node %d: Route table:\n", index);
rtable.rt_dumptable();
return TCL_OK;
}
if(strncasecmp(argv[1], "start", 2) == 0) {
btimer.handle((Event*) 0);

#ifndef aodv_LINK_LAYER_DETECTION
htimer.handle((Event*) 0);
ntimer.handle((Event*) 0);
#endif
rtim
er.h
andl
e((E
vent
*)
0);
retu
rn
TCL_
OK;}
● Output:

● Result: The code to implement AODV (Adhoc on demand distance


vector routing protocol) in NS2 was successfully completed.
Experiment 5
● Aim: Implementation of DSDV (Destination Sequence Distance Vector) in
NS2.
● Software Used: Network Simulator 2(NS2)
● Code:
# wrls1.tcl
# A 3-node example for ad-hoc simulation with DSDV

# Define options
setval(chan)
Channel/WirelessChannel
;#
channel
type
setval(prop)
propagationmodel

Propagation/TwoRayGround

;#

radio-
setval(netif)
Phy/WirelessPhy
;#
network
interfacetype

setval(mac)
Mac/802_11
;#
MACtype
set val(stop) 150 ;# time of simulation end
setval(ifq)
set ns [new Simulator]
set tracefd [open simple-dsdv.tr w]
set windowVsTime2 [open win.tr w]
set namtrace [open simwrls.nam w]

$ns trace-all $tracefd


$ns use-newtrace
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
# set up topography object
set topo [new Topography]

$topo load_flatgrid $val(x)

$val(y) create-god $val(nn)

#
# Create nn mobilenodes [$val(nn)] and attach them to the
channel.
#

# configure the nodes


$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON

for {set i 0} {$i < $val(nn) } { incr i } {


set node_($i) [$ns node]
}

# Provide initial location of mobilenodes


$node_(0) set X_ 5.0
$node_(0) set Y_ 5.0
$node_(0) set Z_ 0.0

$node_(1) set X_ 490.0


$node_(1) set Y_ 285.0
$node_(1) set Z_ 0.0

$node_(2) set X_ 150.0


$node_(2) set Y_ 240.0
$node_(2) set Z_ 0.0

# Generation of movements
$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
$ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
$ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"

# Set a TCP connection between node_(0) and node_(1)


set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcp
$ns attach-agent $node_(1) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 10.0 "$ftp start"

# Printing the window size


proc plotWindow {tcpSource file} {
global ns
set time 0.01 set
now [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $file "$now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file" }
$ns at 10.1 "plotWindow $tcp $windowVsTime2"

# Define node initial position in nam


for {set i 0} {$i < $val(nn)} { incr i } {
# 30 defines the node size for nam
$ns initial_node_pos $node_($i) 30
}
# Telling nodes when the simulation ends
for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "$node_($i) reset";
}

# ending nam and the simulation


$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at 150.01 "puts \"end simulation\" ; $ns halt"
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
$ns run
}
● Output:
● Result: The code to implement DSDV (Destination Sequence Distance
Vector) in NS2 was successfully completed.
E
xperiment 6
● Aim: Implementation of DSR (Dynamic Source Routing) in NS2.
● Software Used: Network Simulator 2(NS2)
● Code:
clc;
#Filename: sample16.tcl #***************DSR ROUTING
PROTOCOL***************************#
# Simulator Instance Creation
set ns [new Simulator]
#Fixing the co-ordinate of simutaion area
set val(x) 500
set val(y) 500
# Define options
set val(chan) Channel/WirelessChannel ;
# channel type
set val(prop) Propagation/TwoRayGround ;
# radio-propagation model
set val(netif) Phy/WirelessPhy ;
# network interface type
set val(mac) Mac/802_11 ;
# MAC type
set val(ifq) CMUPriQueue ;
# Interface queue type
set val(ll) LL ;
# link layer type
set val(ant) Antenna/OmniAntenna ;
# antenna model
set val(ifqlen) 50 ;
# max packet in ifq
set val(nn) 10 ;
# number of mobilenodes
set val(rp) DSR ;
# routing protocol
set val(x) 500 ;
# X dimension of topography
set val(y) 500 ;
# Y dimension of topography
set val(stop) 10.0 ;
# time of simulation end
# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# general operational descriptor- storing the hop details in
the network
create-god $val(nn)
# configure the nodes
$ns node-config -adhocRouting $val(rp) \ -llType $val(ll) \ -
macType $val(mac) \ -ifqType $val(ifq) \ -ifqLen $val(ifqlen) \
-antType $val(ant) \ -propType $val(prop) \ -phyType
$val(netif) \ -channelType $val(chan) \ -topoInstance $topo \ -
agentTrace ON \ -routerTrace ON \ -macTrace OFF \ -
movementTrace ON
# Node Creation for
{set i 0} {$i < 3} {incr i}
{ set node_($i) [$ns node] $node_($i) color black }
for {set i 0} {$i < 3} {incr i} { $node_($i)
set X_ [expr rand()*$val(x)]
$node_($i) set Y_ [expr rand()*$val(y)]
$node_($i) set Z_ 0 }

Defining Communication between node0 and all nodes


****************************
for {set i 1} {$i < 3} {incr i}
{ # Defining a transport agent for sending set udp [new
Agent/UDP]
# Attaching transport agent to sender node $ns attach-agent
$node_($i) $udp
# Defining a transport agent for receiving set null [new
Agent/Null]
# Attaching transport agent to receiver node $ns attach-agent
$node_(0) $null
#Connecting sending and receiving transport agents
$ns connect $udp $null
#Defining Application instance
set cbr [new Application/Traffic/CBR]
# Attaching transport agent to application agent
$cbr attach-agent
$udp #Packet size in bytes and interval in seconds definition
$cbr set packetSize_ 512
$cbr set interval_ 0.1 # data packet generation starting time
$ns at 1.0 "$cbr start" # data packet generation ending time
#$ns at 6.0 "$cbr stop"
}
● Output:
● Result: The code to implement DSR (Dynamic Source Routing) in NS2 was
successfully completed.
E
xperiment 7
● Aim: Implementation of FAMA-NCS protocol in NS2.
● Software Used: Network Simulator 2(NS2).
● Working Specification:
● Output:

● Result: The code to implement FAMA-NCS protocol in NS2 was


successfully completed.
E
xperiment 8
● Aim: Implementation of synchronous protocol in NS2.
● Software Used: Network Simulator (NS2).
● Code:
set ns [new Simulator]
$ns rtproto LS
set node1 [$ns node]
set node2 [$ns node]
set node3 [$ns node]
set node4 [$ns node]
set node5 [$ns node]
set node6 [$ns node]
set node7 [$ns node]
set tf [open out.tr w]
$ns trace-all $tf
set nf [open out.nam w]
$ns namtrace-all $nf
$node1 label "node 1"
$node1 label "node 2"
$node1 label "node 3"
$node1 label "node 4"
$node1 label "node 5"
$node1 label "node 6"
$node1 label "node 7"
$node1 label-color blue
$node2 label-color red
$node3 label-color red
$node4 label-color blue
$node5 label-color blue
$node6 label-color blue
$node7 label-color blue
$ns duplex-link $node1 $node2 1.5Mb 10ms DropTail
$ns duplex-link $node2 $node3 1.5Mb 10ms DropTail
$ns duplex-link $node3 $node4 1.5Mb 10ms DropTail
$ns duplex-link $node4 $node5 1.5Mb 10ms DropTail
$ns duplex-link $node5 $node6 1.5Mb 10ms DropTail
$ns duplex-link $node6 $node7 1.5Mb 10ms DropTail
$ns duplex-link $node7 $node1 1.5Mb 10ms DropTail
$ns duplex-link-op $node1 $node2 orient left-down
$ns duplex-link-op $node2 $node3 orient left-down
$ns duplex-link-op $node3 $node4 orient right-down
$ns duplex-link-op $node4 $node5 orient right
$ns duplex-link-op $node5 $node6 orient right-up
$ns duplex-link-op $node6 $node7 orient left-up
$ns duplex-link-op $node7 $node1 orient left-up
set tcp2 [new Agent/TCP]
$ns attach-agent $node1 $tcp2
set sink2 [new Agent/TCPSink]
$ns attach-agent $node4 $sink2
$ns connect $tcp2 $sink2
set traffic_ftp2 [new Application/FTP]
$traffic_ftp2 attach-agent $tcp2
proc finish{} {

global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0

}
$ns at 0.5 "traffic_ftp2 start"
$ns rtmodel-at 1.0 down $node2 $node3
$ns rtmodel-at 2.0 up $node2 $node3
$ns at 3.0 "traffic_ftp2 start"
$ns at 4.0 "traffic_ftp2 stop"
$ns at 5.0 "finish"
$ns run
● Output:
● Result: The code to implement synchronous protocol in NS2 was
successfully completed.
Content Beyond Syllabus-1
● Aim: Implementation of CTS and RTS approach in NS2.
● Software Used: Network Simulator 2(NS2)
● Code:
clc;
set MESSAGE_PORT 42
set BROADCAST_ADDR -1
#set val(chan) Channel/WirelessChannel ;#Channel
Type
set val(prop) Propagation/TwoRayGround ;# radio-
propagation model
set val(netif) Phy/WirelessPhy ;# network
interface type
set val(mac) Mac/802_11 ;# MAC type
#set val(mac) M ;# MAC type
#set val(mac) a
c
Mac/Simple
set val(ifq) Queue/DropTail/ ;# interfa
PriQueue ce
queue type
set val(ll) LL ;# link
layer type
set val(ant) Antenna/OmniAntenna ;# antenna
model
set val(ifqlen) 32768 ;# max
packet in ifq
set val(rp) DumbAgent
set ns [new Simulator]
set f [open rts-cts-data-ack.tr w]
$ns trace-all $f
$ns eventtrace-all
set nf [open rts-cts-data-ack-temp.nam w]
$ns namtrace-all-wireless $nf 700 200
# set up topography object
set topo [new Topography]
$topo load_flatgrid 700 200
$ns color 3 green;
$ns color 8 red;
$ns color 1 black;
$ns color 7 purple;
$ns color 6 tan;
$ns color 2 orange;
# Create God
create-god 3
#set mac0 [new Mac/802_11]
$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType Channel/WirelessChannel \
-topoInstance $topo \
-agentTrace ON \
-routerTrace OFF \
-macTrace ON \
-movementTrace OFF
for {set i 0} {$i < 3} {incr i} {
set node_($i) [$ns node]
$node_($i) random-motion 0
}
$node_(0) color black
$node_(1) color black
$node_(2) color black
$node_(0) set X_ 200.0
$node_(0) set Y_ 30.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 330.0
$node_(1) set Y_ 150.0
$node_(1) set Z_ 0.0
$node_(2) set X_ 60.0
$node_(2) set Y_ 30.0
$node_(2) set Z_ 0.0
$ns at 0.6 "$node_(2) setdest 330.0 30.0 10000.0"
$ns at 1.1 "$node_(2) setdest 500.0 30.0 10000.0"
# subclass Agent/MessagePassing to make it do flooding
Class Agent/MessagePassing/Flooding -superclass
Agent/MessagePassing
Agent/MessagePassing/Flooding instproc recv {source sport size
data} {
$self instvar messages_seen node_global ns 1
# extract message ID from message
set message_id [lindex [split $data ":"] 0]
puts "\nNode [$node_ node-addr] got message
$message_id\n"
if {[lsearch $messages_seen $message_id] == -1} {
lappend messages_seen $message_id
$ns trace-annotate "[$node_ node-addr] received
{$data} from $source"
$ns trace-annotate "[$node_ node-addr] sending
message $message_id"
$self sendto $size $data 1 $sport
} else {
$ns trace-annotate "[$node_ node-addr] received
redundant message $message_id from $source"
}
}
Agent/MessagePassing/Flooding instproc send_message {size
message_id data port} {
$self instvar messages_seen node_
global ns MESSAGE_PORT 1
lappend messages_seen $message_id
$ns trace-annotate "[$node_ node-addr] sending message
$message_id"
$self sendto $size "$message_id:$data" 1 $port
}
# attach a new Agent/MessagePassing/Flooding to each node on
port $MESSAGE_PORT
for {set i 0} {$i < 3} {incr i} {
set a($i) [new Agent/MessagePassing/Flooding]
$node_($i) attach $a($i) $MESSAGE_PORT
$a($i) set messages_seen {}
}
$ns at 0.1 "$a(0) send_message 500 1 {first_message}
$MESSAGE_PORT"
$ns at 0.1 "$a(2) send_message 500 2 {second_message}
$MESSAGE_PORT"
$ns at 0.8 "$a(0) send_message 500 5 {fifth_message}
$MESSAGE_PORT"
$ns at 0.8 "$a(2) send_message 500 6 {sixth_message}
$MESSAGE_PORT"
$ns at 1.3 "$a(2) send_message 500 15 {fifteenth_message}
$MESSAGE_PORT"
$ns at 1.3 "$a(0) send_message 500 16 {sixteenth_message}
$MESSAGE_PORT"
for {set i 0} {$i < 3} {incr i} {
$ns initial_node_pos $node_($i) 30
$ns at 20.0 "$node_($i) reset";
}
$ns at 20.0 "finish"
$ns at 20.1 "puts \"NS EXITING...\"; $ns halt"
#INSERT ANNOTATIONS HERE
proc finish {} {
global ns f nf val
$ns flush-trace
close $f
close $nf
}
puts "Starting Simulation..."
$ns run

● Output:
● Result: The code to implement CTS and RTS approach in NS2 was
successfully completed.
Content Beyond Syllabus-2
● Aim: Black Hole Attacks in Network Simulator NS2.
● Software Used: Network Simulator 2(NS2).
● Code:
nsaddr_t malicious1;
nsaddr_t malicious2;
nsaddr_t malicious3;
#code for initializing black hole attackers:
int
AODV::command(intargc, const char*const* argv) {
if(argc == 2) {
Tcl&amp;tcl = Tcl::instance();
if(strncasecmp(argv[1], "id", 2) 0 {
== )
tcl.resultf("%d",
index); return TCL_OK;
}
if(strcmp(argv[1], "blackhole1") 0 {
== )
malicious1= index;
printf("malicious %d",
malicious1);
return TCL_OK;
}
if(strcmp(argv[1], "blackhole2") 0 {
== )
malicious2=index;
printf("malicious %d",
malicious2);
return TCL_OK;
}
if(strcmp(argv[1], "blackhole3") 0 {
== )
malicious3= index;
printf("malicious %d",
malicious3);
return TCL_OK;
}
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;
LIST_INIT(&amp;nbhead);
LIST_INIT(&amp;bihead);
malicious1=999;
malicious2=999;
malicious3=999;a
● Output:

● Result: The code to implement Black Hole Attacks in Network


Simulator NS2 was successfully completed.

You might also like