1
0
Fork 0
mirror of https://github.com/pavel-odintsov/fastnetmon synced 2024-06-09 05:16:17 +02:00

Extracted traffic format encoding into separate static library

This commit is contained in:
Pavel Odintsov 2023-02-21 09:26:35 -08:00
parent 71096caf9c
commit 960d35166b
5 changed files with 82 additions and 70 deletions

View File

@ -532,6 +532,9 @@ if (ENABLE_GOBGP_SUPPORT)
# Build Flow Data binary
add_library(traffic_data_library STATIC traffic_data.pb.cc)
add_library(protobuf_traffic_format STATIC traffic_output_formats/protobuf/protobuf_traffic_format.cpp)
target_link_libraries(protobuf_traffic_format traffic_data_library)
# Build gRPC and protocol buffers libraries and link they to gobgp_action
add_library(gobgp_api_client_pb_cc STATIC actions/gobgp.pb.cc)
add_library(gobgp_api_client_grpc_pb_cc STATIC actions/gobgp.grpc.pb.cc)
@ -763,6 +766,8 @@ target_link_libraries(fastnetmon ipfix_rfc)
target_link_libraries(fastnetmon_logic bgp_protocol exabgp_action)
target_link_libraries(fastnetmon_logic protobuf_traffic_format)
# Link to our functions
target_link_libraries(fastnetmon fast_library)
@ -774,8 +779,6 @@ if (ENABLE_GOBGP_SUPPORT)
target_link_libraries(fastnetmon gobgp_action)
endif()
target_link_libraries(fastnetmon traffic_data_library)
target_link_libraries(fastnetmon exabgp_action)
if (ENABLE_AFPACKET_SUPPORT)

View File

@ -35,8 +35,6 @@
#include <capnp/serialize-packed.h>
#endif
#include "traffic_data.pb.h"
boost::regex regular_expression_cidr_pattern("^\\d+\\.\\d+\\.\\d+\\.\\d+\\/\\d+$");
boost::regex regular_expression_host_pattern("^\\d+\\.\\d+\\.\\d+\\.\\d+$");
@ -2362,68 +2360,3 @@ std::string country_static_string_to_dynamic_string(const boost::beast::static_s
return country_code_dynamic_string;
}
// Encode simple packet into Protobuf
bool write_simple_packet_to_protobuf(const simple_packet_t& packet, TrafficData& traffic_data) {
extern log4cpp::Category& logger;
traffic_data.set_protocol(packet.protocol);
traffic_data.set_sampling_ratio(packet.sample_ratio);
if (packet.ip_protocol_version == 4) {
traffic_data.set_source_ip(&packet.src_ip, sizeof(packet.src_ip));
traffic_data.set_destination_ip(&packet.dst_ip, sizeof(packet.dst_ip));
} else if (packet.ip_protocol_version == 6) {
traffic_data.set_source_ip(&packet.src_ipv6, sizeof(packet.src_ipv6));
traffic_data.set_destination_ip(&packet.dst_ipv6, sizeof(packet.dst_ipv6));
}
traffic_data.set_ip_version(packet.ip_protocol_version);
traffic_data.set_ttl(packet.ttl);
traffic_data.set_source_port(packet.source_port);
traffic_data.set_destination_port(packet.destination_port);
traffic_data.set_octets(packet.length);
traffic_data.set_packets(packet.number_of_packets);
traffic_data.set_tcp_flags(packet.flags);
traffic_data.set_ip_fragmented(packet.ip_fragmented);
traffic_data.set_timestamp_seconds(packet.ts.tv_sec);
traffic_data.set_timestamp_milliseconds(packet.ts.tv_usec);
traffic_data.set_octets(packet.length);
if (packet.packet_direction == INCOMING) {
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_INCOMING);
} else if (packet.packet_direction == OUTGOING) {
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_OUTGOING);
} else if (packet.packet_direction == INTERNAL) {
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_INTERNAL);
} else if (packet.packet_direction == OTHER) {
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_OTHER);
} else {
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_UNKNOWN);
}
if (packet.source == MIRROR) {
traffic_data.set_telemetry_type(TELEMETRY_TYPE_MIRROR);
} else if (packet.source == SFLOW) {
traffic_data.set_telemetry_type(TELEMETRY_TYPE_SFLOW);
} else if (packet.source == NETFLOW) {
traffic_data.set_telemetry_type(TELEMETRY_TYPE_NETFLOW);
} else if (packet.source == TERAFLOW) {
traffic_data.set_telemetry_type(TELEMETRY_TYPE_TERA_FLOW);
} else {
traffic_data.set_telemetry_type(TELEMETRY_TYPE_UNKNOWN);
}
traffic_data.set_source_asn(packet.src_asn);
traffic_data.set_destination_asn(packet.dst_asn);
traffic_data.set_input_interface(packet.input_interface);
traffic_data.set_output_interface(packet.output_interface);
// In current version we support only IPv4 agent IP
traffic_data.set_agent_address(&packet.agent_ip_address, sizeof(packet.agent_ip_address));
return true;
}

View File

@ -37,6 +37,11 @@
#include "actions/exabgp_action.hpp"
// Traffic output formats
#include "traffic_output_formats/protobuf/protobuf_traffic_format.hpp"
#include "traffic_data.pb.h"
// Yes, maybe it's not an good idea but with this we can guarantee working code in example plugin
#include "example_plugin/example_collector.hpp"
@ -2643,7 +2648,7 @@ void export_to_kafka(const simple_packet_t& current_packet) {
std::string output_data;
if (!traffic_data.SerializeToString(output_data)) {
if (!traffic_data.SerializeToString(&output_data)) {
// Encoding error happened
return;
}

View File

@ -0,0 +1,68 @@
#include "../../traffic_data.pb.h"
#include "../../fastnetmon_types.hpp"
#include "../../all_logcpp_libraries.hpp"
// Encode simple packet into Protobuf
bool write_simple_packet_to_protobuf(const simple_packet_t& packet, TrafficData& traffic_data) {
extern log4cpp::Category& logger;
traffic_data.set_protocol(packet.protocol);
traffic_data.set_sampling_ratio(packet.sample_ratio);
if (packet.ip_protocol_version == 4) {
traffic_data.set_source_ip(&packet.src_ip, sizeof(packet.src_ip));
traffic_data.set_destination_ip(&packet.dst_ip, sizeof(packet.dst_ip));
} else if (packet.ip_protocol_version == 6) {
traffic_data.set_source_ip(&packet.src_ipv6, sizeof(packet.src_ipv6));
traffic_data.set_destination_ip(&packet.dst_ipv6, sizeof(packet.dst_ipv6));
}
traffic_data.set_ip_version(packet.ip_protocol_version);
traffic_data.set_ttl(packet.ttl);
traffic_data.set_source_port(packet.source_port);
traffic_data.set_destination_port(packet.destination_port);
traffic_data.set_octets(packet.length);
traffic_data.set_packets(packet.number_of_packets);
traffic_data.set_tcp_flags(packet.flags);
traffic_data.set_ip_fragmented(packet.ip_fragmented);
traffic_data.set_timestamp_seconds(packet.ts.tv_sec);
traffic_data.set_timestamp_milliseconds(packet.ts.tv_usec);
traffic_data.set_octets(packet.length);
if (packet.packet_direction == INCOMING) {
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_INCOMING);
} else if (packet.packet_direction == OUTGOING) {
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_OUTGOING);
} else if (packet.packet_direction == INTERNAL) {
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_INTERNAL);
} else if (packet.packet_direction == OTHER) {
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_OTHER);
} else {
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_UNKNOWN);
}
if (packet.source == MIRROR) {
traffic_data.set_telemetry_type(TELEMETRY_TYPE_MIRROR);
} else if (packet.source == SFLOW) {
traffic_data.set_telemetry_type(TELEMETRY_TYPE_SFLOW);
} else if (packet.source == NETFLOW) {
traffic_data.set_telemetry_type(TELEMETRY_TYPE_NETFLOW);
} else if (packet.source == TERAFLOW) {
traffic_data.set_telemetry_type(TELEMETRY_TYPE_TERA_FLOW);
} else {
traffic_data.set_telemetry_type(TELEMETRY_TYPE_UNKNOWN);
}
traffic_data.set_source_asn(packet.src_asn);
traffic_data.set_destination_asn(packet.dst_asn);
traffic_data.set_input_interface(packet.input_interface);
traffic_data.set_output_interface(packet.output_interface);
// In current version we support only IPv4 agent IP
traffic_data.set_agent_address(&packet.agent_ip_address, sizeof(packet.agent_ip_address));
return true;
}

View File

@ -0,0 +1,3 @@
#include "../../traffic_data.pb.h"
bool write_simple_packet_to_protobuf(const simple_packet_t& packet, TrafficData& traffic_data);