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