LEFT | RIGHT |
(no file at all) | |
| 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. |
| 4 * |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 * |
| 18 * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu> |
| 19 */ |
| 20 · |
| 21 #include "reactive-jammer.h" |
| 22 #include "ns3/simulator.h" |
| 23 #include "ns3/assert.h" |
| 24 #include "ns3/log.h" |
| 25 #include "ns3/double.h" |
| 26 #include "ns3/uinteger.h" |
| 27 |
| 28 NS_LOG_COMPONENT_DEFINE ("ReactiveJammer"); |
| 29 |
| 30 /* |
| 31 * Reactive Jammer |
| 32 */ |
| 33 namespace ns3 { |
| 34 ········ |
| 35 NS_OBJECT_ENSURE_REGISTERED (ReactiveJammer); |
| 36 ········ |
| 37 TypeId |
| 38 ReactiveJammer::GetTypeId (void) |
| 39 { |
| 40 static TypeId tid = TypeId ("ns3::ReactiveJammer") |
| 41 .SetParent<Jammer> () |
| 42 .AddConstructor<ReactiveJammer> () |
| 43 .AddAttribute ("ReactiveJammerTxPower", |
| 44 "Power to send jamming signal for reactive jammer, in Watts."
, |
| 45 DoubleValue (0.001), // 1mW = 0dBm |
| 46 MakeDoubleAccessor (&ReactiveJammer::SetTxPower, |
| 47 &ReactiveJammer::GetTxPower), |
| 48 MakeDoubleChecker<double> ()) |
| 49 .AddAttribute ("ReactiveJammerJammingDuration", |
| 50 "Jamming duration for constant jammer.", |
| 51 TimeValue (MilliSeconds (5.0)), |
| 52 MakeTimeAccessor (&ReactiveJammer::SetJammingDuration, |
| 53 &ReactiveJammer::GetJammingDuration), |
| 54 MakeTimeChecker ()) |
| 55 .AddAttribute ("ReactiveJammerRxTxSwitchingDelay", |
| 56 "Reactive jammer rx to tx switching delay.", |
| 57 TimeValue (MicroSeconds (500.0)), |
| 58 MakeTimeAccessor (&ReactiveJammer::SetRxTxSwitchingDelay, |
| 59 &ReactiveJammer::GetRxTxSwitchingDelay), |
| 60 MakeTimeChecker ()) |
| 61 .AddAttribute ("ReactiveJammerReactionStrategy", |
| 62 "Reaction strategy of the reactive jammer", |
| 63 UintegerValue (0), // default to energy-aware |
| 64 MakeUintegerAccessor (&ReactiveJammer::SetReactionStrategy, |
| 65 &ReactiveJammer::GetReactionStrategy), |
| 66 MakeUintegerChecker<uint32_t> ()) |
| 67 .AddAttribute ("ReactiveJammerFixedProbability", |
| 68 "Fixed probability of reacting to packets, for reactive jamme
r.", |
| 69 DoubleValue (1.0), // default to *always* react to packets |
| 70 MakeDoubleAccessor (&ReactiveJammer::SetFixedProbability, |
| 71 &ReactiveJammer::GetFixedProbability), |
| 72 MakeDoubleChecker<double> ()) |
| 73 .AddAttribute ("ReactiveJammerRxTimeout", |
| 74 "Reactive jammer RX timeout.", |
| 75 TimeValue (Seconds (2.0)), |
| 76 MakeTimeAccessor (&ReactiveJammer::SetRxTimeout, |
| 77 &ReactiveJammer::GetRxTimeout), |
| 78 MakeTimeChecker ()) |
| 79 .AddAttribute ("ReactiveJammerReactToMitigation", |
| 80 "Reactive jammer react to mitigation flag, set to enable chas
ing.", |
| 81 UintegerValue (false), // default to not react to mitigation |
| 82 MakeUintegerAccessor (&ReactiveJammer::SetReactToMitigation, |
| 83 &ReactiveJammer::GetReactToMitigation), |
| 84 MakeUintegerChecker<bool> ()) |
| 85 ; |
| 86 return tid; |
| 87 } |
| 88 |
| 89 ReactiveJammer::ReactiveJammer () |
| 90 : m_random (0.0, 1.0), |
| 91 m_reactToMitigation (false) |
| 92 { |
| 93 } |
| 94 |
| 95 ReactiveJammer::~ReactiveJammer () |
| 96 { |
| 97 } |
| 98 |
| 99 void |
| 100 ReactiveJammer::SetUtility (Ptr<WirelessModuleUtility> utility) |
| 101 { |
| 102 NS_LOG_FUNCTION (this << utility); |
| 103 NS_ASSERT (utility != NULL); |
| 104 m_utility = utility; |
| 105 } |
| 106 |
| 107 void |
| 108 ReactiveJammer::SetEnergySource (Ptr<EnergySource> source) |
| 109 { |
| 110 NS_LOG_FUNCTION (this << source); |
| 111 NS_ASSERT (source != NULL); |
| 112 m_source = source; |
| 113 } |
| 114 |
| 115 void |
| 116 ReactiveJammer::SetTxPower (double power) |
| 117 { |
| 118 NS_LOG_FUNCTION (this << power); |
| 119 m_txPower = power; |
| 120 } |
| 121 |
| 122 double |
| 123 ReactiveJammer::GetTxPower (void) const |
| 124 { |
| 125 NS_LOG_FUNCTION (this); |
| 126 return m_txPower; |
| 127 } |
| 128 |
| 129 void |
| 130 ReactiveJammer::SetJammingDuration (Time duration) |
| 131 { |
| 132 NS_LOG_FUNCTION (this << duration); |
| 133 m_jammingDuration = duration; |
| 134 } |
| 135 |
| 136 Time |
| 137 ReactiveJammer::GetJammingDuration (void) const |
| 138 { |
| 139 NS_LOG_FUNCTION (this); |
| 140 return m_jammingDuration; |
| 141 } |
| 142 |
| 143 void |
| 144 ReactiveJammer::SetRxTxSwitchingDelay (Time delay) |
| 145 { |
| 146 NS_LOG_FUNCTION (this << delay); |
| 147 NS_ASSERT (delay.GetSeconds () >= 0); |
| 148 m_rxTxSwitchingDelay = delay; |
| 149 } |
| 150 |
| 151 Time |
| 152 ReactiveJammer::GetRxTxSwitchingDelay (void) const |
| 153 { |
| 154 NS_LOG_FUNCTION (this); |
| 155 return m_rxTxSwitchingDelay; |
| 156 } |
| 157 |
| 158 void |
| 159 ReactiveJammer::SetReactionStrategy(ReactionStrategy strategy) |
| 160 { |
| 161 NS_LOG_FUNCTION (this << strategy); |
| 162 m_reactionStrategy = strategy; |
| 163 } |
| 164 |
| 165 uint32_t |
| 166 ReactiveJammer::GetReactionStrategy(void) const |
| 167 { |
| 168 NS_LOG_FUNCTION (this); |
| 169 return m_reactionStrategy; |
| 170 } |
| 171 |
| 172 void |
| 173 ReactiveJammer::SetFixedProbability(double probability) |
| 174 { |
| 175 NS_LOG_FUNCTION (this << probability); |
| 176 NS_ASSERT (probability >=0 && probability <= 1); |
| 177 m_fixedProbability = probability; |
| 178 } |
| 179 |
| 180 double |
| 181 ReactiveJammer::GetFixedProbability (void) const |
| 182 { |
| 183 NS_LOG_FUNCTION (this); |
| 184 return m_fixedProbability; |
| 185 } |
| 186 |
| 187 void |
| 188 ReactiveJammer::SetRxTimeout (Time rxTimeout) |
| 189 { |
| 190 NS_LOG_FUNCTION (this << rxTimeout); |
| 191 m_rxTimeout = rxTimeout; |
| 192 } |
| 193 |
| 194 Time |
| 195 ReactiveJammer::GetRxTimeout (void) const |
| 196 { |
| 197 NS_LOG_FUNCTION (this); |
| 198 return m_rxTimeout; |
| 199 } |
| 200 |
| 201 void |
| 202 ReactiveJammer::SetReactToMitigation (const bool flag) |
| 203 { |
| 204 NS_LOG_FUNCTION (this << flag); |
| 205 m_reactToMitigation = flag; |
| 206 } |
| 207 |
| 208 bool |
| 209 ReactiveJammer::GetReactToMitigation (void) const |
| 210 { |
| 211 NS_LOG_FUNCTION (this); |
| 212 return m_reactToMitigation; |
| 213 } |
| 214 |
| 215 /* |
| 216 * Private functions start here. |
| 217 */ |
| 218 |
| 219 void |
| 220 ReactiveJammer::DoDispose (void) |
| 221 { |
| 222 NS_LOG_FUNCTION (this); |
| 223 m_jammingEvent.Cancel (); |
| 224 } |
| 225 |
| 226 void |
| 227 ReactiveJammer::DoStopJammer (void) |
| 228 { |
| 229 NS_LOG_FUNCTION (this); |
| 230 m_jammingEvent.Cancel (); |
| 231 } |
| 232 |
| 233 void |
| 234 ReactiveJammer::DoJamming (void) |
| 235 { |
| 236 NS_LOG_FUNCTION (this); |
| 237 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 238 ", Started!"); |
| 239 |
| 240 if (m_reactToMitigation) |
| 241 { |
| 242 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 243 ", After jammer starts, scheduling RX timeout!"); |
| 244 m_rxTimeoutEvent = Simulator::Schedule (m_rxTimeout, |
| 245 &ReactiveJammer::RxTimeoutHandler, |
| 246 this); |
| 247 } |
| 248 } |
| 249 |
| 250 bool |
| 251 ReactiveJammer::DoStartRxHandler (Ptr<Packet> packet, double startRss) |
| 252 { |
| 253 NS_LOG_FUNCTION (this << packet << startRss); |
| 254 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 255 ", Started receiving a packet!"); |
| 256 |
| 257 if (IsPacketToBeJammed (packet)) |
| 258 { |
| 259 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 260 ", Decided to jam this packet!"); |
| 261 m_jammingEvent.Cancel (); // cancel previously scheduled event |
| 262 // react to packet |
| 263 m_jammingEvent = Simulator::Schedule (m_rxTxSwitchingDelay, |
| 264 &ReactiveJammer::ReactToPacket, |
| 265 this); |
| 266 } |
| 267 else |
| 268 { |
| 269 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 270 ", Decided NOT to jam this packet!"); |
| 271 } |
| 272 |
| 273 if (m_reactToMitigation) |
| 274 { |
| 275 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 276 ", React to mitigation enabled! Rescheduling RX Timeout"); |
| 277 m_rxTimeoutEvent.Cancel (); // cancel RX timeout event |
| 278 // reschedule next RX timeout |
| 279 m_rxTimeoutEvent = Simulator::Schedule (m_rxTimeout, |
| 280 &ReactiveJammer::RxTimeoutHandler, |
| 281 this); |
| 282 } |
| 283 else |
| 284 { |
| 285 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 286 ", React to mitigation disabled!"); |
| 287 } |
| 288 |
| 289 return false; // Reactive Jammer always ignores incoming packets |
| 290 } |
| 291 |
| 292 bool |
| 293 ReactiveJammer::DoEndRxHandler (Ptr<Packet> packet, double averageRss) |
| 294 { |
| 295 NS_LOG_FUNCTION (this << packet << averageRss); |
| 296 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 297 ", Ignoring incoming packet!"); |
| 298 return false; |
| 299 } |
| 300 |
| 301 void |
| 302 ReactiveJammer::DoEndTxHandler(Ptr<Packet> packet, double txPower) |
| 303 { |
| 304 NS_LOG_FUNCTION (this << packet << txPower); |
| 305 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 306 ", Done sending jamming signal with power = " << txPower); |
| 307 } |
| 308 |
| 309 bool |
| 310 ReactiveJammer::IsPacketToBeJammed (Ptr<Packet> packet) |
| 311 { |
| 312 NS_LOG_FUNCTION (this << packet); |
| 313 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 314 ", Deciding whether to react to packet!"); |
| 315 |
| 316 double energyFraction; |
| 317 switch (m_reactionStrategy) |
| 318 { |
| 319 case ENERGY_AWARE: |
| 320 energyFraction = m_source->GetEnergyFraction (); |
| 321 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 322 ", Energy fraction = " << energyFraction); |
| 323 // make probabilistic decision based on energy fraction |
| 324 return (m_random.GetValue() < energyFraction); |
| 325 case FIXED_PROBABILITY: |
| 326 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 327 ", Fixed probability " << m_fixedProbability); |
| 328 // make probabilistic decision based on fixed probability |
| 329 return (m_random.GetValue () < m_fixedProbability); |
| 330 default: |
| 331 NS_FATAL_ERROR ("ReactiveJammer:At Node #" << GetId () << |
| 332 ", Error! Unknown strategy of reaction."); |
| 333 break; |
| 334 } |
| 335 return false; |
| 336 } |
| 337 |
| 338 void |
| 339 ReactiveJammer::ReactToPacket (void) |
| 340 { |
| 341 NS_LOG_FUNCTION (this); |
| 342 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 343 ", Sending jamming signal with power = " << m_txPower << " W"); |
| 344 |
| 345 // send jamming signal |
| 346 double actualPower = m_utility->SendJammingSignal (m_txPower, m_jammingDuratio
n); |
| 347 if (actualPower != 0.0) |
| 348 { |
| 349 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 350 ", Jamming signal sent with power = " << actualPower << " W"
); |
| 351 } |
| 352 else |
| 353 { |
| 354 NS_LOG_ERROR ("ReactiveJammer:At Node #" << GetId () << |
| 355 ", Failed to send jamming signal!"); |
| 356 } |
| 357 } |
| 358 |
| 359 void |
| 360 ReactiveJammer::RxTimeoutHandler (void) |
| 361 { |
| 362 NS_LOG_FUNCTION (this); |
| 363 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << ", RX timeout at " << |
| 364 Simulator::Now ().GetSeconds () << "s"); |
| 365 NS_ASSERT (m_utility != NULL); |
| 366 |
| 367 m_rxTimeoutEvent.Cancel (); // cancel previously scheduled RX timeout |
| 368 |
| 369 if (!m_reactToMitigation) |
| 370 { |
| 371 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 372 ", React to mitigation is turned OFF!"); |
| 373 return; |
| 374 } |
| 375 |
| 376 // calculate channel number |
| 377 uint16_t currentChannel = m_utility->GetPhyLayerInfo ().currentChannel; |
| 378 uint16_t nextChannel = currentChannel + 1; |
| 379 if (nextChannel >= m_utility->GetPhyLayerInfo ().numOfChannels) |
| 380 { |
| 381 nextChannel = 1; // wrap around and start form 1 |
| 382 } |
| 383 |
| 384 NS_LOG_DEBUG ("ReactiveJammer:At Node #" << GetId () << |
| 385 ", Switching from channel " << currentChannel << " >-> " << |
| 386 nextChannel); |
| 387 |
| 388 m_utility->SwitchChannel (nextChannel); // hop to next channel |
| 389 |
| 390 // schedule next RX timeout |
| 391 m_rxTimeoutEvent = Simulator::Schedule (m_rxTimeout, |
| 392 &ReactiveJammer::RxTimeoutHandler, |
| 393 this); |
| 394 } |
| 395 |
| 396 } // namespace ns3 |
| 397 ·· |
LEFT | RIGHT |