Practical 7
Practical 7
7 ROLL NO: 27
THEORY:
1. The Dynamic Host Configuration Protocol (DHCP) is a network management
protocol used on Internet Protocol (IP) networks for automatically assigning IP
addresses and other communication parameters to devices connected to the
network using a client–server architecture.
2. The technology eliminates the need for individually configuring network devices
manually, and consists of two network components, a centrally installed network
DHCP server and client instances of the protocol stack on each computer or
device.
3. When connected to the network, and periodically thereafter, a client requests a
set of parameters from the DHCP server using the DHCP protocol.
4. Working:
a. DHCP works by leasing IP addresses and IP information to network clients
for a period of time. For the lease to happen, the following negotiation
process occurs:
b. During the boot process, a client computer that is configured as a DHCP
client sends out a broadcast packet called DHCPDISCOVER. This Discover
packet contains the client’s computer name and Media Access Control
(MAC) address so the DHCP servers can respond to it.
c. DHCP servers on the network respond to the broadcast with a
DHCPOFFER. If several DHCP servers respond to the request, the client
accepts the first offer that it receives.
d. The client responds via a broadcast message called a DHCPREQUEST. If
other DHCP servers made offers, they also see their lease offers were not
accepted by the broadcast message, so they rescind their offers.
e. The DHCP server whose offer was accepted responds with a DHCPACK
message, which acknowledges the lease acceptance and contains the
ATHARVA KALE PRACTICAL NO. 7 ROLL NO: 27
CODE:
#include "ns3/core-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("DhcpExample");
Int main (int argc, char *argv[])
{
CommandLine cmd (__FILE__);
NodeContainer p2pNodes;
p2pNodes.Add (net.Get (4));
p2pNodes.Create (1);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);
ATHARVA KALE PRACTICAL NO. 7 ROLL NO: 27
InternetStackHelper tcpip;
tcpip.Install (nodes);
tcpip.Install (router);
tcpip.Install (p2pNodes.Get (1));
Ipv4AddressHelper address;
address.SetBase ("172.30.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);
// manually add a routing entry because we don't want to add a dynamic routing
Ipv4StaticRoutingHelper ipv4RoutingHelper;
Ptr<Ipv4> ipv4Ptr = p2pNodes.Get (1)->GetObject<Ipv4> ();
Ptr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting (ipv4Ptr);
staticRoutingA->AddNetworkRouteTo (Ipv4Address ("172.30.0.0"), Ipv4Mask ("/24"),
Ipv4Address ("172.30.1.1"), 1);
// DHCP server
ApplicationContainer dhcpServerApp = dhcpHelper.InstallDhcpServer (devNet.Get (3),
Ipv4Address ("172.30.0.12"),
ATHARVA KALE PRACTICAL NO. 7 ROLL NO: 27
// DHCP clients
NetDeviceContainer dhcpClientNetDevs;
dhcpClientNetDevs.Add (devNet.Get (0));
dhcpClientNetDevs.Add (devNet.Get (1));
dhcpClientNetDevs.Add (devNet.Get (2));
if (tracing)
{
csma.EnablePcapAll ("dhcp-csma");
pointToPoint.EnablePcapAll ("dhcp-p2p");
}
AnimationInterface anim("dhcp.xml");
NS_LOG_INFO ("Run Simulation.");
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");
}
ATHARVA KALE PRACTICAL NO. 7 ROLL NO: 27
OUTPUT:
o ./waf Output:
o Python Visualizer:
ATHARVA KALE PRACTICAL NO. 7 ROLL NO: 27
o NetAnim Output:
CONCLUSION:
Hence we successfully simulated DHCP server and n clients.