|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Michal Sedlak <[email protected]> |
| 4 | + * @brief In/out fields |
| 5 | + * |
| 6 | + * Copyright: (C) 2024 CESNET, z.s.p.o. |
| 7 | + * SPDX-License-Identifier: BSD-3-Clause |
| 8 | + */ |
| 9 | + |
| 10 | +#include <aggregator/extraFields.hpp> |
| 11 | +#include <aggregator/inOutField.hpp> |
| 12 | +#include <aggregator/viewFactory.hpp> |
| 13 | + |
| 14 | +namespace fdsdump { |
| 15 | +namespace aggregator { |
| 16 | + |
| 17 | +InOutKeyField::InOutKeyField( |
| 18 | + std::unique_ptr<Field> in_field, std::unique_ptr<Field> out_field) : |
| 19 | + m_in_field(std::move(in_field)), |
| 20 | + m_out_field(std::move(out_field)) |
| 21 | +{ |
| 22 | + if (m_in_field->data_type() != m_out_field->data_type()) { |
| 23 | + throw std::invalid_argument("in and out field not of same type"); |
| 24 | + } |
| 25 | + |
| 26 | + set_data_type(m_in_field->data_type()); |
| 27 | + set_name(m_in_field->name()); |
| 28 | +} |
| 29 | + |
| 30 | +bool |
| 31 | +InOutKeyField::load(FlowContext &ctx, Value &value) const |
| 32 | +{ |
| 33 | + switch (ctx.view_dir) { |
| 34 | + case ViewDirection::In: |
| 35 | + return m_in_field->load(ctx, value); |
| 36 | + case ViewDirection::Out: |
| 37 | + return m_out_field->load(ctx, value); |
| 38 | + case ViewDirection::None: |
| 39 | + assert(0); |
| 40 | + } |
| 41 | + return false; |
| 42 | +} |
| 43 | + |
| 44 | +std::string |
| 45 | +InOutKeyField::repr() const |
| 46 | +{ |
| 47 | + return std::string("InOutKeyField(") + |
| 48 | + + "name=" + name() + ", data_type=" + data_type_to_str(data_type()) |
| 49 | + + ", size=" + std::to_string(size()) + ", offset=" + std::to_string(offset()) |
| 50 | + + ", in=" + m_in_field->repr() + ", out=" + m_out_field->repr() + ")"; |
| 51 | +} |
| 52 | + |
| 53 | +bool |
| 54 | +InOutKeyField::operator==(const InOutKeyField &other) const |
| 55 | +{ |
| 56 | + return *m_in_field.get() == *other.m_in_field.get() |
| 57 | + && *m_out_field.get() == *other.m_out_field.get(); |
| 58 | +} |
| 59 | + |
| 60 | +bool |
| 61 | +InOutKeyField::operator==(const Field &other) const |
| 62 | +{ |
| 63 | + if (const auto *other_inout = dynamic_cast<const InOutKeyField *>(&other)) { |
| 64 | + return *this == *other_inout; |
| 65 | + } |
| 66 | + return false; |
| 67 | +} |
| 68 | + |
| 69 | +std::unique_ptr<Field> |
| 70 | +InOutKeyField::create_from(const Field &field) |
| 71 | +{ |
| 72 | + std::unique_ptr<Field> new_field; |
| 73 | + |
| 74 | + if (field == ViewFactory::create_key_field("ip")) { |
| 75 | + new_field.reset(new InOutKeyField( |
| 76 | + ViewFactory::create_key_field("dstip"), |
| 77 | + ViewFactory::create_key_field("srcip"))); |
| 78 | + |
| 79 | + } else if (field == ViewFactory::create_key_field("ip4")) { |
| 80 | + new_field.reset(new InOutKeyField( |
| 81 | + ViewFactory::create_key_field("iana:destinationIPv4Address"), |
| 82 | + ViewFactory::create_key_field("iana:sourceIPv4Address"))); |
| 83 | + |
| 84 | + } else if (field == ViewFactory::create_key_field("ip6")) { |
| 85 | + new_field.reset(new InOutKeyField( |
| 86 | + ViewFactory::create_key_field("iana:destinationIPv6Address"), |
| 87 | + ViewFactory::create_key_field("iana:sourceIPv6Address"))); |
| 88 | + |
| 89 | + } else if (field == ViewFactory::create_key_field("port")) { |
| 90 | + new_field.reset(new InOutKeyField( |
| 91 | + ViewFactory::create_key_field("dstport"), |
| 92 | + ViewFactory::create_key_field("srcport"))); |
| 93 | + |
| 94 | + } else if (const auto *subnet_field = dynamic_cast<const SubnetField *>(&field)) { |
| 95 | + auto inner = InOutKeyField::create_from(*subnet_field->m_source_field.get()); |
| 96 | + new_field.reset(new SubnetField(std::move(inner), subnet_field->m_prefix_len)); |
| 97 | + } |
| 98 | + |
| 99 | + return new_field; |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +InOutValueField::InOutValueField(std::unique_ptr<Field> field, ViewDirection dir) : |
| 104 | + m_field(std::move(field)), |
| 105 | + m_dir(dir) |
| 106 | +{ |
| 107 | + assert(m_dir == ViewDirection::In || m_dir == ViewDirection::Out); |
| 108 | + set_data_type(m_field->data_type()); |
| 109 | + set_name((m_dir == ViewDirection::In ? "in " : "out ") + m_field->name()); |
| 110 | +} |
| 111 | + |
| 112 | +void |
| 113 | +InOutValueField::init(Value &value) const |
| 114 | +{ |
| 115 | + m_field->init(value); |
| 116 | +} |
| 117 | + |
| 118 | +bool |
| 119 | +InOutValueField::aggregate(FlowContext &ctx, Value &aggregated_value) const |
| 120 | +{ |
| 121 | + if (m_dir == ctx.view_dir) { |
| 122 | + return m_field->aggregate(ctx, aggregated_value); |
| 123 | + } |
| 124 | + return true; |
| 125 | +} |
| 126 | + |
| 127 | +void |
| 128 | +InOutValueField::merge(Value &value, const Value &other) const |
| 129 | +{ |
| 130 | + m_field->merge(value, other); |
| 131 | +} |
| 132 | + |
| 133 | +std::string |
| 134 | +InOutValueField::repr() const |
| 135 | +{ |
| 136 | + std::string dir; |
| 137 | + if (m_dir == ViewDirection::None) { |
| 138 | + dir = "None"; |
| 139 | + } else if (m_dir == ViewDirection::In) { |
| 140 | + dir = "In"; |
| 141 | + } else if (m_dir == ViewDirection::Out) { |
| 142 | + dir = "Out"; |
| 143 | + } |
| 144 | + return std::string("InOutValueField(") + |
| 145 | + + "name=" + name() + ", data_type=" + data_type_to_str(data_type()) |
| 146 | + + ", size=" + std::to_string(size()) + ", offset=" + std::to_string(offset()) |
| 147 | + + ", dir=" + dir |
| 148 | + + ", field=" + m_field->repr() + ")"; |
| 149 | +} |
| 150 | + |
| 151 | +bool |
| 152 | +InOutValueField::operator==(const InOutValueField &other) const |
| 153 | +{ |
| 154 | + return *m_field.get() == *other.m_field.get(); |
| 155 | +} |
| 156 | + |
| 157 | +bool |
| 158 | +InOutValueField::operator==(const Field &other) const |
| 159 | +{ |
| 160 | + if (const auto *other_inout = dynamic_cast<const InOutValueField *>(&other)) { |
| 161 | + return *this == *other_inout; |
| 162 | + } |
| 163 | + return false; |
| 164 | +} |
| 165 | + |
| 166 | +} // aggregator |
| 167 | +} // fdsdump |
0 commit comments