Ubuntu 中 ns2 模拟女巫攻击

发布于 2025-01-12 11:30:36 字数 6729 浏览 1 评论 0原文

这是一个异常检测模拟 ns-2.35 我在 Ubuntu ver16.04 中运行

*set val(chan)          Channel/WirelessChannel      ;# channel type
# here we detect the channel Wireless channel or Satelite channel
set val(prop)          Propagation/TwoRayGround     ;# radio-propagation model
set val(netif)         Phy/WirelessPhy/802_15_4     ;# network interface type
# here phy means physical and we set the kind of physical layer
set val(mac)           Mac/802_15_4                 ;# MAC type
# here we choose the mac type : simple, 802_11...,...
set val(ifq)           Queue/DropTail/PriQueue      ;# interface queue type
# queue type is droptail with priority : routing packets have higher priority then data packets
set val(ll)            LL                           ;# link layer type
set val(ant)           Antenna/OmniAntenna          ;# antenna model
# Omniantenna is antenna without any directions
set val(ifqlen)        100                  ;# max packet in ifq
set val(nn)            220              ;# number of mobilenodes
# setting a val array with index -> set val(nn) means an array named nn with 220 value
set val(anchor)        20               ;# number of anchornodes
set val(anchor_start)  200              ;# start index of anchornodes
set val(mal_start)      215
set val(mal)           5                ;# number of maliciousnodes
set val(rp)            APIT             ;# protocol type
# It's routing protocol type -> DumpAgent(Do nothing), APIT(Localization Algorithm)
set val(x)             100              ;# X dimension of topographychan
set val(y)             80               ;# Y dimension of topography
# x and y are dimensions of the whole space that nodes are in 
set val(stop)          5                ;# simulation period
set val(energymodel)   EnergyModel          ;# Energy Model
set val(initialenergy) 100              ;# value

set ns [new Simulator] ;# creating simulator
set tracefd [open trace-apit-sybi-detect.tr w]
set namtrace [open nam-apit-sybil-detect.nam w]

set node_ [$ns node]

$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $val(x) $val(y) 
# in order to show all things happen in wireless we use this
# anything happens in the channel or nodes will be added in trace file

set dist(5m)  7.69113e-06
set dist(9m)  2.37381e-06
set dist(10m) 1.92278e-06
set dist(11m) 1.58908e-06
set dist(12m) 1.33527e-06
set dist(13m) 1.13774e-06
set dist(14m) 9.81011e-07
set dist(15m) 8.54570e-07
set dist(16m) 7.51087e-07
set dist(20m) 4.80696e-07
set dist(25m) 3.07645e-07
set dist(30m) 2.13643e-07
set dist(35m) 1.56962e-07
set dist(40m) 1.20174e-07
Phy/WirelessPhy set CSThresh_ $dist(40m)
Phy/WirelessPhy set RXThresh_ $dist(40m)
# Configure initial position 
#these are needed by nam 


set topo       [new Topography] 
$topo load_flatgrid $val(x) $val(y)
# set up topography object
# creating an object from topology class
# in this command dimensions of x and y is created and will be grid in order to show the exact location of each node


create-god $val(nn)
#All things happen in nodes in simulation are managed by GOD class so we created an object from GOD class

set chan_ [new $val(chan)]
#create 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) \
             -channel $chan \
             -topoInstance $topo \
             -agentTrace ON \
             -routerTrace ON \
             -macTrace  OFF \
             -movementTrace OFF \
             -energyModel $val(energymodel) 
             -initialEnergy $val(initialenergy) 
             -rxPower 35.28e-3 
             -txPower 31.32e-3 
             -idlePower 712e-6 
             -sleepPower 144e-9

             #-IncomingErrProc MultistateErrorProc 
             #-OutgoingErrProc MultistateErrorProc


#Initialize Global Variables
proc myRand {} {
    global rand_id
    set range 8
    set rand_id [expr {0 + int(rand() * $range)}]
    return $rand_id
}

proc myRand_sybil {} {
    global rand_id
    set range 50
    set rand_id [expr {0 + int(rand() * $range)}]
    return $rand_id
}
Node/MobileNode instproc setPt { val } {
    $self instvar netif_
    $netif_(0) setTxPower $val
}


#nodes creation
for {set i 0} {$i < $val(nn) } { incr i } {
        set node_($i) [$ns node]
}


# Position ,Color ,Label of Anchors
for {set i 0} {$i < $val(anchor_start)} { incr i } {
    $node_($i) set X_ [expr {0 + int(rand() * $val(x))}]
    $node_($i) set Y_ [expr {0 + int(rand() * $val(y))}]

}
for {set i $val(anchor_start)} {$i < $val(mal_start)} { incr i } {
    $node_($i) set X_ [expr {0 + int(rand() * $val(x))}]
    $node_($i) set Y_ [expr {0 + int(rand() * $val(y))}]
    $node_($i) label "Anchor"

}
for {set i $val(mal_start)} {$i < $val(nn)} { incr i } {
    $node_($i) set X_ [expr {0 + int(rand() * $val(x))}]
    $node_($i) set Y_ [expr {0 + int(rand() * $val(y))}]
    $node_($i) label "Malicious"
}


for {set i 0} {$i < $val(nn)} { incr i } {
        $ns initial_node_pos $node_($i) 5
        $node_($i) setPt 31.32e-3
}



#here we created the nodes and the location of nodes is random
#we can also use the $node_($i) random_motion command for random location of nodes. 
#if we set $node_($i) random_motion 0 the location of nodes will be fixed


#start sending beacon message and anchor hello message
for {set i $val(anchor_start)} {$i < $val(mal_start) } { incr i } {
        $ns at 0.0 "[$node_($i) set ragent_] anchor "
        $node_($i) color "blue"
        $ns at 0.0 "$node_($i) color blue"
        $node_($i) setPt 125.28e-3
}

set ma_sybil [myRand_sybil]
for {set i $val(mal_start)} {$i < $val(nn) } { incr i } {
$ns at 0.0 "[$node_($i) set ragent_] malicious [ expr {$ma_sybil}]"
$node_($i) color "red"
$ns at 0.0 "$node_($i) color red"
$node_($i) setPt 125.28e-3
}

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

# Stop the simulation prosedure
# ending nam and the simulation
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at [expr $val(stop) + 0.01] "puts \"end simulation\"; $ns halt"
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
}

$ns run*

但是在终端中运行 tcl 文件后的结果是这样的:

num_nodes is set 220 无法读取“chan”:没有这样的变量 执行时 “-llType $val(ll)
-macType $val(mac)
-ifqType $val(ifq)
-ifqLen $val(ifqlen)
-antType $val(..." (文件“sybil_drd_1.tcl”第 77 行)

请您告诉我应该做什么来修复此错误。

this is an anomaly detection simulation ns-2.35 I ran in Ubuntu ver16.04

*set val(chan)          Channel/WirelessChannel      ;# channel type
# here we detect the channel Wireless channel or Satelite channel
set val(prop)          Propagation/TwoRayGround     ;# radio-propagation model
set val(netif)         Phy/WirelessPhy/802_15_4     ;# network interface type
# here phy means physical and we set the kind of physical layer
set val(mac)           Mac/802_15_4                 ;# MAC type
# here we choose the mac type : simple, 802_11...,...
set val(ifq)           Queue/DropTail/PriQueue      ;# interface queue type
# queue type is droptail with priority : routing packets have higher priority then data packets
set val(ll)            LL                           ;# link layer type
set val(ant)           Antenna/OmniAntenna          ;# antenna model
# Omniantenna is antenna without any directions
set val(ifqlen)        100                  ;# max packet in ifq
set val(nn)            220              ;# number of mobilenodes
# setting a val array with index -> set val(nn) means an array named nn with 220 value
set val(anchor)        20               ;# number of anchornodes
set val(anchor_start)  200              ;# start index of anchornodes
set val(mal_start)      215
set val(mal)           5                ;# number of maliciousnodes
set val(rp)            APIT             ;# protocol type
# It's routing protocol type -> DumpAgent(Do nothing), APIT(Localization Algorithm)
set val(x)             100              ;# X dimension of topographychan
set val(y)             80               ;# Y dimension of topography
# x and y are dimensions of the whole space that nodes are in 
set val(stop)          5                ;# simulation period
set val(energymodel)   EnergyModel          ;# Energy Model
set val(initialenergy) 100              ;# value

set ns [new Simulator] ;# creating simulator
set tracefd [open trace-apit-sybi-detect.tr w]
set namtrace [open nam-apit-sybil-detect.nam w]

set node_ [$ns node]

$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $val(x) $val(y) 
# in order to show all things happen in wireless we use this
# anything happens in the channel or nodes will be added in trace file

set dist(5m)  7.69113e-06
set dist(9m)  2.37381e-06
set dist(10m) 1.92278e-06
set dist(11m) 1.58908e-06
set dist(12m) 1.33527e-06
set dist(13m) 1.13774e-06
set dist(14m) 9.81011e-07
set dist(15m) 8.54570e-07
set dist(16m) 7.51087e-07
set dist(20m) 4.80696e-07
set dist(25m) 3.07645e-07
set dist(30m) 2.13643e-07
set dist(35m) 1.56962e-07
set dist(40m) 1.20174e-07
Phy/WirelessPhy set CSThresh_ $dist(40m)
Phy/WirelessPhy set RXThresh_ $dist(40m)
# Configure initial position 
#these are needed by nam 


set topo       [new Topography] 
$topo load_flatgrid $val(x) $val(y)
# set up topography object
# creating an object from topology class
# in this command dimensions of x and y is created and will be grid in order to show the exact location of each node


create-god $val(nn)
#All things happen in nodes in simulation are managed by GOD class so we created an object from GOD class

set chan_ [new $val(chan)]
#create 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) \
             -channel $chan \
             -topoInstance $topo \
             -agentTrace ON \
             -routerTrace ON \
             -macTrace  OFF \
             -movementTrace OFF \
             -energyModel $val(energymodel) 
             -initialEnergy $val(initialenergy) 
             -rxPower 35.28e-3 
             -txPower 31.32e-3 
             -idlePower 712e-6 
             -sleepPower 144e-9

             #-IncomingErrProc MultistateErrorProc 
             #-OutgoingErrProc MultistateErrorProc


#Initialize Global Variables
proc myRand {} {
    global rand_id
    set range 8
    set rand_id [expr {0 + int(rand() * $range)}]
    return $rand_id
}

proc myRand_sybil {} {
    global rand_id
    set range 50
    set rand_id [expr {0 + int(rand() * $range)}]
    return $rand_id
}
Node/MobileNode instproc setPt { val } {
    $self instvar netif_
    $netif_(0) setTxPower $val
}


#nodes creation
for {set i 0} {$i < $val(nn) } { incr i } {
        set node_($i) [$ns node]
}


# Position ,Color ,Label of Anchors
for {set i 0} {$i < $val(anchor_start)} { incr i } {
    $node_($i) set X_ [expr {0 + int(rand() * $val(x))}]
    $node_($i) set Y_ [expr {0 + int(rand() * $val(y))}]

}
for {set i $val(anchor_start)} {$i < $val(mal_start)} { incr i } {
    $node_($i) set X_ [expr {0 + int(rand() * $val(x))}]
    $node_($i) set Y_ [expr {0 + int(rand() * $val(y))}]
    $node_($i) label "Anchor"

}
for {set i $val(mal_start)} {$i < $val(nn)} { incr i } {
    $node_($i) set X_ [expr {0 + int(rand() * $val(x))}]
    $node_($i) set Y_ [expr {0 + int(rand() * $val(y))}]
    $node_($i) label "Malicious"
}


for {set i 0} {$i < $val(nn)} { incr i } {
        $ns initial_node_pos $node_($i) 5
        $node_($i) setPt 31.32e-3
}



#here we created the nodes and the location of nodes is random
#we can also use the $node_($i) random_motion command for random location of nodes. 
#if we set $node_($i) random_motion 0 the location of nodes will be fixed


#start sending beacon message and anchor hello message
for {set i $val(anchor_start)} {$i < $val(mal_start) } { incr i } {
        $ns at 0.0 "[$node_($i) set ragent_] anchor "
        $node_($i) color "blue"
        $ns at 0.0 "$node_($i) color blue"
        $node_($i) setPt 125.28e-3
}

set ma_sybil [myRand_sybil]
for {set i $val(mal_start)} {$i < $val(nn) } { incr i } {
$ns at 0.0 "[$node_($i) set ragent_] malicious [ expr {$ma_sybil}]"
$node_($i) color "red"
$ns at 0.0 "$node_($i) color red"
$node_($i) setPt 125.28e-3
}

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

# Stop the simulation prosedure
# ending nam and the simulation
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at [expr $val(stop) + 0.01] "puts \"end simulation\"; $ns halt"
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
}

$ns run*

But the results after running tcl file in terminal is this:

num_nodes is set 220
can't read "chan": no such variable
while executing
"-llType $val(ll)
-macType $val(mac)
-ifqType $val(ifq)
-ifqLen $val(ifqlen)
-antType $val(..."
(file "sybil_drd_1.tcl" line 77)

May you please tell me what should I do to fix this Error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文