Skip to content

Commit 7ad8868

Browse files
committed
network: Modernize Address class to use std::array and spaceship operator
1 parent ceba2f1 commit 7ad8868

File tree

11 files changed

+116
-139
lines changed

11 files changed

+116
-139
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ Alberto Gallegos Ramonet ([email protected])
279279
Manoj Kumar Rana ([email protected])
280280
Andrea Ranieri ([email protected])
281281
Ashish Reddy ([email protected])
282+
Dhiraj Lokesh ([email protected])
282283
Bruno Ranieri ([email protected])
283284
Deepti Rajagopal ([email protected])
284285
Satyarth Ratnani ([email protected])

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This file is a best-effort approach to solving this issue; we will do our best b
2828
* Initializing a Ipv[4,6]Address from a string using the constructor or the `Set` function will result in a crash if the string can not be parsed as an IPv4 or IPv6 address.
2929
* The `Ipv[4,6]Address::IsInitialized()` function has been deprecated and returns always `true`. The default value of Ipv4Address created with the constructor that takes no arguments is 0.0.0.0 (previously, it was 102.102.102.102), and an Ipv4Address instance can be checked against that unspecified address value (or use std::optional to denote an address that has not been set yet).
3030
* A new static function `Ipv[4,6]Address::CheckCompatible()` has been added to safely check if a string can be parsed as an IPv4 or IPv6 address.
31+
* (network): The address class comparison is now based on std::strong_ordering operator<=> comparison operator.
3132

3233
### Changes to build system
3334

src/internet/model/icmpv6-l4-protocol.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,6 @@ Icmpv6L4Protocol::HandleRS(Ptr<Packet> packet,
600600
Ptr<Ipv6L3Protocol> ipv6 = m_node->GetObject<Ipv6L3Protocol>();
601601
Icmpv6RS rsHeader;
602602
packet->RemoveHeader(rsHeader);
603-
Address hardwareAddress;
604603
Icmpv6OptionLinkLayerAddress lla(true);
605604
NdiscCache::Entry* entry = nullptr;
606605
Ptr<NdiscCache> cache = FindCache(interface->GetDevice());

src/network/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ build_lib(
158158
HEADER_FILES ${header_files}
159159
LIBRARIES_TO_LINK ${libstats}
160160
TEST_SOURCES
161+
test/address-test.cc
161162
test/bit-serializer-test.cc
162163
test/buffer-test.cc
163164
test/drop-tail-queue-test-suite.cc

src/network/model/address.cc

Lines changed: 8 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: GPL-2.0-only
55
*
66
* Author: Mathieu Lacage <[email protected]>
7+
* Modified by: Dhiraj Lokesh <[email protected]>
78
*/
89

910
#include "address.h"
@@ -22,41 +23,14 @@ NS_LOG_COMPONENT_DEFINE("Address");
2223

2324
Address::KindTypeRegistry Address::m_typeRegistry;
2425

25-
Address::Address()
26-
: m_type(UNASSIGNED_TYPE),
27-
m_len(0)
28-
{
29-
// Buffer left uninitialized
30-
NS_LOG_FUNCTION(this);
31-
}
32-
3326
Address::Address(uint8_t type, const uint8_t* buffer, uint8_t len)
3427
: m_type(type),
3528
m_len(len)
3629
{
3730
NS_LOG_FUNCTION(this << static_cast<uint32_t>(type) << &buffer << static_cast<uint32_t>(len));
3831
NS_ASSERT_MSG(m_len <= MAX_SIZE, "Address length too large");
3932
NS_ASSERT_MSG(type != UNASSIGNED_TYPE, "Type 0 is reserved for uninitialized addresses.");
40-
std::memcpy(m_data, buffer, m_len);
41-
}
42-
43-
Address::Address(const Address& address)
44-
: m_type(address.m_type),
45-
m_len(address.m_len)
46-
{
47-
NS_ASSERT(m_len <= MAX_SIZE);
48-
std::memcpy(m_data, address.m_data, m_len);
49-
}
50-
51-
Address&
52-
Address::operator=(const Address& address)
53-
{
54-
NS_ASSERT(m_len <= MAX_SIZE);
55-
m_type = address.m_type;
56-
m_len = address.m_len;
57-
NS_ASSERT(m_len <= MAX_SIZE);
58-
std::memcpy(m_data, address.m_data, m_len);
59-
return *this;
33+
std::copy(buffer, buffer + len, m_data.begin());
6034
}
6135

6236
void
@@ -97,7 +71,7 @@ Address::CopyTo(uint8_t buffer[MAX_SIZE]) const
9771
{
9872
NS_LOG_FUNCTION(this << &buffer);
9973
NS_ASSERT(m_len <= MAX_SIZE);
100-
std::memcpy(buffer, m_data, m_len);
74+
std::copy(m_data.begin(), m_data.begin() + m_len, buffer);
10175
return m_len;
10276
}
10377

@@ -108,7 +82,7 @@ Address::CopyAllTo(uint8_t* buffer, uint8_t len) const
10882
NS_ASSERT(len - m_len > 1);
10983
buffer[0] = m_type;
11084
buffer[1] = m_len;
111-
std::memcpy(buffer + 2, m_data, m_len);
85+
std::copy(m_data.begin(), m_data.begin() + m_len, buffer + 2);
11286
return m_len + 2;
11387
}
11488

@@ -119,7 +93,7 @@ Address::CopyFrom(const uint8_t* buffer, uint8_t len)
11993
NS_ASSERT_MSG(m_len <= MAX_SIZE, "Address length too large");
12094
NS_ASSERT_MSG(m_type != UNASSIGNED_TYPE,
12195
"Type-0 addresses are reserved. Please use SetType before using CopyFrom.");
122-
std::memcpy(m_data, buffer, len);
96+
std::copy(buffer, buffer + len, m_data.begin());
12397
m_len = len;
12498
return m_len;
12599
}
@@ -133,7 +107,7 @@ Address::CopyAllFrom(const uint8_t* buffer, uint8_t len)
133107
m_len = buffer[1];
134108

135109
NS_ASSERT(len - m_len > 1);
136-
std::memcpy(m_data, buffer + 2, m_len);
110+
std::copy(buffer + 2, buffer + 2 + m_len, m_data.begin());
137111
return m_len + 2;
138112
}
139113

@@ -177,7 +151,7 @@ Address::Serialize(TagBuffer buffer) const
177151
NS_LOG_FUNCTION(this << &buffer);
178152
buffer.WriteU8(m_type);
179153
buffer.WriteU8(m_len);
180-
buffer.Write(m_data, m_len);
154+
buffer.Write(m_data.data(), m_len);
181155
}
182156

183157
void
@@ -187,65 +161,11 @@ Address::Deserialize(TagBuffer buffer)
187161
m_type = buffer.ReadU8();
188162
m_len = buffer.ReadU8();
189163
NS_ASSERT(m_len <= MAX_SIZE);
190-
buffer.Read(m_data, m_len);
164+
buffer.Read(m_data.data(), m_len);
191165
}
192166

193167
ATTRIBUTE_HELPER_CPP(Address);
194168

195-
bool
196-
operator==(const Address& a, const Address& b)
197-
{
198-
if (a.m_type != b.m_type)
199-
{
200-
return false;
201-
}
202-
if (a.m_len != b.m_len)
203-
{
204-
return false;
205-
}
206-
return std::memcmp(a.m_data, b.m_data, a.m_len) == 0;
207-
}
208-
209-
bool
210-
operator!=(const Address& a, const Address& b)
211-
{
212-
return !(a == b);
213-
}
214-
215-
bool
216-
operator<(const Address& a, const Address& b)
217-
{
218-
if (a.m_type < b.m_type)
219-
{
220-
return true;
221-
}
222-
else if (a.m_type > b.m_type)
223-
{
224-
return false;
225-
}
226-
if (a.m_len < b.m_len)
227-
{
228-
return true;
229-
}
230-
else if (a.m_len > b.m_len)
231-
{
232-
return false;
233-
}
234-
NS_ASSERT(a.GetLength() == b.GetLength());
235-
for (uint8_t i = 0; i < a.GetLength(); i++)
236-
{
237-
if (a.m_data[i] < b.m_data[i])
238-
{
239-
return true;
240-
}
241-
else if (a.m_data[i] > b.m_data[i])
242-
{
243-
return false;
244-
}
245-
}
246-
return false;
247-
}
248-
249169
std::ostream&
250170
operator<<(std::ostream& os, const Address& address)
251171
{

src/network/model/address.h

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: GPL-2.0-only
55
*
66
* Author: Mathieu Lacage <[email protected]>
7+
* Modified by: Dhiraj Lokesh <[email protected]>
78
*/
89

910
#ifndef ADDRESS_H
@@ -14,6 +15,8 @@
1415
#include "ns3/attribute-helper.h"
1516
#include "ns3/attribute.h"
1617

18+
#include <array>
19+
#include <compare>
1720
#include <ostream>
1821
#include <stdint.h>
1922
#include <unordered_map>
@@ -156,7 +159,7 @@ class Address
156159
/**
157160
* Create an invalid address
158161
*/
159-
Address();
162+
Address() = default;
160163
/**
161164
* @brief Create an address from a type and a buffer.
162165
*
@@ -171,26 +174,14 @@ class Address
171174
* @param len the length of the buffer.
172175
*/
173176
Address(uint8_t type, const uint8_t* buffer, uint8_t len);
174-
/**
175-
* @brief Create an address from another address.
176-
* @param address the address to copy
177-
*/
178-
Address(const Address& address);
179-
/**
180-
* @brief Basic assignment operator.
181-
* @param address the address to copy
182-
* @return the address
183-
*/
184-
Address& operator=(const Address& address);
185-
186177
/**
187178
* Set the address type.
188179
*
189180
* Works only if the type is not yet set.
190181
* @see Register()
191182
*
192-
* @param length address length
193183
* @param kind address kind
184+
* @param length address length
194185
*/
195186
void SetType(const std::string& kind, uint8_t length);
196187
/**
@@ -278,8 +269,8 @@ class Address
278269
* It is not allowed to have two different addresses with the same
279270
* kind and length.
280271
*
281-
* @param length address length
282272
* @param kind address kind, such as "MacAddress"
273+
* @param length address length
283274
* @return a new type id.
284275
*/
285276
static uint8_t Register(const std::string& kind, uint8_t length);
@@ -303,34 +294,15 @@ class Address
303294
*/
304295
void Deserialize(TagBuffer buffer);
305296

306-
private:
307297
/**
308-
* @brief Equal to operator.
298+
* @brief Three-way comparison operator.
309299
*
310-
* @param a the first operand
311-
* @param b the first operand
312-
* @return true if the operands are equal
300+
* @param other the other address to compare with
301+
* @return comparison result
313302
*/
314-
friend bool operator==(const Address& a, const Address& b);
315-
316-
/**
317-
* @brief Not equal to operator.
318-
*
319-
* @param a the first operand
320-
* @param b the first operand
321-
* @return true if the operands are not equal
322-
*/
323-
friend bool operator!=(const Address& a, const Address& b);
324-
325-
/**
326-
* @brief Less than operator.
327-
*
328-
* @param a the first operand
329-
* @param b the first operand
330-
* @return true if the operand a is less than operand b
331-
*/
332-
friend bool operator<(const Address& a, const Address& b);
303+
constexpr std::strong_ordering operator<=>(const Address& other) const = default;
333304

305+
private:
334306
/**
335307
* @brief Stream insertion operator.
336308
*
@@ -349,16 +321,13 @@ class Address
349321
*/
350322
friend std::istream& operator>>(std::istream& is, Address& address);
351323

352-
uint8_t m_type; //!< Type of the address
353-
uint8_t m_len; //!< Length of the address
354-
uint8_t m_data[MAX_SIZE]; //!< The address value
324+
uint8_t m_type{UNASSIGNED_TYPE}; //!< Type of the address
325+
uint8_t m_len{0}; //!< Length of the address
326+
std::array<uint8_t, MAX_SIZE> m_data{}; //!< The address value
355327
};
356328

357329
ATTRIBUTE_HELPER_HEADER(Address);
358330

359-
bool operator==(const Address& a, const Address& b);
360-
bool operator!=(const Address& a, const Address& b);
361-
bool operator<(const Address& a, const Address& b);
362331
std::ostream& operator<<(std::ostream& os, const Address& address);
363332
std::istream& operator>>(std::istream& is, Address& address);
364333

0 commit comments

Comments
 (0)