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
65 changes: 65 additions & 0 deletions internet-apps/csma-ping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import ns.applications
import ns.core
import ns.internet
import ns.network
import ns.point_to_point
import ns.csma
import ns.internet_apps


c = ns.network.NodeContainer()
c.Create(4)


csma = ns.csma.CsmaHelper()
csma.SetChannelAttribute("DataRate", ns.core.StringValue("100Mbps"))
csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))

csma.SetDeviceAttribute ("EncapsulationMode", ns.core.StringValue ("Llc"));
devs = csma.Install (c);

stack = ns.internet.InternetStackHelper()
stack.Install(c)


ip = ns.internet.Ipv4AddressHelper()
ip.SetBase(ns.network.Ipv4Address("10.1.1.0"),
ns.network.Ipv4Mask("255.255.255.0"))
addresses = ns.internet.Ipv4InterfaceContainer()

addresses = ip.Assign(devs)


ns.core.Config.SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", ns.core.StringValue ("2"))

dst = ns.network.InetSocketAddress (addresses.GetAddress (3))
onoff =ns.applications.OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
onoff.SetAttribute ("DataRate", ns.core.StringValue("15000"));
onoff.SetAttribute ("PacketSize", ns.core.UintegerValue (1200));

apps=ns.network.ApplicationContainer ()
apps = onoff.Install (c.Get (0));
apps.Start (ns.core.Seconds (1.0));
apps.Stop (ns.core.Seconds (10.0));

sink = ns.applications.PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst)
apps = sink.Install (c.Get (3));
apps.Start (ns.core.Seconds (0.0));
apps.Stop (ns.core.Seconds (11.0));

ping =ns.internet_apps.V4PingHelper(addresses.GetAddress(2));
pingers = ns.network.NodeContainer()
pingers.Add (c.Get (0));
pingers.Add (c.Get (1));
pingers.Add (c.Get (3));
apps = ping.Install (pingers);
apps.Start (ns.core.Seconds (2.0));
apps.Stop (ns.core.Seconds (5.0));


csma.EnablePcapAll("csma-ping", False)

ns.core.Simulator.Run()
ns.core.Simulator.Destroy()


93 changes: 93 additions & 0 deletions internet-apps/iping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import ns.applications
import ns.core
import ns.internet
import ns.network
import ns.internet_apps
import ns.point_to_point
import sys

cmd = ns.core.CommandLine()
cmd.c = 4294967295
cmd.t = 0
cmd.i = 1.0
cmd.w = 15.0
cmd.s = 56
cmd.v = False
cmd.q = False

cmd.AddValue("c", "Stops after sending specified number(count) of ECHO_REQUEST packets.")
cmd.AddValue("i", "Specify the interval seconds between sending each packet.")
cmd.AddValue("t", "Set the IP Time to Live.")
cmd.AddValue("w", "Specify a timeout, in seconds, before ping exits regardless of how many packets have been sent or received.")
cmd.AddValue("s", "Specifies the number of data bytes to be sent.")
cmd.AddValue("v", "Verbose output.")
cmd.AddValue("q", "Quiet output. Nothing is displayed except the summary lines at startup time and when finished.")

cmd.Parse(sys.argv)

count = int(cmd.c)
ttl = int(cmd.t)
deadline = float(cmd.w)
packetsize = int(cmd.s)
interval = float(cmd.i)
verbose = bool(cmd.v)
quiet = bool(cmd.q)

if count <=0:
print ("ping: bad number of packets to transmit.")
sys.exit(1)
if interval < 0:
print ("ping: bad timing interval.")
sys.exit(1)
if ttl < 0:
print ("ping: bad value.")
sys.exit(1)
if packetsize < 0:
print ("ping: illegal negetive packet size.")
sys.exit(1)
if interval < 0.2 and interval >= 0 :
print ("ping: cannot flood minimal interval allowed for user is 200ms.")
sys.exit(1)

c = ns.network.NodeContainer()
c.Create (2)

pointToPoint = ns.point_to_point.PointToPointHelper()
pointToPoint.SetDeviceAttribute ("DataRate", ns.core.StringValue ("5Mbps"))
pointToPoint.SetChannelAttribute ("Delay", ns.core.StringValue ("2ms"))

devices = ns.network.NetDeviceContainer()
devices = pointToPoint.Install(c)

address = ns.internet.Ipv4AddressHelper()
address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
stack = ns.internet.InternetStackHelper()
stack.Install(c)

interfaces = ns.internet.Ipv4InterfaceContainer()
interfaces = address.Assign(devices)

app = ns.internet_apps.V4Ping()

app.SetAttribute ("Remote", ns.network.Ipv4AddressValue (interfaces.GetAddress(1)))
app.SetAttribute ("Verbose", ns.core.BooleanValue (verbose))
app.SetAttribute ("Count", ns.core.UintegerValue (count))
app.SetAttribute ("Ttl", ns.core.UintegerValue (ttl))
app.SetAttribute ("Quiet", ns.core.BooleanValue (quiet) )
app.SetAttribute ("Interval", ns.core.TimeValue (ns.core.Seconds (interval)) )
app.SetAttribute ("Size", ns.core.UintegerValue (packetsize))

c.Get(0).AddApplication(app)

app.SetStartTime(ns.core.Seconds(1.0))
app.SetStopTime(ns.core.Seconds(1.0 + deadline))

pointToPoint.EnablePcapAll("iping", False)

ns.core.Simulator.Run()
ns.core.Simulator.Destroy()