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 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Radu Lupu <rlupu@elcom.pub.ro>
19 * Ankit Deepak <adadeepak8@gmail.com>
20 * Deepti Rajagopal <deeptir96@gmail.com>
21 *
22 */
23
24#ifndef DHCP_CLIENT_H
25#define DHCP_CLIENT_H
26
27#include "dhcp-header.h"
28
29#include "ns3/application.h"
30#include "ns3/traced-value.h"
31
32#include <list>
33
34namespace ns3
35{
36
37class Socket;
38class Packet;
39class RandomVariableStream;
40
41/**
42 * \ingroup dhcp
43 *
44 * \class DhcpClient
45 * \brief Implements the functionality of a DHCP client
46 */
47class DhcpClient : public Application
48{
49 public:
50 /**
51 * \brief Get the type ID.
52 * \return the object TypeId
53 */
54 static TypeId GetTypeId();
55
56 DhcpClient();
57 ~DhcpClient() override;
58
59 /**
60 * \brief Constructor
61 * \param netDevice the NetDevice DHCP should work on
62 */
63 DhcpClient(Ptr<NetDevice> netDevice);
64
65 /**
66 * \brief Get the the NetDevice DHCP should work on
67 * \return the NetDevice DHCP should work on
68 */
70
71 /**
72 * \brief Set the NetDevice DHCP should work on
73 * \param netDevice the NetDevice DHCP should work on
74 */
76
77 /**
78 * \brief Get the IPv4Address of current DHCP server
79 * \return Ipv4Address of current DHCP server
80 */
82
83 int64_t AssignStreams(int64_t stream) override;
84
85 protected:
86 void DoDispose() override;
87
88 private:
89 void StartApplication() override;
90 void StopApplication() override;
91
92 /// client states
93 enum States
94 {
95 WAIT_OFFER = 1, //!< State of a client that waits for the offer
96 REFRESH_LEASE = 2, //!< State of a client that needs to refresh the lease
97 WAIT_ACK = 9 //!< State of a client that waits for acknowledgment
98 };
99
100 static const int DHCP_PEER_PORT = 67; //!< DHCP server port
101
102 /**
103 * \brief Handles changes in LinkState
104 */
105 void LinkStateHandler();
106
107 /**
108 * \brief Handles incoming packets from the network
109 * \param socket Socket bound to port 68 of the DHCP client
110 */
111 void NetHandler(Ptr<Socket> socket);
112
113 /**
114 * \brief Sends DHCP DISCOVER and changes the client state to WAIT_OFFER
115 */
116 void Boot();
117
118 /**
119 * \brief Stores DHCP offers in m_offerList
120 * \param header DhcpHeader of the DHCP OFFER message
121 */
122 void OfferHandler(DhcpHeader header);
123
124 /**
125 * \brief Selects an OFFER from m_offerList
126 */
127 void Select();
128
129 /**
130 * \brief Sends the DHCP REQUEST message and changes the client state to WAIT_ACK
131 */
132 void Request();
133
134 /**
135 * \brief Receives the DHCP ACK and configures IP address of the client.
136 * It also triggers the timeout, renew and rebind events.
137 * \param header DhcpHeader of the DHCP ACK message
138 * \param from Address of DHCP server that sent the DHCP ACK
139 */
140 void AcceptAck(DhcpHeader header, Address from);
141
142 /**
143 * \brief Remove the current DHCP information and restart the process
144 */
145 void RemoveAndStart();
146
147 uint8_t m_state; //!< State of the DHCP client
148 bool m_firstBoot; //!< First boot (used to add the link state change callback)
149 Ptr<NetDevice> m_device; //!< NetDevice pointer
150 Ptr<Socket> m_socket; //!< Socket for remote communication
151 Ipv4Address m_remoteAddress; //!< Initially set to 255.255.255.255 to start DHCP
152 Ipv4Address m_offeredAddress; //!< Address offered to the client
153 Ipv4Address m_myAddress; //!< Address assigned to the client
154 Address m_chaddr; //!< chaddr of the interface (stored as an Address for convenience).
155 Ipv4Mask m_myMask; //!< Mask of the address assigned
156 Ipv4Address m_server; //!< Address of the DHCP server
157 Ipv4Address m_gateway; //!< Address of the gateway
158 EventId m_requestEvent; //!< Address refresh event
159 EventId m_discoverEvent; //!< Message retransmission event
160 EventId m_refreshEvent; //!< Message refresh event
161 EventId m_rebindEvent; //!< Message rebind event
162 EventId m_nextOfferEvent; //!< Message next offer event
163 EventId m_timeout; //!< The timeout period
164 EventId m_collectEvent; //!< Offer collection event
165 Time m_lease; //!< Store the lease time of address
166 Time m_renew; //!< Store the renew time of address
167 Time m_rebind; //!< Store the rebind time of address
168 Time m_nextoffer; //!< Time to try the next offer (if request gets no reply)
169 Ptr<RandomVariableStream> m_ran; //!< Uniform random variable for transaction ID
170 Time m_rtrs; //!< Defining the time for retransmission
171 Time m_collect; //!< Time for which client should collect offers
172 bool m_offered; //!< Specify if the client has got any offer
173 std::list<DhcpHeader> m_offerList; //!< Stores all the offers given to the client
174 uint32_t m_tran; //!< Stores the current transaction number to be used
177};
178
179} // namespace ns3
180
181#endif /* DHCP_CLIENT_H */
a polymophic address class
Definition: address.h:101
The base class for all ns3 applications.
Definition: application.h:62
Implements the functionality of a DHCP client.
Definition: dhcp-client.h:48
EventId m_requestEvent
Address refresh event.
Definition: dhcp-client.h:158
Ipv4Address m_gateway
Address of the gateway.
Definition: dhcp-client.h:157
static TypeId GetTypeId()
Get the type ID.
Definition: dhcp-client.cc:44
Ipv4Mask m_myMask
Mask of the address assigned.
Definition: dhcp-client.h:155
Time m_rebind
Store the rebind time of address.
Definition: dhcp-client.h:167
void SetDhcpClientNetDevice(Ptr< NetDevice > netDevice)
Set the NetDevice DHCP should work on.
Definition: dhcp-client.cc:125
void LinkStateHandler()
Handles changes in LinkState.
Definition: dhcp-client.cc:247
void DoDispose() override
Destructor implementation.
Definition: dhcp-client.cc:137
uint32_t m_tran
Stores the current transaction number to be used.
Definition: dhcp-client.h:174
void RemoveAndStart()
Remove the current DHCP information and restart the process.
Definition: dhcp-client.cc:515
Ptr< Socket > m_socket
Socket for remote communication.
Definition: dhcp-client.h:150
bool m_firstBoot
First boot (used to add the link state change callback)
Definition: dhcp-client.h:148
Time m_collect
Time for which client should collect offers.
Definition: dhcp-client.h:171
Address m_chaddr
chaddr of the interface (stored as an Address for convenience).
Definition: dhcp-client.h:154
States
client states
Definition: dhcp-client.h:94
@ WAIT_OFFER
State of a client that waits for the offer.
Definition: dhcp-client.h:95
@ WAIT_ACK
State of a client that waits for acknowledgment.
Definition: dhcp-client.h:97
@ REFRESH_LEASE
State of a client that needs to refresh the lease.
Definition: dhcp-client.h:96
void StopApplication() override
Application specific shutdown code.
Definition: dhcp-client.cc:217
Ptr< NetDevice > m_device
NetDevice pointer.
Definition: dhcp-client.h:149
EventId m_timeout
The timeout period.
Definition: dhcp-client.h:163
Ptr< RandomVariableStream > m_ran
Uniform random variable for transaction ID.
Definition: dhcp-client.h:169
EventId m_rebindEvent
Message rebind event.
Definition: dhcp-client.h:161
static const int DHCP_PEER_PORT
DHCP server port.
Definition: dhcp-client.h:100
std::list< DhcpHeader > m_offerList
Stores all the offers given to the client.
Definition: dhcp-client.h:173
Ptr< NetDevice > GetDhcpClientNetDevice()
Get the the NetDevice DHCP should work on.
Definition: dhcp-client.cc:119
Ipv4Address m_server
Address of the DHCP server.
Definition: dhcp-client.h:156
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this Application object.
Definition: dhcp-client.cc:156
EventId m_nextOfferEvent
Message next offer event.
Definition: dhcp-client.h:162
void StartApplication() override
Application specific startup code.
Definition: dhcp-client.cc:164
void Boot()
Sends DHCP DISCOVER and changes the client state to WAIT_OFFER.
Definition: dhcp-client.cc:336
bool m_offered
Specify if the client has got any offer.
Definition: dhcp-client.h:172
void OfferHandler(DhcpHeader header)
Stores DHCP offers in m_offerList.
Definition: dhcp-client.cc:367
void Request()
Sends the DHCP REQUEST message and changes the client state to WAIT_ACK.
Definition: dhcp-client.cc:406
TracedCallback< const Ipv4Address & > m_expiry
Trace of lease expire.
Definition: dhcp-client.h:176
TracedCallback< const Ipv4Address & > m_newLease
Trace of new lease.
Definition: dhcp-client.h:175
Ipv4Address GetDhcpServer()
Get the IPv4Address of current DHCP server.
Definition: dhcp-client.cc:131
uint8_t m_state
State of the DHCP client.
Definition: dhcp-client.h:147
EventId m_discoverEvent
Message retransmission event.
Definition: dhcp-client.h:159
EventId m_refreshEvent
Message refresh event.
Definition: dhcp-client.h:160
Ipv4Address m_myAddress
Address assigned to the client.
Definition: dhcp-client.h:153
Time m_rtrs
Defining the time for retransmission.
Definition: dhcp-client.h:170
~DhcpClient() override
Definition: dhcp-client.cc:113
void Select()
Selects an OFFER from m_offerList.
Definition: dhcp-client.cc:381
Time m_nextoffer
Time to try the next offer (if request gets no reply)
Definition: dhcp-client.h:168
Ipv4Address m_remoteAddress
Initially set to 255.255.255.255 to start DHCP.
Definition: dhcp-client.h:151
void NetHandler(Ptr< Socket > socket)
Handles incoming packets from the network.
Definition: dhcp-client.cc:304
void AcceptAck(DhcpHeader header, Address from)
Receives the DHCP ACK and configures IP address of the client.
Definition: dhcp-client.cc:454
Ipv4Address m_offeredAddress
Address offered to the client.
Definition: dhcp-client.h:152
Time m_renew
Store the renew time of address.
Definition: dhcp-client.h:166
EventId m_collectEvent
Offer collection event.
Definition: dhcp-client.h:164
Time m_lease
Store the lease time of address.
Definition: dhcp-client.h:165
BOOTP header with DHCP messages supports the following options: Subnet Mask (1), Address Request (50)...
Definition: dhcp-header.h:85
An identifier for simulation events.
Definition: event-id.h:55
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.