diff --git a/icmpv6-redirect.py b/icmpv6-redirect.py new file mode 100644 index 0000000..4c205f8 --- /dev/null +++ b/icmpv6-redirect.py @@ -0,0 +1,134 @@ +#/* -*- Mode: Python; -*- */ +#/* +# * Copyright (c) 2017 NITK Surathkal +# * +# * 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 +# * +# * Ported to Python by: Akshay Anandrao Honrao +# */ + + +# Network topology +# +# STA2 +# | +# | +# R1 R2 +# | | +# | | +# ------------ +# | +# | +# STA 1 +# +# - Initial configuration : +# - STA1 default route : R1 +# - R1 static route to STA2 : R2 +# - STA2 default route : R2 +# - STA1 send Echo Request to STA2 using its default route to R1 +# - R1 receive Echo Request from STA1, and forward it to R2 +# - R1 send an ICMPv6 Redirection to STA1 with Target STA2 and Destination R2 +# - Next Echo Request from STA1 to STA2 are directly sent to R2 + + +import ns.network +import ns.core +import ns.applications +import ns.internet +import ns.internet_apps +import ns.csma +import sys + +cmd = ns.core.CommandLine() +cmd.AddValue ("verbose", "turn on log components") +cmd.Parse(sys.argv) + +if verbose == "True": + ns.core.LogComponentEnable("Icmpv6RedirectExample", ns.core.LOG_LEVEL_INFO) + ns.core.LogComponentEnable("Icmpv6L4Protocol", ns.core.LOG_LEVEL_INFO) + ns.core.LogComponentEnable("Ipv6L3Protocol", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable("Ipv6StaticRouting", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable("Ipv6Interface", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable("Icmpv6L4Protocol", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable("NdiscCache", ns.core.LOG_LEVEL_ALL) + +print "Creat nodes." +stal = ns.nodes.CreatObject() +r1 = ns.nodes.CreatObject() +r2 = ns.nodes.CreatObject() +sta2 = ns.nodes.CreatObject() + +net1 = ns.network.NodeContainer(stal, r1, r2) +net2 = ns.network.NodeContainer(r2, sta2) +all1 = ns.network.NodeContainer(stal, r1, r2, sta2) #all is python keyword hence all1 is used instead of all. + +internetv6 = ns.internet.InternetStackHelper() +internetv6.Install(all1) + +print "Create channels." +csma = ns.csma.CsmaHelper() +csma.SetChannelAttribute("DataRate", ns.core.DataRateValue (5000000)) +csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(6560))) +ndc1 = csma.Install(net1) +ndc2 = csma.Install(net2) + +print "Assign IPv6 Addresses." +ipv6 = ns.internet.Ipv6AddressHelper() + +ipv6.SetBase (ns.network.Ipv6Address ("2001:1::"), ns.network.Ipv6Prefix (64)) +iic1 = ipv6.Assign (ndc1) +iic1.SetForwarding (2, True) +iic1.SetForwarding (1, True) +iic1.SetDefaultRouteInAllNodes (1) + +ipv6.SetBase (ns.network.Ipv6Address ("2001:2::"), ns.network.Ipv6Prefix (64)) +iic2 = ipv6.Assign (ndc2) +iic2.SetForwarding (0, True) +iic1.SetDefaultRouteInAllNodes (0) + +routingHelper = ns.internet.Ipv6StaticRoutingHelper() + +# manually inject a static route to the second router. +routing = ns.internet.Ipv6StaticRouting() +routing = routingHelper.GetStaticRouting (r1.GetObject()) +routing.AddHostRouteTo (iic2.GetAddress (1, 1), iic1.GetAddress (2, 0), iic1.GetInterfaceIndex (1)) + +routingStream = ns.internet.OutputStreamWrapper() +routingHelper.PrintRoutingTableAt (ns.core.Seconds (0.0), r1, routingStream) +routingHelper.PrintRoutingTableAt (ns.core.Seconds (3.0), sta1, routingStream) + +print "Create Applications." +cmd.packetSize = 1024 +cmd.maxPacketCount = 5 +interPacketInterval = ns.core.Seconds (1.) +ping6 = ns.applications.Ping6Helper() + +ping6.SetLocal (iic1.GetAddress (0, 1)) +ping6.SetRemote (iic2.GetAddress (1, 1)) +ping6.SetAttribute ("MaxPackets", ns.core.UintegerValue (maxPacketCount)) +ping6.SetAttribute ("Interval", ns.core.TimeValue (interPacketInterval)) +ping6.SetAttribute ("PacketSize", ns.core.UintegerValue (packetSize)) +apps = ns.network.ApplicationContainer() +apps = ping6.Install(stal) +apps.Start (ns.core.Seconds (2.0)) +apps.Stop (ns.core.Seconds (10.0)) + +Ascii = ns.internet.AsciiTraceHelper() +csma.EnableAsciiAll (Ascii.CreateFileStream ("icmpv6-redirect.tr")) +csma.EnablePcapAll ("icmpv6-redirect", True) + +print "Run Simulation." +ns.core.Simulator.Run () +ns.core.Simulator.Destroy () +print "Done." \ No newline at end of file diff --git a/radvd.py b/radvd.py new file mode 100644 index 0000000..4f5e1e3 --- /dev/null +++ b/radvd.py @@ -0,0 +1,157 @@ +#/* -*- Mode: Python; -*- */ +#/* +#* Copyright (c) 2017 NITK Surathkal +#* +# * 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 + #* + #* Ported to Python by: Ritwick Mishra + #* + #*/ + +#// Network topology +#// // +#// // n0 R n1 +#// // | _ | +#// // ====|_|==== +#// // router +#// // - R sends RA to n0's subnet (2001:1::/64); +#// // - R sends RA to n1's subnet (2001:2::/64); +#// // - n0 ping6 n1. +#// // +#// // - Tracing of queues and packet receptions to file "radvd.tr" + + + +import ns.core +import ns.internet +import ns.applications +import ns.radvd +import ns.csma + + +print "RadvdExample" + +verbose = False + +cmd. ns.core.CommandLine +cmd.AddValue ("verbose", "turn on log components") +cmd.Parse (sys.argv) +if verbose: + + ns.core.LogComponentEnable ("Ipv6L3Protocol", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable ("Ipv6RawSocketImpl", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable ("Icmpv6L4Protocol", ns.core.LOG_LEVEL_ALL) + LogComponentEnable ("Ipv6StaticRouting", ns.core.LOG_LEVEL_ALL) + LogComponentEnable ("Ipv6Interface", ns.core.LOG_LEVEL_ALL) + LogComponentEnable ("RadvdApplication", ns.core.LOG_LEVEL_ALL) + LogComponentEnable ("Ping6Application", ns.core.LOG_LEVEL_ALL) + +print "Create nodes." +n0 = ns.network.CreateObject () +r = ns.network.CreateObject () +n1 = ns.network.CreateObject () + +net1 = ns.network.NodeContainer(n0, r) +net2 = ns.network.NodeContainer(r, n1) +allnet = ns.network.NodeContainer(n0, r, n1) # allnet is like all in cpp program + +print "Create IPv6 Internet Stack" +internetv6 = ns.internet.InternetStackHelper() +internetv6.Install(all) + +print "Create channels." +csma = ns.csma.CsmaHelper() +csma.SetChannelAttribute ("DataRate", DataRateValue (5000000)) +csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))) +d1 = csma.Install (net1) #/* n0 - R */ +d2 = csma.Install (net2) #/* R - n1 */ + +print "Create networks and assign IPv6 Addresses." +ipv6 = ns.internet.Ipv6AddressHelper; + +#/* first subnet */ +ipv6.SetBase (ns.network.Ipv6Address ("2001:1::"), ns.network.Ipv6Prefix (64)) +tmp = ns.network.NetDeviceContainer() +tmp.Add (d1.Get (0)) #/* n0 */ +iic1 = ns.internet.Ipv6InterfaceContainer() +iic1 = ipv6.AssignWithoutAddress (tmp) #/* n0 interface */ + +tmp2 = ns.network.NetDeviceContainer() +tmp2.Add (d1.Get (1)) #/* R */ +iicr1 = ns.internet.Ipv6InterfaceContainer() +iicr1 = ipv6.Assign (tmp2) #/* R interface to the first subnet is just statically assigned */ +iicr1.SetForwarding (0, True) +iic1.Add (iicr1) +#/* second subnet R - n1 */ +ipv6.SetBase (ns.network.Ipv6Address ("2001:2::"), ns.network.Ipv6Prefix (64)) + +tmp3 = ns.network.NetDeviceContainer() +tmp3.Add (d2.Get (0)); #/* R */ +iicr2 = ns.internet.Ipv6InterfaceContainer() +iicr2 = ipv6.Assign (tmp3)#/* R interface */ +iicr2.SetForwarding (0, True) + +tmp4 = ns.network.NetDeviceContainer() +tmp4.Add (d2.Get (1)) #/* n1 */ +iic2 = ns.internet.Ipv6InterfaceContainer() +iic2 = ipv6.AssignWithoutAddress (tmp4) +iic2.Add (iicr2) + +#/* radvd configuration */ +radvdHelper = ns.applications.RadvdHelper () + +#/* R interface (n0 - R) */ +#/* n0 will receive unsolicited (periodic) RA */ +radvdHelper.AddAnnouncedPrefix (iic1.GetInterfaceIndex (1), ns.network.Ipv6Address("2001:1::0"), 64) + +#/* R interface (R - n1) */ +#/* n1 will have to use RS, as RA are not sent automatically */ +radvdHelper.AddAnnouncedPrefix(iic2.GetInterfaceIndex (1), ns.network.Ipv6Address("2001:2::0"), 64) +radvdHelper.GetRadvdInterface (iic2.GetInterfaceIndex (1)).SetSendAdvert (False) + +radvdApps = ns.network.ApplicationContainer() +radvdApps = radvdHelper.Install (r) +radvdApps.Start (ns.coreSeconds (1.0)) +radvdApps.Stop (Seconds (10.0)) + +#* Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via R */ +packetSize = 1024 +maxPacketCount = 5 +interPacketInterval = Time() +interPacketInterval = Seconds (1.) +ping6 = ns.applications.Ping6Helper() + +#/* ping6.SetLocal (iic1.GetAddress (0, 1)); */ +ping6.SetRemote (ns.network.Ipv6Address ("2001:2::200:ff:fe00:4")) #/* should be n1 address after autoconfiguration */ +ping6.SetIfIndex (iic1.GetInterfaceIndex (0)) + +ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)) +ping6.SetAttribute ("Interval", TimeValue (interPacketInterval)) +ping6.SetAttribute ("PacketSize", UintegerValue (packetSize)) +apps = ns.network.ApplicationContainer() +apps = ping6.Install (net1.Get (0)) +apps.Start (Seconds (2.0)) +apps.Stop (Seconds (7.0)) + +Ascii = ns.applications.AsciiTraceHelper () +csma.EnableAsciiAll (Ascii.CreateFileStream ("radvd.tr")) +csma.EnablePcapAll ("radvd", True) + +print ("Run Simulation.") +ns.core.Simulator.Run () +ns.core.Simulator.Destroy () +print ("Done.") + + + diff --git a/wsn-ping6.py b/wsn-ping6.py new file mode 100644 index 0000000..a9b781d --- /dev/null +++ b/wsn-ping6.py @@ -0,0 +1,155 @@ +#/* -*- Mode: Python; -*- */ +#/* +# * Copyright (c) 2017 NITK surathkal +# * +# * 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 +# * +# * Ported to Python by: Abhilash Sanap +# */ + +#// Network topology +#// +#// n0 n1 +#// | | +#// ================= +#// WSN (802.15.4) +#// +#// - ICMPv6 echo request flows from n0 to n1 and back with ICMPv6 echo reply +#// - DropTail queues +#// - Tracing of queues and packet receptions to file "wsn-ping6.tr" +#// +#// This example is based on the "ping6.cc" example. + +import ns.core +import ns.network +import ns.internet +import ns.applications +import sys +import ns +import ns3.lr-wpan +import ns.internet-apps +import ns.mobility +import ns.lr-wpan + +print "Ping6WsnExample" + +verbose = False + +cmd = ns.core.CommandLine() +cmd.AddValue ("verbose", "turn on log components") +cmd.Parse (sys.argv) + +if (verbose): + + ns.core.LogComponentEnable ("Ping6WsnExample", ns.core.LOG_LEVEL_INFO) + ns.core.LogComponentEnable ("Ipv6EndPointDemux", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable ("Ipv6L3Protocol", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable ("Ipv6StaticRouting", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable ("Ipv6ListRouting", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable ("Ipv6Interface", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable ("Icmpv6L4Protocol", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable ("Ping6Application", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable ("NdiscCache", ns.core.LOG_LEVEL_ALL) + ns.core.LogComponentEnable ("SixLowPanNetDevice", ns.core.LOG_LEVEL_ALL) + + +print "Create nodes." +nodes = ns.network.NodeContainer() +nodes.Create(2) + + #Set seed for random numbers + + ns.core.SeedManager.SetSeed (167) + + # Install mobility + +mobility=ns.network.MobilityHelper () +mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel") + +nodesPositionAlloc = ns.network.ListPositionAllocator.CreateObject () +nodesPositionAlloc.Add(Vector (0.0, 0.0, 0.0)) +nodesPositionAlloc.Add(Vector (50.0, 0.0, 0.0)) +mobility.SetPositionAllocator (nodesPositionAlloc) +mobility.Install (nodes) + +print "Create channels." + +lrWpanHelper = ns.network.LrWpanhelper() + +devContainer = lrWpanHelper.Install(nodes) +lrWpanHelper.AssociateToPan (devContainer,10) + +# Add and install the LrWpanNetDevice for each node + +print "Created "+devContainer.GetN()+" devices\n" +print "There are "+nodes.GetN()+" nodes\n" + +# Install IPv4/IPv6 stack + +print "Install Internet Stack." + +internetv6 = ns.internet.InternetStackHelper() +internetv6.SetIpv4StackInstall (False) +internetv6.Install (nodes) + +# Install 6LowPan Layer + +print "Install 6LowPAN." + +sixlowpan = ns.internet.SixLowPanHelper() +six1 = sixlowpan.Install (devContainer) + +print "Assign addresses." + +ipv6=ns.internet.Ipv6AddressHelper() +ipv6.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64)) + +i = ns.internet.Ipv6InterfaceContainer() +i = ipv6.Assign(six1) + +print "Create Applications." + +# Create a Ping6 application to send ICMPv6 echo request from node zero to +# all-nodes (ff02::1). + + +cmd.packetSize = 10 +cmd.maxPacketCount = 5 + +interPacketInterval = ns.core.TimeValue(ns.core.Seconds (1.)) +ping6 = ns.applications.PingHelper () + +ping6.SetLocal (i.GetAddress (0, 1)) +ping6.SetRemote (i.GetAddress (1, 1)) + +ping6.SetAttribute ("MaxPackets", ns.core.UintegerValue (maxPacketCount)) +ping6.SetAttribute ("Interval", ns.core.TimeValue (interPacketInterval)) +ping6.SetAttribute ("PacketSize", ns.core.UintegerValue (packetSize)) + +apps = ns.network.ApplicationContainer() +apps = ping6.Install(nodes.Get(0)) + +apps.Start (ns.core.Seconds (2.0)) +apps.Stop (ns.core.Seconds (10.0)) + +Ascii = ns.applications.AsciiTraceHelper() +lrWpanHelper.EnableAsciiAll (Ascii.CreateFileStream ("ping6wsn.tr")) +lrWpanHelper.EnablePcapAll (std::string ("ping6wsn"), True) + +print "Run Simulation." + +ns.core.Simulator.Run () +ns.core.Simulator.Destroy () + +print "Done."