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
dhcp-example.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 UPB
3  * Copyright (c) 2017 NITK Surathkal
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Radu Lupu <rlupu@elcom.pub.ro>
19  * Ankit Deepak <adadeepak8@gmail.com>
20  * Deepti Rajagopal <deeptir96@gmail.com>
21  *
22  */
23 
24 /*
25  * Network layout:
26  *
27  * R0 is a DHCP server. The DHCP server announced R1 as the default router.
28  * Nodes N1 will send UDP Echo packets to node A.
29  *
30  *
31  * ┌-------------------------------------------------┐
32  * | DHCP Clients |
33  * | 172.30.0.14 |
34  * | DHCP static |
35  * | ┌──────┐ ┌──────┐ ┌──────┐ |
36  * | │ N0 │ │ N1 │ │ N2 │ | ┌──────┐
37  * | └──────┘ └──────┘ └──────┘ | ┌────│ A │
38  * | │ │ │ | │ └──────┘
39  * └-------│--------------│---------------│----------┘ │ 172.30.1.2
40  * DHCP Server │ │ │ │
41  * ┌──────┐ │ │ │ ┌──────┐ │
42  * │ R0 │────────┴──────────────┴───────────────┴──────│ R1 │────┘
43  * └──────┘ └──────┘172.30.1.1
44  * 172.30.0.12 172.30.0.17
45  *
46  * Things to notice:
47  * 1) The routes in A are manually set to have R1 as the default router,
48  * just because using a dynamic outing in this example is an overkill.
49  * 2) R1's address is set statically though the DHCP server helper interface.
50  * This is useful to prevent address conflicts with the dynamic pool.
51  * Not necessary if the DHCP pool is not conflicting with static addresses.
52  * 3) N2 has a dynamically-assigned, static address (i.e., a fixed address assigned via DHCP).
53  *
54  */
55 
56 #include "ns3/applications-module.h"
57 #include "ns3/core-module.h"
58 #include "ns3/csma-module.h"
59 #include "ns3/internet-apps-module.h"
60 #include "ns3/internet-module.h"
61 #include "ns3/network-module.h"
62 #include "ns3/point-to-point-module.h"
63 
64 using namespace ns3;
65 
66 NS_LOG_COMPONENT_DEFINE("DhcpExample");
67 
68 int
69 main(int argc, char* argv[])
70 {
71  CommandLine cmd(__FILE__);
72 
73  bool verbose = false;
74  bool tracing = false;
75  cmd.AddValue("verbose", "turn on the logs", verbose);
76  cmd.AddValue("tracing", "turn on the tracing", tracing);
77 
78  cmd.Parse(argc, argv);
79 
80  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
81 
82  if (verbose)
83  {
84  LogComponentEnable("DhcpServer", LOG_LEVEL_ALL);
85  LogComponentEnable("DhcpClient", LOG_LEVEL_ALL);
86  LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
87  LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
88  }
89 
90  Time stopTime = Seconds(20);
91 
92  NS_LOG_INFO("Create nodes.");
94  NodeContainer router;
95  nodes.Create(3);
96  router.Create(2);
97 
98  NodeContainer net(nodes, router);
99 
100  NS_LOG_INFO("Create channels.");
102  csma.SetChannelAttribute("DataRate", StringValue("5Mbps"));
103  csma.SetChannelAttribute("Delay", StringValue("2ms"));
104  csma.SetDeviceAttribute("Mtu", UintegerValue(1500));
105  NetDeviceContainer devNet = csma.Install(net);
106 
108  p2pNodes.Add(net.Get(4));
109  p2pNodes.Create(1);
110 
112  pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
113  pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
114 
116  p2pDevices = pointToPoint.Install(p2pNodes);
117 
118  InternetStackHelper tcpip;
119  tcpip.Install(nodes);
120  tcpip.Install(router);
121  tcpip.Install(p2pNodes.Get(1));
122 
124  address.SetBase("172.30.1.0", "255.255.255.0");
126  p2pInterfaces = address.Assign(p2pDevices);
127 
128  // manually add a routing entry because we don't want to add a dynamic routing
129  Ipv4StaticRoutingHelper ipv4RoutingHelper;
130  Ptr<Ipv4> ipv4Ptr = p2pNodes.Get(1)->GetObject<Ipv4>();
131  Ptr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting(ipv4Ptr);
132  staticRoutingA->AddNetworkRouteTo(Ipv4Address("172.30.0.0"),
133  Ipv4Mask("/24"),
134  Ipv4Address("172.30.1.1"),
135  1);
136 
137  NS_LOG_INFO("Setup the IP addresses and create DHCP applications.");
138  DhcpHelper dhcpHelper;
139 
140  // The router must have a fixed IP.
141  Ipv4InterfaceContainer fixedNodes =
142  dhcpHelper.InstallFixedAddress(devNet.Get(4), Ipv4Address("172.30.0.17"), Ipv4Mask("/24"));
143  // Not really necessary, IP forwarding is enabled by default in IPv4.
144  fixedNodes.Get(0).first->SetAttribute("IpForward", BooleanValue(true));
145 
146  // DHCP server
147  ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer(devNet.Get(3),
148  Ipv4Address("172.30.0.12"),
149  Ipv4Address("172.30.0.0"),
150  Ipv4Mask("/24"),
151  Ipv4Address("172.30.0.10"),
152  Ipv4Address("172.30.0.15"),
153  Ipv4Address("172.30.0.17"));
154 
155  // This is just to show how it can be done.
156  DynamicCast<DhcpServer>(dhcpServerApp.Get(0))
157  ->AddStaticDhcpEntry(devNet.Get(2)->GetAddress(), Ipv4Address("172.30.0.14"));
158 
159  dhcpServerApp.Start(Seconds(0.0));
160  dhcpServerApp.Stop(stopTime);
161 
162  // DHCP clients
163  NetDeviceContainer dhcpClientNetDevs;
164  dhcpClientNetDevs.Add(devNet.Get(0));
165  dhcpClientNetDevs.Add(devNet.Get(1));
166  dhcpClientNetDevs.Add(devNet.Get(2));
167 
168  ApplicationContainer dhcpClients = dhcpHelper.InstallDhcpClient(dhcpClientNetDevs);
169  dhcpClients.Start(Seconds(1.0));
170  dhcpClients.Stop(stopTime);
171 
173 
175  serverApps.Start(Seconds(0.0));
176  serverApps.Stop(stopTime);
177 
178  UdpEchoClientHelper echoClient(p2pInterfaces.GetAddress(1), 9);
179  echoClient.SetAttribute("MaxPackets", UintegerValue(100));
180  echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
181  echoClient.SetAttribute("PacketSize", UintegerValue(1024));
182 
184  clientApps.Start(Seconds(10.0));
185  clientApps.Stop(stopTime);
186 
188 
189  if (tracing)
190  {
191  csma.EnablePcapAll("dhcp-csma");
192  pointToPoint.EnablePcapAll("dhcp-p2p");
193  }
194 
195  NS_LOG_INFO("Run Simulation.");
196  Simulator::Run();
198  NS_LOG_INFO("Done.");
199 
200  return 0;
201 }
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.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
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
The helper class used to configure and install DHCP applications on nodes.
Definition: dhcp-helper.h:45
Ipv4InterfaceContainer InstallFixedAddress(Ptr< NetDevice > netDevice, Ipv4Address addr, Ipv4Mask mask)
Assign a fixed IP addresses to a net device.
Definition: dhcp-helper.cc:203
ApplicationContainer InstallDhcpServer(Ptr< NetDevice > netDevice, Ipv4Address serverAddr, Ipv4Address poolAddr, Ipv4Mask poolMask, Ipv4Address minAddr, Ipv4Address maxAddr, Ipv4Address gateway=Ipv4Address())
Install DHCP server of a node / NetDevice.
Definition: dhcp-helper.cc:129
ApplicationContainer InstallDhcpClient(Ptr< NetDevice > netDevice) const
Install DHCP client of a nodes / NetDevice.
Definition: dhcp-helper.cc:61
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.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
holds a vector of std::pair of Ptr<Ipv4> and interface index.
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr<Ipv4> and interface stored at the location specified by the index.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
Helper class that adds ns3::Ipv4StaticRouting objects.
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Hold an unsigned integer type.
Definition: uinteger.h:45
Time stopTime
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
NodeContainer nodes
echoClient
Definition: first.py:59
address
Definition: first.py:47
serverApps
Definition: first.py:54
pointToPoint
Definition: first.py:38
echoServer
Definition: first.py:52
clientApps
Definition: first.py:64
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
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
p2pNodes
Definition: second.py:50
p2pInterfaces
Definition: second.py:75
csma
Definition: second.py:63
p2pDevices
Definition: second.py:61
cmd
Definition: second.py:40
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.