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 /**
72 * @ingroup dhcp6
73 *
74 * @class InterfaceConfig
75 * @brief DHCPv6 client config details about each interface on the node.
76 */
77 class InterfaceConfig : public SimpleRefCount<InterfaceConfig>
78 {
79 public:
80 /**
81 * The default constructor.
82 */
84
85 /**
86 * Destructor.
87 */
89
90 /// The Dhcp6Client on which the interface is present.
92
93 /// The IPv6 interface index of this configuration.
95
96 /// The socket that has been opened for this interface.
98
99 /// The DHCPv6 state of the client interface.
101
102 /// The server DUID.
104
105 /// The IAIDs associated with this DHCPv6 client interface.
106 std::vector<uint32_t> m_iaids;
107
108 TrickleTimer m_solicitTimer; //!< TrickleTimer to schedule Solicit messages.
109 Time m_msgStartTime; //!< Time when message exchange starts.
110 uint32_t m_transactId; //!< Transaction ID of the client-initiated message.
111 uint8_t m_nAcceptedAddresses; //!< Number of addresses accepted by client.
112
113 /// List of addresses offered to the client.
114 std::vector<Ipv6Address> m_offeredAddresses;
115
116 /// List of addresses to be declined by the client.
117 std::vector<Ipv6Address> m_declinedAddresses;
118
119 Time m_renewTime; //!< REN_MAX_RT, Time after which lease should be renewed.
120 Time m_rebindTime; //!< REB_MAX_RT, Time after which client should send a Rebind message.
121 Time m_prefLifetime; //!< Preferred lifetime of the address.
122 Time m_validLifetime; //!< Valid lifetime of the address.
123 EventId m_renewEvent; //!< Event ID for the Renew event.
124 EventId m_rebindEvent; //!< Event ID for the rebind event
125
126 /// Store all the Event IDs for the addresses being Released.
127 std::vector<EventId> m_releaseEvent;
128
129 /**
130 * @brief Accept the DHCPv6 offer.
131 * @param offeredAddress The IPv6 address that has been accepted.
132 */
133 void AcceptedAddress(const Ipv6Address& offeredAddress);
134
135 /// Callback for the accepted addresses - needed for cleanup.
136 std::optional<Callback<void, const Ipv6Address&>> m_acceptedAddressCb{std::nullopt};
137
138 /**
139 * @brief Add a declined address to the list maintained by the client.
140 * @param offeredAddress The IPv6 address to be declined.
141 */
142 void DeclinedAddress(const Ipv6Address& offeredAddress);
143
144 /// Callback for the declined addresses - needed for cleanup.
145 std::optional<Callback<void, const Ipv6Address&>> m_declinedAddressCb{std::nullopt};
146
147 /**
148 * @brief Send a Decline message to the DHCPv6 server
149 */
150 void DeclineOffer();
151
152 /**
153 * @brief Cleanup the internal callbacks and timers.
154 *
155 * MUST be used before removing an interface to avoid circular references locks.
156 */
157 void Cleanup();
158 };
159
160 /**
161 * @brief Verify the incoming advertise message.
162 * @param header The DHCPv6 header received.
163 * @param iDev The incoming NetDevice.
164 * @return True if the Advertise is valid.
165 */
167
168 /**
169 * @brief Send a request to the DHCPv6 server.
170 * @param iDev The outgoing NetDevice
171 * @param header The DHCPv6 header
172 * @param server The address of the server
173 */
174 void SendRequest(Ptr<NetDevice> iDev, Dhcp6Header header, Inet6SocketAddress server);
175
176 /**
177 * @brief Send a request to the DHCPv6 server.
178 * @param iDev The outgoing NetDevice
179 * @param header The DHCPv6 header
180 * @param server The address of the server
181 */
183
184 /**
185 * @brief Check lease status after sending a Decline or Release message.
186 * @param iDev The incoming NetDevice
187 * @param header The DHCPv6 header
188 * @param server The address of the server
189 */
190 void CheckLeaseStatus(Ptr<NetDevice> iDev, Dhcp6Header header, Inet6SocketAddress server) const;
191
192 /**
193 * @brief Send a renew message to the DHCPv6 server.
194 * @param interfaceIndex The interface whose leases are to be renewed.
195 */
196 void SendRenew(uint32_t interfaceIndex);
197
198 /**
199 * @brief Send a rebind message to the DHCPv6 server.
200 * @param interfaceIndex The interface whose leases are to be rebound.
201 */
202 void SendRebind(uint32_t interfaceIndex);
203
204 /**
205 * @brief Send a Release message to the DHCPv6 server.
206 * @param address The address whose lease is to be released.
207 */
208 void SendRelease(Ipv6Address address);
209
210 /**
211 * @brief Handles incoming packets from the network
212 * @param socket incoming Socket
213 */
214 void NetHandler(Ptr<Socket> socket);
215
216 /**
217 * @brief Handle changes in the link state.
218 * @param isUp Indicates whether the interface is up.
219 * @param ifIndex The interface index.
220 */
221 void LinkStateHandler(bool isUp, int32_t ifIndex);
222
223 /**
224 * @brief Callback for when an M flag is received.
225 *
226 * The M flag is carried by Router Advertisements (RA), and it is a signal
227 * that the DHCPv6 client must search for a DHCPv6 Server.
228 *
229 * @param recvInterface The interface on which the M flag was received.
230 */
231 void ReceiveMflag(uint32_t recvInterface);
232
233 /**
234 * @brief Used to send the Solicit message and start the DHCPv6 client.
235 * @param device The client interface.
236 */
237 void Boot(Ptr<NetDevice> device);
238
239 /**
240 * @brief Retrieve all existing IAIDs.
241 * @return A list of all IAIDs.
242 */
243 std::vector<uint32_t> GetIaids();
244
245 /// Map each interface to its corresponding configuration details.
246 std::unordered_map<uint32_t, Ptr<InterfaceConfig>> m_interfaces;
247
248 Duid m_clientDuid; //!< The client DUID.
250
251 /// Track the IPv6 Address - IAID association.
252 std::unordered_map<Ipv6Address, uint32_t, Ipv6AddressHash> m_iaidMap;
253
254 /// Random variable to set transaction ID
256
257 Time m_solicitInterval; //!< SOL_MAX_RT, time between solicitations.
258
259 /**
260 * Random jitter before sending the first Solicit. Equivalent to
261 * SOL_MAX_DELAY (RFC 8415, Section 7.6)
262 */
264
265 /// Random variable used to create the IAID.
267};
268
269/**
270 * @brief Stream output operator
271 * @param os output stream
272 * @param h the Dhcp6Client
273 * @return updated stream
274 */
275std::ostream& operator<<(std::ostream& os, const Dhcp6Client& h);
276
277} // namespace ns3
278
279#endif
The base class for all ns3 applications.
Definition application.h:51
DHCPv6 client config details about each interface on the node.
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.
std::unordered_map< Ipv6Address, uint32_t, Ipv6AddressHash > m_iaidMap
Track the IPv6 Address - IAID association.
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.
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.
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.
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.
void DoDispose() override
Destructor implementation.
Implements the DHCPv6 header.
Implements the unique identifier for DHCPv6.
Definition dhcp6-duid.h:27
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.
A template-based reference counting class.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Forward calls to a chain of Callback.
A Trickle Timer following RFC 6206 .
a unique identifier for an interface.
Definition type-id.h:49
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