A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-raw-ip-socket.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5//
6// Network topology
7// (sender) (receiver)
8// n0 n1 n2 n3
9// | | | |
10// =====================
11//
12// Node n0 sends data to node n3 over a raw IP socket. The protocol
13// number used is 2.
14// The default traffic rate is to send 1250 bytes/second for 10 kb/s
15// The data rate can be toggled by command line argument
16
17#include "ns3/applications-module.h"
18#include "ns3/core-module.h"
19#include "ns3/csma-module.h"
20#include "ns3/internet-apps-module.h"
21#include "ns3/internet-module.h"
22#include "ns3/network-module.h"
23
24#include <cassert>
25#include <fstream>
26#include <iostream>
27#include <string>
28
29using namespace ns3;
30
31NS_LOG_COMPONENT_DEFINE("CsmaRawIpSocketExample");
32
33/**
34 * Receive sink function
35 *
36 * \param p the packet.
37 * \param ad the sender's address.
38 */
39static void
41{
42 // Enable the below line to see the packet contents printed out at the
43 // receive sink
44 // std::cout << Simulator::Now().As (Time::S) << " " << *p << std::endl;
45}
46
47int
48main(int argc, char* argv[])
49{
50#if 0
51 LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO);
52#endif
53 uint32_t dataRate = 10;
54 CommandLine cmd(__FILE__);
55 cmd.AddValue("dataRate", "application dataRate (Kb/s)", dataRate);
56 cmd.Parse(argc, argv);
57
58 // Here, we will explicitly create four nodes.
59 NS_LOG_INFO("Create nodes.");
61 c.Create(4);
62
63 // connect all our nodes to a shared channel.
64 NS_LOG_INFO("Build Topology.");
66 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
67 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
68 csma.SetDeviceAttribute("EncapsulationMode", StringValue("Llc"));
69 NetDeviceContainer devs = csma.Install(c);
70
71 // add an ip stack to all nodes.
72 NS_LOG_INFO("Add ip stack.");
73 InternetStackHelper ipStack;
74 ipStack.Install(c);
75
76 // assign ip addresses
77 NS_LOG_INFO("Assign ip addresses.");
79 ip.SetBase("192.168.1.0", "255.255.255.0");
80 Ipv4InterfaceContainer addresses = ip.Assign(devs);
81
82 NS_LOG_INFO("Create Source");
83 // IP protocol configuration
84 Config::SetDefault("ns3::Ipv4RawSocketImpl::Protocol", StringValue("2"));
85 InetSocketAddress dst(addresses.GetAddress(3));
86 OnOffHelper onoff = OnOffHelper("ns3::Ipv4RawSocketFactory", dst);
87 onoff.SetConstantRate(DataRate(dataRate * 1000));
88 onoff.SetAttribute("PacketSize", UintegerValue(1250));
89
90 ApplicationContainer apps = onoff.Install(c.Get(0));
91 apps.Start(Seconds(0.5));
92 apps.Stop(Seconds(11));
93
94 NS_LOG_INFO("Create Sink.");
95 PacketSinkHelper sink = PacketSinkHelper("ns3::Ipv4RawSocketFactory", dst);
96 apps = sink.Install(c.Get(3));
97 apps.Start(Seconds(0.0));
98 apps.Stop(Seconds(12.0));
99
100 NS_LOG_INFO("Configure Tracing.");
101 // first, pcap tracing in non-promiscuous mode
102 csma.EnablePcapAll("csma-raw-ip-socket", false);
103 // then, print what the packet sink receives.
104 Config::ConnectWithoutContext("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
106
108
109 NS_LOG_INFO("Run Simulation.");
112 NS_LOG_INFO("Done.");
113
114 return 0;
115}
a polymophic address class
Definition address.h:90
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.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
Class for representing data rates.
Definition data-rate.h:78
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.
static void EnablePrinting()
Enable printing packets metadata.
Definition packet.cc:585
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
static void SinkRx(Ptr< const Packet > p, const Address &ad)
Receive sink function.
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition config.cc:943
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
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:291
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:684
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44