A Discrete-Event Network Simulator
API
dhcp-example.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 UPB
4  * Copyright (c) 2017 NITK Surathkal
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Radu Lupu <rlupu@elcom.pub.ro>
20  * Ankit Deepak <adadeepak8@gmail.com>
21  * Deepti Rajagopal <deeptir96@gmail.com>
22  *
23  */
24 
25 /*
26  * Network layout:
27  *
28  * R0 is a DHCP server. The DHCP server announced R1 as the default router.
29  * Nodes N1 will send UDP Echo packets to node A.
30  *
31  *
32  * ┌-------------------------------------------------┐
33  * | DHCP Clients |
34  * | 172.30.0.14 |
35  * | DHCP static |
36  * | ┌──────┐ ┌──────┐ ┌──────┐ |
37  * | │ N0 │ │ N1 │ │ N2 │ | ┌──────┐
38  * | └──────┘ └──────┘ └──────┘ | ┌────│ A │
39  * | │ │ │ | │ └──────┘
40  * └-------│--------------│---------------│----------┘ │ 172.30.1.2
41  * DHCP Server │ │ │ │
42  * ┌──────┐ │ │ │ ┌──────┐ │
43  * │ R0 │────────┴──────────────┴───────────────┴──────│ R1 │────┘
44  * └──────┘ └──────┘172.30.1.1
45  * 172.30.0.12 172.30.0.17
46  *
47  * Things to notice:
48  * 1) The routes in A are manually set to have R1 as the default router,
49  * just because using a dynamic outing in this example is an overkill.
50  * 2) R1's address is set statically though the DHCP server helper interface.
51  * This is useful to prevent address conflicts with the dynamic pool.
52  * Not necessary if the DHCP pool is not conflicting with static addresses.
53  * 3) N2 has a dynamically-assigned, static address (i.e., a fixed address assigned via DHCP).
54  *
55  */
56 
57 #include "ns3/core-module.h"
58 #include "ns3/internet-apps-module.h"
59 #include "ns3/csma-module.h"
60 #include "ns3/internet-module.h"
61 #include "ns3/point-to-point-module.h"
62 #include "ns3/applications-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"), Ipv4Mask ("/24"),
133  Ipv4Address ("172.30.1.1"), 1);
134 
135  NS_LOG_INFO ("Setup the IP addresses and create DHCP applications.");
136  DhcpHelper dhcpHelper;
137 
138  // The router must have a fixed IP.
139  Ipv4InterfaceContainer fixedNodes = dhcpHelper.InstallFixedAddress (devNet.Get (4), Ipv4Address ("172.30.0.17"), Ipv4Mask ("/24"));
140  // Not really necessary, IP forwarding is enabled by default in IPv4.
141  fixedNodes.Get (0).first->SetAttribute ("IpForward", BooleanValue (true));
142 
143  // DHCP server
144  ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer (devNet.Get (3), Ipv4Address ("172.30.0.12"),
145  Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
146  Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.15"),
147  Ipv4Address ("172.30.0.17"));
148 
149  // This is just to show how it can be done.
150  DynamicCast<DhcpServer> (dhcpServerApp.Get (0))->AddStaticDhcpEntry (devNet.Get (2)->GetAddress (), Ipv4Address ("172.30.0.14"));
151 
152  dhcpServerApp.Start (Seconds (0.0));
153  dhcpServerApp.Stop (stopTime);
154 
155  // DHCP clients
156  NetDeviceContainer dhcpClientNetDevs;
157  dhcpClientNetDevs.Add (devNet.Get (0));
158  dhcpClientNetDevs.Add (devNet.Get (1));
159  dhcpClientNetDevs.Add (devNet.Get (2));
160 
161  ApplicationContainer dhcpClients = dhcpHelper.InstallDhcpClient (dhcpClientNetDevs);
162  dhcpClients.Start (Seconds (1.0));
163  dhcpClients.Stop (stopTime);
164 
166 
168  serverApps.Start (Seconds (0.0));
169  serverApps.Stop (stopTime);
170 
171  UdpEchoClientHelper echoClient (p2pInterfaces.GetAddress (1), 9);
172  echoClient.SetAttribute ("MaxPackets", UintegerValue (100));
173  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
174  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
175 
176  ApplicationContainer clientApps = echoClient.Install (nodes.Get (1));
177  clientApps.Start (Seconds (10.0));
178  clientApps.Stop (stopTime);
179 
180  Simulator::Stop (stopTime + Seconds (10.0));
181 
182  if (tracing)
183  {
184  csma.EnablePcapAll ("dhcp-csma");
185  pointToPoint.EnablePcapAll ("dhcp-p2p");
186  }
187 
188  NS_LOG_INFO ("Run Simulation.");
189  Simulator::Run ();
191  NS_LOG_INFO ("Done.");
192 }
holds a vector of ns3::Application pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:228
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
The helper class used to configure and install DHCP applications on nodes.
Definition: dhcp-helper.h:44
Ipv4InterfaceContainer InstallFixedAddress(Ptr< NetDevice > netDevice, Ipv4Address addr, Ipv4Mask mask)
Assign a fixed IP addresses to a net device.
Definition: dhcp-helper.cc:192
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:124
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:41
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
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:269
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.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Hold variables of type string.
Definition: string.h:41
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
AttributeValue implementation for Time.
Definition: nstime.h:1353
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:44
Time stopTime
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
echoClient
Definition: first.py:56
address
Definition: first.py:44
serverApps
Definition: first.py:52
pointToPoint
Definition: first.py:35
echoServer
Definition: first.py:50
clientApps
Definition: first.py:61
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
p2pNodes
Definition: second.py:50
p2pInterfaces
Definition: second.py:75
csma
Definition: second.py:63
p2pDevices
Definition: second.py:61
cmd
Definition: second.py:35
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.
Definition: wifi-bianchi.cc:85