Skip to content

Commit 37df586

Browse files
committed
fdsdump: rework IPAddr constructors to be more explicit and get rid of default constructor
1 parent 0030f6d commit 37df586

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

src/tools/fdsdump/src/common/fieldView.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,11 @@ IPAddr
106106
FieldView::as_ipaddr() const
107107
{
108108
if (m_field.size == 4U) {
109-
const uint32_t *value = reinterpret_cast<uint32_t *>(m_field.data);
110-
return IPAddr(*value);
109+
return IPAddr::ip4(m_field.data);
111110
}
112111

113112
if (m_field.size == 16U) {
114-
return IPAddr(m_field.data);
113+
return IPAddr::ip6(m_field.data);
115114
}
116115

117116
throw std::invalid_argument("Conversion error (ipaddr)");

src/tools/fdsdump/src/common/ipaddr.hpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,18 @@ union IPAddr {
2020
uint64_t u64[2];
2121

2222
/**
23-
* @brief Default constructor (zeroes)
23+
* @brief Default constructor (uninitialized)
2424
*/
25-
IPAddr()
25+
IPAddr() = default;
26+
27+
/**
28+
* @brief Zeroes constructor
29+
*/
30+
static IPAddr zero()
2631
{
27-
memset(this, 0, sizeof(*this));
32+
IPAddr ip;
33+
memset(&ip, 0, sizeof(ip));
34+
return ip;
2835
}
2936

3037
/**
@@ -37,20 +44,24 @@ union IPAddr {
3744
/**
3845
* @brief Create an IPv4 address (from 4 bytes array)
3946
*/
40-
explicit IPAddr(const uint32_t ip4)
47+
static IPAddr ip4(const uint8_t ip4[4])
4148
{
42-
u64[0] = 0;
43-
u32[2] = htonl(0x0000FFFF);
44-
u32[3] = ip4;
45-
};
49+
IPAddr ip;
50+
ip.u64[0] = 0;
51+
ip.u32[2] = htonl(0x0000FFFF);
52+
std::memcpy(&ip.u8[12], &ip4[0], 4U);
53+
return ip;
54+
}
4655

4756
/**
4857
* @brief Create an IPv6 address (from 16 bytes array)
4958
*/
50-
explicit IPAddr(const uint8_t ip6[16])
59+
static IPAddr ip6(const uint8_t ip6[16])
5160
{
52-
std::memcpy(&u8[0], &ip6[0], 16U);
53-
};
61+
IPAddr ip;
62+
std::memcpy(&ip.u8[0], &ip6[0], 16U);
63+
return ip;
64+
}
5465

5566
/**
5667
* @brief Check if the address is IPv4

src/tools/fdsdump/src/lister/storageSorter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ get_ip_max(Field &field, const struct Flow &flow, IPAddr &result)
346346
{
347347
bool is_reverse {flow.dir == DIRECTION_REV};
348348
bool found {false};
349-
IPAddr max {};
349+
IPAddr max = IPAddr::zero();
350350

351351
auto selector = [&](struct fds_drec_field &data) -> void {
352352
IPAddr value = FieldView(data).as_ipaddr();
@@ -377,7 +377,7 @@ get_ip_min(Field &field, const Flow &flow, IPAddr &result)
377377
{
378378
bool is_reverse {flow.dir == DIRECTION_REV};
379379
bool found {false};
380-
IPAddr min {};
380+
IPAddr min = IPAddr::zero();
381381

382382
auto selector = [&](struct fds_drec_field &data) -> void {
383383
IPAddr value = FieldView(data).as_ipaddr();
@@ -409,8 +409,8 @@ StorageSorter::cmp_ip_desc(
409409
const Flow &lhs,
410410
const Flow &rhs)
411411
{
412-
IPAddr lhs_max;
413-
IPAddr rhs_max;
412+
IPAddr lhs_max = IPAddr::zero();
413+
IPAddr rhs_max = IPAddr::zero();
414414
bool lhs_is_defined = get_ip_max(field, lhs, lhs_max);
415415
bool rhs_is_defined = get_ip_max(field, rhs, rhs_max);
416416

@@ -431,8 +431,8 @@ StorageSorter::cmp_ip_asc(
431431
const Flow &lhs,
432432
const Flow &rhs)
433433
{
434-
IPAddr lhs_min;
435-
IPAddr rhs_min;
434+
IPAddr lhs_min = IPAddr::zero();
435+
IPAddr rhs_min = IPAddr::zero();
436436
bool lhs_is_defined = get_ip_min(field, lhs, lhs_min);
437437
bool rhs_is_defined = get_ip_min(field, rhs, rhs_min);
438438

0 commit comments

Comments
 (0)