|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "fieldHandler.hpp" |
| 4 | +#include "fieldManager.hpp" |
| 5 | +#include "fieldSchema.hpp" |
| 6 | +#include "processPlugin.hpp" |
| 7 | +#include "flowRecord.hpp" |
| 8 | + |
| 9 | +enum Direction : std::size_t { Forward = 0, Reverse = 1 }; |
| 10 | + |
| 11 | +template<typename T> |
| 12 | +struct DirectionalField { |
| 13 | + T values[2]; |
| 14 | + |
| 15 | + T& operator[](Direction d) { return values[static_cast<std::size_t>(d)]; } |
| 16 | + const T& operator[](Direction d) const { return values[static_cast<std::size_t>(d)]; } |
| 17 | +}; |
| 18 | + |
| 19 | +/* |
| 20 | +struct DummyExport { |
| 21 | + uint8_t ipTtl; |
| 22 | + uint8_t ipTtlRev; |
| 23 | + uint8_t ipFlag; |
| 24 | + uint8_t ipFlagRev; |
| 25 | + uint16_t tcpWindow = 100; |
| 26 | + uint16_t tcpWindowRev; |
| 27 | + uint64_t tcpOption; |
| 28 | + uint64_t tcpOptionRev; |
| 29 | + uint32_t tcpMss; |
| 30 | + uint32_t tcpMssRev; |
| 31 | + uint16_t tcpSynSize; |
| 32 | + std::vector<uint64_t> packets; |
| 33 | + std::vector<uint64_t> packetsRev; |
| 34 | +}; |
| 35 | +*/ |
| 36 | + |
| 37 | +struct DummyExport { |
| 38 | + DirectionalField<uint8_t> ipTtl; |
| 39 | + DirectionalField<uint8_t> ipFlag; |
| 40 | + DirectionalField<uint16_t> tcpWindow; |
| 41 | + DirectionalField<uint64_t> tcpOption; |
| 42 | + DirectionalField<uint32_t> tcpMss; |
| 43 | + uint16_t tcpSynSize; |
| 44 | + DirectionalField<std::vector<uint64_t>> packets; |
| 45 | +}; |
| 46 | + |
| 47 | +enum class DummyFields : std::size_t { |
| 48 | + IP_TTL = 0, |
| 49 | + IP_TTL_REV, |
| 50 | + IP_FLG, |
| 51 | + IP_FLG_REV, |
| 52 | + TCP_WIN, |
| 53 | + TCP_WIN_REV, |
| 54 | + TCP_OPT, |
| 55 | + TCP_OPT_REV, |
| 56 | + TCP_MSS, |
| 57 | + TCP_MSS_REV, |
| 58 | + TCP_SYN_SIZE, |
| 59 | + PACKETS, |
| 60 | + PACKETS_REV, |
| 61 | + FIELDS_SIZE, |
| 62 | +}; |
| 63 | + |
| 64 | +static FieldSchema createDummySchema() |
| 65 | +{ |
| 66 | + FieldSchema schema("basicplus"); |
| 67 | + |
| 68 | + schema.addScalarField<uint8_t>( |
| 69 | + "IP_TTL", |
| 70 | + FieldDirection::Forward, |
| 71 | + offsetof(DummyExport, ipTtl.values[Direction::Forward])); |
| 72 | + schema.addScalarField<uint8_t>( |
| 73 | + "IP_TTL_REV", |
| 74 | + FieldDirection::Reverse, |
| 75 | + offsetof(DummyExport, ipTtl.values[Direction::Reverse])); |
| 76 | + schema.addScalarField<uint8_t>( |
| 77 | + "IP_FLG", |
| 78 | + FieldDirection::Forward, |
| 79 | + offsetof(DummyExport, ipFlag.values[Direction::Forward])); |
| 80 | + schema.addScalarField<uint8_t>( |
| 81 | + "IP_FLG_REV", |
| 82 | + FieldDirection::Reverse, |
| 83 | + offsetof(DummyExport, ipFlag.values[Direction::Reverse])); |
| 84 | + schema.addScalarField<uint16_t>( |
| 85 | + "TCP_WIN", |
| 86 | + FieldDirection::Forward, |
| 87 | + offsetof(DummyExport, tcpWindow.values[Direction::Forward])); |
| 88 | + schema.addScalarField<uint16_t>( |
| 89 | + "TCP_WIN_REV", |
| 90 | + FieldDirection::Reverse, |
| 91 | + offsetof(DummyExport, tcpWindow.values[Direction::Reverse])); |
| 92 | + schema.addScalarField<uint64_t>( |
| 93 | + "TCP_OPT", |
| 94 | + FieldDirection::Forward, |
| 95 | + offsetof(DummyExport, tcpOption.values[Direction::Forward])); |
| 96 | + schema.addScalarField<uint64_t>( |
| 97 | + "TCP_OPT_REV", |
| 98 | + FieldDirection::Reverse, |
| 99 | + offsetof(DummyExport, tcpOption.values[Direction::Reverse])); |
| 100 | + schema.addScalarField<uint32_t>( |
| 101 | + "TCP_MSS", |
| 102 | + FieldDirection::Forward, |
| 103 | + offsetof(DummyExport, tcpMss.values[Direction::Forward])); |
| 104 | + schema.addScalarField<uint32_t>( |
| 105 | + "TCP_MSS_REV", |
| 106 | + FieldDirection::Reverse, |
| 107 | + offsetof(DummyExport, tcpMss.values[Direction::Reverse])); |
| 108 | + schema.addScalarField<uint16_t>( |
| 109 | + "TCP_SYN_SIZE", |
| 110 | + FieldDirection::DirectionalIndifferent, |
| 111 | + offsetof(DummyExport, tcpSynSize)); |
| 112 | + |
| 113 | + schema.addVectorField<uint64_t>( |
| 114 | + "PACKETS", |
| 115 | + FieldDirection::Forward, |
| 116 | + [](const void* thisPtr) -> std::span<const uint64_t> { |
| 117 | + return reinterpret_cast<const DummyExport*>(thisPtr) |
| 118 | + ->packets.values[Direction::Forward]; |
| 119 | + }); |
| 120 | + |
| 121 | + schema.addVectorField<uint64_t>( |
| 122 | + "PACKETS_REV", |
| 123 | + FieldDirection::Forward, |
| 124 | + [](const void* thisPtr) -> std::span<const uint64_t> { |
| 125 | + return reinterpret_cast<const DummyExport*>(thisPtr) |
| 126 | + ->packets.values[Direction::Reverse]; |
| 127 | + }); |
| 128 | + |
| 129 | + schema.addBiflowPair("IP_TTL", "IP_TTL_REV"); |
| 130 | + schema.addBiflowPair("IP_FLG", "IP_FLG_REV"); |
| 131 | + schema.addBiflowPair("TCP_WIN", "TCP_WIN_REV"); |
| 132 | + schema.addBiflowPair("TCP_OPT", "TCP_OPT_REV"); |
| 133 | + schema.addBiflowPair("TCP_MSS", "TCP_MSS_REV"); |
| 134 | + schema.addBiflowPair("PACKETS", "PACKETS_REV"); |
| 135 | + |
| 136 | + return schema; |
| 137 | +} |
| 138 | + |
| 139 | +class DummyPlugin |
| 140 | + : private FieldHandlers<DummyFields> |
| 141 | + , public ProcessPlugin { |
| 142 | +public: |
| 143 | + DummyPlugin(const std::string& params, FieldManager& manager) |
| 144 | + { |
| 145 | + const FieldSchema schema = createDummySchema(); |
| 146 | + const FieldSchemaHandler schemaHandler = manager.registerSchema(schema); |
| 147 | + |
| 148 | + (void) params; |
| 149 | + |
| 150 | + m_fieldHandlers[DummyFields::TCP_WIN] = schemaHandler.getFieldHandler("TCP_WIN"); |
| 151 | + m_fieldHandlers[DummyFields::TCP_OPT] = schemaHandler.getFieldHandler("TCP_OPT"); |
| 152 | + m_fieldHandlers[DummyFields::TCP_OPT_REV] = schemaHandler.getFieldHandler("TCP_OPT_REV"); |
| 153 | + m_fieldHandlers[DummyFields::PACKETS] = schemaHandler.getFieldHandler("PACKETS"); |
| 154 | + m_fieldHandlers[DummyFields::PACKETS_REV] = schemaHandler.getFieldHandler("PACKETS_REV"); |
| 155 | + // je potreba inicializovat vsechny fieldy |
| 156 | + } |
| 157 | + |
| 158 | + FlowAction onFlowCreate(FlowRecord& flowRecord, const Packet& packet) override |
| 159 | + { |
| 160 | + (void) packet; |
| 161 | + |
| 162 | + m_exportData.tcpOption[Direction::Forward] = 156; |
| 163 | + m_exportData.tcpOption[Direction::Reverse] = 1689; |
| 164 | + |
| 165 | + // kdyz field dostane hodnotu, je potreba ho oznacit jako dostupny |
| 166 | + |
| 167 | + m_fieldHandlers[DummyFields::TCP_OPT].setAsAvailable(flowRecord); |
| 168 | + m_fieldHandlers[DummyFields::TCP_OPT_REV].setAsAvailable(flowRecord); |
| 169 | + |
| 170 | + return FlowAction::RequestNoData; |
| 171 | + } |
| 172 | + |
| 173 | + FlowAction onFlowUpdate(FlowRecord& flowRecord, const Packet& packet) override |
| 174 | + { |
| 175 | + (void) flowRecord; |
| 176 | + (void) packet; |
| 177 | + |
| 178 | + m_exportData.packets[Direction::Forward] = {1, 2, 3, 4, 5, 6}; |
| 179 | + m_fieldHandlers[DummyFields::PACKETS].setAsAvailable(flowRecord); |
| 180 | + |
| 181 | + return FlowAction::RequestNoData; |
| 182 | + } |
| 183 | + |
| 184 | + void onFlowExport() override {} |
| 185 | + |
| 186 | + const void* getExportData() const noexcept override { return &m_exportData; } |
| 187 | + |
| 188 | + ProcessPlugin* clone(std::byte* constructAtAddress) const override |
| 189 | + { |
| 190 | + return std::construct_at(reinterpret_cast<DummyPlugin*>(constructAtAddress), *this); |
| 191 | + } |
| 192 | + |
| 193 | + std::string getName() const override { return "DummyPlugin"; } |
| 194 | + |
| 195 | + ~DummyPlugin() override {} |
| 196 | + |
| 197 | + DummyPlugin(const DummyPlugin& other) = default; |
| 198 | + DummyPlugin(DummyPlugin&& other) = delete; |
| 199 | + |
| 200 | +private: |
| 201 | + DummyExport m_exportData; |
| 202 | +}; |
0 commit comments