A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dhcp6-client.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 NITK Surathkal
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Kavya Bhat <kavyabhat@gmail.com>
7 *
8 */
9
10#ifndef DHCP6_CLIENT_H
11#define DHCP6_CLIENT_H
12
13#include "dhcp6-duid.h"
14#include "dhcp6-header.h"
15
16#include "ns3/application.h"
17#include "ns3/inet6-socket-address.h"
18#include "ns3/net-device-container.h"
19#include "ns3/random-variable-stream.h"
20#include "ns3/socket.h"
21#include "ns3/traced-callback.h"
22#include "ns3/trickle-timer.h"
23
24#include <optional>
25
26namespace ns3
27{
28
29/**
30 * @ingroup dhcp6
31 *
32 * @class Dhcp6Client
33 * @brief Implements the DHCPv6 client.
34 */
36{
37 public:
38 /**
39 * @brief Get the type ID.
40 * @return the object TypeId
41 */
42 static TypeId GetTypeId();
43
45
46 /**
47 * @brief Get the DUID.
48 * @return The DUID which identifies the client.
49 */
50 Duid GetSelfDuid() const;
51
52 /// State of the DHCPv6 client.
53 enum class State
54 {
55 WAIT_ADVERTISE, // Waiting for an advertise message
56 WAIT_REPLY, // Waiting for a reply message
57 RENEW, // Renewing the lease
58 WAIT_REPLY_AFTER_DECLINE, // Waiting for a reply after sending a decline message
59 WAIT_REPLY_AFTER_RELEASE, // Waiting for a reply after sending a release message
60 };
61
62 int64_t AssignStreams(int64_t stream) override;
63
64 // protected:
65 void DoDispose() override;
66
67 // private:
68 void StartApplication() override;
69 void StopApplication() override;
70
71 private:
72 /**
73 * @ingroup dhcp6
74 *
75 * @class InterfaceConfig
76 * @brief DHCPv6 client config details about each interface on the node.
77 */
78 class InterfaceConfig : public SimpleRefCount<InterfaceConfig>
79 {
80 public:
81 /**
82 * The default constructor.
83 */
85
86 /**
87 * Destructor.
88 */
90
91 /// The Dhcp6Client on which the interface is present.
93
94 /// The IPv6 interface index of this configuration.
96
97 /// The socket that has been opened for this interface.
99
100 /// The DHCPv6 state of the client interface.
102
103 /// The server DUID.
105
106 /// The IAIDs associated with this DHCPv6 client interface.
107 std::vector<uint32_t> m_iaids;
108
109 TrickleTimer m_solicitTimer; //!< TrickleTimer to schedule Solicit messages.
110 Time m_msgStartTime; //!< Time when message exchange starts.
111 uint32_t m_transactId; //!< Transaction ID of the client-initiated message.
112 uint8_t m_nAcceptedAddresses; //!< Number of addresses accepted by client.
113
114 /// List of addresses offered to the client.
115 std::vector<Ipv6Address> m_offeredAddresses;
116
117 /// List of addresses to be declined by the client.
118 std::vector<Ipv6Address> m_declinedAddresses;
119
120 Time m_renewTime; //!< REN_MAX_RT, Time after which lease should be renewed.
121 Time m_rebindTime; //!< REB_MAX_RT, Time after which client should send a Rebind message.
122 Time m_prefLifetime; //!< Preferred lifetime of the address.
123 Time m_validLifetime; //!< Valid lifetime of the address.
124 EventId m_renewEvent; //!< Event ID for the Renew event.
125 EventId m_rebindEvent; //!< Event ID for the rebind event
126
127 /// Store all the Event IDs for the addresses being Released.
128 std::vector<EventId> m_releaseEvent;
129
130 /**
131 * @brief Accept the DHCPv6 offer.
132 * @param offeredAddress The IPv6 address that has been accepted.
133 */
134 void AcceptedAddress(const Ipv6Address& offeredAddress);
135
136 /// Callback for the accepted addresses - needed for cleanup.
137 std::optional<Callback<void, const Ipv6Address&>> m_acceptedAddressCb{std::nullopt};
138
139 /**
140 * @brief Add a declined address to the list maintained by the client.
141 * @param offeredAddress The IPv6 address to be declined.
142 */
143 void DeclinedAddress(const Ipv6Address& offeredAddress);
144
145 /// Callback for the declined addresses - needed for cleanup.
146 std::optional<Callback<void, const Ipv6Address&>> m_declinedAddressCb{std::nullopt};
147
148 /**
149 * @brief Send a Decline message to the DHCPv6 server
150 */
151 void DeclineOffer();
152
153 /**
154 * @brief Cleanup the internal callbacks and timers.
155 *
156 * MUST be used before removing an interface to avoid circular references locks.
157 */
158 void Cleanup();
159 };
160
161 /**
162 * @brief Verify the incoming advertise message.
163 * @param header The DHCPv6 header received.
164 * @param iDev The incoming NetDevice.
165 * @return True if the Advertise is valid.
166 */
168
169 /**
170 * @brief Send a request to the DHCPv6 server.
171 * @param iDev The outgoing NetDevice
172 * @param header The DHCPv6 header
173 * @param server The address of the server
174 */
175 void SendRequest(Ptr<NetDevice> iDev, Dhcp6Header header, Inet6SocketAddress server);
176
177 /**
178 * @brief Send a request to the DHCPv6 server.
179 * @param iDev The outgoing NetDevice
180 * @param header The DHCPv6 header
181 * @param server The address of the server
182 */
184
185 /**
186 * @brief Check lease status after sending a Decline or Release message.
187 * @param iDev The incoming NetDevice
188 * @param header The DHCPv6 header
189 * @param server The address of the server
190 */
191 void CheckLeaseStatus(Ptr<NetDevice> iDev, Dhcp6Header header, Inet6SocketAddress server) const;
192
193 /**
194 * @brief Send a renew message to the DHCPv6 server.
195 * @param interfaceIndex The interface whose leases are to be renewed.
196 */
197 void SendRenew(uint32_t interfaceIndex);
198
199 /**
200 * @brief Send a rebind message to the DHCPv6 server.
201 * @param interfaceIndex The interface whose leases are to be rebound.
202 */
203 void SendRebind(uint32_t interfaceIndex);
204
205 /**
206 * @brief Send a Release message to the DHCPv6 server.
207 * @param address The address whose lease is to be released.
208 */
209 void SendRelease(Ipv6Address address);
210
211 /**
212 * @brief Handles incoming packets from the network
213 * @param socket incoming Socket
214 */
215 void NetHandler(Ptr<Socket> socket);
216
217 /**
218 * @brief Handle changes in the link state.
219 * @param isUp Indicates whether the interface is up.
220 * @param ifIndex The interface index.
221 */
222 void LinkStateHandler(bool isUp, int32_t ifIndex);
223
224 /**
225 * @brief Callback for when an M flag is received.
226 *
227 * The M flag is carried by Router Advertisements (RA), and it is a signal
228 * that the DHCPv6 client must search for a DHCPv6 Server.
229 *
230 * @param recvInterface The interface on which the M flag was received.
231 */
232 void ReceiveMflag(uint32_t recvInterface);
233
234 /**
235 * @brief Used to send the Solicit message and start the DHCPv6 client.
236 *
237 * This function also builds the client's DUID on first invocation.
238 *
239 * @param device The client interface.
240 */
241 void Boot(Ptr<NetDevice> device);
242
243 /**
244 * @brief Retrieve all existing IAIDs.
245 * @return A list of all IAIDs.
246 */
247 std::vector<uint32_t> GetIaids();
248
249 /// Map each interface to its corresponding configuration details.
250 std::unordered_map<uint32_t, Ptr<InterfaceConfig>> m_interfaces;
251
253
254 /// Track the IPv6 Address - IAID association.
255 std::unordered_map<Ipv6Address, uint32_t> m_iaidMap;
256
257 /// Random variable to set transaction ID
259
260 Time m_solicitInterval; //!< SOL_MAX_RT, time between solicitations.
261
262 /**
263 * Random jitter before sending the first Solicit. Equivalent to
264 * SOL_MAX_DELAY (RFC 8415, Section 7.6)
265 */
267
268 /// Random variable used to create the IAID.
270
271 Duid::Type m_duidType; //!< DUID type.
272 uint16_t m_DuidEnIdentifierLength; //!< DUID-EN identifier length.
273
274 /**
275 * The client DUID, built on first usage in the Boot() function.
276 * We avoid to use `StartApplication()` to randomize the time
277 * of DUID-LLT.
278 */
280};
281
282/**
283 * @brief Stream output operator
284 * @param os output stream
285 * @param h the Dhcp6Client
286 * @return updated stream
287 */
288std::ostream& operator<<(std::ostream& os, const Dhcp6Client& h);
289
290} // namespace ns3
291
292#endif
State m_state
The DHCPv6 state of the client interface.
void DeclineOffer()
Send a Decline message to the DHCPv6 server.
Ptr< Dhcp6Client > m_client
The Dhcp6Client on which the interface is present.
Time m_msgStartTime
Time when message exchange starts.
uint8_t m_nAcceptedAddresses
Number of addresses accepted by client.
Time m_prefLifetime
Preferred lifetime of the address.
std::vector< Ipv6Address > m_declinedAddresses
List of addresses to be declined by the client.
std::optional< Callback< void, const Ipv6Address & > > m_declinedAddressCb
Callback for the declined addresses - needed for cleanup.
Time m_rebindTime
REB_MAX_RT, Time after which client should send a Rebind message.
Duid m_serverDuid
The server DUID.
Ptr< Socket > m_socket
The socket that has been opened for this interface.
void AcceptedAddress(const Ipv6Address &offeredAddress)
Accept the DHCPv6 offer.
std::vector< Ipv6Address > m_offeredAddresses
List of addresses offered to the client.
std::vector< uint32_t > m_iaids
The IAIDs associated with this DHCPv6 client interface.
EventId m_rebindEvent
Event ID for the rebind event.
uint32_t m_transactId
Transaction ID of the client-initiated message.
EventId m_renewEvent
Event ID for the Renew event.
InterfaceConfig()
The default constructor.
std::optional< Callback< void, const Ipv6Address & > > m_acceptedAddressCb
Callback for the accepted addresses - needed for cleanup.
void DeclinedAddress(const Ipv6Address &offeredAddress)
Add a declined address to the list maintained by the client.
TrickleTimer m_solicitTimer
TrickleTimer to schedule Solicit messages.
void Cleanup()
Cleanup the internal callbacks and timers.
Time m_renewTime
REN_MAX_RT, Time after which lease should be renewed.
std::vector< EventId > m_releaseEvent
Store all the Event IDs for the addresses being Released.
uint32_t m_interfaceIndex
The IPv6 interface index of this configuration.
Time m_validLifetime
Valid lifetime of the address.
Implements the DHCPv6 client.
void CheckLeaseStatus(Ptr< NetDevice > iDev, Dhcp6Header header, Inet6SocketAddress server) const
Check lease status after sending a Decline or Release message.
std::vector< uint32_t > GetIaids()
Retrieve all existing IAIDs.
Duid GetSelfDuid() const
Get the DUID.
std::unordered_map< uint32_t, Ptr< InterfaceConfig > > m_interfaces
Map each interface to its corresponding configuration details.
void ReceiveMflag(uint32_t recvInterface)
Callback for when an M flag is received.
void LinkStateHandler(bool isUp, int32_t ifIndex)
Handle changes in the link state.
std::unordered_map< Ipv6Address, uint32_t > m_iaidMap
Track the IPv6 Address - IAID association.
Ptr< RandomVariableStream > m_solicitJitter
Random jitter before sending the first Solicit.
void SendRelease(Ipv6Address address)
Send a Release message to the DHCPv6 server.
static TypeId GetTypeId()
Get the type ID.
TracedCallback< const Ipv6Address & > m_newLease
Trace the new lease.
bool ValidateAdvertise(Dhcp6Header header, Ptr< NetDevice > iDev)
Verify the incoming advertise message.
void Boot(Ptr< NetDevice > device)
Used to send the Solicit message and start the DHCPv6 client.
State
State of the DHCPv6 client.
Duid::Type m_duidType
DUID type.
void ProcessReply(Ptr< NetDevice > iDev, Dhcp6Header header, Inet6SocketAddress server)
Send a request to the DHCPv6 server.
Ptr< RandomVariableStream > m_transactionId
Random variable to set transaction ID.
void SendRequest(Ptr< NetDevice > iDev, Dhcp6Header header, Inet6SocketAddress server)
Send a request to the DHCPv6 server.
uint16_t m_DuidEnIdentifierLength
DUID-EN identifier length.
void StartApplication() override
Application specific startup code.
void StopApplication() override
Application specific shutdown code.
void NetHandler(Ptr< Socket > socket)
Handles incoming packets from the network.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this Application object.
Ptr< RandomVariableStream > m_iaidStream
Random variable used to create the IAID.
void SendRebind(uint32_t interfaceIndex)
Send a rebind message to the DHCPv6 server.
void SendRenew(uint32_t interfaceIndex)
Send a renew message to the DHCPv6 server.
Time m_solicitInterval
SOL_MAX_RT, time between solicitations.
Duid m_clientDuid
The client DUID, built on first usage in the Boot() function.
void DoDispose() override
Destructor implementation.
Implements the DHCPv6 header.
Implements the unique identifier for DHCPv6.
Definition dhcp6-duid.h:27
Type
DUID type.
Definition dhcp6-duid.h:31
An identifier for simulation events.
Definition event-id.h:45
An Inet6 address class.
Describes an IPv6 address.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
Forward calls to a chain of Callback.
A Trickle Timer following RFC 6206.
a unique identifier for an interface.
Definition type-id.h:50
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148