|
| 1 | +#include "fieldSchema.hpp" |
| 2 | +#include "icmp.hpp" |
| 3 | + |
| 4 | +#include <functional> |
| 5 | + |
| 6 | +#include <netinet/icmp6.h> |
| 7 | +#include <netinet/ip_icmp.h> |
| 8 | + |
| 9 | +namespace ipxp { |
| 10 | + |
| 11 | +using AddHandler = std::function<void(const FieldHandler&, IcmpFields)>; |
| 12 | + |
| 13 | +static void registerIcmpTypeField(FieldSchema& schema, AddHandler addHandler) |
| 14 | +{ |
| 15 | + const auto getterFunction = [](const void* context) { |
| 16 | + return reinterpret_cast<const IcmpPluginData*>(context)->type; |
| 17 | + }; |
| 18 | + |
| 19 | + const FieldHandler icmpTypeHandler = schema.addScalarField("ICMP_TYPE", getterFunction); |
| 20 | + |
| 21 | + addHandler(icmpTypeHandler, IcmpFields::ICMP_TYPE); |
| 22 | +} |
| 23 | + |
| 24 | +static void registerIcmpCodeField(FieldSchema& schema, AddHandler addHandler) |
| 25 | +{ |
| 26 | + const auto getterFunction = [](const void* context) { |
| 27 | + return reinterpret_cast<const IcmpPluginData*>(context)->code; |
| 28 | + }; |
| 29 | + |
| 30 | + const FieldHandler icmpCodeHandler = schema.addScalarField("ICMP_CODE", getterFunction); |
| 31 | + |
| 32 | + addHandler(icmpCodeHandler, IcmpFields::ICMP_CODE); |
| 33 | +} |
| 34 | + |
| 35 | +static void registerIcmpTypeCodeField(FieldSchema& schema, AddHandler addHandler) |
| 36 | +{ |
| 37 | + const auto getterFunction = [](const void* context) { |
| 38 | + const auto* icmpPluginData = reinterpret_cast<const IcmpPluginData*>(context); |
| 39 | + return static_cast<uint16_t>(icmpPluginData->type) << 8 | icmpPluginData->code; |
| 40 | + }; |
| 41 | + |
| 42 | + const FieldHandler icmpTypeCodeHandler |
| 43 | + = schema.addScalarField("ICMP_TYPE_CODE", getterFunction); |
| 44 | + |
| 45 | + addHandler(icmpTypeCodeHandler, IcmpFields::ICMP_TYPE_CODE); |
| 46 | +} |
| 47 | + |
| 48 | +inline AddHandler createAddHandlerFunction(FieldHandlers<IcmpFields>& handlers) |
| 49 | +{ |
| 50 | + return [&handlers](const FieldHandler& handler, IcmpFields field) { |
| 51 | + handlers.insert(field, handler); |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +void createFieldSchema(FieldManager& fieldManager, FieldHandlers<IcmpFields>& handlers) |
| 56 | +{ |
| 57 | + FieldSchema fieldSchema = fieldManager.createFieldSchema("icmp"); |
| 58 | + |
| 59 | + const auto addHandlerFunction = createAddHandlerFunction(handlers); |
| 60 | + |
| 61 | + registerIcmpTypeField(fieldSchema, addHandlerFunction); |
| 62 | + registerIcmpCodeField(fieldSchema, addHandlerFunction); |
| 63 | + registerIcmpTypeCodeField(fieldSchema, addHandlerFunction); |
| 64 | +} |
| 65 | + |
| 66 | +IcmpPlugin::IcmpPlugin(const std::string& parameters, FieldManager& fieldManager) |
| 67 | +{ |
| 68 | + (void) parameters; |
| 69 | + |
| 70 | + createFieldSchema(fieldManager, m_fieldHandlers); |
| 71 | +} |
| 72 | + |
| 73 | +template<typename T> |
| 74 | +static void parseTypeCode(const Packet& packet, IcmpPluginData& pluginData) |
| 75 | +{ |
| 76 | + const auto* icmpHeader = reinterpret_cast<const T*>(packet.payload); |
| 77 | + pluginData.type = icmpHeader->type; |
| 78 | + pluginData.code = icmpHeader->code; |
| 79 | +} |
| 80 | + |
| 81 | +void IcmpPlugin::parseIcmp( |
| 82 | + const FlowRecord& flowRecord, |
| 83 | + const Packet& packet, |
| 84 | + IcmpPluginData& pluginData) |
| 85 | +{ |
| 86 | + // if (packet.ipVersion == IPV4) { |
| 87 | + // parseTypeCode<icmphdr>(packet, pluginData); |
| 88 | + // } else { |
| 89 | + // parseTypeCode<icmp6_hdr>(packet, pluginData); |
| 90 | + // } |
| 91 | + |
| 92 | + m_fieldHandlers[IcmpFields::ICMP_TYPE].setAsAvailable(flowRecord); |
| 93 | + m_fieldHandlers[IcmpFields::ICMP_CODE].setAsAvailable(flowRecord); |
| 94 | + m_fieldHandlers[IcmpFields::ICMP_TYPE_CODE].setAsAvailable(flowRecord); |
| 95 | +} |
| 96 | + |
| 97 | +static bool isIcmpPacket(const Packet& packet) noexcept |
| 98 | +{ |
| 99 | + /* |
| 100 | + if (packet.l4Protocol != ICMP_V4 && packet.l4Protocol != ICMP_V6) { |
| 101 | + return false; |
| 102 | + } |
| 103 | +
|
| 104 | + // TODO porovnat s velikosti cele ICMP header? |
| 105 | + constexpr std::size_t MIN_PAYLOAD_LENGTH = 16; |
| 106 | + if (packet.payload_len < MIN_PAYLOAD_LENGTH) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + */ |
| 110 | + |
| 111 | + return true; |
| 112 | +} |
| 113 | + |
| 114 | +PluginInitResult IcmpPlugin::onInit(const FlowContext& flowContext, void* pluginContext) |
| 115 | +{ |
| 116 | + assert(pluginContext != nullptr && "Icmp: Plugin context must not be null"); |
| 117 | + |
| 118 | + if (!isIcmpPacket(flowContext.packet)) { |
| 119 | + return { |
| 120 | + .constructionState = ConstructionState::NotConstructed, |
| 121 | + .updateRequirement = UpdateRequirement::NoUpdateNeeded, |
| 122 | + .flowAction = FlowAction::NoAction, |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + IcmpPluginData* pluginData |
| 127 | + = std::construct_at(reinterpret_cast<IcmpPluginData*>(pluginContext)); |
| 128 | + |
| 129 | + parseIcmp(flowContext.flowRecord, flowContext.packet, *pluginData); |
| 130 | + |
| 131 | + return { |
| 132 | + .constructionState = ConstructionState::Constructed, |
| 133 | + .updateRequirement = UpdateRequirement::NoUpdateNeeded, |
| 134 | + .flowAction = FlowAction::NoAction, |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +void IcmpPlugin::onDestroy(void* pluginContext) |
| 139 | +{ |
| 140 | + assert(pluginContext != nullptr && "Icmp: Plugin context must not be null"); |
| 141 | + |
| 142 | + std::destroy_at(reinterpret_cast<IcmpPluginData*>(pluginContext)); |
| 143 | +} |
| 144 | + |
| 145 | +PluginDataMemoryLayout IcmpPlugin::getDataMemoryLayout() const noexcept |
| 146 | +{ |
| 147 | + return { |
| 148 | + .size = sizeof(IcmpPluginData), |
| 149 | + .alignment = alignof(IcmpPluginData), |
| 150 | + }; |
| 151 | +} |
| 152 | + |
| 153 | +} // namespace ipxp |
0 commit comments