Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions icmpv6-redirect.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
# */


# 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."
157 changes: 157 additions & 0 deletions radvd.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
#*
#*/

#// 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<Node> ()
r = ns.network.CreateObject<Node> ()
n1 = ns.network.CreateObject<Node> ()

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.")



Loading