A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dhcp6-duid.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_DUID_H
11#define DHCP6_DUID_H
12
13#include "ns3/buffer.h"
14#include "ns3/node.h"
15#include "ns3/nstime.h"
16
17namespace ns3
18{
19
20/**
21 * @ingroup dhcp6
22 *
23 * @class Duid
24 * @brief Implements the unique identifier for DHCPv6.
25 */
26class Duid
27{
28 public:
29 /// DUID type.
30 enum class Type
31 {
32 LLT = 1, // Link-layer address plus time
33 EN, // Vendor-assigned unique ID based on Enterprise Number
34 LL, // Link-layer address
35 UUID, // Universally Unique Identifier (UUID) [RFC6355]
36 };
37
38 /**
39 * @ingroup dhcp6
40 *
41 * @class DuidHash
42 * @brief Class providing a hash for DUIDs
43 */
45 {
46 public:
47 /**
48 * @brief Returns the hash of a DUID.
49 * @param x the DUID
50 * @returns the hash
51 *
52 * This method uses std::hash rather than class Hash
53 * as speed is more important than cryptographic robustness.
54 */
55 size_t operator()(const Duid& x) const noexcept;
56 };
57
58 /**
59 * Initialize a DUID.
60 *
61 * The DUID initialization logic is the following:
62 * - DUID-LLT and DUID-LL: the identifier is created by selecting
63 * the NetDevices with the longest MAC address length, and among
64 * them, the one with the smallest index.
65 * - DUID-LLT: the creation time is taken from the simulation time.
66 * - DUID-EN: the Enterprise number is 0xf00dcafe (arbitrary number,
67 * currently unused), and the identifier is based on the Node ID.
68 * - DUID-UUID: the UUID is based on the Node ID.
69 *
70 * @param duidType the DUID type.
71 * @param node the node this DUID refers to
72 * @param enIdentifierLength the DUID-EN identifier length (unused in other DUID types)
73 */
74 void Initialize(Type duidType, Ptr<Node> node, uint32_t enIdentifierLength);
75
76 /**
77 * @brief Initialize the DUID-LL for a client or server.
78 * @param node The node for which the DUID is to be generated.
79 */
80 void InitializeLL(Ptr<Node> node);
81
82 /**
83 * @brief Initialize the DUID-LLT for a client or server.
84 * @param node The node for which the DUID is to be generated.
85 */
86 void InitializeLLT(Ptr<Node> node);
87
88 /**
89 * @brief Initialize the DUID-EN for a client or server.
90 * @param enNumber The Enterprise Number.
91 * @param identifier The device Identifier.
92 */
93 void InitializeEN(uint32_t enNumber, const std::vector<uint8_t>& identifier);
94
95 /**
96 * @brief Initialize the DUID-EN for a client or server.
97 * @param uuid The UUID.
98 */
99 void InitializeUUID(const std::vector<uint8_t>& uuid);
100
101 /**
102 * @brief Check if the DUID is invalid.
103 * @returns true if the DUID is invalid.
104 */
105 bool IsInvalid() const;
106
107 /**
108 * @brief Get the DUID type
109 * @returns the DUID type.
110 */
111 Type GetDuidType() const;
112
113 /**
114 * @brief Get the length of the DUID.
115 * @returns the DUID length.
116 */
117 uint8_t GetLength() const;
118
119 /**
120 * @brief Return the identifier of the node.
121 * @returns the identifier.
122 */
123 std::vector<uint8_t> GetIdentifier() const;
124
125 /**
126 * @brief Get the DUID serialized size.
127 * @returns The DUID serialized sized in bytes.
128 */
130
131 /**
132 * @brief Serialize the DUID.
133 * @param start The buffer iterator.
134 */
135 void Serialize(Buffer::Iterator start) const;
136
137 /**
138 * @brief Deserialize the DUID.
139 * @param start The buffer iterator.
140 * @param len The number of bytes to be read.
141 * @returns The number of bytes read.
142 */
144
145 /**
146 * @brief Comparison operator.
147 *
148 * @note Defined because gcc-13.3 raises an error in optimized builds.
149 * Other than that, the comparison is semantically identical to the default one.
150 *
151 * @param other DUID to compare to this one.
152 * @return true if the duids are equal.
153 */
154 bool operator==(const Duid& other) const;
155
156 /**
157 * Spaceship comparison operator. All the other comparison operators
158 * are automatically generated from this one.
159 *
160 * @note Defined because gcc-13.3 raises an error in optimized builds.
161 * Other than that, the comparison is semantically identical to the default one.
162 *
163 * @param a left operand DUID to compare.
164 * @param b right operand DUID to compare.
165 * @returns The result of the comparison.
166 */
167 friend std::strong_ordering operator<=>(const Duid& a, const Duid& b);
168
169 /**
170 * @brief Stream output operator.
171 *
172 * The DUID is printed according to its type, e.g.:
173 * - DUID-LL HWtype: 1 Identifier: 0x000000000002
174 * - DUID-LLT HWtype: 1 Time: 0 Identifier: 0x000000000002
175 * - DUID-EN EnNumber: 0xf00dcafe Identifier: 0x000102030405060708090a
176 * - DUID-UUID UUID: 0x000102030405060708090a0b0c0d0e0f
177 *
178 * @param os output stream
179 * @param duid The reference to the DUID object.
180 * @returns updated stream
181 */
182 friend std::ostream& operator<<(std::ostream& os, const Duid& duid);
183
184 private:
185 /**
186 * DUID data offset.
187 *
188 * Each DUID has some fixed-length fields (e.g., Hardware Type), and a
189 * variable length part. This is the offset to the variable length part.
190 */
192 {
193 LLT = 6,
194 EN = 4,
195 LL = 2,
197 };
198
199 /**
200 * Find a suitable NetDevice address to create a DUID-LL or DUID-LLT.
201 *
202 * We select the NetDevices with the longest MAC address length,
203 * and among them, the one with the smallest index.
204 *
205 * @param node The node for which the DUID is to be generated.
206 * @returns the L2 address for the DUID Hardware Identifier.
207 */
209
210 /**
211 * Utility function to convert a 32-bit number to
212 * an array with its bytes in network order.
213 *
214 * @param[in] source the number to convert.
215 * @param[out] dest the converted number.
216 */
217 void HtoN(uint32_t source, std::vector<uint8_t>::iterator dest);
218
220
221 std::vector<uint8_t> m_blob{}; //!< DUID data, content depending on the DUID type.
222};
223
224} // namespace ns3
225
226#endif
cairo_uint64_t x
_cairo_uint_96by64_32x64_divrem:
a polymophic address class
Definition address.h:114
iterator in a Buffer instance
Definition buffer.h:89
Class providing a hash for DUIDs.
Definition dhcp6-duid.h:45
size_t operator()(const Duid &x) const noexcept
Returns the hash of a DUID.
Implements the unique identifier for DHCPv6.
Definition dhcp6-duid.h:27
uint32_t Deserialize(Buffer::Iterator start, uint32_t len)
Deserialize the DUID.
uint8_t GetLength() const
Get the length of the DUID.
Definition dhcp6-duid.cc:35
void InitializeLL(Ptr< Node > node)
Initialize the DUID-LL for a client or server.
Definition dhcp6-duid.cc:53
Type m_duidType
DUID type.
Definition dhcp6-duid.h:219
bool operator==(const Duid &other) const
Comparison operator.
void Serialize(Buffer::Iterator start) const
Serialize the DUID.
void HtoN(uint32_t source, std::vector< uint8_t >::iterator dest)
Utility function to convert a 32-bit number to an array with its bytes in network order.
friend std::strong_ordering operator<=>(const Duid &a, const Duid &b)
Spaceship comparison operator.
bool IsInvalid() const
Check if the DUID is invalid.
Definition dhcp6-duid.cc:29
Offset
DUID data offset.
Definition dhcp6-duid.h:192
void InitializeEN(uint32_t enNumber, const std::vector< uint8_t > &identifier)
Initialize the DUID-EN for a client or server.
void InitializeUUID(const std::vector< uint8_t > &uuid)
Initialize the DUID-EN for a client or server.
void Initialize(Type duidType, Ptr< Node > node, uint32_t enIdentifierLength)
Initialize a DUID.
std::vector< uint8_t > GetIdentifier() const
Return the identifier of the node.
Definition dhcp6-duid.cc:41
uint32_t GetSerializedSize() const
Get the DUID serialized size.
Type GetDuidType() const
Get the DUID type.
Definition dhcp6-duid.cc:47
friend std::ostream & operator<<(std::ostream &os, const Duid &duid)
Stream output operator.
std::vector< uint8_t > m_blob
DUID data, content depending on the DUID type.
Definition dhcp6-duid.h:221
void InitializeLLT(Ptr< Node > node)
Initialize the DUID-LLT for a client or server.
Definition dhcp6-duid.cc:74
Address FindSuitableNetDeviceAddress(Ptr< Node > node)
Find a suitable NetDevice address to create a DUID-LL or DUID-LLT.
Type
DUID type.
Definition dhcp6-duid.h:31
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Every class exported by the ns3 library is enclosed in the ns3 namespace.