Static Routing (Wired)
[Topology]
Test-static-routing1.cc (put the file under scratch)
#include <iostream> #include <fstream> #include <string> #include <cassert>
#include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/point-to-point-module.h" #include "ns3/csma-module.h" #include "ns3/applications-module.h" #include "ns3/ipv4-static-routing-helper.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("StaticRoutingSlash32Test");
int main (int argc, char *argv[]) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
CommandLine cmd (__FILE__); cmd.Parse (argc, argv);
/* Build nodes. */ NodeContainer term_0; term_0.Create (1); NodeContainer term_1; term_1.Create (1); NodeContainer router_0; router_0.Create (1); NodeContainer router_1; router_1.Create (1);
/* Build link. */ PointToPointHelper p2p_p2p_0; p2p_p2p_0.SetDeviceAttribute ("DataRate", StringValue ("10Mbps")); p2p_p2p_0.SetChannelAttribute ("Delay", StringValue ("1ms")); PointToPointHelper p2p_p2p_1; p2p_p2p_1.SetDeviceAttribute ("DataRate", StringValue ("10Mbps")); p2p_p2p_1.SetChannelAttribute ("Delay", StringValue ("1ms")); PointToPointHelper p2p_p2p_2; p2p_p2p_2.SetDeviceAttribute ("DataRate", StringValue ("10Mbps")); p2p_p2p_2.SetChannelAttribute ("Delay", StringValue ("1ms"));
/* Build link net device container. */ NodeContainer all_p2p_0; all_p2p_0.Add (router_0); all_p2p_0.Add (term_0); NetDeviceContainer ndc_p2p_0 = p2p_p2p_0.Install (all_p2p_0); NodeContainer all_p2p_1; all_p2p_1.Add (router_0); all_p2p_1.Add (router_1); NetDeviceContainer ndc_p2p_1 = p2p_p2p_1.Install (all_p2p_1); NodeContainer all_p2p_2; all_p2p_2.Add (router_1); all_p2p_2.Add (term_1); NetDeviceContainer ndc_p2p_2 = p2p_p2p_2.Install (all_p2p_2);
/* Install the IP stack. */ InternetStackHelper internetStackH; internetStackH.Install (term_0); internetStackH.Install (term_1); internetStackH.Install (router_0); internetStackH.Install (router_1);
/* IP assign. */ Ipv4AddressHelper ipv4; ipv4.SetBase ("10.0.0.0", "255.255.255.0"); Ipv4InterfaceContainer iface_ndc_p2p_0 = ipv4.Assign (ndc_p2p_0); ipv4.SetBase ("10.0.1.0", "255.255.255.0"); Ipv4InterfaceContainer iface_ndc_p2p_1 = ipv4.Assign (ndc_p2p_1); ipv4.SetBase ("10.0.2.0", "255.255.255.0"); Ipv4InterfaceContainer iface_ndc_p2p_2 = ipv4.Assign (ndc_p2p_2);
//Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Ptr<Ipv4> ipv4N0 = term_0.Get (0)->GetObject<Ipv4> (); Ptr<Ipv4>
ipv4R0 = router_0.Get (0)->GetObject<Ipv4>
(); Ptr<Ipv4>
ipv4R1 = router_1.Get (0)->GetObject<Ipv4>
(); Ptr<Ipv4>
ipv4N1 = term_1.Get (0)->GetObject<Ipv4>
(); Ipv4StaticRoutingHelper
ipv4RoutingHelper; // Create static routes from N0 to R0 Ptr<Ipv4StaticRouting>
staticRoutingN0 = ipv4RoutingHelper.GetStaticRouting (ipv4N0); staticRoutingN0->AddHostRouteTo (Ipv4Address ("10.0.2.2"),
Ipv4Address ("10.0.0.1"), 1); // The ifIndex
we want on node R0 is 2; 0 corresponds to loopback, 1 to the first point to
point link (N0-R1) // 2 to the second point to point link
(R0-R1) Ptr<Ipv4StaticRouting>
staticRoutingR0 = ipv4RoutingHelper.GetStaticRouting (ipv4R0); staticRoutingR0->AddHostRouteTo (Ipv4Address ("10.0.2.2"),
Ipv4Address ("10.0.1.2"), 2); staticRoutingR0->AddHostRouteTo (Ipv4Address ("10.0.0.2"),
Ipv4Address ("10.0.0.2"), 1); Ptr<Ipv4StaticRouting>
staticRoutingR1 = ipv4RoutingHelper.GetStaticRouting (ipv4R1); staticRoutingR1->AddHostRouteTo (Ipv4Address ("10.0.2.2"),
Ipv4Address ("10.0.2.2"), 2); staticRoutingR1->AddHostRouteTo (Ipv4Address ("10.0.0.2"),
Ipv4Address ("10.0.1.1"), 1);
Ptr<Ipv4StaticRouting>
staticRoutingN1 = ipv4RoutingHelper.GetStaticRouting (ipv4N1); staticRoutingN1->AddHostRouteTo (Ipv4Address ("10.0.0.2"),
Ipv4Address ("10.0.2.1"), 1);
UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install (term_1.Get(0)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); UdpEchoClientHelper echoClient (iface_ndc_p2p_2.GetAddress (1), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (term_0.Get(0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); Simulator::Stop (Seconds (11.0)); Simulator::Run (); Simulator::Destroy ();
return 0; } |
Execution
If you remove the red lines, you can only get
The server cannot receive the packet sent from client.
Reference
https://signetlabdei.github.io/lorawan-docs/html/static-routing-slash32_8cc_source.html
Last
Modified: 2022/2/11 done
[Author]
Dr. Chih-Heng Ke
Department of Computer
Science and Information Engineering, National Quemoy University, Kinmen,
Taiwan
Email: smallko@gmail.com