|
3 | 3 | * @brief Plugin for parsing basicplus traffic. |
4 | 4 | * @author Jakub Antonín Štigler xstigl00@[email protected] |
5 | 5 | * @author Pavel Siska <[email protected]> |
| 6 | + * @author Damir Zainullin <[email protected]> |
6 | 7 | * @date 2025 |
7 | 8 | * |
8 | | - * Copyright (c) 2025 CESNET |
| 9 | + * Provides a plugin that parses VLAN traffic, |
| 10 | + * stores it in per-flow plugin data, and exposes that field via FieldManager. |
9 | 11 | * |
10 | | - * SPDX-License-Identifier: BSD-3-Clause |
| 12 | + * @copyright Copyright (c) 2025 CESNET, z.s.p.o. |
11 | 13 | */ |
12 | 14 |
|
13 | 15 | #pragma once |
14 | 16 |
|
15 | | -#include <cstring> |
| 17 | +#include "vlanFields.hpp" |
16 | 18 |
|
17 | | -#ifdef WITH_NEMEA |
18 | | -#include "fields.h" |
19 | | -#endif |
20 | | - |
21 | | -#include <cstdint> |
22 | 19 | #include <sstream> |
23 | 20 | #include <string> |
24 | 21 |
|
25 | | -#include <ipfixprobe/flowifc.hpp> |
26 | | -#include <ipfixprobe/ipfix-elements.hpp> |
27 | | -#include <ipfixprobe/packet.hpp> |
28 | | -#include <ipfixprobe/processPlugin.hpp> |
29 | | - |
30 | | -namespace ipxp { |
31 | | - |
32 | | -#define VLAN_UNIREC_TEMPLATE "VLAN_ID" |
| 22 | +#include <fieldHandlersEnum.hpp> |
| 23 | +#include <fieldManager.hpp> |
| 24 | +#include <processPlugin.hpp> |
33 | 25 |
|
34 | | -UR_FIELDS(uint16 VLAN_ID) |
| 26 | +namespace ipxp::process::vlan { |
35 | 27 |
|
36 | 28 | /** |
37 | | - * \brief Flow record extension header for storing parsed VLAN data. |
38 | | - */ |
39 | | -struct RecordExtVLAN : public RecordExt { |
40 | | - // vlan id is in the host byte order |
41 | | - uint16_t vlan_id; |
42 | | - |
43 | | - RecordExtVLAN(int pluginID) |
44 | | - : RecordExt(pluginID) |
45 | | - , vlan_id(0) |
46 | | - { |
47 | | - } |
48 | | - |
49 | | -#ifdef WITH_NEMEA |
50 | | - virtual void fill_unirec(ur_template_t* tmplt, void* record) |
51 | | - { |
52 | | - ur_set(tmplt, record, F_VLAN_ID, vlan_id); |
53 | | - } |
54 | | - |
55 | | - const char* get_unirec_tmplt() const { return VLAN_UNIREC_TEMPLATE; } |
56 | | -#endif |
57 | | - |
58 | | - virtual int fill_ipfix(uint8_t* buffer, int size) |
59 | | - { |
60 | | - const int LEN = sizeof(vlan_id); |
61 | | - |
62 | | - if (size < LEN) { |
63 | | - return -1; |
64 | | - } |
65 | | - |
66 | | - *reinterpret_cast<uint16_t*>(buffer) = htons(vlan_id); |
67 | | - return LEN; |
68 | | - } |
69 | | - |
70 | | - const char** get_ipfix_tmplt() const |
71 | | - { |
72 | | - static const char* ipfix_template[] = {IPFIX_VLAN_TEMPLATE(IPFIX_FIELD_NAMES) NULL}; |
73 | | - return ipfix_template; |
74 | | - } |
75 | | - |
76 | | - std::string get_text() const |
77 | | - { |
78 | | - std::ostringstream out; |
79 | | - out << "vlan_id=\"" << vlan_id << '"'; |
80 | | - return out.str(); |
81 | | - } |
82 | | -}; |
83 | | - |
84 | | -/** |
85 | | - * \brief Process plugin for parsing VLAN packets. |
| 29 | + * @class VLANPlugin |
| 30 | + * @brief A plugin for parsing VLAN traffic. |
| 31 | + * |
| 32 | + * Collects and exports VLAN ID. |
86 | 33 | */ |
87 | 34 | class VLANPlugin : public ProcessPlugin { |
88 | 35 | public: |
89 | | - VLANPlugin(const std::string& params, int pluginID); |
90 | | - OptionsParser* get_parser() const { return new OptionsParser("vlan", "Parse VLAN traffic"); } |
91 | | - std::string get_name() const { return "vlan"; } |
92 | | - RecordExt* get_ext() const { return new RecordExtVLAN(m_pluginID); } |
93 | | - ProcessPlugin* copy(); |
94 | | - |
95 | | - int post_create(Flow& rec, const Packet& pkt); |
| 36 | + /** |
| 37 | + * @brief Constructs the VLAN plugin and initializes field handlers. |
| 38 | + * @param params String with plugin-specific parameters for configuration(currently unused). |
| 39 | + * @param manager Reference to the FieldManager for field handler registration. |
| 40 | + */ |
| 41 | + VLANPlugin(const std::string& params, FieldManager& manager); |
| 42 | + |
| 43 | + /** |
| 44 | + * @brief Initializes plugin data for a new flow. |
| 45 | + * |
| 46 | + * Constructs `VLANExport` in `pluginContext` and sets VLAN value. |
| 47 | + * |
| 48 | + * 0 VLAN value means no VLAN tag present. |
| 49 | + * |
| 50 | + * @param flowContext Contextual information about the flow to fill new record. |
| 51 | + * @param pluginContext Pointer to pre-allocated memory to create record. |
| 52 | + * @return Result of the initialization process. |
| 53 | + */ |
| 54 | + OnInitResult onInit(const FlowContext& flowContext, void* pluginContext) override; |
| 55 | + |
| 56 | + /** |
| 57 | + * @brief Destroys plugin data. |
| 58 | + * |
| 59 | + * Calls the destructor of `VLANContext` in `pluginContext`. |
| 60 | + * |
| 61 | + * @param pluginContext Pointer to `VLANContext`. |
| 62 | + */ |
| 63 | + void onDestroy(void* pluginContext) noexcept override; |
| 64 | + |
| 65 | + /** |
| 66 | + * @brief Provides memory layout information for `VLANContext`. |
| 67 | + * |
| 68 | + * Returns the size and alignment requirements of `VLANContext`. |
| 69 | + * |
| 70 | + * @return Memory layout details for `VLANContext`. |
| 71 | + */ |
| 72 | + PluginDataMemoryLayout getDataMemoryLayout() const noexcept override; |
| 73 | + |
| 74 | +private: |
| 75 | + FieldHandlers<VLANFields> m_fieldHandlers; |
96 | 76 | }; |
97 | 77 |
|
98 | | -} // namespace ipxp |
| 78 | +} // namespace ipxp::process::vlan |
0 commit comments