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 <fstream>
26 #include "ns3/core-module.h"
27 #include "ns3/network-module.h"
28 #include "ns3/internet-apps-module.h"
29 #include "ns3/csma-module.h"
30 #include "ns3/internet-module.h"
31 #include "ns3/point-to-point-module.h"
32 #include "ns3/applications-module.h"
33 
34 using namespace ns3;
35 
36 NS_LOG_COMPONENT_DEFINE ("DhcpExample");
37 
38 int
39 main (int argc, char *argv[])
40 {
42 
43  bool verbose = false;
44  bool tracing = false;
45  cmd.AddValue ("verbose", "turn on the logs", verbose);
46  cmd.AddValue ("tracing", "turn on the tracing", tracing);
47 
48  cmd.Parse (argc, argv);
49 
50  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
51 
52  if (verbose)
53  {
54  LogComponentEnable ("DhcpServer", LOG_LEVEL_ALL);
55  LogComponentEnable ("DhcpClient", LOG_LEVEL_ALL);
56  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
57  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
58  }
59 
60  Time stopTime = Seconds (20);
61 
62  NS_LOG_INFO ("Create nodes.");
64  NodeContainer router;
65  nodes.Create (3);
66  router.Create (2);
67 
68  NodeContainer net (nodes, router);
69 
70  NS_LOG_INFO ("Create channels.");
72  csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
73  csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
74  csma.SetDeviceAttribute ("Mtu", UintegerValue (1500));
75  NetDeviceContainer devNet = csma.Install (net);
76 
78  p2pNodes.Add (net.Get (4));
79  p2pNodes.Create (1);
80 
82  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
83  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
84 
86  p2pDevices = pointToPoint.Install (p2pNodes);
87 
88  InternetStackHelper tcpip;
89  tcpip.Install (nodes);
90  tcpip.Install (router);
91  tcpip.Install (p2pNodes.Get (1));
92 
94  address.SetBase ("172.30.1.0", "255.255.255.0");
96  p2pInterfaces = address.Assign (p2pDevices);
97 
98  // manually add a routing entry because we don't want to add a dynamic routing
99  Ipv4StaticRoutingHelper ipv4RoutingHelper;
100  Ptr<Ipv4> ipv4Ptr = p2pNodes.Get (1)->GetObject<Ipv4> ();
101  Ptr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting (ipv4Ptr);
102  staticRoutingA->AddNetworkRouteTo (Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
103  Ipv4Address ("172.30.1.1"), 1);
104 
105  NS_LOG_INFO ("Setup the IP addresses and create DHCP applications.");
106  DhcpHelper dhcpHelper;
107 
108  // The router must have a fixed IP.
109  Ipv4InterfaceContainer fixedNodes = dhcpHelper.InstallFixedAddress (devNet.Get (4), Ipv4Address ("172.30.0.17"), Ipv4Mask ("/24"));
110  // Not really necessary, IP forwarding is enabled by default in IPv4.
111  fixedNodes.Get (0).first->SetAttribute ("IpForward", BooleanValue (true));
112 
113  // DHCP server
114  ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer (devNet.Get (3), Ipv4Address ("172.30.0.12"),
115  Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
116  Ipv4Address ("172.30.0.10"), Ipv4Address ("172.30.0.15"),
117  Ipv4Address ("172.30.0.17"));
118 
119  // This is just to show how it can be done.
120  DynamicCast<DhcpServer> (dhcpServerApp.Get (0))->AddStaticDhcpEntry (devNet.Get (2)->GetAddress (), Ipv4Address ("172.30.0.14"));
121 
122  dhcpServerApp.Start (Seconds (0.0));
123  dhcpServerApp.Stop (stopTime);
124 
125  // DHCP clients
126  NetDeviceContainer dhcpClientNetDevs;
127  dhcpClientNetDevs.Add (devNet.Get (0));
128  dhcpClientNetDevs.Add (devNet.Get (1));
129  dhcpClientNetDevs.Add (devNet.Get (2));
130 
131  ApplicationContainer dhcpClients = dhcpHelper.InstallDhcpClient (dhcpClientNetDevs);
132  dhcpClients.Start (Seconds (1.0));
133  dhcpClients.Stop (stopTime);
134 
136 
137  ApplicationContainer serverApps = echoServer.Install (p2pNodes.Get (1));
138  serverApps.Start (Seconds (0.0));
139  serverApps.Stop (stopTime);
140 
141  UdpEchoClientHelper echoClient (p2pInterfaces.GetAddress (1), 9);
142  echoClient.SetAttribute ("MaxPackets", UintegerValue (100));
143  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
144  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
145 
146  ApplicationContainer clientApps = echoClient.Install (nodes.Get (1));
147  clientApps.Start (Seconds (10.0));
148  clientApps.Stop (stopTime);
149 
150  Simulator::Stop (stopTime + Seconds (10.0));
151 
152  if (tracing)
153  {
154  csma.EnablePcapAll ("dhcp-csma");
155  pointToPoint.EnablePcapAll ("dhcp-p2p");
156  }
157 
158  NS_LOG_INFO ("Run Simulation.");
159  Simulator::Run ();
161  NS_LOG_INFO ("Done.");
162 }
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:113
tuple pointToPoint
Definition: first.py:28
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr and interface stored at the location specified by the index...
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:71
AttributeValue implementation for Boolean.
Definition: boolean.h:36
holds a vector of std::pair of Ptr and interface index.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Hold variables of type string.
Definition: string.h:41
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
NetDeviceContainer Install(NodeContainer c)
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
tuple echoServer
Definition: first.py:43
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:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
aggregate IP/TCP/UDP functionality to existing Nodes.
LOG_INFO and above.
Definition: log.h:103
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:217
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
Ipv4InterfaceContainer InstallFixedAddress(Ptr< NetDevice > netDevice, Ipv4Address addr, Ipv4Mask mask)
Assign a fixed IP addresses to a net device.
Definition: dhcp-helper.cc:171
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
tuple cmd
Definition: second.py:35
double stopTime
tuple clientApps
Definition: first.py:54
tuple nodes
Definition: first.py:25
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
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
AttributeValue implementation for Time.
Definition: nstime.h:1055
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Hold an unsigned integer type.
Definition: uinteger.h:44
tuple p2pInterfaces
Definition: second.py:75
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...
tuple p2pNodes
Definition: second.py:50
Parse command-line arguments.
Definition: command-line.h:205
tuple p2pDevices
Definition: second.py:61
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
tuple echoClient
Definition: first.py:49
tuple serverApps
Definition: first.py:45
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.
Ptr< Application > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
ApplicationContainer InstallDhcpClient(Ptr< NetDevice > netDevice) const
Install DHCP client of a nodes / NetDevice.
Definition: dhcp-helper.cc:60
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:65
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
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...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
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...
Helper class that adds ns3::Ipv4StaticRouting objects.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
Print everything.
Definition: log.h:112
void Parse(int argc, char *argv[])
Parse the program arguments.
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.
tuple address
Definition: first.py:37
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
bool verbose
tuple csma
Definition: second.py:63
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const