-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapcsmaext6.cc
275 lines (225 loc) · 10 KB
/
apcsmaext6.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "ns3/core-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/netanim-module.h"
#include "ns3/olsr-helper.h"
#include "ns3/dsr-main-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/ipv4-global-routing.h"
#include "ns3/dsr-helper.h"
#include "ns3/dsdv-helper.h"
#include "ns3/rip-helper.h"
// add another two aps and a csma line
// the global routes doesn't work
// try to use static routes for AP
// add static route to host
// set different channel with same ssid
// set detailed routes for node 1 and 5
//
// Default Network Topology
//
//
// Wifi 192.168.1.0
// AP(ssid1) AP(ssid1)
// * * * *
// | | | |
// n1 n2 n3 n4
// || ||
// || ||
// || ||
// ||1 ||1
// ||9 ||9
// ||2 ||2
// ||. ||.
// ||1 c ||1 c
// ||6 s ||6 s
// ||8 m ||8 m
// ||. a ||. a
// ||2 ||2
// ||. ||.
// ||0 ||0
// || ||
// || ||
// n5 n6 n7 n8
// | | | |
// * * * *
// AP(ssid2) AP(ssid2)
// Wifi 192.168.3.0
//
//
//
using namespace ns3;
// using namespace std;
NS_LOG_COMPONENT_DEFINE ("apcsmaext6");
int main(int argc, char *argv[])
{
uint32_t nodeCount = 8;
bool tracing = true;
bool verbose = true;
std::string phymode = "HtMcs0";
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
LogComponentEnable ("apcsmaext6", LOG_LEVEL_INFO);
}
GlobalValue::Bind ("ChecksumEnabled", BooleanValue(true));
NodeContainer nc;
nc.Create(nodeCount);
NS_LOG_INFO("Divide 8 nodes into 3 network");
NodeContainer wn1nc, csmanc, wn2nc;
uint16_t i=0;
csmanc.Add(nc.Get(i));
for(i=1; i<3; i++)
wn1nc.Add(nc.Get(i));
csmanc.Add(nc.Get(i));
csmanc.Add(nc.Get(++i));
for(++i; i<7; i++)
wn2nc.Add(nc.Get(i));
csmanc.Add(nc.Get(i));
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default();
phy.SetChannel( channel.Create() );
phy.Set("ChannelNumber", UintegerValue(1));
WifiHelper wifi;
wifi.SetStandard(WIFI_PHY_STANDARD_80211n_5GHZ);
wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
"DataMode", StringValue(phymode),
"ControlMode", StringValue(phymode));
WifiMacHelper mac;
Ssid ssid1 = Ssid("ssid1");
Ssid ssid2 = Ssid("ssid2");
// Ssid ssid3 = Ssid("ssid3");
// Ssid ssid4 = Ssid("ssid4");
NS_LOG_INFO("Set wireless network1's ap1 mac info");
NetDeviceContainer wn1ndc;
mac.SetType("ns3::ApWifiMac",
"Ssid", SsidValue(ssid1));
wn1ndc.Add( wifi.Install(phy, mac, csmanc.Get(0)));
NS_LOG_INFO("Set wireless network1's station mac info");
mac.SetType("ns3::StaWifiMac",
"Ssid", SsidValue(ssid1));
wn1ndc.Add( wifi.Install(phy, mac, wn1nc.Get(0)));
phy.SetChannel( channel.Create() );
phy.Set("ChannelNumber", UintegerValue(6));
wn1ndc.Add( wifi.Install(phy, mac, wn1nc.Get(1)));
NS_LOG_INFO("Set wireless network1's ap2 mac info");
mac.SetType("ns3::ApWifiMac",
"Ssid", SsidValue(ssid1));
wn1ndc.Add( wifi.Install(phy, mac, csmanc.Get(1)));
NS_LOG_INFO("Set wireless network2's ap1 mac info");
YansWifiChannelHelper channel2 = YansWifiChannelHelper::Default();
YansWifiPhyHelper phy2 = YansWifiPhyHelper::Default();
phy2.SetChannel( channel2.Create() );
phy2.Set("ChannelNumber", UintegerValue(11));
NetDeviceContainer wn2ndc;
mac.SetType("ns3::ApWifiMac",
"Ssid", SsidValue(ssid2));
wn2ndc.Add( wifi.Install(phy2, mac, csmanc.Get(2)));
NS_LOG_INFO("Set wireless network2's station mac info");
mac.SetType("ns3::StaWifiMac",
"Ssid", SsidValue(ssid2));
wn2ndc.Add( wifi.Install(phy2, mac, wn2nc.Get(0)) );
phy2.SetChannel( channel2.Create() );
phy2.Set("ChannelNumber", UintegerValue(1));
wn2ndc.Add( wifi.Install(phy2, mac, wn2nc.Get(1)) );
NS_LOG_INFO("Set wireless network2's ap2 mac info");
mac.SetType("ns3::ApWifiMac",
"Ssid", SsidValue(ssid2));
wn2ndc.Add( wifi.Install(phy2, mac, csmanc.Get(3)));
NS_LOG_INFO("Set csma network's channel");
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
NetDeviceContainer csmandc;
csmandc = csma.Install(csmanc);
InternetStackHelper stack;
Ipv4StaticRoutingHelper staticRoutingHelper;
// OlsrHelper olsr;
// RipHelper rip;
// DsdvHelper dsdv;
// DsrHelper dsr;
// DsrMainHelper dsrMH;
stack.SetRoutingHelper(staticRoutingHelper);
stack.Install (nc);
NS_LOG_INFO("Set all network's ip address");
Ipv4AddressHelper addr;
addr.SetBase("192.168.1.0", "255.255.255.0");
Ipv4InterfaceContainer wn1intf;
wn1intf = addr.Assign(wn1ndc);
addr.SetBase("192.168.3.0", "255.255.255.0");
Ipv4InterfaceContainer wn2intf;
wn2intf = addr.Assign(wn2ndc);
addr.SetBase("192.168.2.0", "255.255.255.0");
Ipv4InterfaceContainer csmaintf;
csmaintf = addr.Assign(csmandc);
// add static route to host
Ptr<Ipv4StaticRouting> ptr_staticRouting = staticRoutingHelper.GetStaticRouting(wn1nc.Get(0)->GetObject<Ipv4>());
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.2.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.1.1"), 1, 0);
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.2.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.1.4"), 1, 1);
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.3.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.1.1"), 1, 0);
NS_LOG_INFO("Set static routing for network1's ap1 and ap2");
ptr_staticRouting = staticRoutingHelper.GetStaticRouting(csmanc.Get(0)->GetObject<Ipv4>());
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.3.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.2.3"), 2, 0);
ptr_staticRouting = staticRoutingHelper.GetStaticRouting(csmanc.Get(1)->GetObject<Ipv4>());
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.3.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.2.4"), 2, 0);
NS_LOG_INFO("Set static routing for network2's ap1 and ap2");
ptr_staticRouting = staticRoutingHelper.GetStaticRouting(csmanc.Get(2)->GetObject<Ipv4>());
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.1.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.2.1"), 2, 0);
ptr_staticRouting = staticRoutingHelper.GetStaticRouting(csmanc.Get(3)->GetObject<Ipv4>());
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.1.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.2.2"), 2, 0);
ptr_staticRouting = staticRoutingHelper.GetStaticRouting(wn2nc.Get(1)->GetObject<Ipv4>());
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.2.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.3.1"), 1, 0);
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.2.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.3.4"), 1, 1);
ptr_staticRouting->AddNetworkRouteTo(Ipv4Address("192.168.1.0"), Ipv4Mask("255.255.255.0"), Ipv4Address("192.168.3.1"), 1, 0);
for(i=0; i<nc.GetN(); i++){
Ptr<Ipv4> ptr_ipv4 = nc.Get(i)->GetObject<Ipv4>();
std::cout<<ptr_ipv4->GetAddress(1,0).GetLocal()<<std::endl;
}
std::cout<<nc.Get(0)->GetObject<Ipv4>()->GetAddress(2,0).GetLocal()<<std::endl;
std::cout<<nc.Get(3)->GetObject<Ipv4>()->GetAddress(2,0).GetLocal()<<std::endl;
std::cout<<nc.Get(4)->GetObject<Ipv4>()->GetAddress(2,0).GetLocal()<<std::endl;
std::cout<<nc.Get(7)->GetObject<Ipv4>()->GetAddress(2,0).GetLocal()<<std::endl;
NS_LOG_INFO("Set mobility model and position allocator");
MobilityHelper mobility;
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
for(i=0; i<4; i++)
positionAlloc->Add (Vector (i*50, 0.0, 0.0));
for(i=4; i<nodeCount; i++)
positionAlloc->Add (Vector ((i%4)*50, 100.0, 0.0));
mobility.SetPositionAllocator(positionAlloc);
mobility.Install(nc);
NS_LOG_INFO("Create traffic producing application ...");
UdpEchoServerHelper echoServer (9);
uint16_t serverid = 5;
uint16_t clientid = 1;
ApplicationContainer serverApps = echoServer.Install (nc.Get (serverid));
serverApps.Start (Seconds (0.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (wn2intf.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (3));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nc.Get (clientid));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Stop (Seconds(10.0));
if(tracing == true){
csma.EnablePcapAll("pcap/apcsmaext6");
phy.EnablePcapAll("pcap/apcsmaext6");
}
NS_LOG_INFO("Establish global routes");
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("route/apcsmaext6.routes", std::ios::out);
Ipv4GlobalRoutingHelper::PrintRoutingTableAllAt (Seconds (8), routingStream);
AnimationInterface anim("xml/apcsmaext6");
Simulator::Run();
Simulator::Destroy();
return 0;
}