#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/packet-sink.h"
#include "ns3/olsr-routing-protocol.h"
#include "ns3/olsr-helper.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("DynamicGlobalRoutingExample");
int main (int argc, char *argv[])
{
// The below value configures the default behavior of global routing.
// By default, it is disabled. To respond to interface events, set to true
Config::SetDefault ("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue (true));
/*
LogComponentEnable ("UdpTraceClient", LOG_LEVEL_INFO);
LogComponentEnable ("UdpServer", LOG_LEVEL_INFO);
LogComponentEnable ("PacketSink", LOG_LEVEL_INFO);
LogComponentEnable("PacketSocketClient", LOG_LEVEL_INFO);
LogComponentEnable("RttEstimator", LOG_LEVEL_INFO);
LogComponentEnable("DataRate", LOG_LEVEL_INFO);
LogComponentEnable("InternetStackHelper", LOG_LEVEL_INFO);
LogComponentEnable("GlobalRoutingHelper", LOG_LEVEL_INFO);
LogComponentEnable("Ipv4ListRouting", LOG_LEVEL_INFO);
*/
// Allow the user to override any of the defaults and the above
// Bind ()s at run-time, via command-line arguments
CommandLine cmd;
cmd.Parse (argc, argv);
NS_LOG_INFO ("Create nodes.");
NodeContainer c;
c.Create (15);
/* Nodi sorgente/destinazione */
/* Nodo sorgente H1 Nodo destinazione H2 */
NodeContainer n0n11 = NodeContainer (c.Get (0), c.Get (11)); //R1 - N1
NodeContainer n10n14 = NodeContainer (c.Get (10), c.Get (14)); //R11 - N2
NodeContainer n6n12 = NodeContainer (c.Get (6), c.Get (12)); //R7 - N3 Client
NodeContainer n2n13 = NodeContainer (c.Get (2), c.Get (13)); //R3 - N4 Server
Address serverAddress;
/* Routers */
NodeContainer n0n3 = NodeContainer (c.Get (0), c.Get (3));
NodeContainer n0n7 = NodeContainer (c.Get (0), c.Get (7));
NodeContainer n1n3 = NodeContainer (c.Get (1), c.Get (3));
NodeContainer n1n6 = NodeContainer (c.Get (1), c.Get (6));
NodeContainer n1n9 = NodeContainer (c.Get (1), c.Get (9));
NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
NodeContainer n6n2 = NodeContainer (c.Get (6), c.Get (2));
NodeContainer n5n3 = NodeContainer (c.Get (5), c.Get (3));
NodeContainer n9n2 = NodeContainer (c.Get (9), c.Get (2));
NodeContainer n9n5 = NodeContainer (c.Get (9), c.Get (5));
NodeContainer n5n8 = NodeContainer (c.Get (5), c.Get (8));
NodeContainer n9n4 = NodeContainer (c.Get (9), c.Get (4));
NodeContainer n2n7 = NodeContainer (c.Get (2), c.Get (7));
NodeContainer n7n4 = NodeContainer (c.Get (7), c.Get (4));
NodeContainer n7n10 = NodeContainer (c.Get (7), c.Get (10));
NodeContainer n8n10 = NodeContainer (c.Get (8), c.Get (10));
NS_LOG_INFO ("Enabling OLSR Routing.");
OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRouting;
Ipv4ListRoutingHelper list;
list.Add (staticRouting, 0);
list.Add (olsr, 10);
InternetStackHelper internet;
internet.SetRoutingHelper (list);
internet.Install (c);
// We create the channels first without any IP addressing information
NS_LOG_INFO ("Create channels.");
PointToPointHelper p2p;
p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
/* Link sui Router Point to Point */
NetDeviceContainer d0d3 = p2p.Install(n0n3);
NetDeviceContainer d0d7 = p2p.Install(n0n7);
NetDeviceContainer d1d3 = p2p.Install(n1n3);
NetDeviceContainer d1d6 = p2p.Install(n1n6);
NetDeviceContainer d1d9 = p2p.Install(n1n9);
NetDeviceContainer d1d2 = p2p.Install(n1n2);
NetDeviceContainer d6d2 = p2p.Install(n6n2);
NetDeviceContainer d5d3 = p2p.Install(n5n3);
NetDeviceContainer d9d2 = p2p.Install(n9n2);
NetDeviceContainer d9d5 = p2p.Install(n9n5);
NetDeviceContainer d5d8 = p2p.Install(n5n8);
NetDeviceContainer d9d4 = p2p.Install(n9n4);
NetDeviceContainer d2d7 = p2p.Install(n2n7);
NetDeviceContainer d7d4 = p2p.Install(n7n4);
NetDeviceContainer d7d10 = p2p.Install(n7n10);
NetDeviceContainer d8d10 = p2p.Install(n8n10);
/* Link sugli Host*/
NetDeviceContainer d0d11 = p2p.Install(n0n11); // Client
NetDeviceContainer d10d14 = p2p.Install(n10n14); //Server
/* Link sugli Host*/
NetDeviceContainer d6d12 = p2p.Install(n6n12); // Client
NetDeviceContainer d2d13 = p2p.Install(n2n13); //Server
// Later, we add IP addresses.
NS_LOG_INFO ("Assign IP Addresses.");
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.1.0", "255.255.255.252");
Ipv4InterfaceContainer i0i3 = ipv4.Assign (d0d3);
i0i3.SetMetric (0, 3);
i0i3.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.4", "255.255.255.252");
Ipv4InterfaceContainer i0i7 = ipv4.Assign (d0d7);
i0i7.SetMetric (0, 3);
i0i7.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.8", "255.255.255.252");
Ipv4InterfaceContainer i1i3 = ipv4.Assign (d1d3);
i1i3.SetMetric(0,1);
i1i3.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.12", "255.255.255.252");
Ipv4InterfaceContainer i1i6 = ipv4.Assign (d1d6);
i1i6.SetMetric (0, 3);
i1i6.SetMetric (1, 3);
Ipv4Interface qs = Ipv4Interface ();
qs.SetDevice(d1d6.Get(0));
qs.SetMetric(3);
ipv4.SetBase ("10.1.1.16", "255.255.255.252");
Ipv4InterfaceContainer i1i9 = ipv4.Assign (d1d9);
i1i9.SetMetric (0, 3);
i1i9.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.20", "255.255.255.252");
Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);
i1i2.SetMetric (0, 3);
i1i2.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.24", "255.255.255.252");
Ipv4InterfaceContainer i6i2 = ipv4.Assign (d6d2);
i6i2.SetMetric (0, 3);
i6i2.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.28", "255.255.255.252");
Ipv4InterfaceContainer i5i3 = ipv4.Assign (d5d3);
i5i3.SetMetric (0, 3);
i5i3.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.32", "255.255.255.252");
Ipv4InterfaceContainer i9i2 = ipv4.Assign (d9d2);
i9i2.SetMetric (0, 3);
i9i2.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.36", "255.255.255.252");
Ipv4InterfaceContainer i9i5 = ipv4.Assign (d9d5);
i9i5.SetMetric (0, 3);
i9i5.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.40", "255.255.255.252");
Ipv4InterfaceContainer i5i8 = ipv4.Assign (d5d8);
i5i8.SetMetric (0, 3);
i5i8.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.44", "255.255.255.252");
Ipv4InterfaceContainer i9i4 = ipv4.Assign (d9d4);
i9i4.SetMetric (0, 3);
i9i4.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.48", "255.255.255.252");
Ipv4InterfaceContainer i2i7 = ipv4.Assign (d2d7);
i2i7.SetMetric (0, 3);
i2i7.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.56", "255.255.255.252");
Ipv4InterfaceContainer i7i4 = ipv4.Assign (d7d4);
i7i4.SetMetric (0, 3);
i7i4.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.60", "255.255.255.252");
Ipv4InterfaceContainer i7i10 = ipv4.Assign (d7d10);
i7i10.SetMetric (0, 3);
i7i10.SetMetric (1, 3);
ipv4.SetBase ("10.1.1.64", "255.255.255.252");
Ipv4InterfaceContainer i8i10 = ipv4.Assign (d8d10);
i8i10.SetMetric (0, 3);
i8i10.SetMetric (1, 3);
/* Assegno l'indirizzo al collegamento client */
ipv4.SetBase ("10.1.2.0", "255.255.255.128");
ipv4.Assign (d0d11);
/* Assegno l'indirizzo al collegamento server */
ipv4.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer i = ipv4.Assign (d10d14);
serverAddress = Address (i.GetAddress (1));
/* Assegno l'indirizzo al collegamento client */
ipv4.SetBase ("10.1.4.0", "255.255.255.128");
ipv4.Assign (d6d12);
/* Assegno l'indirizzo al collegamento server */
ipv4.SetBase ("10.1.5.0", "255.255.255.0");
Ipv4InterfaceContainer i1 = ipv4.Assign (d2d13);
// Create router nodes, initialize routing database and set up the routing
// tables in the nodes.
// Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
NS_LOG_INFO ("Create Applications.");
//
// Create one udpServer applications on node one.
//
/* Server UDP */
uint16_t port = 4000;
uint32_t MaxPacketSize = 1472; // Back off 20 (IP) + 8 (UDP) bytes from MTU
UdpServerHelper server (port);
ApplicationContainer apps = server.Install (c.Get (14));
apps.Start (Seconds (502.0));
apps.Stop (Seconds (1000.0));
/* Client UDP */
UdpTraceClientHelper client (serverAddress, port,"");
client.SetAttribute ("MaxPacketSize", UintegerValue (MaxPacketSize));
apps = client.Install (c.Get (11));
apps.Start (Seconds (502.0));
apps.Stop (Seconds (1000.0));
/* Client TCP */
// serverAddress1 = Address ();
uint16_t port1 = 9; // well-known echo port number
BulkSendHelper source ("ns3::TcpSocketFactory",
InetSocketAddress (i1.GetAddress (1), port1));
source.SetAttribute ("MaxBytes", UintegerValue (MaxPacketSize));
ApplicationContainer sourceApps = source.Install (c.Get (12));
sourceApps.Start (Seconds (502.0));
sourceApps.Stop (Seconds (1000.0));
/* Server TCP */
PacketSinkHelper sink ("ns3::TcpSocketFactory",
InetSocketAddress (Ipv4Address::GetAny (), port1));
ApplicationContainer sinkApps = sink.Install (c.Get (13));
sinkApps.Start (Seconds (502.0));
sinkApps.Stop (Seconds (10000.0));
// Trace routing tables
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("dynamic-global-routing.routes", std::ios::out);
staticRouting.PrintRoutingTableAllEvery(Seconds (1), routingStream);
//
// Now, do the actual simulation.
//
NS_LOG_INFO ("Run Simulation.");
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");
}