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 * Author: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu> |
| 19 */ |
| 20 |
| 21 #include "eavesdropper-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 ("EavesdropperJammer"); |
| 29 |
| 30 /* |
| 31 * Eavesdropper Jammer. |
| 32 */ |
| 33 namespace ns3 { |
| 34 |
| 35 NS_OBJECT_ENSURE_REGISTERED (EavesdropperJammer); |
| 36 |
| 37 TypeId |
| 38 EavesdropperJammer::GetTypeId (void) |
| 39 { |
| 40 static TypeId tid = TypeId ("ns3::EavesdropperJammer") |
| 41 .SetParent<Jammer> () |
| 42 .AddConstructor<EavesdropperJammer> () |
| 43 .AddAttribute ("EavesdropperJammerRxTimeout", |
| 44 "Rx timeout.", |
| 45 TimeValue (Seconds (1.0)), |
| 46 MakeTimeAccessor (&EavesdropperJammer::SetRxTimeout, |
| 47 &EavesdropperJammer::GetRxTimeout), |
| 48 MakeTimeChecker ()) |
| 49 .AddAttribute ("EavesdropperJammerScanMode", |
| 50 "Whether to run in scan mode.", |
| 51 UintegerValue (0), |
| 52 MakeUintegerAccessor (&EavesdropperJammer::SetScanMode, |
| 53 &EavesdropperJammer::GetScanMode), |
| 54 MakeUintegerChecker<bool> ()) |
| 55 .AddAttribute ("EavesdropperJammerNumOfScanCycle", |
| 56 "Eavesdropper jammer scan cycles.", |
| 57 UintegerValue (2), |
| 58 MakeUintegerAccessor (&EavesdropperJammer::SetScanCycles, |
| 59 &EavesdropperJammer::GetScanCycles), |
| 60 MakeUintegerChecker<uint32_t> ()) |
| 61 ; |
| 62 return tid; |
| 63 } |
| 64 |
| 65 EavesdropperJammer::EavesdropperJammer () |
| 66 : m_scanCount (0), |
| 67 m_scanComplete (false), |
| 68 m_isScan (false) |
| 69 { |
| 70 m_numOfPktsReceived.clear (); // clear packet count list |
| 71 } |
| 72 |
| 73 EavesdropperJammer::~EavesdropperJammer () |
| 74 { |
| 75 } |
| 76 |
| 77 void |
| 78 EavesdropperJammer::SetUtility (Ptr<WirelessModuleUtility> utility) |
| 79 { |
| 80 NS_LOG_FUNCTION (this << utility); |
| 81 NS_ASSERT (utility != NULL); |
| 82 m_utility = utility; |
| 83 } |
| 84 |
| 85 void |
| 86 EavesdropperJammer::SetEnergySource (Ptr<EnergySource> source) |
| 87 { |
| 88 NS_LOG_FUNCTION (this << source); |
| 89 NS_ASSERT (source != NULL); |
| 90 m_source = source; |
| 91 } |
| 92 |
| 93 void |
| 94 EavesdropperJammer::SetRxTimeout (Time timeout) |
| 95 { |
| 96 NS_LOG_FUNCTION (this << timeout); |
| 97 m_rxTimeout = timeout; |
| 98 } |
| 99 |
| 100 Time |
| 101 EavesdropperJammer::GetRxTimeout (void) const |
| 102 { |
| 103 NS_LOG_FUNCTION (this); |
| 104 return m_rxTimeout; |
| 105 } |
| 106 |
| 107 void |
| 108 EavesdropperJammer::SetScanMode (bool mode) |
| 109 { |
| 110 NS_LOG_FUNCTION (this << mode); |
| 111 m_isScan = mode; |
| 112 } |
| 113 |
| 114 bool |
| 115 EavesdropperJammer::GetScanMode (void) const |
| 116 { |
| 117 NS_LOG_FUNCTION (this); |
| 118 return m_isScan; |
| 119 } |
| 120 |
| 121 void |
| 122 EavesdropperJammer::SetScanCycles (uint32_t cycles) |
| 123 { |
| 124 NS_LOG_FUNCTION (this << cycles); |
| 125 m_totalScanCycles = cycles; |
| 126 } |
| 127 |
| 128 uint32_t |
| 129 EavesdropperJammer::GetScanCycles (void) const |
| 130 { |
| 131 NS_LOG_FUNCTION (this); |
| 132 return m_totalScanCycles; |
| 133 } |
| 134 |
| 135 void |
| 136 EavesdropperJammer::ClearPacketCountList (void) |
| 137 { |
| 138 NS_LOG_FUNCTION (this); |
| 139 m_numOfPktsReceived.clear (); |
| 140 } |
| 141 |
| 142 /* |
| 143 * Private functions start here. |
| 144 */ |
| 145 |
| 146 void |
| 147 EavesdropperJammer::DoDispose (void) |
| 148 { |
| 149 NS_LOG_FUNCTION (this); |
| 150 } |
| 151 |
| 152 void |
| 153 EavesdropperJammer::DoStopJammer (void) |
| 154 { |
| 155 NS_LOG_FUNCTION (this); |
| 156 m_rxTimeoutEvent.Cancel (); |
| 157 } |
| 158 |
| 159 void |
| 160 EavesdropperJammer::DoJamming (void) |
| 161 { |
| 162 NS_LOG_FUNCTION (this); |
| 163 NS_LOG_DEBUG ("EavesdropperJammer:At Node #" << GetId () << |
| 164 ", Scheduling initial RX timeout at DoJamming"); |
| 165 |
| 166 if (m_isScan) // check if in scan mode |
| 167 { |
| 168 m_rxTimeoutEvent.Cancel (); // cancel previous RX timeout |
| 169 // schedule RX timeout |
| 170 m_rxTimeoutEvent = Simulator::Schedule (m_rxTimeout, |
| 171 &EavesdropperJammer::RxTimeoutHand
ler, |
| 172 this); |
| 173 } |
| 174 } |
| 175 |
| 176 bool |
| 177 EavesdropperJammer::DoStartRxHandler (Ptr<Packet> packet, double startRss) |
| 178 { |
| 179 NS_LOG_FUNCTION (this << packet << startRss); |
| 180 return true; |
| 181 } |
| 182 |
| 183 bool |
| 184 EavesdropperJammer::DoEndRxHandler (Ptr<Packet> packet, double averageRss) |
| 185 { |
| 186 NS_LOG_FUNCTION (this << packet << averageRss); |
| 187 NS_LOG_DEBUG ("EavesdropperJammer:At Node #" << GetId () << ", Handling packet
!"); |
| 188 |
| 189 if (!IsJammerOn ()) |
| 190 { |
| 191 NS_LOG_DEBUG ("EavesdropperJammer:At Node #" << GetId () << ", Jammer is O
FF!"); |
| 192 return false; |
| 193 } |
| 194 |
| 195 if (m_isScan) |
| 196 { |
| 197 // initialize packet count list ONLY if it's empty. |
| 198 if (m_numOfPktsReceived.empty ()) |
| 199 { |
| 200 m_numOfPktsReceived.assign (m_utility->GetPhyLayerInfo ().numOfChannel
s, |
| 201 false); |
| 202 } |
| 203 if (!m_scanComplete) |
| 204 { |
| 205 // check if packet is valid, NULL means receive failed |
| 206 if (packet != NULL) |
| 207 { |
| 208 uint16_t currentChannelNumber = m_utility->GetPhyLayerInfo ().curr
entChannel; |
| 209 m_numOfPktsReceived[currentChannelNumber]++; |
| 210 } |
| 211 } |
| 212 return true; |
| 213 } |
| 214 |
| 215 // TODO non-scan mode is to be implemented in future releases. |
| 216 return true; |
| 217 } |
| 218 |
| 219 void |
| 220 EavesdropperJammer::DoEndTxHandler (Ptr<Packet> packet, double txPower) |
| 221 { |
| 222 NS_LOG_FUNCTION (this << packet << txPower); |
| 223 // we should never reach this function, because eavesdropper never transmits. |
| 224 NS_FATAL_ERROR ("EavesdropperJammer:At Node #" << GetId () << |
| 225 ", DoEndTxHandler Called!"); |
| 226 } |
| 227 |
| 228 void |
| 229 EavesdropperJammer::RxTimeoutHandler (void) |
| 230 { |
| 231 NS_LOG_FUNCTION (this); |
| 232 NS_LOG_DEBUG ("EavesdropperJammer:At Node #" << GetId () << |
| 233 ", RX timeout occurred!"); |
| 234 |
| 235 m_rxTimeoutEvent.Cancel (); // cancel previously scheduled event |
| 236 |
| 237 /* |
| 238 * RX timeout is always scheduled so that if a jammer is stopped during scan, |
| 239 * it can be resumed. When jammer is turned off or scan cycles are completed, |
| 240 * no more channel switch will be performed. |
| 241 */ |
| 242 |
| 243 if (Simulator::IsFinished ()) // do not reschedule when simulation ends |
| 244 { |
| 245 return; |
| 246 } |
| 247 |
| 248 // switch to next channel |
| 249 if (IsJammerOn () && !m_scanComplete) |
| 250 { |
| 251 // check to see if we need t restart from next channel. |
| 252 uint16_t channelNumber = m_utility->GetPhyLayerInfo ().currentChannel; |
| 253 if (++channelNumber >= m_utility->GetPhyLayerInfo ().numOfChannels + 1) |
| 254 { |
| 255 channelNumber = 1; // wrap around |
| 256 } |
| 257 NS_LOG_DEBUG ("EavesdropperJammer:At Node #" << GetId () << |
| 258 ", Switching channel from " << |
| 259 m_utility->GetPhyLayerInfo ().currentChannel << " >-> " << |
| 260 channelNumber); |
| 261 m_utility->SwitchChannel (channelNumber); |
| 262 m_scanCount++; // increase scan count |
| 263 } |
| 264 |
| 265 // check if scan is complete |
| 266 if (m_scanCount / m_utility->GetPhyLayerInfo ().numOfChannels >= |
| 267 m_totalScanCycles) |
| 268 { |
| 269 m_scanComplete = true; |
| 270 NS_LOG_DEBUG ("EavesdropperJammer:At Node #" << GetId () << |
| 271 ", Scan completed!"); |
| 272 } |
| 273 |
| 274 // schedule next RX timeout |
| 275 m_rxTimeoutEvent = Simulator::Schedule (m_rxTimeout, |
| 276 &EavesdropperJammer::RxTimeoutHandler, |
| 277 this); |
| 278 } |
| 279 |
| 280 } // namespace ns3 |
LEFT | RIGHT |