Skip to content

Commit 2853682

Browse files
author
Pavel Siska
committed
WIP: Updated API
1 parent 53abf93 commit 2853682

32 files changed

+2847
-0
lines changed

process-new-api/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CXX := g++
2+
CXXFLAGS := -std=c++20 -Wall -Wextra -pedantic -Wunused -Wconversion -Wsign-conversion -g -fsanitize=address
3+
INCLUDES := -I/usr/include
4+
LDFLAGS := -lboost_system -lboost_filesystem
5+
6+
TARGET := ipx
7+
SRC := main.cpp fieldManager.cpp icmp.cpp
8+
9+
.PHONY: all clean
10+
11+
all: $(TARGET)
12+
13+
$(TARGET): $(SRC)
14+
$(CXX) $(CXXFLAGS) $(INCLUDES) $^ -o $@ $(LDFLAGS)
15+
16+
clean:
17+
rm -f $(TARGET)

process-new-api/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ICMP Plugin
2+
3+
## Overview
4+
5+
The ICMP plugin extracts ICMP-specific information from IPv4 and IPv6 packets.
6+
7+
## Fields
8+
9+
| Field Name | Data Type | Description |
10+
|-----------------|-----------|----------------------------------------|
11+
| ICMP_TYPE | uint8_t | ICMP type from the packet |
12+
| ICMP_CODE | uint8_t | ICMP code from the packet |
13+
| ICMP_TYPE_CODE | uint16_t | Combined type and code (type \| code) |

process-new-api/fieldAccessor.hpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* @file
3+
* @author Pavel Siska <[email protected]>
4+
* @brief Accessor templates for scalar and vector fields with type-safe getters.
5+
*
6+
* This header defines the `ScalarAccessor` and `VectorAccessor` templates, which provide
7+
* a type-safe and performant way to access scalar and vector field values from external
8+
* data structures using function pointers.
9+
*
10+
* These accessors encapsulate a getter function pointer to retrieve the value from
11+
* a void pointer to the external data, ensuring compile-time type safety via concepts.
12+
*
13+
* @copyright Copyright (c) 2025 CESNET, z.s.p.o.
14+
*/
15+
16+
#pragma once
17+
18+
#include "fieldTypeConcepts.hpp"
19+
20+
#include <cassert>
21+
#include <span>
22+
23+
namespace ipxp {
24+
25+
/**
26+
* @brief Accessor for scalar values.
27+
*
28+
* Provides a mechanism to access a scalar value of type `T` from an external data
29+
* structure via a getter function pointer.
30+
*
31+
* @tparam T Type of scalar value (must satisfy `FlowDataTypeScalar ` concept).
32+
*/
33+
template<FlowDataTypeScalar T>
34+
class ScalarAccessor {
35+
public:
36+
/// Function pointer type returning a scalar value of type T from a data pointer.
37+
using GetterFunction = T (*)(const void*);
38+
39+
/**
40+
* @brief Constructs the ScalarAccessor with a getter function.
41+
*
42+
* @param getterFunction Function pointer used to extract the scalar value.
43+
*
44+
* @note The getter function pointer must not be null.
45+
*/
46+
explicit constexpr ScalarAccessor(GetterFunction getterFunction)
47+
: m_getterFunction(getterFunction)
48+
{
49+
assert(m_getterFunction && "ScalarAccessor: getter function must not be nullptr");
50+
}
51+
52+
/**
53+
* @brief Retrieves the scalar value by invoking the getter function.
54+
*
55+
* @param data Pointer to the external data structure.
56+
* @return Extracted scalar value of type T.
57+
*/
58+
[[nodiscard]] T operator()(const void* data) const { return m_getterFunction(data); }
59+
60+
ScalarAccessor() = delete;
61+
ScalarAccessor(const ScalarAccessor&) = default;
62+
ScalarAccessor& operator=(const ScalarAccessor&) = default;
63+
ScalarAccessor(ScalarAccessor&&) = default;
64+
ScalarAccessor& operator=(ScalarAccessor&&) = default;
65+
66+
private:
67+
const GetterFunction m_getterFunction;
68+
};
69+
70+
/**
71+
* @brief Accessor for vector values.
72+
*
73+
* Provides a mechanism to access a span of values of type `T` from an external data
74+
* structure via a getter function pointer.
75+
*
76+
* @tparam T Type of vector element (must satisfy `FlowDataTypeVector` concept).
77+
*/
78+
template<FlowDataTypeVector T>
79+
class VectorAccessor {
80+
public:
81+
/// Function pointer type returning a span of constant values of type T from a data pointer.
82+
using GetterFunction = std::span<const T> (*)(const void*);
83+
84+
/**
85+
* @brief Constructs the VectorAccessor with a getter function.
86+
*
87+
* @param getterFunction Function pointer used to extract the vector span.
88+
*
89+
* @note The getter function pointer must not be null.
90+
*/
91+
explicit constexpr VectorAccessor(GetterFunction getterFunction)
92+
: m_getterFunction(getterFunction)
93+
{
94+
assert(m_getterFunction && "VectorAccessor: getter function must not be nullptr");
95+
}
96+
97+
/**
98+
* @brief Retrieves the vector span by invoking the getter function.
99+
*
100+
* @param data Pointer to the external data structure.
101+
* @return Extracted span of constant values of type T.
102+
*/
103+
[[nodiscard]] std::span<const T> operator()(const void* data) const
104+
{
105+
return m_getterFunction(data);
106+
}
107+
108+
VectorAccessor() = delete;
109+
VectorAccessor(const VectorAccessor&) = default;
110+
VectorAccessor& operator=(const VectorAccessor&) = default;
111+
VectorAccessor(VectorAccessor&&) = default;
112+
VectorAccessor& operator=(VectorAccessor&&) = default;
113+
114+
private:
115+
const GetterFunction m_getterFunction;
116+
};
117+
118+
} // namespace ipxp
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @file
3+
* @author Pavel Siska <[email protected]>
4+
* @brief Provides the FieldDescriptor class for accessing field metadata and value getters.
5+
*
6+
* This class encapsulates information about a field in a FlowRecord, including its
7+
* name, group, bit index, and a generic value accessor. It allows checking if the
8+
* field is present in a specific FlowRecord. Construction is restricted to FieldManager.
9+
*
10+
* @copyright Copyright (c) 2025 CESNET, z.s.p.o.
11+
*/
12+
13+
#pragma once
14+
15+
#include "fieldGenericValueGetter.hpp"
16+
#include "fieldInfo.hpp"
17+
#include "flowRecord.hpp"
18+
19+
#include <cstdint>
20+
#include <string>
21+
#include <string_view>
22+
23+
namespace ipxp {
24+
25+
/**
26+
* @class FieldDescriptor
27+
* @brief Read-only access to field metadata and value getters.
28+
*
29+
* FieldDescriptor represents a single field within a FlowRecord. It provides
30+
* information about the field's name, logical group, bit index, and a generic
31+
* value accessor. It allows checking if the field is present in a specific
32+
* FlowRecord using the `isInRecord()` method.
33+
*
34+
* Instances are immutable and can only be created by FieldManager, ensuring
35+
* controlled registration and consistent state of fields.
36+
*/
37+
class FieldDescriptor {
38+
public:
39+
/**
40+
* @brief Returns the name of the field.
41+
* @return Field name as string_view.
42+
*/
43+
[[nodiscard]]
44+
std::string_view getName() const noexcept
45+
{
46+
return m_fieldInfo.name;
47+
}
48+
49+
/**
50+
* @brief Returns the logical group name of the field.
51+
* @return Group name (e.g., "tcp", "http").
52+
*/
53+
[[nodiscard]]
54+
std::string_view getGroup() const noexcept
55+
{
56+
return m_fieldInfo.group;
57+
}
58+
59+
/**
60+
* @brief Returns the generic value accessor for the field.
61+
* @return Reference to GenericValueGetter variant.
62+
*/
63+
[[nodiscard]]
64+
const GenericValueGetter& getValueGetter() const noexcept
65+
{
66+
return m_fieldInfo.getter;
67+
}
68+
69+
/**
70+
* @brief Returns the bit index used to check field presence in FlowRecord.
71+
* @return Bit index as size_t.
72+
*/
73+
[[nodiscard]]
74+
std::size_t getBitIndex() const noexcept
75+
{
76+
return m_fieldInfo.bitIndex;
77+
}
78+
79+
/**
80+
* @brief Checks whether this field is present in a given FlowRecord.
81+
* @param record Flow record to query.
82+
* @return True if the field is available in the record.
83+
*/
84+
[[nodiscard]]
85+
bool isInRecord(const FlowRecord& record) const
86+
{
87+
return record.fieldsAvailable.test(m_fieldInfo.bitIndex);
88+
}
89+
90+
private:
91+
// FieldDescriptor can only be constructed by FieldManager
92+
friend class FieldManager;
93+
94+
explicit FieldDescriptor(FieldInfo fieldInfo)
95+
: m_fieldInfo(std::move(fieldInfo))
96+
{
97+
}
98+
99+
const FieldInfo m_fieldInfo;
100+
};
101+
102+
} // namespace ipxp
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @file
3+
* @author Pavel Siska <[email protected]>
4+
* @brief Type-erased variants for scalar and vector field accessors.
5+
*
6+
* Defines `ScalarValueGetter`, `VectorValueGetter`, and `GenericValueGetter` as `std::variant`
7+
* types that hold accessors for all supported scalar and vector field types.
8+
*
9+
* These variants allow uniform storage and usage of heterogeneous field accessors
10+
* without template overhead, enabling runtime polymorphism without virtual functions.
11+
*
12+
* @see ScalarAccessor
13+
* @see VectorAccessor
14+
*
15+
* @copyright Copyright (c) 2025 CESNET, z.s.p.o.
16+
*/
17+
18+
#pragma once
19+
20+
#include "fieldAccessor.hpp"
21+
#include "fieldSupportedTypes.hpp"
22+
#include "typeUtils.hpp"
23+
24+
namespace ipxp {
25+
26+
/**
27+
* @brief Variant holding any scalar field accessor for supported scalar types.
28+
*/
29+
using ScalarValueGetter = detail::variant_of_accessors_t<ScalarAccessor, SupportedScalarTypes>;
30+
31+
/**
32+
* @brief Variant holding any vector field accessor for supported vector types.
33+
*/
34+
using VectorValueGetter = detail::variant_of_accessors_t<VectorAccessor, SupportedVectorTypes>;
35+
36+
/**
37+
* @brief Variant holding either a scalar or a vector field accessor.
38+
*
39+
* Allows storing any supported accessor type in a single variable,
40+
* simplifying generic code paths.
41+
*/
42+
using GenericValueGetter = std::variant<ScalarValueGetter, VectorValueGetter>;
43+
44+
} // namespace ipxp

process-new-api/fieldHandler.hpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @file
3+
* @author Pavel Siska <[email protected]>
4+
* @brief Handle representing a single field within a FlowRecord.
5+
*
6+
* This file defines the FieldHandler class which provides an interface
7+
* for marking a field as available or unavailable in a FlowRecord and
8+
* querying its presence status.
9+
*
10+
* @copyright Copyright (c) 2025 CESNET, z.s.p.o.
11+
*/
12+
13+
#pragma once
14+
15+
#include "flowRecord.hpp"
16+
17+
#include <cstdint>
18+
19+
namespace ipxp {
20+
21+
/**
22+
* @brief Represents a handle to a single field within a FlowRecord.
23+
*
24+
* FieldHandler allows checking whether a field is present in a record
25+
* and provides methods to set or clear its presence flag in `FlowRecord::fieldsAvailable`.
26+
*/
27+
class FieldHandler {
28+
public:
29+
/**
30+
* @brief Sets the associated bit in the FlowRecord to indicate field availability.
31+
*
32+
* This method modifies the mutable member `fieldsAvailable` of FlowRecord even if
33+
* the FlowRecord instance is const. This is safe and intentional, as the presence
34+
* information is considered logically mutable.
35+
*
36+
* @param record Reference to FlowRecord (can be const, modifies only mutable member).
37+
*/
38+
void setAsAvailable(const FlowRecord& record) const { record.fieldsAvailable.set(m_bitIndex); }
39+
40+
/**
41+
* @brief Clears the associated bit in the FlowRecord to indicate field unavailability.
42+
*
43+
* This method modifies the mutable member `fieldsAvailable` of FlowRecord even if
44+
* the FlowRecord instance is const. This is safe and intentional, as the presence
45+
* information is considered logically mutable.
46+
*
47+
* @param record Reference to FlowRecord (can be const, modifies only mutable member).
48+
*/
49+
void setAsUnavailable(const FlowRecord& record) const
50+
{
51+
record.fieldsAvailable.reset(m_bitIndex);
52+
}
53+
54+
/**
55+
* @brief Returns the availability status of the field in the given FlowRecord.
56+
*/
57+
[[nodiscard]]
58+
bool getStatus(const FlowRecord& record) const
59+
{
60+
return record.fieldsAvailable.test(m_bitIndex);
61+
}
62+
63+
private:
64+
friend class FieldManager;
65+
66+
/// Constructor used only by FieldManager to create a valid handler.
67+
explicit constexpr FieldHandler(std::size_t bitIndex) noexcept
68+
: m_bitIndex(bitIndex)
69+
{
70+
}
71+
72+
const std::size_t m_bitIndex;
73+
};
74+
75+
} // namespace ipxp

0 commit comments

Comments
 (0)