Static Routing (Wireless AdHoc)


[topology] (put this file under scratch)

#include "ns3/core-module.h"

#include "ns3/global-route-manager.h"

#include "ns3/network-module.h"

#include "ns3/internet-module.h"

#include "ns3/wifi-module.h"

#include "ns3/mobility-module.h"

#include "ns3/netanim-module.h"

#include "ns3/applications-module.h"


using namespace ns3;


int main(int argc, char *argv[])


  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);

  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);


  CommandLine cmd;

  cmd.Parse (argc, argv);


  NodeContainer n;

  n.Create (3);


  WifiHelper wifi;

  wifi.SetStandard (WIFI_STANDARD_80211a);

  WifiMacHelper wifiMac;

  YansWifiPhyHelper wifiPhy;

  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

  wifiMac.SetType ("ns3::AdhocWifiMac");

  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",

                                "DataMode", StringValue ("OfdmRate54Mbps"));

  wifiPhy.SetChannel (wifiChannel.Create ());

  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, n);


  MobilityHelper mobility;

  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();

  positionAlloc->Add (Vector (0.0, 0.0, 0.0));

  positionAlloc->Add (Vector (5.0, 0.0, 0.0));

  positionAlloc->Add (Vector (10.0, 0.0, 0.0));

  mobility.SetPositionAllocator (positionAlloc);

  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");

  mobility.Install (n);


  InternetStackHelper stack;

  Ipv4ListRoutingHelper list;

  Ipv4StaticRoutingHelper staticRouting;

  list.Add (staticRouting, 0);

  stack.SetRoutingHelper (list);

  stack.Install (n);


  Ipv4AddressHelper ipv4;

  ipv4.SetBase ("", "");

  Ipv4InterfaceContainer i = ipv4.Assign (devices);


  Ptr<Ipv4> ipv4N0 = n.Get (0)->GetObject<Ipv4> ();

  Ptr<Ipv4> ipv4N1 = n.Get (1)->GetObject<Ipv4> ();

  Ptr<Ipv4> ipv4N2 = n.Get (2)->GetObject<Ipv4> ();


  Ipv4StaticRoutingHelper ipv4RoutingHelper;

  Ptr<Ipv4StaticRouting> staticRoutingN0 = ipv4RoutingHelper.GetStaticRouting (ipv4N0);

  staticRoutingN0->AddHostRouteTo (Ipv4Address (""), Ipv4Address (""), 1);


  Ptr<Ipv4StaticRouting> staticRoutingN1 = ipv4RoutingHelper.GetStaticRouting (ipv4N1);

  staticRoutingN1->AddHostRouteTo (Ipv4Address (""), Ipv4Address (""), 1);

  staticRoutingN1->AddHostRouteTo (Ipv4Address (""), Ipv4Address (""), 1);


  Ptr<Ipv4StaticRouting> staticRoutingN2 = ipv4RoutingHelper.GetStaticRouting (ipv4N2);

  staticRoutingN2->AddHostRouteTo (Ipv4Address (""), Ipv4Address (""), 1);


  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (n.Get(2));

  serverApps.Start (Seconds (0.0));

  serverApps.Stop (Seconds (10.0));


  UdpEchoClientHelper echoClient (i.GetAddress (2), 9);

  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));

  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));

  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (n.Get(0));

  clientApps.Start (Seconds (2.0));

  clientApps.Stop (Seconds (10.0));


  AsciiTraceHelper ascii;

  wifiPhy.EnableAsciiAll (ascii.CreateFileStream (""));


  uint32_t stopTime = 11;

  Simulator::Stop (Seconds (stopTime));

  Simulator::Run ();

  Simulator::Destroy ();





After simulation, you can also get Open it.

Recond 10: N0 send out the data packet. N1 and N2 can receive it. Because N1 and N2 are within the transmission range of N0. But N2 will drop it. Check the DA. N1’s MAC address is 00:00:00:00:00:02 and N2’s MAC address is 00:00:00:00:00:03.


Record 25: N1 will forward the packet to N2. N0 and N2 can receive it. Because N0 and N2 are within the transmission range of N1. But N1 will drop it. Check the DA. N0’s MAC address is 00:00:00:00:00:01 and N2’s MAC address is 00:00:00:00:00:03.


From the above trace file, we can see that the packet is from N0 to N1, and then N1 to N2.


Back to NS3 Learning Guide

Last Modified: 2022/2/11 done



Dr. Chih-Heng Ke

Department of Computer Science and Information Engineering, National Quemoy University, Kinmen, Taiwan