A Discrete-Event Network Simulator
API
dhcp-client.h
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 #ifndef DHCP_CLIENT_H
26 #define DHCP_CLIENT_H
27 
28 #include "ns3/application.h"
29 #include "ns3/traced-value.h"
30 #include "dhcp-header.h"
31 #include <list>
32 
33 namespace ns3 {
34 
35 class Socket;
36 class Packet;
37 class RandomVariableStream;
38 
45 class DhcpClient : public Application
46 {
47 public:
52  static TypeId
53  GetTypeId (void);
54 
55  DhcpClient ();
56  virtual ~DhcpClient ();
57 
62  DhcpClient (Ptr<NetDevice> netDevice);
63 
69 
74  void SetDhcpClientNetDevice (Ptr<NetDevice> netDevice);
75 
81 
90  int64_t AssignStreams (int64_t stream);
91 
92 protected:
93  virtual void DoDispose (void);
94 
95 private:
97  enum States
98  {
99  WAIT_OFFER = 1,
101  WAIT_ACK = 9
102  };
103 
104  static const int DHCP_PEER_PORT = 67;
105 
106  /*
107  * \brief Starts the DHCP client application
108  */
109  virtual void StartApplication (void);
110 
111  /*
112  * \brief Stops the DHCP client application
113  */
114  virtual void StopApplication (void);
115 
119  void LinkStateHandler (void);
120 
125  void NetHandler (Ptr<Socket> socket);
126 
130  void Boot (void);
131 
136  void OfferHandler (DhcpHeader header);
137 
141  void Select (void);
142 
146  void Request (void);
147 
154  void AcceptAck (DhcpHeader header, Address from);
155 
159  void RemoveAndStart ();
160 
161  uint8_t m_state;
162  bool m_firstBoot;
186  bool m_offered;
187  std::list<DhcpHeader> m_offerList;
188  uint32_t m_tran;
191 };
192 
193 } // namespace ns3
194 
195 #endif /* DHCP_CLIENT_H */
a polymophic address class
Definition: address.h:91
The base class for all ns3 applications.
Definition: application.h:61
Implements the functionality of a DHCP client.
Definition: dhcp-client.h:46
EventId m_requestEvent
Address refresh event.
Definition: dhcp-client.h:172
Ipv4Address m_gateway
Address of the gateway.
Definition: dhcp-client.h:171
Ipv4Mask m_myMask
Mask of the address assigned.
Definition: dhcp-client.h:169
Time m_rebind
Store the rebind time of address.
Definition: dhcp-client.h:181
void SetDhcpClientNetDevice(Ptr< NetDevice > netDevice)
Set the NetDevice DHCP should work on.
Definition: dhcp-client.cc:118
void Select(void)
Selects an OFFER from m_offerList.
Definition: dhcp-client.cc:366
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:148
uint32_t m_tran
Stores the current transaction number to be used.
Definition: dhcp-client.h:188
void RemoveAndStart()
Remove the current DHCP information and restart the process.
Definition: dhcp-client.cc:493
virtual void StopApplication(void)
Application specific shutdown code.
Definition: dhcp-client.cc:210
Ptr< Socket > m_socket
Socket for remote communication.
Definition: dhcp-client.h:164
void LinkStateHandler(void)
Handles changes in LinkState.
Definition: dhcp-client.cc:239
bool m_firstBoot
First boot (used to add the link state change callback)
Definition: dhcp-client.h:162
Time m_collect
Time for which client should collect offers.
Definition: dhcp-client.h:185
Address m_chaddr
chaddr of the interface (stored as an Address for convenience).
Definition: dhcp-client.h:168
States
client states
Definition: dhcp-client.h:98
@ WAIT_OFFER
State of a client that waits for the offer.
Definition: dhcp-client.h:99
@ WAIT_ACK
State of a client that waits for acknowledgment.
Definition: dhcp-client.h:101
@ REFRESH_LEASE
State of a client that needs to refresh the lease.
Definition: dhcp-client.h:100
Ptr< NetDevice > m_device
NetDevice pointer.
Definition: dhcp-client.h:163
EventId m_timeout
The timeout period.
Definition: dhcp-client.h:177
virtual void DoDispose(void)
Destructor implementation.
Definition: dhcp-client.cc:129
Ptr< RandomVariableStream > m_ran
Uniform random variable for transaction ID.
Definition: dhcp-client.h:183
EventId m_rebindEvent
Message rebind event.
Definition: dhcp-client.h:175
static const int DHCP_PEER_PORT
DHCP server port.
Definition: dhcp-client.h:104
std::list< DhcpHeader > m_offerList
Stores all the offers given to the client.
Definition: dhcp-client.h:187
void Boot(void)
Sends DHCP DISCOVER and changes the client state to WAIT_OFFER.
Definition: dhcp-client.cc:325
Ipv4Address m_server
Address of the DHCP server.
Definition: dhcp-client.h:170
EventId m_nextOfferEvent
Message next offer event.
Definition: dhcp-client.h:176
bool m_offered
Specify if the client has got any offer.
Definition: dhcp-client.h:186
virtual ~DhcpClient()
Definition: dhcp-client.cc:107
void Request(void)
Sends the DHCP REQUEST message and changes the client state to WAIT_ACK.
Definition: dhcp-client.cc:390
Ipv4Address GetDhcpServer(void)
Get the IPv4Address of current DHCP server.
Definition: dhcp-client.cc:123
void OfferHandler(DhcpHeader header)
Stores DHCP offers in m_offerList.
Definition: dhcp-client.cc:353
TracedCallback< const Ipv4Address & > m_expiry
Trace of lease expire.
Definition: dhcp-client.h:190
Ptr< NetDevice > GetDhcpClientNetDevice(void)
Get the the NetDevice DHCP should work on.
Definition: dhcp-client.cc:112
TracedCallback< const Ipv4Address & > m_newLease
Trace of new lease.
Definition: dhcp-client.h:189
virtual void StartApplication(void)
Application specific startup code.
Definition: dhcp-client.cc:156
uint8_t m_state
State of the DHCP client.
Definition: dhcp-client.h:161
EventId m_discoverEvent
Message retransmission event.
Definition: dhcp-client.h:173
EventId m_refreshEvent
Message refresh event.
Definition: dhcp-client.h:174
Ipv4Address m_myAddress
Address assigned to the client.
Definition: dhcp-client.h:167
Time m_rtrs
Defining the time for retransmission.
Definition: dhcp-client.h:184
Time m_nextoffer
Time to try the next offer (if request gets no reply)
Definition: dhcp-client.h:182
Ipv4Address m_remoteAddress
Initially set to 255.255.255.255 to start DHCP.
Definition: dhcp-client.h:165
void NetHandler(Ptr< Socket > socket)
Handles incoming packets from the network.
Definition: dhcp-client.cc:294
void AcceptAck(DhcpHeader header, Address from)
Receives the DHCP ACK and configures IP address of the client.
Definition: dhcp-client.cc:435
Ipv4Address m_offeredAddress
Address offered to the client.
Definition: dhcp-client.h:166
Time m_renew
Store the renew time of address.
Definition: dhcp-client.h:180
static TypeId GetTypeId(void)
Get the type ID.
Definition: dhcp-client.cc:42
EventId m_collectEvent
Offer collection event.
Definition: dhcp-client.h:178
Time m_lease
Store the lease time of address.
Definition: dhcp-client.h:179
BOOTP header with DHCP messages supports the following options: Subnet Mask (1), Address Request (50)...
Definition: dhcp-header.h:82
An identifier for simulation events.
Definition: event-id.h:54
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
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.