/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ /* * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation; * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author: Sidharth Nabar , He Wu */ #include "ns3/core-module.h" #include "ns3/common-module.h" #include "ns3/node-module.h" #include "ns3/helper-module.h" #include "ns3/mobility-module.h" #include "ns3/contrib-module.h" #include "ns3/wifi-module.h" #include "ns3/rng-stream.h" #include "ns3/energy-model-module.h" #include "ns3/jamming-module.h" #include #include #include #include NS_LOG_COMPONENT_DEFINE ("JammingModelSampleScript"); using namespace ns3; void ReceivePacket (Ptr socket) { Address addr; socket->GetSockName (addr); InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr); NS_LOG_UNCOND ("--\nReceived one packet! Socket: "<< iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << "\n--"); } static void GenerateTraffic (Ptr socket, uint32_t pktSize, Ptr n, uint32_t pktCount, Time pktInterval ) { if (pktCount > 0) { socket->Send (Create (pktSize)); Simulator::Schedule (pktInterval, &GenerateTraffic, socket, pktSize, n, pktCount-1, pktInterval); } else { socket->Close (); } } int main (int argc, char *argv[]) { // un-comment to see debug messages /* LogComponentEnable ("NslWifiPhy", LOG_LEVEL_DEBUG); LogComponentEnable ("EnergySource", LOG_LEVEL_DEBUG); LogComponentEnable ("BasicEnergySource", LOG_LEVEL_DEBUG); LogComponentEnable ("DeviceEnergyModel", LOG_LEVEL_DEBUG); LogComponentEnable ("BasicRadioEnergyModel", LOG_LEVEL_DEBUG); LogComponentEnable ("WirelessModuleUtility", LOG_LEVEL_DEBUG); LogComponentEnable ("JammerHelper", LOG_LEVEL_DEBUG); LogComponentEnable ("Jammer", LOG_LEVEL_DEBUG); LogComponentEnable ("ReactiveJammer", LOG_LEVEL_DEBUG); LogComponentEnable ("JammingMitigationHelper", LOG_LEVEL_DEBUG); LogComponentEnable ("JammingMitigation", LOG_LEVEL_DEBUG); LogComponentEnable ("MitigateByChannelHop", LOG_LEVEL_DEBUG); */ std::string phyMode ("wifib-1mbs"); double Prss = -80; // -dBm double Jrss = -80; // -dBm for jammer: same as honest nodes uint32_t PpacketSize = 200; // bytes bool verbose = false; // these are not command line arguments for this version uint32_t numPackets = 20; // number of packets to send double interval = 1; // seconds double startTime = 0.0; // seconds double distanceToRx = 10.0; // meters double offset = 81; // This is a magic number used to set the // transmit power, based on other configuration CommandLine cmd; cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode); cmd.AddValue ("Prss", "Intended primary received signal strength (dBm)", Prss); cmd.AddValue ("PpacketSize", "size of application packet sent", PpacketSize); cmd.AddValue ("verbose", "turn on all WifiNetDevice log components", verbose); cmd.Parse (argc, argv); // Convert to time object Time interPacketInterval = Seconds (interval); // disable fragmentation for frames below 2200 bytes Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200")); // turn off RTS/CTS for frames below 2200 bytes Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200")); // Fix non-unicast data rate to be the same as that of unicast Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode)); NodeContainer c; // c.Create (5); // create 4 nodes + 1 jammer NodeContainer networkNodes; networkNodes.Add (c.Get (0)); networkNodes.Add (c.Get (1)); networkNodes.Add (c.Get (2)); networkNodes.Add (c.Get (3)); // The below set of helpers will help us to put together the wifi NICs we want WifiHelper wifi; if (verbose) { wifi.EnableLogComponents (); // Turn on all Wifi logging } wifi.SetStandard (WIFI_PHY_STANDARD_80211b); /** Wifi PHY **/ /***************************************************************************/ // honest node PHY layer NslWifiPhyHelper wifiPhy = NslWifiPhyHelper::Default (); // This is one parameter that matters when using FixedRssLossModel // set it to zero; otherwise, gain will be added wifiPhy.Set ("NslRxGain", DoubleValue (-10) ); wifiPhy.Set ("NslTxGain", DoubleValue (offset + Prss) ); wifiPhy.Set ("NslCcaMode1Threshold", DoubleValue (0.0) ); /***************************************************************************/ /** wifi channel **/ YansWifiChannelHelper wifiChannel ; wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel"); // create wifi channel Ptr wifiChannelPtr = wifiChannel.Create (); wifiPhy.SetChannel (wifiChannelPtr); /** MAC layer **/ // Add a non-QoS upper MAC, and disable rate control NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode",StringValue(phyMode), "ControlMode",StringValue(phyMode)); // Set it to ad-hoc mode wifiMac.SetType ("ns3::AdhocWifiMac"); /** install receivers **/ NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, networkNodes); /** mobility **/ MobilityHelper mobility; Ptr positionAlloc = CreateObject (); positionAlloc->Add (Vector (0.0, 0.0, 0.0)); positionAlloc->Add (Vector (distanceToRx, 0.1*distanceToRx, 0.0)); positionAlloc->Add (Vector (2*distanceToRx, 0.0, 0.0)); positionAlloc->Add (Vector (3*distanceToRx, 0.1*distanceToRx, 0.0)); positionAlloc->Add (Vector (2*distanceToRx, -0.5*distanceToRx, 0.0)); // jammer location mobility.SetPositionAllocator (positionAlloc); mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); mobility.Install (c); /** Energy Model **/ /***************************************************************************/ /* energy source */ BasicEnergySourceHelper basicSourceHelper; // configure energy source basicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (0.1)); /* device energy model */ RadioEnergyHelper radioEnergyHelper; // configure radio energy model radioEnergyHelper.Set ("TxPowerW", DoubleValue (0.0435)); /* energy model helper */ EnergyModelHelper energyHelper; // install on all nodes energyHelper.Install (basicSourceHelper, radioEnergyHelper, c); /***************************************************************************/ /** utility **/ /***************************************************************************/ WirelessModuleUtilityHelper utilityHelper; // set inclusion/exclusion list for all nodes std::vector AllInclusionList; AllInclusionList.push_back("ns3::UdpHeader"); std::vector AllExclusionList; AllExclusionList.push_back("ns3::olsr::PacketHeader"); // assign lists to helper utilityHelper.SetInclusionList (AllInclusionList); utilityHelper.SetExclusionList (AllExclusionList); // install on all nodes utilityHelper.Install (c); /***************************************************************************/ /** jammer **/ // install MAC & PHY onto jammer NetDeviceContainer devices2 = wifi.Install (wifiPhy, wifiMac, c.Get (4)); /***************************************************************************/ JammerHelper jammerHelper; // configure jammer type jammerHelper.SetJammerType ("ns3::ReactiveJammer"); // set jammer parameters jammerHelper.Set ("ReactiveJammerRxTimeout", TimeValue (Seconds (2.0))); // install jammer jammerHelper.Install (c.Get (4)); // Get pointer to JammerYansWifiPhy Ptr jammerPtr = c.Get (4)->GetObject (); /***************************************************************************/ /** mitigation **/ /***************************************************************************/ JammingMitigationHelper mitigationHelper; // configure mitigation type mitigationHelper.SetJammingMitigationType ("ns3::MitigateByChannelHop"); // configure mitigation parameters mitigationHelper.Set("MitigateByChannelHopChannelHopDelay", TimeValue (Seconds (0.0))); // install mitigation on honest nodes mitigationHelper.Install (c.Get (0)); mitigationHelper.Install (c.Get (1)); mitigationHelper.Install (c.Get (2)); mitigationHelper.Install (c.Get (3)); // get ptr to mitigation for node 3 Ptr mitigationPtr = c.Get(3)->GetObject (); /***************************************************************************/ /** Internet stack **/ InternetStackHelper internet; internet.Install (networkNodes); Ipv4AddressHelper ipv4; NS_LOG_INFO ("Assign IP Addresses."); ipv4.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer i = ipv4.Assign (devices); TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); Ptr recvSink = Socket::CreateSocket (networkNodes.Get (3), tid); // node 3 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80); recvSink->Bind (local); recvSink->SetRecvCallback (MakeCallback (&ReceivePacket)); Ptr source = Socket::CreateSocket (networkNodes.Get (0), tid); // node 0 InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80); source->Connect (remote); NS_LOG_UNCOND ("Primary packet RSS=" << Prss << " dBm and jammer RSS=" << Jrss << " dBm "); /** simulation setup **/ // start traffic Simulator::Schedule (Seconds (startTime), &GenerateTraffic, source, PpacketSize, networkNodes.Get (0), numPackets, interPacketInterval); // start jammer Simulator::Schedule (Seconds (startTime + 7.0), &ns3::Jammer::StartJammer, jammerPtr); Simulator::Stop (Seconds (20.0)); Simulator::Run (); Simulator::Destroy (); return 0; }