A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dhcp6-duid.cc
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#include "dhcp6-duid.h"
11
12#include "ns3/address.h"
13#include "ns3/assert.h"
14#include "ns3/ipv6-l3-protocol.h"
15#include "ns3/log.h"
16#include "ns3/loopback-net-device.h"
17#include "ns3/ptr.h"
18
19namespace ns3
20{
21
22NS_LOG_COMPONENT_DEFINE("Dhcp6Duid");
23
25{
28 m_time = Time();
29 m_identifier = std::vector<uint8_t>();
30}
31
32bool
33Duid::operator==(const Duid& o) const
34{
37}
38
39bool
40operator<(const Duid& a, const Duid& b)
41{
42 if (a.m_duidType < b.m_duidType)
43 {
44 return true;
45 }
46 else if (a.m_duidType > b.m_duidType)
47 {
48 return false;
49 }
51 {
52 return true;
53 }
54 else if (a.m_hardwareType > b.m_hardwareType)
55 {
56 return false;
57 }
58 NS_ASSERT(a.GetLength() == b.GetLength());
59 return a.m_identifier < b.m_identifier;
60}
61
62bool
64{
65 return m_identifier.empty();
66}
67
68uint8_t
70{
71 return m_identifier.size();
72}
73
74std::vector<uint8_t>
76{
77 return m_identifier;
78}
79
82{
83 return m_duidType;
84}
85
86void
88{
89 m_duidType = duidType;
90}
91
92uint16_t
94{
95 return m_hardwareType;
96}
97
98void
99Duid::SetHardwareType(uint16_t hardwareType)
100{
101 NS_LOG_FUNCTION(this << hardwareType);
102 m_hardwareType = hardwareType;
103}
104
105void
106Duid::SetDuid(std::vector<uint8_t> identifier)
107{
108 NS_LOG_FUNCTION(this << identifier);
109
111 uint8_t idLen = identifier.size();
112
113 NS_ASSERT_MSG(idLen == 6 || idLen == 8, "Duid: Invalid identifier length.");
114
115 switch (idLen)
116 {
117 case 6:
118 // Ethernet - 48 bit length
120 break;
121 case 8:
122 // EUI-64 - 64 bit length
123 SetHardwareType(27);
124 break;
125 }
126
127 m_identifier.resize(idLen);
128 m_identifier = identifier;
129}
130
131void
133{
134 Ptr<Ipv6L3Protocol> ipv6 = node->GetObject<Ipv6L3Protocol>();
135 uint32_t nInterfaces = ipv6->GetNInterfaces();
136
137 uint32_t maxAddressLength = 0;
138 Address duidAddress;
139
140 for (uint32_t i = 0; i < nInterfaces; i++)
141 {
142 Ptr<NetDevice> device = ipv6->GetNetDevice(i);
143
144 // Discard the loopback device.
146 {
147 continue;
148 }
149
150 // Check if the NetDevice is up.
151 if (device->IsLinkUp())
152 {
153 NS_LOG_DEBUG("Interface " << device->GetIfIndex() << " up on node " << node->GetId());
154 Address address = device->GetAddress();
155 if (address.GetLength() > maxAddressLength)
156 {
157 maxAddressLength = address.GetLength();
158 duidAddress = address;
159 }
160 }
161 }
162
163 NS_ASSERT_MSG(!duidAddress.IsInvalid(), "Duid: No suitable NetDevice found for DUID.");
164
165 NS_LOG_DEBUG("DUID of node " << node->GetId() << " is " << duidAddress);
166
167 // Consider the link-layer address of the first NetDevice in the list.
168 uint8_t buffer[16];
169 duidAddress.CopyTo(buffer);
170
171 std::vector<uint8_t> identifier(duidAddress.GetLength());
172 std::copy(buffer, buffer + duidAddress.GetLength(), identifier.begin());
173 SetDuid(identifier);
174}
175
176Time
178{
179 return m_time;
180}
181
182void
184{
185 NS_LOG_FUNCTION(this << time);
186 m_time = time;
187}
188
191{
192 return 4 + m_identifier.size();
193}
194
195void
197{
198 Buffer::Iterator i = start;
199 i.WriteHtonU16(static_cast<std::underlying_type_t<Duid::Type>>(m_duidType));
201
202 for (const auto& byte : m_identifier)
203 {
204 i.WriteU8(byte);
205 }
206}
207
210{
211 Buffer::Iterator i = start;
212
213 m_duidType = static_cast<Duid::Type>(i.ReadNtohU16());
215 m_identifier.resize(len);
216
217 for (uint32_t j = 0; j < len; j++)
218 {
219 m_identifier[j] = i.ReadU8();
220 }
221
222 return 4 + m_identifier.size();
223}
224
225size_t
226Duid::DuidHash::operator()(const Duid& x) const noexcept
227{
228 uint8_t duidLen = x.GetLength();
229 std::vector<uint8_t> buffer = x.GetIdentifier();
230
231 std::string s(buffer.begin(), buffer.begin() + duidLen);
232 return std::hash<std::string>{}(s);
233}
234} // namespace ns3
a polymophic address class
Definition address.h:90
bool IsInvalid() const
Definition address.cc:60
uint8_t GetLength() const
Get the length of the underlying address.
Definition address.cc:67
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition address.cc:75
iterator in a Buffer instance
Definition buffer.h:89
void WriteU8(uint8_t data)
Definition buffer.h:870
void WriteHtonU16(uint16_t data)
Definition buffer.h:904
uint16_t ReadNtohU16()
Definition buffer.h:943
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
void SetHardwareType(uint16_t hardwareType)
Set the hardware type.
Definition dhcp6-duid.cc:99
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:69
Type m_duidType
Type of the DUID.
Definition dhcp6-duid.h:176
Time m_time
Time at which the DUID is generated.
Definition dhcp6-duid.h:179
void SetDuidType(Type duidType)
Set the DUID type.
Definition dhcp6-duid.cc:87
void SetDuid(std::vector< uint8_t > identifier)
Set the identifier as the DUID.
void Serialize(Buffer::Iterator start) const
Serialize the DUID.
uint16_t m_hardwareType
Valid hardware type assigned by IANA.
Definition dhcp6-duid.h:178
bool IsInvalid() const
Check if the DUID is invalid.
Definition dhcp6-duid.cc:63
bool operator==(const Duid &duid) const
Comparison operator.
Definition dhcp6-duid.cc:33
void SetTime(Time time)
Set the time at which DUID is generated.
std::vector< uint8_t > GetIdentifier() const
Return the identifier of the node.
Definition dhcp6-duid.cc:75
uint32_t GetSerializedSize() const
Get the DUID serialized size.
Type GetDuidType() const
Get the DUID type.
Definition dhcp6-duid.cc:81
uint16_t GetHardwareType() const
Get the hardware type.
Definition dhcp6-duid.cc:93
void Initialize(Ptr< Node > node)
Initialize the DUID for a client or server.
Duid()
Default constructor.
Definition dhcp6-duid.cc:24
std::vector< uint8_t > m_identifier
Identifier of the node in bytes.
Definition dhcp6-duid.h:180
Time GetTime() const
Get the time at which the DUID is generated.
Type
DUID type.
Definition dhcp6-duid.h:36
IPv6 layer implementation.
Smart pointer class similar to boost::intrusive_ptr.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
bool operator<(const EventId &a, const EventId &b)
Definition event-id.h:168