A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dhcp-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 UPB
3 * Copyright (c) 2017 NITK Surathkal
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Author: Radu Lupu <rlupu@elcom.pub.ro>
8 * Ankit Deepak <adadeepak8@gmail.com>
9 * Deepti Rajagopal <deeptir96@gmail.com>
10 *
11 */
12
13/*
14 * Network layout:
15 *
16 * R0 is a DHCP server. The DHCP server announced R1 as the default router.
17 * Nodes N1 will send UDP Echo packets to node A.
18 *
19 *
20 * ┌-------------------------------------------------┐
21 * | DHCP Clients |
22 * | 172.30.0.14 |
23 * | DHCP static |
24 * | ┌──────┐ ┌──────┐ ┌──────┐ |
25 * | │ N0 │ │ N1 │ │ N2 │ | ┌──────┐
26 * | └──────┘ └──────┘ └──────┘ | ┌────│ A │
27 * | │ │ │ | │ └──────┘
28 * └-------│--------------│---------------│----------┘ │ 172.30.1.2
29 * DHCP Server │ │ │ │
30 * ┌──────┐ │ │ │ ┌──────┐ │
31 * │ R0 │────────┴──────────────┴───────────────┴──────│ R1 │────┘
32 * └──────┘ └──────┘172.30.1.1
33 * 172.30.0.12 172.30.0.17
34 *
35 * Things to notice:
36 * 1) The routes in A are manually set to have R1 as the default router,
37 * just because using a dynamic outing in this example is an overkill.
38 * 2) R1's address is set statically though the DHCP server helper interface.
39 * This is useful to prevent address conflicts with the dynamic pool.
40 * Not necessary if the DHCP pool is not conflicting with static addresses.
41 * 3) N2 has a dynamically-assigned, static address (i.e., a fixed address assigned via DHCP).
42 *
43 */
44
45#include "ns3/applications-module.h"
46#include "ns3/core-module.h"
47#include "ns3/csma-module.h"
48#include "ns3/internet-apps-module.h"
49#include "ns3/internet-module.h"
50#include "ns3/network-module.h"
51#include "ns3/point-to-point-module.h"
52
53using namespace ns3;
54
55NS_LOG_COMPONENT_DEFINE("DhcpExample");
56
57int
58main(int argc, char* argv[])
59{
60 CommandLine cmd(__FILE__);
61
62 bool verbose = false;
63 bool tracing = false;
64 cmd.AddValue("verbose", "turn on the logs", verbose);
65 cmd.AddValue("tracing", "turn on the tracing", tracing);
66
67 cmd.Parse(argc, argv);
68
69 // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
70
71 if (verbose)
72 {
73 LogComponentEnable("DhcpServer", LOG_LEVEL_ALL);
74 LogComponentEnable("DhcpClient", LOG_LEVEL_ALL);
75 LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
76 LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
77 }
78
79 Time stopTime = Seconds(20);
80
81 NS_LOG_INFO("Create nodes.");
83 NodeContainer router;
84 nodes.Create(3);
85 router.Create(2);
86
87 NodeContainer net(nodes, router);
88
89 NS_LOG_INFO("Create channels.");
91 csma.SetChannelAttribute("DataRate", StringValue("5Mbps"));
92 csma.SetChannelAttribute("Delay", StringValue("2ms"));
93 csma.SetDeviceAttribute("Mtu", UintegerValue(1500));
94 NetDeviceContainer devNet = csma.Install(net);
95
97 p2pNodes.Add(net.Get(4));
98 p2pNodes.Create(1);
99
101 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
102 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
103
105 p2pDevices = pointToPoint.Install(p2pNodes);
106
108 tcpip.Install(nodes);
109 tcpip.Install(router);
110 tcpip.Install(p2pNodes.Get(1));
111
113 address.SetBase("172.30.1.0", "255.255.255.0");
115 p2pInterfaces = address.Assign(p2pDevices);
116
117 // manually add a routing entry because we don't want to add a dynamic routing
118 Ipv4StaticRoutingHelper ipv4RoutingHelper;
119 Ptr<Ipv4> ipv4Ptr = p2pNodes.Get(1)->GetObject<Ipv4>();
120 Ptr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting(ipv4Ptr);
121 staticRoutingA->AddNetworkRouteTo(Ipv4Address("172.30.0.0"),
122 Ipv4Mask("/24"),
123 Ipv4Address("172.30.1.1"),
124 1);
125
126 NS_LOG_INFO("Setup the IP addresses and create DHCP applications.");
127 DhcpHelper dhcpHelper;
128
129 // The router must have a fixed IP.
130 Ipv4InterfaceContainer fixedNodes =
131 dhcpHelper.InstallFixedAddress(devNet.Get(4), Ipv4Address("172.30.0.17"), Ipv4Mask("/24"));
132 // Not really necessary, IP forwarding is enabled by default in IPv4.
133 fixedNodes.Get(0).first->SetAttribute("IpForward", BooleanValue(true));
134
135 // DHCP server
136 ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer(devNet.Get(3),
137 Ipv4Address("172.30.0.12"),
138 Ipv4Address("172.30.0.0"),
139 Ipv4Mask("/24"),
140 Ipv4Address("172.30.0.10"),
141 Ipv4Address("172.30.0.15"),
142 Ipv4Address("172.30.0.17"));
143
144 // This is just to show how it can be done.
145 DynamicCast<DhcpServer>(dhcpServerApp.Get(0))
146 ->AddStaticDhcpEntry(devNet.Get(2)->GetAddress(), Ipv4Address("172.30.0.14"));
147
148 dhcpServerApp.Start(Seconds(0.0));
149 dhcpServerApp.Stop(stopTime);
150
151 // DHCP clients
152 NetDeviceContainer dhcpClientNetDevs;
153 dhcpClientNetDevs.Add(devNet.Get(0));
154 dhcpClientNetDevs.Add(devNet.Get(1));
155 dhcpClientNetDevs.Add(devNet.Get(2));
156
157 ApplicationContainer dhcpClients = dhcpHelper.InstallDhcpClient(dhcpClientNetDevs);
158 dhcpClients.Start(Seconds(1.0));
159 dhcpClients.Stop(stopTime);
160
162
164 serverApps.Start(Seconds(0.0));
165 serverApps.Stop(stopTime);
166
168 echoClient.SetAttribute("MaxPackets", UintegerValue(100));
169 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
170 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
171
173 clientApps.Start(Seconds(10.0));
174 clientApps.Stop(stopTime);
175
177
178 if (tracing)
179 {
180 csma.EnablePcapAll("dhcp-csma");
181 pointToPoint.EnablePcapAll("dhcp-p2p");
182 }
183
184 NS_LOG_INFO("Run Simulation.");
187 NS_LOG_INFO("Done.");
188
189 return 0;
190}
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.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
The helper class used to configure and install DHCP applications on nodes.
Definition dhcp-helper.h:34
Ipv4InterfaceContainer InstallFixedAddress(Ptr< NetDevice > netDevice, Ipv4Address addr, Ipv4Mask mask)
Assign a fixed IP addresses to a net device.
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.
ApplicationContainer InstallDhcpClient(Ptr< NetDevice > netDevice) const
Install DHCP client of a nodes / NetDevice.
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.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
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
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.
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 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:34
Time stopTime
#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
NodeContainer nodes
echoClient
Definition first.py:48
address
Definition first.py:36
serverApps
Definition first.py:43
pointToPoint
Definition first.py:27
echoServer
Definition first.py:41
clientApps
Definition first.py:53
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
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
p2pNodes
Definition second.py:39
p2pInterfaces
Definition second.py:64
p2pDevices
Definition second.py:50
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.