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/network-module.h"
59 #include "ns3/internet-apps-module.h"
60 #include "ns3/csma-module.h"
61 #include "ns3/internet-module.h"
62 #include "ns3/point-to-point-module.h"
63 #include "ns3/applications-module.h"
64 
65 using namespace ns3;
66 
67 NS_LOG_COMPONENT_DEFINE ("DhcpExample");
68 
69 int
70 main (int argc, char *argv[])
71 {
72  CommandLine cmd (__FILE__);
73 
74  bool verbose = false;
75  bool tracing = false;
76  cmd.AddValue ("verbose", "turn on the logs", verbose);
77  cmd.AddValue ("tracing", "turn on the tracing", tracing);
78 
79  cmd.Parse (argc, argv);
80 
81  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
82 
83  if (verbose)
84  {
85  LogComponentEnable ("DhcpServer", LOG_LEVEL_ALL);
86  LogComponentEnable ("DhcpClient", LOG_LEVEL_ALL);
87  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
88  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
89  }
90 
91  Time stopTime = Seconds (20);
92 
93  NS_LOG_INFO ("Create nodes.");
95  NodeContainer router;
96  nodes.Create (3);
97  router.Create (2);
98 
99  NodeContainer net (nodes, router);
100 
101  NS_LOG_INFO ("Create channels.");
103  csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
104  csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
105  csma.SetDeviceAttribute ("Mtu", UintegerValue (1500));
106  NetDeviceContainer devNet = csma.Install (net);
107 
109  p2pNodes.Add (net.Get (4));
110  p2pNodes.Create (1);
111 
113  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
114  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
115 
117  p2pDevices = pointToPoint.Install (p2pNodes);
118 
119  InternetStackHelper tcpip;
120  tcpip.Install (nodes);
121  tcpip.Install (router);
122  tcpip.Install (p2pNodes.Get (1));
123 
125  address.SetBase ("172.30.1.0", "255.255.255.0");
127  p2pInterfaces = address.Assign (p2pDevices);
128 
129  // manually add a routing entry because we don't want to add a dynamic routing
130  Ipv4StaticRoutingHelper ipv4RoutingHelper;
131  Ptr<Ipv4> ipv4Ptr = p2pNodes.Get (1)->GetObject<Ipv4> ();
132  Ptr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting (ipv4Ptr);
133  staticRoutingA->AddNetworkRouteTo (Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
134  Ipv4Address ("172.30.1.1"), 1);
135 
136  NS_LOG_INFO ("Setup the IP addresses and create DHCP applications.");
137  DhcpHelper dhcpHelper;
138 
139  // The router must have a fixed IP.
140  Ipv4InterfaceContainer fixedNodes = dhcpHelper.InstallFixedAddress (devNet.Get (4), Ipv4Address ("172.30.0.17"), Ipv4Mask ("/24"));
141  // Not really necessary, IP forwarding is enabled by default in IPv4.
142  fixedNodes.Get (0).first->SetAttribute ("IpForward", BooleanValue (true));
143 
144  // DHCP server
145  ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer (devNet.Get (3), Ipv4Address ("172.30.0.12"),
146  Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
147  Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.15"),
148  Ipv4Address ("172.30.0.17"));
149 
150  // This is just to show how it can be done.
151  DynamicCast<DhcpServer> (dhcpServerApp.Get (0))->AddStaticDhcpEntry (devNet.Get (2)->GetAddress (), Ipv4Address ("172.30.0.14"));
152 
153  dhcpServerApp.Start (Seconds (0.0));
154  dhcpServerApp.Stop (stopTime);
155 
156  // DHCP clients
157  NetDeviceContainer dhcpClientNetDevs;
158  dhcpClientNetDevs.Add (devNet.Get (0));
159  dhcpClientNetDevs.Add (devNet.Get (1));
160  dhcpClientNetDevs.Add (devNet.Get (2));
161 
162  ApplicationContainer dhcpClients = dhcpHelper.InstallDhcpClient (dhcpClientNetDevs);
163  dhcpClients.Start (Seconds (1.0));
164  dhcpClients.Stop (stopTime);
165 
167 
169  serverApps.Start (Seconds (0.0));
170  serverApps.Stop (stopTime);
171 
172  UdpEchoClientHelper echoClient (p2pInterfaces.GetAddress (1), 9);
173  echoClient.SetAttribute ("MaxPackets", UintegerValue (100));
174  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
175  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
176 
177  ApplicationContainer clientApps = echoClient.Install (nodes.Get (1));
178  clientApps.Start (Seconds (10.0));
179  clientApps.Stop (stopTime);
180 
181  Simulator::Stop (stopTime + Seconds (10.0));
182 
183  if (tracing)
184  {
185  csma.EnablePcapAll ("dhcp-csma");
186  pointToPoint.EnablePcapAll ("dhcp-p2p");
187  }
188 
189  NS_LOG_INFO ("Run Simulation.");
190  Simulator::Run ();
192  NS_LOG_INFO ("Done.");
193 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::ApplicationContainer::Get
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
Definition: application-container.cc:62
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
stopTime
Time stopTime
Definition: tcp-linux-reno.cc:52
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition: boolean.h:37
ns3::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
second.p2pInterfaces
p2pInterfaces
Definition: second.py:75
ns3::NetDevice::GetAddress
virtual Address GetAddress(void) const =0
tracing
bool tracing
Flag to enable/disable generation of tracing files.
Definition: wifi-bianchi.cc:85
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ApplicationContainer::Stop
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Definition: application-container.cc:107
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::UdpEchoClientHelper
Create an application which sends a UDP packet and waits for an echo of this packet.
Definition: udp-echo-helper.h:107
ns3::LOG_LEVEL_ALL
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition: node-container.cc:98
ns3::CsmaHelper
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
first.nodes
nodes
Definition: first.py:32
ns3::Ipv4
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
ns3::Ipv4StaticRoutingHelper
Helper class that adds ns3::Ipv4StaticRouting objects.
Definition: ipv4-static-routing-helper.h:43
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::Ipv4StaticRoutingHelper::GetStaticRouting
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...
Definition: ipv4-static-routing-helper.cc:58
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
ns3::InternetStackHelper::Install
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: internet-stack-helper.cc:366
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
second.p2pNodes
p2pNodes
Definition: second.py:50
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
verbose
bool verbose
Definition: openflow-switch.cc:50
first.address
address
Definition: first.py:44
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::DhcpHelper::InstallDhcpClient
ApplicationContainer InstallDhcpClient(Ptr< NetDevice > netDevice) const
Install DHCP client of a nodes / NetDevice.
Definition: dhcp-helper.cc:61
ns3::DhcpHelper
The helper class used to configure and install DHCP applications on nodes.
Definition: dhcp-helper.h:44
second.cmd
cmd
Definition: second.py:35
second.p2pDevices
p2pDevices
Definition: second.py:61
ns3::Ipv4InterfaceContainer::Get
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.
Definition: ipv4-interface-container.cc:90
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
first.echoClient
echoClient
Definition: first.py:56
first.clientApps
clientApps
Definition: first.py:61
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition: application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
first.serverApps
serverApps
Definition: first.py:52
first.pointToPoint
pointToPoint
Definition: first.py:35
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::PointToPointHelper
Build a set of PointToPointNetDevice objects.
Definition: point-to-point-helper.h:45
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::Ipv4Mask
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::DhcpHelper::InstallDhcpServer
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
ns3::DhcpHelper::InstallFixedAddress
Ipv4InterfaceContainer InstallFixedAddress(Ptr< NetDevice > netDevice, Ipv4Address addr, Ipv4Mask mask)
Assign a fixed IP addresses to a net device.
Definition: dhcp-helper.cc:192
ns3::UdpEchoServerHelper
Create a server application which waits for input UDP packets and sends them back to the original sen...
Definition: udp-echo-helper.h:38
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::NetDeviceContainer::Get
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Definition: net-device-container.cc:62
second.csma
csma
Definition: second.py:63
ns3::NetDeviceContainer::Add
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Definition: net-device-container.cc:67
first.echoServer
echoServer
Definition: first.py:50