A Discrete-Event Network Simulator
API
csma-raw-ip-socket.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 //
18 // Network topology
19 // (sender) (receiver)
20 // n0 n1 n2 n3
21 // | | | |
22 // =====================
23 //
24 // Node n0 sends data to node n3 over a raw IP socket. The protocol
25 // number used is 2.
26 // The default traffic rate is to send 1250 bytes/second for 10 kb/s
27 // The data rate can be toggled by command line argument
28 
29 #include <iostream>
30 #include <fstream>
31 #include <string>
32 #include <cassert>
33 
34 #include "ns3/core-module.h"
35 #include "ns3/network-module.h"
36 #include "ns3/csma-module.h"
37 #include "ns3/applications-module.h"
38 #include "ns3/internet-apps-module.h"
39 #include "ns3/internet-module.h"
40 
41 using namespace ns3;
42 
43 NS_LOG_COMPONENT_DEFINE ("CsmaRawIpSocketExample");
44 
45 static void SinkRx (Ptr<const Packet> p, const Address &ad)
46 {
47  // Enable the below line to see the packet contents printed out at the
48  // receive sink
49  //std::cout << Simulator::Now().GetSeconds () << " " << *p << std::endl;
50 }
51 
52 int
53 main (int argc, char *argv[])
54 {
55 #if 0
56  LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO);
57 #endif
58  uint32_t dataRate = 10;
60  cmd.AddValue ("dataRate", "application dataRate (Kb/s)", dataRate);
61  cmd.Parse (argc, argv);
62 
63  // Here, we will explicitly create four nodes.
64  NS_LOG_INFO ("Create nodes.");
65  NodeContainer c;
66  c.Create (4);
67 
68  // connect all our nodes to a shared channel.
69  NS_LOG_INFO ("Build Topology.");
71  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
72  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
73  csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc"));
74  NetDeviceContainer devs = csma.Install (c);
75 
76  // add an ip stack to all nodes.
77  NS_LOG_INFO ("Add ip stack.");
78  InternetStackHelper ipStack;
79  ipStack.Install (c);
80 
81  // assign ip addresses
82  NS_LOG_INFO ("Assign ip addresses.");
84  ip.SetBase ("192.168.1.0", "255.255.255.0");
85  Ipv4InterfaceContainer addresses = ip.Assign (devs);
86 
87  NS_LOG_INFO ("Create Source");
88  // IP protocol configuration
89  Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
90  InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3));
91  OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
92  onoff.SetConstantRate (DataRate (dataRate*1000));
93  onoff.SetAttribute ("PacketSize", UintegerValue (1250));
94 
95  ApplicationContainer apps = onoff.Install (c.Get (0));
96  apps.Start (Seconds (0.5));
97  apps.Stop (Seconds (11));
98 
99  NS_LOG_INFO ("Create Sink.");
100  PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
101  apps = sink.Install (c.Get (3));
102  apps.Start (Seconds (0.0));
103  apps.Stop (Seconds (12.0));
104 
105  NS_LOG_INFO ("Configure Tracing.");
106  // first, pcap tracing in non-promiscuous mode
107  csma.EnablePcapAll ("csma-raw-ip-socket", false);
108  // then, print what the packet sink receives.
109  Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
110  MakeCallback (&SinkRx));
111 
113 
114 
115  NS_LOG_INFO ("Run Simulation.");
116  Simulator::Run ();
118  NS_LOG_INFO ("Done.");
119 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:47
holds a vector of ns3::Application pointers.
an Inet address class
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
holds a vector of std::pair of Ptr and interface index.
Hold variables of type string.
Definition: string.h:41
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
aggregate IP/TCP/UDP functionality to existing Nodes.
LOG_INFO and above.
Definition: log.h:103
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
a polymophic address class
Definition: address.h:90
Class for representing data rates.
Definition: data-rate.h:88
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:351
static void SinkRx(Ptr< const Packet > p, const Address &ad)
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:535
AttributeValue implementation for Time.
Definition: nstime.h:957
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:824
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:63
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
tuple csma
Definition: second.py:63
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const