A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
nsclick-udp-client-server-wifi.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5// Adaptation of examples/udp/udp-client-server.cc for
6// Click based nodes running wifi.
7//
8// Network topology:
9//
10// (1.4)
11// (( n4 ))
12//
13// 172.16.1.0/24
14//
15// (1.1) (1.2) (1.3)
16// n0 )) (( n1 )) (( n2
17// WLAN
18//
19// - UDP flows from n0 to n1 and n2 to n1.
20// - All nodes are Click based.
21// - The single ethernet interface that each node
22// uses is named 'eth0' in the Click file.
23// - Node 4 is running in promiscuous mode and can listen in on
24// the packets being exchanged between n0-n1 and n2-n1.
25//
26
27#include "ns3/applications-module.h"
28#include "ns3/click-internet-stack-helper.h"
29#include "ns3/core-module.h"
30#include "ns3/internet-module.h"
31#include "ns3/ipv4-click-routing.h"
32#include "ns3/mobility-module.h"
33#include "ns3/network-module.h"
34#include "ns3/wifi-module.h"
35
36#include <fstream>
37
38using namespace ns3;
39
40NS_LOG_COMPONENT_DEFINE("NsclickUdpClientServerWifi");
41
42void
44{
45 // Access the handlers
46 NS_LOG_INFO(clickRouter->ReadHandler("wifi/arpquerier", "table"));
47 NS_LOG_INFO(clickRouter->ReadHandler("wifi/arpquerier", "stats"));
48}
49
50void
52{
53 // Access the handler
55 clickRouter->WriteHandler("wifi/arpquerier", "insert", "172.16.1.2 00:00:00:00:00:02"));
56}
57
58int
59main(int argc, char* argv[])
60{
61 std::string clickConfigFolder = "src/click/examples";
62
63 CommandLine cmd(__FILE__);
64 cmd.AddValue("clickConfigFolder",
65 "Base folder for click configuration files",
66 clickConfigFolder);
67 cmd.Parse(argc, argv);
68
69 //
70 // Enable logging
71 //
72 LogComponentEnable("NsclickUdpClientServerWifi", LOG_LEVEL_INFO);
73
74 //
75 // Explicitly create the nodes required by the topology (shown above).
76 //
77 NS_LOG_INFO("Create nodes.");
79 n.Create(4);
80
81 NS_LOG_INFO("Create channels.");
82 //
83 // Explicitly create the channels required by the topology (shown above).
84 //
85 std::string phyMode("DsssRate1Mbps");
86
87 // disable fragmentation for frames below 2200 bytes
88 Config::SetDefault("ns3::WifiRemoteStationManager::FragmentationThreshold",
89 StringValue("2200"));
90 // turn off RTS/CTS for frames below 2200 bytes
91 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("2200"));
92 // Fix non-unicast data rate to be the same as that of unicast
93 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
94
96 wifi.SetStandard(WIFI_STANDARD_80211b);
97
98 YansWifiPhyHelper wifiPhy;
99 // This is one parameter that matters when using FixedRssLossModel
100 // set it to zero; otherwise, gain will be added
101 wifiPhy.Set("RxGain", DoubleValue(0));
102 // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
104
105 YansWifiChannelHelper wifiChannel;
106 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
107 // The below FixedRssLossModel will cause the rss to be fixed regardless
108 // of the distance between the two stations, and the transmit power
109 wifiChannel.AddPropagationLoss("ns3::FixedRssLossModel", "Rss", DoubleValue(-80));
110 wifiPhy.SetChannel(wifiChannel.Create());
111
112 // Add an upper mac and disable rate control
113 WifiMacHelper wifiMac;
114 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
115 "DataMode",
116 StringValue(phyMode),
117 "ControlMode",
118 StringValue(phyMode));
119 // Set it to adhoc mode
120 wifiMac.SetType("ns3::AdhocWifiMac");
121 NetDeviceContainer d = wifi.Install(wifiPhy, wifiMac, n);
122
125 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
126 positionAlloc->Add(Vector(10.0, 0.0, 0.0));
127 positionAlloc->Add(Vector(20.0, 0.0, 0.0));
128 positionAlloc->Add(Vector(0.0, 10.0, 0.0));
129 mobility.SetPositionAllocator(positionAlloc);
130 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
131 mobility.Install(n);
132
133 //
134 // Install Click on the nodes
135 //
137 clickinternet.SetClickFile(n.Get(0),
138 clickConfigFolder + "/nsclick-wifi-single-interface.click");
139 clickinternet.SetClickFile(n.Get(1),
140 clickConfigFolder + "/nsclick-wifi-single-interface.click");
141 clickinternet.SetClickFile(n.Get(2),
142 clickConfigFolder + "/nsclick-wifi-single-interface.click");
143
144 // Node 4 is to run in promiscuous mode. This can be verified
145 // from the pcap trace Node4_in_eth0.pcap generated after running
146 // this script.
147 clickinternet.SetClickFile(n.Get(3),
148 clickConfigFolder + "/nsclick-wifi-single-interface-promisc.click");
149 clickinternet.SetRoutingTableElement(n, "rt");
150 clickinternet.Install(n);
152 //
153 // We've got the "hardware" in place. Now we need to add IP addresses.
154 //
155 NS_LOG_INFO("Assign IP Addresses.");
156 ipv4.SetBase("172.16.1.0", "255.255.255.0");
157 Ipv4InterfaceContainer i = ipv4.Assign(d);
158
159 NS_LOG_INFO("Create Applications.");
160 //
161 // Create one udpServer applications on node one.
162 //
163 uint16_t port = 4000;
165 ApplicationContainer apps = server.Install(n.Get(1));
166 apps.Start(Seconds(1.0));
167 apps.Stop(Seconds(10.0));
168
169 //
170 // Create one UdpClient application to send UDP datagrams from node zero to
171 // node one.
172 //
173 uint32_t MaxPacketSize = 1024;
174 Time interPacketInterval = Seconds(0.5);
175 uint32_t maxPacketCount = 320;
177 client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
178 client.SetAttribute("Interval", TimeValue(interPacketInterval));
179 client.SetAttribute("PacketSize", UintegerValue(MaxPacketSize));
180 apps = client.Install(NodeContainer(n.Get(0), n.Get(2)));
181 apps.Start(Seconds(2.0));
182 apps.Stop(Seconds(10.0));
183
184 wifiPhy.EnablePcap("nsclick-udp-client-server-wifi", d);
185
186 // Force the MAC address of the second node: The current ARP
187 // implementation of Click sends only one ARP request per incoming
188 // packet for an unknown destination and does not retransmit if no
189 // response is received. With the scenario of this example, all ARP
190 // requests of node 3 are lost due to interference from node
191 // 1. Hence, we fill in the ARP table of node 2 before at the
192 // beginning of the simulation
196
197 //
198 // Now, do the actual simulation.
199 //
200 NS_LOG_INFO("Run Simulation.");
204 NS_LOG_INFO("Done.");
205
206 return 0;
207}
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.
aggregate Click/IP/TCP/UDP functionality to existing Nodes.
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Class to allow a node to use Click for external routing.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class used to assign positions and mobility models to nodes.
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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
Create a server application which waits for input UDP packets and uses the information carried into t...
Hold an unsigned integer type.
Definition uinteger.h:34
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
void Set(std::string name, const AttributeValue &v)
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
manage and create wifi channel objects for the YANS model.
void SetPropagationDelay(std::string name, Ts &&... args)
void AddPropagationLoss(std::string name, Ts &&... args)
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
@ WIFI_STANDARD_80211b
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
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
wifi
Definition third.py:84
mobility
Definition third.py:92
void WriteArp(Ptr< Ipv4ClickRouting > clickRouter)
void ReadArp(Ptr< Ipv4ClickRouting > clickRouter)