A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dhcp-client.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 UPB
3 * Copyright (c) 2017 NITK Surathkal
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Author: Radu Lupu <rlupu@elcom.pub.ro>
8 * Ankit Deepak <adadeepak8@gmail.com>
9 * Deepti Rajagopal <deeptir96@gmail.com>
10 *
11 */
12
13#ifndef DHCP_CLIENT_H
14#define DHCP_CLIENT_H
15
16#include "dhcp-header.h"
17
18#include "ns3/application.h"
19#include "ns3/traced-value.h"
20
21#include <list>
22
23namespace ns3
24{
25
26class Socket;
27class Packet;
28class RandomVariableStream;
29
30/**
31 * \ingroup dhcp
32 *
33 * \class DhcpClient
34 * \brief Implements the functionality of a DHCP client
35 */
36class DhcpClient : public Application
37{
38 public:
39 /**
40 * \brief Get the type ID.
41 * \return the object TypeId
42 */
43 static TypeId GetTypeId();
44
45 DhcpClient();
46 ~DhcpClient() override;
47
48 /**
49 * \brief Constructor
50 * \param netDevice the NetDevice DHCP should work on
51 */
52 DhcpClient(Ptr<NetDevice> netDevice);
53
54 /**
55 * \brief Get the the NetDevice DHCP should work on
56 * \return the NetDevice DHCP should work on
57 */
59
60 /**
61 * \brief Set the NetDevice DHCP should work on
62 * \param netDevice the NetDevice DHCP should work on
63 */
65
66 /**
67 * \brief Get the IPv4Address of current DHCP server
68 * \return Ipv4Address of current DHCP server
69 */
71
72 int64_t AssignStreams(int64_t stream) override;
73
74 protected:
75 void DoDispose() override;
76
77 private:
78 void StartApplication() override;
79 void StopApplication() override;
80
81 /// client states
82 enum States
83 {
84 WAIT_OFFER = 1, //!< State of a client that waits for the offer
85 REFRESH_LEASE = 2, //!< State of a client that needs to refresh the lease
86 WAIT_ACK = 9 //!< State of a client that waits for acknowledgment
87 };
88
89 static const int DHCP_PEER_PORT = 67; //!< DHCP server port
90
91 /**
92 * \brief Handles changes in LinkState
93 */
94 void LinkStateHandler();
95
96 /**
97 * \brief Handles incoming packets from the network
98 * \param socket Socket bound to port 68 of the DHCP client
99 */
100 void NetHandler(Ptr<Socket> socket);
101
102 /**
103 * \brief Sends DHCP DISCOVER and changes the client state to WAIT_OFFER
104 */
105 void Boot();
106
107 /**
108 * \brief Stores DHCP offers in m_offerList
109 * \param header DhcpHeader of the DHCP OFFER message
110 */
111 void OfferHandler(DhcpHeader header);
112
113 /**
114 * \brief Selects an OFFER from m_offerList
115 */
116 void Select();
117
118 /**
119 * \brief Sends the DHCP REQUEST message and changes the client state to WAIT_ACK
120 */
121 void Request();
122
123 /**
124 * \brief Receives the DHCP ACK and configures IP address of the client.
125 * It also triggers the timeout, renew and rebind events.
126 * \param header DhcpHeader of the DHCP ACK message
127 * \param from Address of DHCP server that sent the DHCP ACK
128 */
129 void AcceptAck(DhcpHeader header, Address from);
130
131 /**
132 * \brief Remove the current DHCP information and restart the process
133 */
134 void RemoveAndStart();
135
136 uint8_t m_state; //!< State of the DHCP client
137 bool m_firstBoot; //!< First boot (used to add the link state change callback)
138 Ptr<NetDevice> m_device; //!< NetDevice pointer
139 Ptr<Socket> m_socket; //!< Socket for remote communication
140 Ipv4Address m_remoteAddress; //!< Initially set to 255.255.255.255 to start DHCP
141 Ipv4Address m_offeredAddress; //!< Address offered to the client
142 Ipv4Address m_myAddress; //!< Address assigned to the client
143 Address m_chaddr; //!< chaddr of the interface (stored as an Address for convenience).
144 Ipv4Mask m_myMask; //!< Mask of the address assigned
145 Ipv4Address m_server; //!< Address of the DHCP server
146 Ipv4Address m_gateway; //!< Address of the gateway
147 EventId m_requestEvent; //!< Address refresh event
148 EventId m_discoverEvent; //!< Message retransmission event
149 EventId m_refreshEvent; //!< Message refresh event
150 EventId m_rebindEvent; //!< Message rebind event
151 EventId m_nextOfferEvent; //!< Message next offer event
152 EventId m_timeout; //!< The timeout period
153 EventId m_collectEvent; //!< Offer collection event
154 Time m_lease; //!< Store the lease time of address
155 Time m_renew; //!< Store the renew time of address
156 Time m_rebind; //!< Store the rebind time of address
157 Time m_nextoffer; //!< Time to try the next offer (if request gets no reply)
158 Ptr<RandomVariableStream> m_ran; //!< Uniform random variable for transaction ID
159 Time m_rtrs; //!< Defining the time for retransmission
160 Time m_collect; //!< Time for which client should collect offers
161 bool m_offered; //!< Specify if the client has got any offer
162 std::list<DhcpHeader> m_offerList; //!< Stores all the offers given to the client
163 uint32_t m_tran; //!< Stores the current transaction number to be used
166};
167
168} // namespace ns3
169
170#endif /* DHCP_CLIENT_H */
a polymophic address class
Definition address.h:90
The base class for all ns3 applications.
Definition application.h:51
Implements the functionality of a DHCP client.
Definition dhcp-client.h:37
EventId m_requestEvent
Address refresh event.
Ipv4Address m_gateway
Address of the gateway.
static TypeId GetTypeId()
Get the type ID.
Ipv4Mask m_myMask
Mask of the address assigned.
Time m_rebind
Store the rebind time of address.
void SetDhcpClientNetDevice(Ptr< NetDevice > netDevice)
Set the NetDevice DHCP should work on.
void LinkStateHandler()
Handles changes in LinkState.
void DoDispose() override
Destructor implementation.
uint32_t m_tran
Stores the current transaction number to be used.
void RemoveAndStart()
Remove the current DHCP information and restart the process.
Ptr< Socket > m_socket
Socket for remote communication.
bool m_firstBoot
First boot (used to add the link state change callback)
Time m_collect
Time for which client should collect offers.
Address m_chaddr
chaddr of the interface (stored as an Address for convenience).
States
client states
Definition dhcp-client.h:83
@ WAIT_OFFER
State of a client that waits for the offer.
Definition dhcp-client.h:84
@ WAIT_ACK
State of a client that waits for acknowledgment.
Definition dhcp-client.h:86
@ REFRESH_LEASE
State of a client that needs to refresh the lease.
Definition dhcp-client.h:85
void StopApplication() override
Application specific shutdown code.
Ptr< NetDevice > m_device
NetDevice pointer.
EventId m_timeout
The timeout period.
Ptr< RandomVariableStream > m_ran
Uniform random variable for transaction ID.
EventId m_rebindEvent
Message rebind event.
static const int DHCP_PEER_PORT
DHCP server port.
Definition dhcp-client.h:89
std::list< DhcpHeader > m_offerList
Stores all the offers given to the client.
Ptr< NetDevice > GetDhcpClientNetDevice()
Get the the NetDevice DHCP should work on.
Ipv4Address m_server
Address of the DHCP server.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this Application object.
EventId m_nextOfferEvent
Message next offer event.
void StartApplication() override
Application specific startup code.
void Boot()
Sends DHCP DISCOVER and changes the client state to WAIT_OFFER.
bool m_offered
Specify if the client has got any offer.
void OfferHandler(DhcpHeader header)
Stores DHCP offers in m_offerList.
void Request()
Sends the DHCP REQUEST message and changes the client state to WAIT_ACK.
TracedCallback< const Ipv4Address & > m_expiry
Trace of lease expire.
TracedCallback< const Ipv4Address & > m_newLease
Trace of new lease.
Ipv4Address GetDhcpServer()
Get the IPv4Address of current DHCP server.
uint8_t m_state
State of the DHCP client.
EventId m_discoverEvent
Message retransmission event.
EventId m_refreshEvent
Message refresh event.
Ipv4Address m_myAddress
Address assigned to the client.
Time m_rtrs
Defining the time for retransmission.
~DhcpClient() override
void Select()
Selects an OFFER from m_offerList.
Time m_nextoffer
Time to try the next offer (if request gets no reply)
Ipv4Address m_remoteAddress
Initially set to 255.255.255.255 to start DHCP.
void NetHandler(Ptr< Socket > socket)
Handles incoming packets from the network.
void AcceptAck(DhcpHeader header, Address from)
Receives the DHCP ACK and configures IP address of the client.
Ipv4Address m_offeredAddress
Address offered to the client.
Time m_renew
Store the renew time of address.
EventId m_collectEvent
Offer collection event.
Time m_lease
Store the lease time of address.
BOOTP header with DHCP messages.
Definition dhcp-header.h:73
An identifier for simulation events.
Definition event-id.h:45
Ipv4 addresses are stored in host order in this class.
a class to represent an Ipv4 address mask
Smart pointer class similar to boost::intrusive_ptr.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition type-id.h:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.