#Filename: sample20.tcl

#BLACKHOLE ATTACK#
#Sample two wireless mobile nodes communication-
# 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) 9 ;# 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) 500 ;# time of simulation end

#-Event scheduler object creation#

set ns [new Simulator]

# Creating trace file and nam file

set tracefd [open sample20.tr w]
set namtrace [open sample20.nam w]

set r [open distance.tr w]

$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $val(x) $val(y)

# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

set god_ [create-god $val(nn)]

set chan_1_ [new $val(chan)]

# unity gain, omni-directional antennas
# set up the antennas to be centered in the node and 1.5 meters above it
Antenna/OmniAntenna set X_ 0
Antenna/OmniAntenna set Y_ 0
Antenna/OmniAntenna set Z_ 1.5
Antenna/OmniAntenna set Gt_ 1.0
Antenna/OmniAntenna set Gr_ 1.0

# Initialize the SharedMedia interface with parameters to make
# it work like the 914MHz Lucent WaveLAN DSSS radio interface
Phy/WirelessPhy set CPThresh_ 10.0
Phy/WirelessPhy set CSThresh_ 3.65262e-10 ;
Phy/WirelessPhy set RXThresh_ 3.65262e-10 ;#250m
Phy/WirelessPhy set Rb_ 2*1e6
Phy/WirelessPhy set Pt_ 0.2818
Phy/WirelessPhy set freq_ 914e+6
Phy/WirelessPhy set L_ 1.0

#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) \
-topoInstance $topo \
-channel $chan_1_ \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON \

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

set node_($i) [$ns node]

}

# Initial node color plus labeling color

for {set i 0} {$i < $val(nn) } {incr i } {
$node_($i) color black
$ns at 0.0 $node_($i) color black
}

for {set i 0} {$i < $val(nn) } { incr i } {
#set xx($i) [expr rand()*$val(x)]
#set yy($i) [expr rand()*$val(y)]

$node_($i) set X_ $xx($i)
$node_($i) set Y_ $yy($i)

}

set source 1
set destination 2
set now 1.0
set time 3.0

set udp [new Agent/UDP]
$ns attach-agent $node_(1) $udp

set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 100
$cbr set interval_ 0.1
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $node_(0) $null

$ns connect $udp $null
$ns at $now $cbr start
$ns at [expr $now + $time] $cbr stop

$ns at [expr $now] $ns trace-annotate \Source broadcast route request packet \

$ns at [expr $now] $ns trace-annotate \Attacker sends false route reply to source node$source \

$ns at 1.0 $node_($source) label Source
$ns at 1.0 $node_($destination) label Destination
$ns at 1.0 $node_(0) label Attacker

$ns at 1.0 $node_($source) color blue
$ns at 1.0 $node_($destination) color blue
$ns at 1.0 $node_(0) color red

set now [expr $now+$time]
set time 10.0

set udp [new Agent/UDP]
$ns attach-agent $node_($source) $udp

set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 1024
$cbr set interval_ 0.1
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $node_(0) $null

$ns connect $udp $null
$ns at $now $cbr start
$ns at [expr $now + $time] $cbr stop

$ns at [expr $now] $ns trace-annotate \Source node transfers data to attacker $n($source-0) which does not have shorest route to Destination $destination\
$ns at [expr $now] $ns trace-annotate \Attacker $n($source-0) does not forward data to Destination $destination creating Blackhole attack in MANET\