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 }
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:229
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:256
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.
virtual Address GetAddress(void) const =0
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:103
AttributeValue implementation for Time.
Definition: nstime.h:1308
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:1244
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:88