Skip to content

Commit 89ebb6f

Browse files
Dhiraj LokeshTommyPec
authored andcommitted
network: (fixes #2551) Use std::array and three-way comparison operator in Ipv[4,6]Address classes
1 parent 30eb416 commit 89ebb6f

File tree

5 files changed

+59
-265
lines changed

5 files changed

+59
-265
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ void
250250
Icmpv6L4Protocol::DoDAD(Ipv6Address target, Ptr<Ipv6Interface> interface)
251251
{
252252
NS_LOG_FUNCTION(this << target << interface);
253-
Ipv6Address addr;
254253
Ptr<Ipv6L3Protocol> ipv6 = m_node->GetObject<Ipv6L3Protocol>();
255254

256255
NS_ASSERT(ipv6);

src/network/utils/ipv4-address.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ namespace ns3
2525

2626
NS_LOG_COMPONENT_DEFINE("Ipv4Address");
2727

28-
Ipv4Mask::Ipv4Mask()
29-
{
30-
NS_LOG_FUNCTION(this);
31-
}
32-
3328
Ipv4Mask::Ipv4Mask(uint32_t mask)
3429
: m_mask(mask)
3530
{

src/network/utils/ipv4-address.h

Lines changed: 14 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ns3/attribute-helper.h"
1414
#include "ns3/deprecated.h"
1515

16+
#include <compare>
1617
#include <ostream>
1718
#include <stdint.h>
1819

@@ -223,6 +224,14 @@ class Ipv4Address
223224
*/
224225
static Ipv4Address GetLoopback();
225226

227+
/**
228+
* @brief Three-way comparison operator.
229+
*
230+
* @param other the other address to compare with
231+
* @returns comparison result
232+
*/
233+
std::strong_ordering operator<=>(const Ipv4Address& other) const = default;
234+
226235
private:
227236
/**
228237
* @brief Get the underlying address type (automatically assigned).
@@ -231,33 +240,6 @@ class Ipv4Address
231240
*/
232241
static uint8_t GetType();
233242
uint32_t m_address{0}; //!< IPv4 address
234-
235-
/**
236-
* @brief Equal to operator.
237-
*
238-
* @param a the first operand.
239-
* @param b the first operand.
240-
* @returns true if the operands are equal.
241-
*/
242-
friend bool operator==(const Ipv4Address& a, const Ipv4Address& b);
243-
244-
/**
245-
* @brief Not equal to operator.
246-
*
247-
* @param a the first operand.
248-
* @param b the first operand.
249-
* @returns true if the operands are not equal.
250-
*/
251-
friend bool operator!=(const Ipv4Address& a, const Ipv4Address& b);
252-
253-
/**
254-
* @brief Less than to operator.
255-
*
256-
* @param a the first operand.
257-
* @param b the first operand.
258-
* @returns true if the first operand is less than the second.
259-
*/
260-
friend bool operator<(const Ipv4Address& a, const Ipv4Address& b);
261243
};
262244

263245
/**
@@ -277,7 +259,7 @@ class Ipv4Mask
277259
/**
278260
* Will initialize to a zero-length mask, which will match any address.
279261
*/
280-
Ipv4Mask();
262+
Ipv4Mask() = default;
281263
/**
282264
* @param mask bitwise integer representation of the mask
283265
*
@@ -335,22 +317,12 @@ class Ipv4Mask
335317
static Ipv4Mask GetOnes();
336318

337319
/**
338-
* @brief Equal to operator.
339-
*
340-
* @param a the first operand.
341-
* @param b the first operand.
342-
* @returns true if the operands are equal.
343-
*/
344-
friend bool operator==(const Ipv4Mask& a, const Ipv4Mask& b);
345-
346-
/**
347-
* @brief Not equal to operator.
320+
* @brief Three-way comparison operator.
348321
*
349-
* @param a the first operand.
350-
* @param b the first operand.
351-
* @returns true if the operands are not equal.
322+
* @param a the other mask to compare with
323+
* @returns comparison result
352324
*/
353-
friend bool operator!=(const Ipv4Mask& a, const Ipv4Mask& b);
325+
std::strong_ordering operator<=>(const Ipv4Mask& a) const = default;
354326

355327
private:
356328
uint32_t m_mask{0}; //!< IP mask
@@ -392,24 +364,6 @@ std::istream& operator>>(std::istream& is, Ipv4Address& address);
392364
*/
393365
std::istream& operator>>(std::istream& is, Ipv4Mask& mask);
394366

395-
inline bool
396-
operator==(const Ipv4Address& a, const Ipv4Address& b)
397-
{
398-
return a.m_address == b.m_address;
399-
}
400-
401-
inline bool
402-
operator!=(const Ipv4Address& a, const Ipv4Address& b)
403-
{
404-
return a.m_address != b.m_address;
405-
}
406-
407-
inline bool
408-
operator<(const Ipv4Address& a, const Ipv4Address& b)
409-
{
410-
return a.m_address < b.m_address;
411-
}
412-
413367
/**
414368
* @ingroup address
415369
*
@@ -429,18 +383,6 @@ class Ipv4AddressHash
429383
size_t operator()(const Ipv4Address& x) const;
430384
};
431385

432-
inline bool
433-
operator==(const Ipv4Mask& a, const Ipv4Mask& b)
434-
{
435-
return a.m_mask == b.m_mask;
436-
}
437-
438-
inline bool
439-
operator!=(const Ipv4Mask& a, const Ipv4Mask& b)
440-
{
441-
return a.m_mask != b.m_mask;
442-
}
443-
444386
} // namespace ns3
445387

446388
#endif /* IPV4_ADDRESS_H */

src/network/utils/ipv6-address.cc

Lines changed: 22 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "ns3/assert.h"
1616
#include "ns3/log.h"
1717

18+
#include <algorithm>
1819
#include <iomanip>
1920
#include <memory>
2021

@@ -135,30 +136,13 @@ extern "C"
135136
}
136137
#endif
137138

138-
Ipv6Address::Ipv6Address()
139-
{
140-
NS_LOG_FUNCTION(this);
141-
memset(m_address, 0x00, 16);
142-
}
143-
144-
Ipv6Address::Ipv6Address(const Ipv6Address& addr)
145-
{
146-
// Do not add function logging here, to avoid stack overflow
147-
memcpy(m_address, addr.m_address, 16);
148-
}
149-
150-
Ipv6Address::Ipv6Address(const Ipv6Address* addr)
151-
{
152-
// Do not add function logging here, to avoid stack overflow
153-
memcpy(m_address, addr->m_address, 16);
154-
}
155-
156139
Ipv6Address::Ipv6Address(const char* address)
157140
{
158141
NS_LOG_FUNCTION(this << address);
159142

160-
if (inet_pton(AF_INET6, address, m_address) <= 0)
143+
if (inet_pton(AF_INET6, address, m_address.data()) <= 0)
161144
{
145+
m_address.fill(0x00);
162146
NS_ABORT_MSG("Error, can not build an IPv6 address from an invalid string: " << address);
163147
return;
164148
}
@@ -182,23 +166,16 @@ Ipv6Address::CheckCompatible(const std::string& addressStr)
182166
Ipv6Address::Ipv6Address(uint8_t address[16])
183167
{
184168
NS_LOG_FUNCTION(this << &address);
185-
/* 128 bit => 16 bytes */
186-
memcpy(m_address, address, 16);
187-
}
188-
189-
Ipv6Address::~Ipv6Address()
190-
{
191-
/* do nothing */
192-
NS_LOG_FUNCTION(this);
169+
std::copy(address, address + 16, m_address.begin());
193170
}
194171

195172
void
196173
Ipv6Address::Set(const char* address)
197174
{
198175
NS_LOG_FUNCTION(this << address);
199-
if (inet_pton(AF_INET6, address, m_address) <= 0)
176+
if (inet_pton(AF_INET6, address, m_address.data()) <= 0)
200177
{
201-
memset(m_address, 0x00, 16);
178+
m_address.fill(0x00);
202179
NS_ABORT_MSG("Error, can not set an IPv6 address from an invalid string: " << address);
203180
return;
204181
}
@@ -207,16 +184,15 @@ Ipv6Address::Set(const char* address)
207184
void
208185
Ipv6Address::Set(uint8_t address[16])
209186
{
210-
/* 128 bit => 16 bytes */
211187
NS_LOG_FUNCTION(this << &address);
212-
memcpy(m_address, address, 16);
188+
std::copy(address, address + 16, m_address.begin());
213189
}
214190

215191
void
216192
Ipv6Address::Serialize(uint8_t buf[16]) const
217193
{
218194
NS_LOG_FUNCTION(this << &buf);
219-
memcpy(buf, m_address, 16);
195+
std::copy(m_address.begin(), m_address.end(), buf);
220196
}
221197

222198
Ipv6Address
@@ -524,7 +500,7 @@ Ipv6Address::Print(std::ostream& os) const
524500

525501
char str[INET6_ADDRSTRLEN];
526502

527-
if (inet_ntop(AF_INET6, m_address, str, INET6_ADDRSTRLEN))
503+
if (inet_ntop(AF_INET6, m_address.data(), str, INET6_ADDRSTRLEN))
528504
{
529505
os << str;
530506
}
@@ -556,9 +532,9 @@ bool
556532
Ipv6Address::IsIpv4MappedAddress() const
557533
{
558534
NS_LOG_FUNCTION(this);
559-
static uint8_t v4MappedPrefix[12] =
535+
static std::array<uint8_t, 12> v4MappedPrefix =
560536
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff};
561-
return memcmp(m_address, v4MappedPrefix, sizeof(v4MappedPrefix)) == 0;
537+
return std::equal(v4MappedPrefix.begin(), v4MappedPrefix.end(), m_address.begin());
562538
}
563539

564540
Ipv6Address
@@ -570,10 +546,9 @@ Ipv6Address::CombinePrefix(const Ipv6Prefix& prefix) const
570546
uint8_t pref[16];
571547
unsigned int i = 0;
572548

573-
memcpy(addr, m_address, 16);
549+
std::copy(m_address.begin(), m_address.end(), addr);
574550
((Ipv6Prefix)prefix).GetBytes(pref);
575551

576-
/* a little bit ugly... */
577552
for (i = 0; i < 16; i++)
578553
{
579554
addr[i] = addr[i] & pref[i];
@@ -740,7 +715,7 @@ void
740715
Ipv6Address::GetBytes(uint8_t buf[16]) const
741716
{
742717
NS_LOG_FUNCTION(this << &buf);
743-
memcpy(buf, m_address, 16);
718+
std::copy(m_address.begin(), m_address.end(), buf);
744719
}
745720

746721
bool
@@ -774,17 +749,10 @@ operator>>(std::istream& is, Ipv6Address& address)
774749
return is;
775750
}
776751

777-
Ipv6Prefix::Ipv6Prefix()
778-
{
779-
NS_LOG_FUNCTION(this);
780-
memset(m_prefix, 0x00, 16);
781-
m_prefixLength = 0;
782-
}
783-
784752
Ipv6Prefix::Ipv6Prefix(const char* prefix)
785753
{
786754
NS_LOG_FUNCTION(this << prefix);
787-
if (inet_pton(AF_INET6, prefix, m_prefix) <= 0)
755+
if (inet_pton(AF_INET6, prefix, m_prefix.data()) <= 0)
788756
{
789757
NS_ABORT_MSG("Error, can not build an IPv6 prefix from an invalid string: " << prefix);
790758
}
@@ -794,14 +762,14 @@ Ipv6Prefix::Ipv6Prefix(const char* prefix)
794762
Ipv6Prefix::Ipv6Prefix(uint8_t prefix[16])
795763
{
796764
NS_LOG_FUNCTION(this << &prefix);
797-
memcpy(m_prefix, prefix, 16);
765+
std::copy(prefix, prefix + 16, m_prefix.begin());
798766
m_prefixLength = GetMinimumPrefixLength();
799767
}
800768

801769
Ipv6Prefix::Ipv6Prefix(const char* prefix, uint8_t prefixLength)
802770
{
803771
NS_LOG_FUNCTION(this << prefix);
804-
if (inet_pton(AF_INET6, prefix, m_prefix) <= 0)
772+
if (inet_pton(AF_INET6, prefix, m_prefix.data()) <= 0)
805773
{
806774
NS_ABORT_MSG("Error, can not build an IPv6 prefix from an invalid string: " << prefix);
807775
}
@@ -816,7 +784,7 @@ Ipv6Prefix::Ipv6Prefix(const char* prefix, uint8_t prefixLength)
816784
Ipv6Prefix::Ipv6Prefix(uint8_t prefix[16], uint8_t prefixLength)
817785
{
818786
NS_LOG_FUNCTION(this << &prefix);
819-
memcpy(m_prefix, prefix, 16);
787+
std::copy(prefix, prefix + 16, m_prefix.begin());
820788

821789
uint8_t autoLength = GetMinimumPrefixLength();
822790
NS_ASSERT_MSG(autoLength <= prefixLength,
@@ -833,7 +801,7 @@ Ipv6Prefix::Ipv6Prefix(uint8_t prefix)
833801
unsigned int mod = 0;
834802
unsigned int i = 0;
835803

836-
memset(m_prefix, 0x00, 16);
804+
m_prefix.fill(0x00);
837805
m_prefixLength = prefix;
838806

839807
NS_ASSERT(prefix <= 128);
@@ -845,7 +813,7 @@ Ipv6Prefix::Ipv6Prefix(uint8_t prefix)
845813
// __warn_memset_zero_len compiler errors in some gcc>4.5.x
846814
if (nb > 0)
847815
{
848-
memset(m_prefix, 0xff, nb);
816+
memset(m_prefix.data(), 0xff, nb);
849817
}
850818
if (mod)
851819
{
@@ -862,24 +830,6 @@ Ipv6Prefix::Ipv6Prefix(uint8_t prefix)
862830
}
863831
}
864832

865-
Ipv6Prefix::Ipv6Prefix(const Ipv6Prefix& prefix)
866-
{
867-
memcpy(m_prefix, prefix.m_prefix, 16);
868-
m_prefixLength = prefix.m_prefixLength;
869-
}
870-
871-
Ipv6Prefix::Ipv6Prefix(const Ipv6Prefix* prefix)
872-
{
873-
memcpy(m_prefix, prefix->m_prefix, 16);
874-
m_prefixLength = prefix->m_prefixLength;
875-
}
876-
877-
Ipv6Prefix::~Ipv6Prefix()
878-
{
879-
/* do nothing */
880-
NS_LOG_FUNCTION(this);
881-
}
882-
883833
bool
884834
Ipv6Prefix::IsMatch(Ipv6Address a, Ipv6Address b) const
885835
{
@@ -938,16 +888,16 @@ void
938888
Ipv6Prefix::GetBytes(uint8_t buf[16]) const
939889
{
940890
NS_LOG_FUNCTION(this << &buf);
941-
memcpy(buf, m_prefix, 16);
891+
memcpy(buf, m_prefix.data(), 16);
942892
}
943893

944894
Ipv6Address
945895
Ipv6Prefix::ConvertToIpv6Address() const
946896
{
947897
uint8_t prefixBytes[16];
948-
memcpy(prefixBytes, m_prefix, 16);
898+
memcpy(prefixBytes, m_prefix.data(), 16);
949899

950-
Ipv6Address convertedPrefix = Ipv6Address(prefixBytes);
900+
auto convertedPrefix = Ipv6Address(prefixBytes);
951901
return convertedPrefix;
952902
}
953903

0 commit comments

Comments
 (0)