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