A Discrete-Event Network Simulator
API
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 
34 namespace ns3
35 {
36 
37 class Socket;
38 class Packet;
39 class RandomVariableStream;
40 
47 class DhcpClient : public Application
48 {
49  public:
54  static TypeId GetTypeId();
55 
56  DhcpClient();
57  ~DhcpClient() override;
58 
63  DhcpClient(Ptr<NetDevice> netDevice);
64 
70 
75  void SetDhcpClientNetDevice(Ptr<NetDevice> netDevice);
76 
82 
91  int64_t AssignStreams(int64_t stream);
92 
93  protected:
94  void DoDispose() override;
95 
96  private:
98  enum States
99  {
102  WAIT_ACK = 9
103  };
104 
105  static const int DHCP_PEER_PORT = 67;
106 
107  /*
108  * \brief Starts the DHCP client application
109  */
110  void StartApplication() override;
111 
112  /*
113  * \brief Stops the DHCP client application
114  */
115  void StopApplication() override;
116 
120  void LinkStateHandler();
121 
126  void NetHandler(Ptr<Socket> socket);
127 
131  void Boot();
132 
137  void OfferHandler(DhcpHeader header);
138 
142  void Select();
143 
147  void Request();
148 
155  void AcceptAck(DhcpHeader header, Address from);
156 
160  void RemoveAndStart();
161 
162  uint8_t m_state;
163  bool m_firstBoot;
187  bool m_offered;
188  std::list<DhcpHeader> m_offerList;
189  uint32_t m_tran;
192 };
193 
194 } // namespace ns3
195 
196 #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:173
Ipv4Address m_gateway
Address of the gateway.
Definition: dhcp-client.h:172
static TypeId GetTypeId()
Get the type ID.
Definition: dhcp-client.cc:44
Ipv4Mask m_myMask
Mask of the address assigned.
Definition: dhcp-client.h:170
Time m_rebind
Store the rebind time of address.
Definition: dhcp-client.h:182
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
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Definition: dhcp-client.cc:156
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:189
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:165
bool m_firstBoot
First boot (used to add the link state change callback)
Definition: dhcp-client.h:163
Time m_collect
Time for which client should collect offers.
Definition: dhcp-client.h:186
Address m_chaddr
chaddr of the interface (stored as an Address for convenience).
Definition: dhcp-client.h:169
States
client states
Definition: dhcp-client.h:99
@ WAIT_OFFER
State of a client that waits for the offer.
Definition: dhcp-client.h:100
@ WAIT_ACK
State of a client that waits for acknowledgment.
Definition: dhcp-client.h:102
@ REFRESH_LEASE
State of a client that needs to refresh the lease.
Definition: dhcp-client.h:101
void StopApplication() override
Application specific shutdown code.
Definition: dhcp-client.cc:217
Ptr< NetDevice > m_device
NetDevice pointer.
Definition: dhcp-client.h:164
EventId m_timeout
The timeout period.
Definition: dhcp-client.h:178
Ptr< RandomVariableStream > m_ran
Uniform random variable for transaction ID.
Definition: dhcp-client.h:184
EventId m_rebindEvent
Message rebind event.
Definition: dhcp-client.h:176
static const int DHCP_PEER_PORT
DHCP server port.
Definition: dhcp-client.h:105
std::list< DhcpHeader > m_offerList
Stores all the offers given to the client.
Definition: dhcp-client.h:188
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:171
EventId m_nextOfferEvent
Message next offer event.
Definition: dhcp-client.h:177
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:187
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:191
TracedCallback< const Ipv4Address & > m_newLease
Trace of new lease.
Definition: dhcp-client.h:190
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:162
EventId m_discoverEvent
Message retransmission event.
Definition: dhcp-client.h:174
EventId m_refreshEvent
Message refresh event.
Definition: dhcp-client.h:175
Ipv4Address m_myAddress
Address assigned to the client.
Definition: dhcp-client.h:168
Time m_rtrs
Defining the time for retransmission.
Definition: dhcp-client.h:185
~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:183
Ipv4Address m_remoteAddress
Initially set to 255.255.255.255 to start DHCP.
Definition: dhcp-client.h:166
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:167
Time m_renew
Store the renew time of address.
Definition: dhcp-client.h:181
EventId m_collectEvent
Offer collection event.
Definition: dhcp-client.h:179
Time m_lease
Store the lease time of address.
Definition: dhcp-client.h:180
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.