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 "random-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 ("RandomJammer"); |
| 29 |
| 30 /* |
| 31 * Random Jammer. |
| 32 */ |
| 33 namespace ns3 { |
| 34 |
| 35 NS_OBJECT_ENSURE_REGISTERED (RandomJammer); |
| 36 |
| 37 TypeId |
| 38 RandomJammer::GetTypeId (void) |
| 39 { |
| 40 static TypeId tid = TypeId ("ns3::RandomJammer") |
| 41 .SetParent<Jammer> () |
| 42 .AddConstructor<RandomJammer> () |
| 43 .AddAttribute ("RandomJammerTxPower", |
| 44 "Power to send jamming signal for random jammer, in Watts.", |
| 45 DoubleValue (0.001), // 0dBm |
| 46 MakeDoubleAccessor (&RandomJammer::SetTxPower, |
| 47 &RandomJammer::GetTxPower), |
| 48 MakeDoubleChecker<double> ()) |
| 49 .AddAttribute ("RandomJammerJammingDuration", |
| 50 "Jamming duration for random jammer.", |
| 51 TimeValue (MilliSeconds (200.0)), |
| 52 MakeTimeAccessor (&RandomJammer::SetJammingDuration, |
| 53 &RandomJammer::GetJammingDuration), |
| 54 MakeTimeChecker ()) |
| 55 .AddAttribute ("RandomJammerRandomInterval", |
| 56 "Random jammer interval.", |
| 57 RandomVariableValue (UniformVariable (0.0, 0.001)), |
| 58 MakeRandomVariableAccessor (&RandomJammer::SetRandomVariable)
, |
| 59 MakeRandomVariableChecker ()) |
| 60 .AddAttribute ("RandomJammerRxTimeout", |
| 61 "Random jammer RX timeout.", |
| 62 TimeValue (Seconds (2.0)), |
| 63 MakeTimeAccessor (&RandomJammer::SetRxTimeout, |
| 64 &RandomJammer::GetRxTimeout), |
| 65 MakeTimeChecker ()) |
| 66 .AddAttribute ("RandomJammerReactToMitigationFlag", |
| 67 "Random jammer react to mitigation flag, set to enable chasin
g.", |
| 68 UintegerValue (true), // default to not chasing |
| 69 MakeUintegerAccessor (&RandomJammer::SetReactToMitigation, |
| 70 &RandomJammer::GetReactToMitigation), |
| 71 MakeUintegerChecker<bool> ()) |
| 72 ; |
| 73 return tid; |
| 74 } |
| 75 |
| 76 RandomJammer::RandomJammer () |
| 77 : m_reactToMitigation (false), |
| 78 m_reacting (false) |
| 79 { |
| 80 } |
| 81 |
| 82 RandomJammer::~RandomJammer () |
| 83 { |
| 84 } |
| 85 |
| 86 void |
| 87 RandomJammer::SetUtility (Ptr<WirelessModuleUtility> utility) |
| 88 { |
| 89 NS_LOG_FUNCTION (this << utility); |
| 90 NS_ASSERT (utility != NULL); |
| 91 m_utility = utility; |
| 92 } |
| 93 |
| 94 void |
| 95 RandomJammer::SetEnergySource (Ptr<EnergySource> source) |
| 96 { |
| 97 NS_LOG_FUNCTION (this << source); |
| 98 NS_ASSERT (source != NULL); |
| 99 m_source = source; |
| 100 } |
| 101 |
| 102 void |
| 103 RandomJammer::SetTxPower (double power) |
| 104 { |
| 105 NS_LOG_FUNCTION (this << power); |
| 106 m_txPower = power; |
| 107 } |
| 108 |
| 109 double |
| 110 RandomJammer::GetTxPower (void) const |
| 111 { |
| 112 NS_LOG_FUNCTION (this); |
| 113 return m_txPower; |
| 114 } |
| 115 |
| 116 void |
| 117 RandomJammer::SetJammingDuration (Time duration) |
| 118 { |
| 119 NS_LOG_FUNCTION (this << duration); |
| 120 m_jammingDuration = duration; |
| 121 } |
| 122 |
| 123 Time |
| 124 RandomJammer::GetJammingDuration (void) const |
| 125 { |
| 126 NS_LOG_FUNCTION (this); |
| 127 return m_jammingDuration; |
| 128 } |
| 129 |
| 130 void |
| 131 RandomJammer::SetRandomVariable (RandomVariable random) |
| 132 { |
| 133 NS_LOG_FUNCTION (this << random); |
| 134 m_randomJammingInterval = random; |
| 135 } |
| 136 |
| 137 void |
| 138 RandomJammer::SetRxTimeout (Time rxTimeout) |
| 139 { |
| 140 NS_LOG_FUNCTION (this << rxTimeout); |
| 141 m_rxTimeout = rxTimeout; |
| 142 } |
| 143 |
| 144 Time |
| 145 RandomJammer::GetRxTimeout (void) const |
| 146 { |
| 147 NS_LOG_FUNCTION (this); |
| 148 return m_rxTimeout; |
| 149 } |
| 150 |
| 151 void |
| 152 RandomJammer::SetReactToMitigation (const bool flag) |
| 153 { |
| 154 NS_LOG_FUNCTION (this << flag); |
| 155 m_reactToMitigation = flag; |
| 156 } |
| 157 |
| 158 bool |
| 159 RandomJammer::GetReactToMitigation (void) const |
| 160 { |
| 161 NS_LOG_FUNCTION (this); |
| 162 return m_reactToMitigation; |
| 163 } |
| 164 |
| 165 /* |
| 166 * Private functions start here. |
| 167 */ |
| 168 |
| 169 void |
| 170 RandomJammer::DoDispose (void) |
| 171 { |
| 172 NS_LOG_FUNCTION (this); |
| 173 m_jammingEvent.Cancel (); |
| 174 } |
| 175 |
| 176 void |
| 177 RandomJammer::DoStopJammer (void) |
| 178 { |
| 179 NS_LOG_FUNCTION (this); |
| 180 m_jammingEvent.Cancel (); |
| 181 } |
| 182 |
| 183 void |
| 184 RandomJammer::DoJamming (void) |
| 185 { |
| 186 NS_LOG_FUNCTION (this); |
| 187 NS_ASSERT (m_utility); |
| 188 |
| 189 if (!IsJammerOn ()) |
| 190 { |
| 191 NS_LOG_DEBUG ("RandomJammer:At Node #" << GetId () << ", Jammer is OFF!"); |
| 192 return; |
| 193 } |
| 194 |
| 195 NS_LOG_DEBUG ("RandomJammer:At Node #" << GetId () << |
| 196 ", Sending jamming signal with power = " << m_txPower << " W" << |
| 197 ", At " << Simulator::Now ().GetSeconds () << "s"); |
| 198 |
| 199 // send jamming signal |
| 200 double actualPower = m_utility->SendJammingSignal (m_txPower, |
| 201 m_jammingDuration); |
| 202 if (actualPower != 0.0) |
| 203 { |
| 204 NS_LOG_DEBUG ("RandomJammer:At Node #" << GetId () << |
| 205 ", Jamming signal sent with power = " << actualPower << |
| 206 " W"); |
| 207 } |
| 208 else |
| 209 { |
| 210 NS_LOG_ERROR ("RandomJammer:At Node #" << GetId () << |
| 211 ", Failed to send jamming signal!"); |
| 212 } |
| 213 |
| 214 /* |
| 215 * Schedule *first* RX timeout if react to mitigation is enabled. We know if |
| 216 * react to jamming mitigation is enabled, there should always be a RX timeout |
| 217 * event scheduled. The RX timeout event can only "expire" if it's never been |
| 218 * scheduled. |
| 219 */ |
| 220 if (m_reactToMitigation && m_rxTimeoutEvent.IsExpired()) |
| 221 { |
| 222 NS_LOG_DEBUG ("RandomJammer:At Node #" << GetId () << |
| 223 ", After jammer starts, scheduling RX timeout!"); |
| 224 m_rxTimeoutEvent = Simulator::Schedule (m_rxTimeout, |
| 225 &RandomJammer::RxTimeoutHandler, |
| 226 this); |
| 227 } |
| 228 |
| 229 m_reacting = false; // always reset reacting flag |
| 230 } |
| 231 |
| 232 bool |
| 233 RandomJammer::DoStartRxHandler (Ptr<Packet> packet, double startRss) |
| 234 { |
| 235 NS_LOG_FUNCTION (this << packet << startRss); |
| 236 |
| 237 if (m_reactToMitigation) |
| 238 { |
| 239 NS_LOG_DEBUG ("RandomJammer:At Node #" << GetId () << |
| 240 ", React to mitigation enabled! Rescheduling RX Timeout"); |
| 241 m_rxTimeoutEvent.Cancel (); // cancel RX timeout event |
| 242 // reschedule next RX timeout |
| 243 m_rxTimeoutEvent = Simulator::Schedule (m_rxTimeout, |
| 244 &RandomJammer::RxTimeoutHandler, |
| 245 this); |
| 246 } |
| 247 else |
| 248 { |
| 249 NS_LOG_DEBUG ("RandomJammer:At Node #" << GetId () << |
| 250 ", React to mitigation disabled!"); |
| 251 } |
| 252 |
| 253 return false; // Random Jammer always rejects the incoming packet |
| 254 } |
| 255 |
| 256 bool |
| 257 RandomJammer::DoEndRxHandler (Ptr<Packet> packet, double averageRss) |
| 258 { |
| 259 NS_LOG_FUNCTION (this << packet << averageRss); |
| 260 NS_LOG_DEBUG ("RandomJammer:At Node #" << GetId () << |
| 261 ", Ignoring incoming packet!"); |
| 262 return false; |
| 263 } |
| 264 |
| 265 void |
| 266 RandomJammer::DoEndTxHandler (Ptr<Packet> packet, double txPower) |
| 267 { |
| 268 NS_LOG_FUNCTION (this << packet << txPower); |
| 269 NS_LOG_DEBUG("RandomJammer:At Node #" << GetId () << |
| 270 ", Jamming packet is sent with power = " << txPower); |
| 271 ·· |
| 272 m_jammingEvent.Cancel (); // cancel previously scheduled event |
| 273 |
| 274 if (m_reacting) |
| 275 { |
| 276 NS_LOG_DEBUG ("RandomJammer:At Node #" << GetId () << |
| 277 ", Not sending jamming signal, jammer reacting to mitigation
!"); |
| 278 // channel switch delay |
| 279 Time delay = m_utility->GetPhyLayerInfo().channelSwitchDelay; |
| 280 // schedule jamming after channel switch delay |
| 281 m_jammingEvent = Simulator::Schedule (delay, &RandomJammer::DoJamming, |
| 282 this); |
| 283 return; // do nothing if already reacting to mitigation |
| 284 } |
| 285 |
| 286 // calculate interval to sending next jamming burst |
| 287 Time intervalToNextJamming = Seconds (m_randomJammingInterval.GetValue () + |
| 288 m_jammingDuration.GetSeconds()); |
| 289 |
| 290 // schedules send jamming signal in utility. |
| 291 m_jammingEvent = Simulator::Schedule (intervalToNextJamming, |
| 292 &RandomJammer::DoJamming, this); |
| 293 } |
| 294 |
| 295 void |
| 296 RandomJammer::RxTimeoutHandler (void) |
| 297 { |
| 298 NS_LOG_FUNCTION (this); |
| 299 NS_LOG_DEBUG ("RandomJammer:At Node #" << GetId () << ", RX timeout at " << |
| 300 Simulator::Now ().GetSeconds () << "s"); |
| 301 NS_ASSERT (m_utility != NULL); |
| 302 |
| 303 m_rxTimeoutEvent.Cancel (); // cancel previously scheduled RX timeout |
| 304 |
| 305 if (!m_reactToMitigation) |
| 306 { |
| 307 NS_LOG_DEBUG ("RandomJammer:At Node #" << GetId () << |
| 308 ", React to mitigation is turned OFF!"); |
| 309 return; // do nothing if react to mitigation flag is not set |
| 310 } |
| 311 |
| 312 // calculate channel number |
| 313 uint16_t currentChannel = m_utility->GetPhyLayerInfo ().currentChannel; |
| 314 uint16_t nextChannel = currentChannel + 1; |
| 315 if (nextChannel >= m_utility->GetPhyLayerInfo ().numOfChannels) |
| 316 { |
| 317 nextChannel = 1; // wrap around and start form 1 |
| 318 } |
| 319 |
| 320 NS_LOG_DEBUG ("RandomJammer:At Node #" << GetId () << |
| 321 ", Switching from channel " << currentChannel << " >-> " << |
| 322 nextChannel << ", At " << Simulator::Now ().GetSeconds () << "s"
); |
| 323 |
| 324 // hop to next channel |
| 325 m_utility->SwitchChannel (nextChannel); |
| 326 |
| 327 /* |
| 328 * Set reacting flag to indicate jammer is reacting. When the flag is set, no |
| 329 * scheduling of new jamming event is allowed. |
| 330 */ |
| 331 m_reacting = true; |
| 332 |
| 333 // schedule next RX timeout |
| 334 m_rxTimeoutEvent = Simulator::Schedule (m_rxTimeout, |
| 335 &RandomJammer::RxTimeoutHandler, |
| 336 this); |
| 337 } |
| 338 |
| 339 } // namespace ns3 |
LEFT | RIGHT |