Skip to content

Commit 01379fe

Browse files
committed
fdsdump: add aggregation field value representation/accessors
1 parent 503975c commit 01379fe

File tree

2 files changed

+339
-0
lines changed

2 files changed

+339
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/**
2+
* @file
3+
* @author Michal Sedlak <[email protected]>
4+
* @brief Aggregator field value representation
5+
*
6+
* Copyright: (C) 2024 CESNET, z.s.p.o.
7+
* SPDX-License-Identifier: BSD-3-Clause
8+
*/
9+
10+
#include <aggregator/value.hpp>
11+
12+
#include <cassert>
13+
#include <stdexcept>
14+
15+
namespace fdsdump {
16+
namespace aggregator {
17+
18+
std::string
19+
data_type_to_str(DataType data_type)
20+
{
21+
switch (data_type) {
22+
case DataType::Unassigned:
23+
return "Unassigned";
24+
case DataType::IPAddress:
25+
return "IPAddress";
26+
case DataType::IPv4Address:
27+
return "IPv4Address";
28+
case DataType::IPv6Address:
29+
return "IPv6Address";
30+
case DataType::MacAddress:
31+
return "MacAddress";
32+
case DataType::Unsigned8:
33+
return "Unsigned8";
34+
case DataType::Signed8:
35+
return "Signed8";
36+
case DataType::Unsigned16:
37+
return "Unsigned16";
38+
case DataType::Signed16:
39+
return "Signed16";
40+
case DataType::Unsigned32:
41+
return "Unsigned32";
42+
case DataType::Signed32:
43+
return "Signed32";
44+
case DataType::Unsigned64:
45+
return "Unsigned64";
46+
case DataType::Signed64:
47+
return "Signed64";
48+
case DataType::DateTime:
49+
return "DateTime";
50+
case DataType::String128B:
51+
return "String128B";
52+
case DataType::Octets128B:
53+
return "Octets128B";
54+
case DataType::VarString:
55+
return "VarString";
56+
}
57+
assert(0);
58+
return "<unknown>";
59+
}
60+
61+
ValueView::ValueView(DataType data_type, Value &value) :
62+
m_data_type(data_type),
63+
m_value(value)
64+
{
65+
}
66+
67+
uint8_t
68+
ValueView::as_u8() const
69+
{
70+
switch (m_data_type) {
71+
case DataType::Unsigned8:
72+
return m_value.u8;
73+
default:
74+
throw std::runtime_error("Cannot view value as u8: incompatible data type");
75+
}
76+
}
77+
78+
uint16_t
79+
ValueView::as_u16() const
80+
{
81+
switch (m_data_type) {
82+
case DataType::Unsigned8:
83+
return m_value.u8;
84+
case DataType::Unsigned16:
85+
return m_value.u16;
86+
default:
87+
throw std::runtime_error("Cannot view value as u16: incompatible data type");
88+
}
89+
}
90+
91+
uint32_t
92+
ValueView::as_u32() const
93+
{
94+
switch (m_data_type) {
95+
case DataType::Unsigned8:
96+
return m_value.u8;
97+
case DataType::Unsigned16:
98+
return m_value.u16;
99+
case DataType::Unsigned32:
100+
return m_value.u32;
101+
default:
102+
throw std::runtime_error("Cannot view value as u32: incompatible data type");
103+
}
104+
}
105+
106+
uint64_t
107+
ValueView::as_u64() const
108+
{
109+
switch (m_data_type) {
110+
case DataType::Unsigned8:
111+
return m_value.u8;
112+
case DataType::Unsigned16:
113+
return m_value.u16;
114+
case DataType::Unsigned32:
115+
return m_value.u32;
116+
case DataType::Unsigned64:
117+
return m_value.u64;
118+
default:
119+
throw std::runtime_error("Cannot view value as u64: incompatible data type");
120+
}
121+
}
122+
123+
int8_t
124+
ValueView::as_i8() const
125+
{
126+
switch (m_data_type) {
127+
case DataType::Signed8:
128+
return m_value.i8;
129+
default:
130+
throw std::runtime_error("Cannot view value as i8: incompatible data type");
131+
}
132+
}
133+
134+
int16_t
135+
ValueView::as_i16() const
136+
{
137+
switch (m_data_type) {
138+
case DataType::Signed8:
139+
return m_value.i8;
140+
case DataType::Signed16:
141+
return m_value.i16;
142+
default:
143+
throw std::runtime_error("Cannot view value as i16: incompatible data type");
144+
}
145+
}
146+
147+
int32_t
148+
ValueView::as_i32() const
149+
{
150+
switch (m_data_type) {
151+
case DataType::Signed8:
152+
return m_value.i8;
153+
case DataType::Signed16:
154+
return m_value.i16;
155+
case DataType::Signed32:
156+
return m_value.i32;
157+
default:
158+
throw std::runtime_error("Cannot view value as i32: incompatible data type");
159+
}
160+
}
161+
162+
int64_t
163+
ValueView::as_i64() const
164+
{
165+
switch (m_data_type) {
166+
case DataType::Signed8:
167+
return m_value.i8;
168+
case DataType::Signed16:
169+
return m_value.i16;
170+
case DataType::Signed32:
171+
return m_value.i32;
172+
case DataType::Signed64:
173+
return m_value.i64;
174+
default:
175+
throw std::runtime_error("Cannot view value as i64: incompatible data type");
176+
}
177+
}
178+
179+
IPAddr
180+
ValueView::as_ip()
181+
{
182+
switch (m_data_type) {
183+
case DataType::IPv4Address:
184+
return IPAddr::ip4(m_value.ipv4);
185+
case DataType::IPv6Address:
186+
return IPAddr::ip6(m_value.ipv6);
187+
case DataType::IPAddress:
188+
return m_value.ip;
189+
default:
190+
throw std::runtime_error("Cannot view value as ip: incompatible data type");
191+
}
192+
}
193+
194+
} // namespace aggregator
195+
} // namespace fdsdump
196+
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/**
2+
* @file
3+
* @author Michal Sedlak <[email protected]>
4+
* @brief Aggregator field value representation
5+
*
6+
* Copyright: (C) 2024 CESNET, z.s.p.o.
7+
* SPDX-License-Identifier: BSD-3-Clause
8+
*/
9+
10+
#pragma once
11+
12+
#include <common/ipaddr.hpp>
13+
14+
#include <cstdint>
15+
#include <cstring>
16+
#include <string>
17+
18+
namespace fdsdump {
19+
namespace aggregator {
20+
21+
/** @brief The possible data types a view value can be. */
22+
enum class DataType {
23+
Unassigned,
24+
IPAddress,
25+
IPv4Address,
26+
IPv6Address,
27+
MacAddress,
28+
Unsigned8,
29+
Signed8,
30+
Unsigned16,
31+
Signed16,
32+
Unsigned32,
33+
Signed32,
34+
Unsigned64,
35+
Signed64,
36+
DateTime,
37+
String128B,
38+
Octets128B,
39+
};
40+
41+
/**
42+
* @brief Get a string representation of the enum value
43+
*/
44+
std::string
45+
data_type_to_str(DataType data_type);
46+
47+
/** @brief The possible view value forms. */
48+
union Value {
49+
IPAddr ip;
50+
uint8_t ipv4[4];
51+
uint8_t ipv6[16];
52+
uint8_t mac[6];
53+
uint8_t u8;
54+
uint16_t u16;
55+
uint32_t u32;
56+
uint64_t u64;
57+
uint64_t ts_millisecs;
58+
int8_t i8;
59+
int16_t i16;
60+
int32_t i32;
61+
int64_t i64;
62+
char str[128];
63+
};
64+
65+
/**
66+
* @brief A view providing accessors to the value of the Value union
67+
*/
68+
class ValueView {
69+
public:
70+
/**
71+
* @brief Construct a view of the aggregator value
72+
*
73+
* @param data_type The data type of the value
74+
* @param value Reference to the value
75+
*/
76+
ValueView(DataType data_type, Value &value);
77+
78+
/**
79+
* @brief Retrieve a reference to the viewed value
80+
*/
81+
Value &value() const { return m_value; }
82+
83+
/**
84+
* @brief View the value as an unsigned 8-bit integer
85+
*/
86+
uint8_t
87+
as_u8() const;
88+
89+
/**
90+
* @brief View the value as an unsigned 16-bit integer
91+
*/
92+
uint16_t
93+
as_u16() const;
94+
95+
/**
96+
* @brief View the value as an unsigned 32-bit integer
97+
*/
98+
uint32_t
99+
as_u32() const;
100+
101+
/**
102+
* @brief View the value as an unsigned 64-bit integer
103+
*/
104+
uint64_t
105+
as_u64() const;
106+
107+
/**
108+
* @brief View the value as a signed 8-bit integer
109+
*/
110+
int8_t
111+
as_i8() const;
112+
113+
/**
114+
* @brief View the value as a signed 16-bit integer
115+
*/
116+
int16_t
117+
as_i16() const;
118+
119+
/**
120+
* @brief View the value as a signed 32-bit integer
121+
*/
122+
int32_t
123+
as_i32() const;
124+
125+
/**
126+
* @brief View the value as a signed 64-bit integer
127+
*/
128+
int64_t
129+
as_i64() const;
130+
131+
/**
132+
* @brief View the value as an IP address
133+
*/
134+
IPAddr
135+
as_ip();
136+
137+
private:
138+
DataType m_data_type;
139+
Value &m_value;
140+
};
141+
142+
} // aggregator
143+
} // fdsdump

0 commit comments

Comments
 (0)