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 "mitigate-by-channel-hop.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 #include "ns3/string.h" |
| 28 #include <math.h> |
| 29 |
| 30 NS_LOG_COMPONENT_DEFINE ("MitigateByChannelHop"); |
| 31 |
| 32 namespace ns3 { |
| 33 |
| 34 NS_OBJECT_ENSURE_REGISTERED (MitigateByChannelHop); |
| 35 |
| 36 TypeId |
| 37 MitigateByChannelHop::GetTypeId (void) |
| 38 { |
| 39 static TypeId tid = TypeId ("ns3::MitigateByChannelHop") |
| 40 .SetParent<JammingMitigation> () |
| 41 .AddConstructor<MitigateByChannelHop> () |
| 42 .AddAttribute ("MitigateByChannelHopDetectionMethod", |
| 43 "Jamming detection method to use.", |
| 44 UintegerValue (1), // default to RSS only |
| 45 MakeUintegerAccessor (&MitigateByChannelHop::SetJammingDete
ctionMethod, |
| 46 &MitigateByChannelHop::GetJammingDete
ctionMethod), |
| 47 MakeUintegerChecker<uint32_t> ()) |
| 48 .AddAttribute ("MitigateByChannelHopDetectionThreshold", |
| 49 "Jamming detection threshold.", |
| 50 DoubleValue (0.5), // default to 0.5 |
| 51 MakeDoubleAccessor (&MitigateByChannelHop::SetJammingDetect
ionThreshold, |
| 52 &MitigateByChannelHop::GetJammingDetect
ionThreshold), |
| 53 MakeDoubleChecker<double> ()) |
| 54 .AddAttribute ("MitigateByChannelHopTxPower", |
| 55 "TX power for channel hop message.", |
| 56 DoubleValue (0.001), // 0.001 W = 0 dBm |
| 57 MakeDoubleAccessor (&MitigateByChannelHop::SetTxPower, |
| 58 &MitigateByChannelHop::GetTxPower), |
| 59 MakeDoubleChecker<double> ()) |
| 60 .AddAttribute ("MitigateByChannelHopChannelHopMessage", |
| 61 "Content of channel hop message.", |
| 62 StringValue ("Channel Hop!"), |
| 63 MakeStringAccessor (&MitigateByChannelHop::SetChannelHopMes
sage, |
| 64 &MitigateByChannelHop::GetChannelHopMes
sage), |
| 65 MakeStringChecker ()) |
| 66 .AddAttribute ("MitigateByChannelHopChannelHopDelay", |
| 67 "Channel hop delay.", |
| 68 TimeValue (Seconds (0.0)), |
| 69 MakeTimeAccessor (&MitigateByChannelHop::SetChannelHopDelay
, |
| 70 &MitigateByChannelHop::GetChannelHopDelay
), |
| 71 MakeTimeChecker ()) |
| 72 .AddAttribute ("MitigateByChannelHopChannelHopSeed", |
| 73 "Seed used in internal RNG.", |
| 74 UintegerValue (12345), // same default defined in rng-strea
m.h |
| 75 MakeUintegerAccessor (&MitigateByChannelHop::SetRngSeed, |
| 76 &MitigateByChannelHop::GetRngSeed), |
| 77 MakeUintegerChecker<uint32_t> ()) |
| 78 .AddAttribute ("MitigateByChannelHopChannelStart", |
| 79 "Starting channel number.", |
| 80 UintegerValue (1), // first available wifi channel number |
| 81 MakeUintegerAccessor (&MitigateByChannelHop::SetStartChanne
lNumber, |
| 82 &MitigateByChannelHop::GetStartChanne
lNumber), |
| 83 MakeUintegerChecker<uint16_t> ()) |
| 84 .AddAttribute ("MitigateByChannelHopChannelEnd", |
| 85 "Ending channel number.", |
| 86 UintegerValue (11), // last available wifi channel number |
| 87 MakeUintegerAccessor (&MitigateByChannelHop::SetEndChannelN
umber, |
| 88 &MitigateByChannelHop::GetEndChannelN
umber), |
| 89 MakeUintegerChecker<uint16_t> ()) |
| 90 ; |
| 91 return tid; |
| 92 } |
| 93 |
| 94 MitigateByChannelHop::MitigateByChannelHop () |
| 95 : m_rngInitialized (false), |
| 96 m_waitingToHop (false) |
| 97 { |
| 98 } |
| 99 |
| 100 MitigateByChannelHop::~MitigateByChannelHop () |
| 101 { |
| 102 } |
| 103 |
| 104 void |
| 105 MitigateByChannelHop::SetUtility (Ptr<WirelessModuleUtility> utility) |
| 106 { |
| 107 NS_LOG_FUNCTION (this << utility); |
| 108 NS_ASSERT (utility != NULL); |
| 109 m_utility = utility; |
| 110 } |
| 111 |
| 112 void |
| 113 MitigateByChannelHop::SetEnergySource (Ptr<EnergySource> source) |
| 114 { |
| 115 NS_LOG_FUNCTION (this << source); |
| 116 NS_ASSERT (source != NULL); |
| 117 m_source = source; |
| 118 } |
| 119 |
| 120 void |
| 121 MitigateByChannelHop::SetJammingDetectionMethod (JammingDetectionMethod method) |
| 122 { |
| 123 NS_LOG_FUNCTION (this << method); |
| 124 m_jammingDetectionMethod = method; |
| 125 } |
| 126 |
| 127 uint32_t |
| 128 MitigateByChannelHop::GetJammingDetectionMethod (void) const |
| 129 { |
| 130 NS_LOG_FUNCTION (this); |
| 131 return m_jammingDetectionMethod; |
| 132 } |
| 133 |
| 134 void |
| 135 MitigateByChannelHop::SetJammingDetectionThreshold (double threshold) |
| 136 { |
| 137 NS_LOG_FUNCTION (this << threshold); |
| 138 m_jammingDetectionThreshold = threshold; |
| 139 } |
| 140 |
| 141 double |
| 142 MitigateByChannelHop::GetJammingDetectionThreshold (void) const |
| 143 { |
| 144 NS_LOG_FUNCTION (this); |
| 145 return m_jammingDetectionThreshold; |
| 146 } |
| 147 |
| 148 void |
| 149 MitigateByChannelHop::SetTxPower (double txPower) |
| 150 { |
| 151 NS_LOG_FUNCTION (this << txPower); |
| 152 m_txPower = txPower; |
| 153 } |
| 154 |
| 155 double |
| 156 MitigateByChannelHop::GetTxPower (void) const |
| 157 { |
| 158 NS_LOG_FUNCTION (this); |
| 159 return m_txPower; |
| 160 } |
| 161 |
| 162 void |
| 163 MitigateByChannelHop::SetChannelHopMessage (std::string message) |
| 164 { |
| 165 NS_LOG_FUNCTION (this); |
| 166 m_channelHopMessage = message; |
| 167 } |
| 168 |
| 169 std::string |
| 170 MitigateByChannelHop::GetChannelHopMessage (void) const |
| 171 { |
| 172 NS_LOG_FUNCTION (this); |
| 173 return m_channelHopMessage; |
| 174 } |
| 175 |
| 176 void |
| 177 MitigateByChannelHop::SetChannelHopDelay (Time delay) |
| 178 { |
| 179 NS_LOG_FUNCTION (this << delay); |
| 180 m_channelHopDelay = delay; |
| 181 } |
| 182 |
| 183 Time |
| 184 MitigateByChannelHop::GetChannelHopDelay (void) const |
| 185 { |
| 186 NS_LOG_FUNCTION (this); |
| 187 return m_channelHopDelay; |
| 188 } |
| 189 |
| 190 double |
| 191 MitigateByChannelHop::DegreeOfJamming (int method) |
| 192 { |
| 193 NS_LOG_FUNCTION (this << method); |
| 194 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 195 ", Calculating degree of jamming!"); |
| 196 |
| 197 /* |
| 198 * Here we use RSS value at the start of packet as reference for calculating |
| 199 * RSS ratio. This is effective for reactive jammers where the RSS is low at |
| 200 * the beginning of packet, but the average RSS is higher. However this may |
| 201 * not be effective for constant jammers, which results a constantly high RSS |
| 202 * value and a low RSS ratio. |
| 203 */ |
| 204 double rssRatio = fabs (m_averageRss - m_startRss) / m_startRss; |
| 205 switch (method) |
| 206 { |
| 207 case PDR_ONLY: |
| 208 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 209 ", PDR only!"); |
| 210 return (1 - m_utility->GetPdr ()); |
| 211 case RSS_ONLY: |
| 212 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 213 ", RSS only!"); |
| 214 return (rssRatio > 1 ? 1 : rssRatio); // max = 1 |
| 215 case PDR_AND_RSS: |
| 216 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 217 ", PDR & RSS!"); |
| 218 // return the average of RSS & PDR |
| 219 return ((rssRatio + (1 - m_utility->GetPdr ())) / 2); |
| 220 default: |
| 221 NS_FATAL_ERROR ("MitigateByChannelHop:At Node #" << GetId () << |
| 222 ", Unknown jamming detection method!"); |
| 223 break; |
| 224 } |
| 225 return -1.0; // error |
| 226 } |
| 227 |
| 228 bool |
| 229 MitigateByChannelHop::IsJammingDetected (int method) |
| 230 { |
| 231 NS_LOG_FUNCTION (this << method); |
| 232 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 233 ", Deciding if jamming is detected!"); |
| 234 |
| 235 double degreeOfJamming = DegreeOfJamming (method); |
| 236 if (degreeOfJamming > m_jammingDetectionThreshold) |
| 237 { |
| 238 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 239 ", Jamming is detected!"); |
| 240 return true; |
| 241 } |
| 242 else |
| 243 { |
| 244 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 245 ", Jamming is NOT detected!"); |
| 246 return false; |
| 247 } |
| 248 } |
| 249 |
| 250 void |
| 251 MitigateByChannelHop::SendChannelHopMessage(void) |
| 252 { |
| 253 NS_LOG_FUNCTION (this); |
| 254 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 255 ", Sending channel hop message >> \n--\n" << m_channelHopMessage
<< |
| 256 "\n--"); |
| 257 |
| 258 // build mitigation packet |
| 259 Ptr<Packet> packet = Create<Packet> ((uint8_t *)m_channelHopMessage.c_str (), |
| 260 m_channelHopMessage.size ()); |
| 261 |
| 262 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 263 ", Sending channel hop packet with power = " << m_txPower << " W
"); |
| 264 |
| 265 // send mitigation signal |
| 266 double actualPower = m_utility->SendMitigationMessage (packet, m_txPower); |
| 267 if (actualPower != 0.0) |
| 268 { |
| 269 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 270 ", Channel hop packet sent with power = " << actualPower <<
" W"); |
| 271 } |
| 272 else |
| 273 { |
| 274 NS_LOG_ERROR ("MitigateByChannelHop:At Node #" << GetId () << |
| 275 ", Failed to send channel hop packet!"); |
| 276 } |
| 277 |
| 278 m_waitingToHop = true; // set waiting for channel hop flag |
| 279 } |
| 280 |
| 281 void |
| 282 MitigateByChannelHop::HopChannel (void) |
| 283 { |
| 284 NS_LOG_FUNCTION (this); |
| 285 NS_ASSERT (m_waitingToHop); // make sure we are waiting to hop channel |
| 286 |
| 287 // calculate channel number to hop to |
| 288 uint16_t channelNumber = RandomSequenceGenerator (); |
| 289 |
| 290 // schedule hop channel after sending is complete |
| 291 Simulator::Schedule (m_channelHopDelay, |
| 292 &ns3::WirelessModuleUtility::SwitchChannel, m_utility, channelNumber); |
| 293 |
| 294 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << ", Hopping from
" << |
| 295 m_utility->GetPhyLayerInfo().currentChannel << " >-> " << |
| 296 channelNumber << ", At " << Simulator::Now ().GetSeconds () << "
s"); |
| 297 |
| 298 m_waitingToHop = false; // reset flag after channel hop |
| 299 } |
| 300 |
| 301 void |
| 302 MitigateByChannelHop::SetRngSeed (uint32_t seed) |
| 303 { |
| 304 NS_LOG_FUNCTION (this << seed); |
| 305 m_seed = seed; |
| 306 m_stream.SetPackageSeed (seed); |
| 307 m_stream.InitializeStream (); |
| 308 } |
| 309 |
| 310 uint32_t |
| 311 MitigateByChannelHop::GetRngSeed (void) const |
| 312 { |
| 313 NS_LOG_FUNCTION (this); |
| 314 return m_seed; |
| 315 } |
| 316 |
| 317 void |
| 318 MitigateByChannelHop::SetStartChannelNumber (uint16_t channelNumber) |
| 319 { |
| 320 NS_LOG_FUNCTION (this << channelNumber); |
| 321 m_channelStart = channelNumber; |
| 322 } |
| 323 |
| 324 uint16_t |
| 325 MitigateByChannelHop::GetStartChannelNumber (void) const |
| 326 { |
| 327 NS_LOG_FUNCTION (this); |
| 328 return m_channelStart; |
| 329 } |
| 330 |
| 331 void |
| 332 MitigateByChannelHop::SetEndChannelNumber (uint16_t channelNumber) |
| 333 { |
| 334 NS_LOG_FUNCTION (this); |
| 335 m_channelEnd = channelNumber; |
| 336 } |
| 337 |
| 338 uint16_t |
| 339 MitigateByChannelHop::GetEndChannelNumber (void) const |
| 340 { |
| 341 NS_LOG_FUNCTION (this); |
| 342 return m_channelEnd; |
| 343 } |
| 344 |
| 345 /* |
| 346 * Private functions start here. |
| 347 */ |
| 348 |
| 349 void |
| 350 MitigateByChannelHop::DoStart (void) |
| 351 { |
| 352 NS_LOG_FUNCTION (this); |
| 353 StartMitigation (); // start mitigation at beginning of simulation |
| 354 } |
| 355 |
| 356 void |
| 357 MitigateByChannelHop::DoDispose (void) |
| 358 { |
| 359 NS_LOG_FUNCTION (this); |
| 360 NS_LOG_DEBUG ("MitigateByChannelHop: At node #" << GetId () << |
| 361 " Current channel number = " << |
| 362 m_utility->GetPhyLayerInfo ().currentChannel); |
| 363 StopMitigation (); |
| 364 } |
| 365 |
| 366 void |
| 367 MitigateByChannelHop::DoMitigation (void) |
| 368 { |
| 369 NS_LOG_FUNCTION (this); |
| 370 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 371 ", Mitigation started!"); |
| 372 } |
| 373 |
| 374 void |
| 375 MitigateByChannelHop::DoStopMitigation (void) |
| 376 { |
| 377 NS_LOG_FUNCTION (this); |
| 378 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 379 ", Mitigation stopped!"); |
| 380 } |
| 381 |
| 382 void |
| 383 MitigateByChannelHop::DoStartRxHandler (Ptr<Packet> packet, double startRss) |
| 384 { |
| 385 NS_LOG_FUNCTION (this << packet << startRss); |
| 386 m_startRss = startRss; |
| 387 } |
| 388 |
| 389 void |
| 390 MitigateByChannelHop::DoEndRxHandler (Ptr<Packet> packet, double averageRss) |
| 391 { |
| 392 NS_LOG_FUNCTION (this << packet << averageRss); |
| 393 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 394 ", Handling incoming packet!"); |
| 395 |
| 396 if (!IsMitigationOn ()) |
| 397 { |
| 398 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 399 ", Mitigation is OFF!"); |
| 400 return; |
| 401 } |
| 402 |
| 403 m_averageRss = averageRss; |
| 404 |
| 405 // detect jamming |
| 406 if (IsJammingDetected (m_jammingDetectionMethod)) |
| 407 { |
| 408 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 409 ", Sending channel hop message at " << |
| 410 Simulator::Now ().GetSeconds () << "s"); |
| 411 SendChannelHopMessage (); |
| 412 } |
| 413 |
| 414 if (packet == NULL) |
| 415 { |
| 416 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 417 ", Failed to receive current packet!"); |
| 418 return; |
| 419 } |
| 420 |
| 421 // check if incoming packet is channel hop message |
| 422 if (FindMessage (packet, m_channelHopMessage)) |
| 423 { |
| 424 if (!m_waitingToHop) // hop only if current node is not waiting to hop |
| 425 { |
| 426 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 427 ", Received channel hop message at " << |
| 428 Simulator::Now ().GetSeconds ()); |
| 429 /* |
| 430 * Pass channel hop message, hopping to next channel is done at the |
| 431 * end of sending channel hopping message. |
| 432 */ |
| 433 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 434 ", Passing channel hop message!"); |
| 435 SendChannelHopMessage (); |
| 436 } |
| 437 else |
| 438 { |
| 439 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 440 ", Received channel hop message!" << |
| 441 " But not hopping because current node is waiting to hop
!"); |
| 442 } |
| 443 } |
| 444 } |
| 445 |
| 446 void |
| 447 MitigateByChannelHop::DoEndTxHandler (Ptr<Packet> packet, double txPower) |
| 448 { |
| 449 NS_LOG_FUNCTION (this << packet << txPower); |
| 450 |
| 451 if (m_waitingToHop) // check waiting for channel hop flag |
| 452 { |
| 453 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 454 ", schedule hop at end of TX!"); |
| 455 // schedule channel hop after some time |
| 456 Simulator::Schedule (m_channelHopDelay, &MitigateByChannelHop::HopChannel, |
| 457 this); |
| 458 } |
| 459 else |
| 460 { |
| 461 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 462 ", Not doing anything at end of TX!"); |
| 463 } |
| 464 } |
| 465 |
| 466 bool |
| 467 MitigateByChannelHop::FindMessage (Ptr<const Packet> packet, std::string target)
const |
| 468 { |
| 469 NS_LOG_FUNCTION (this << packet); |
| 470 NS_ASSERT (packet != NULL); |
| 471 |
| 472 if (packet->GetSize () != target.size ()) // check size of packet first |
| 473 { |
| 474 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 475 ", Size incorrect, not checking data!"); |
| 476 return false; |
| 477 } |
| 478 |
| 479 // copy data from packet |
| 480 uint8_t data[packet->GetSize()]; |
| 481 packet->CopyData (data, packet->GetSize ()); |
| 482 |
| 483 // convert data to string |
| 484 std::string dataString; |
| 485 dataString.assign ((char*)data, packet->GetSize ()); |
| 486 |
| 487 // show packet content |
| 488 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << |
| 489 ", Packet size = " << packet->GetSize () << " content is:\n---\n
" << |
| 490 dataString << "\n---"); |
| 491 |
| 492 return (dataString.compare (target) == 0); // check if is channel hop message |
| 493 } |
| 494 |
| 495 uint16_t |
| 496 MitigateByChannelHop::RandomSequenceGenerator (void) |
| 497 { |
| 498 NS_LOG_FUNCTION (this); |
| 499 uint16_t channelNumber = m_stream.RandInt (m_channelStart, m_channelEnd); |
| 500 NS_LOG_DEBUG ("MitigateByChannelHop:At Node #" << GetId () << ", RNG returned
" << |
| 501 channelNumber); |
| 502 return channelNumber; |
| 503 } |
| 504 |
| 505 } // namespace ns3 |
LEFT | RIGHT |