| 1 |
#include "../../all_logcpp_libraries.hpp" |
| 2 |
#include "../../fastnetmon_types.hpp" |
| 3 |
#include "traffic_data.pb.h" |
| 4 |
|
| 5 |
// Encode simple packet into Protobuf |
| 6 |
bool write_simple_packet_to_protobuf(const simple_packet_t& packet, TrafficData& traffic_data) { |
| 7 |
// Numbers before field match fields from proto file: traffic_data.proto |
| 8 |
|
| 9 |
// 1 we use arrival_time as traffic telemetry protocols do not provide this time in a reliable manner |
| 10 |
traffic_data.set_timestamp_seconds(packet.arrival_time); |
| 11 |
|
| 12 |
// 2 our time source has second only precision and we cannot populate milliseconds yet |
| 13 |
traffic_data.set_timestamp_milliseconds(0); |
| 14 |
|
| 15 |
// 3 |
| 16 |
if (packet.source == MIRROR) { |
| 17 |
traffic_data.set_telemetry_type(TELEMETRY_TYPE_MIRROR); |
| 18 |
} else if (packet.source == SFLOW) { |
| 19 |
traffic_data.set_telemetry_type(TELEMETRY_TYPE_SFLOW); |
| 20 |
} else if (packet.source == NETFLOW) { |
| 21 |
traffic_data.set_telemetry_type(TELEMETRY_TYPE_NETFLOW); |
| 22 |
} else if (packet.source == TERAFLOW) { |
| 23 |
traffic_data.set_telemetry_type(TELEMETRY_TYPE_TERA_FLOW); |
| 24 |
} else { |
| 25 |
traffic_data.set_telemetry_type(TELEMETRY_TYPE_UNKNOWN); |
| 26 |
} |
| 27 |
|
| 28 |
// 4 |
| 29 |
traffic_data.set_ip_version(packet.ip_protocol_version); |
| 30 |
|
| 31 |
// 5 |
| 32 |
if (packet.packet_direction == INCOMING) { |
| 33 |
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_INCOMING); |
| 34 |
} else if (packet.packet_direction == OUTGOING) { |
| 35 |
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_OUTGOING); |
| 36 |
} else if (packet.packet_direction == INTERNAL) { |
| 37 |
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_INTERNAL); |
| 38 |
} else if (packet.packet_direction == OTHER) { |
| 39 |
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_OTHER); |
| 40 |
} else { |
| 41 |
traffic_data.set_traffic_direction(TRAFFIC_DIRECTION_UNKNOWN); |
| 42 |
} |
| 43 |
|
| 44 |
// 6 |
| 45 |
traffic_data.set_sampling_ratio(packet.sample_ratio); |
| 46 |
|
| 47 |
// 7 |
| 48 |
traffic_data.set_protocol(packet.protocol); |
| 49 |
|
| 50 |
// 8 and 9 |
| 51 |
if (packet.ip_protocol_version == 4) { |
| 52 |
traffic_data.set_source_ip(&packet.src_ip, sizeof(packet.src_ip)); |
| 53 |
traffic_data.set_destination_ip(&packet.dst_ip, sizeof(packet.dst_ip)); |
| 54 |
} else if (packet.ip_protocol_version == 6) { |
| 55 |
traffic_data.set_source_ip(&packet.src_ipv6, sizeof(packet.src_ipv6)); |
| 56 |
traffic_data.set_destination_ip(&packet.dst_ipv6, sizeof(packet.dst_ipv6)); |
| 57 |
} |
| 58 |
|
| 59 |
// 10 |
| 60 |
traffic_data.set_source_port(packet.source_port); |
| 61 |
|
| 62 |
// 11 |
| 63 |
traffic_data.set_destination_port(packet.destination_port); |
| 64 |
|
| 65 |
// 12 |
| 66 |
traffic_data.set_packets(packet.number_of_packets); |
| 67 |
|
| 68 |
// 13 |
| 69 |
traffic_data.set_octets(packet.length); |
| 70 |
|
| 71 |
// 14 |
| 72 |
traffic_data.set_ttl(packet.ttl); |
| 73 |
|
| 74 |
// 15 |
| 75 |
traffic_data.set_tcp_flags(packet.flags); |
| 76 |
|
| 77 |
// 16 |
| 78 |
traffic_data.set_ip_fragmented(packet.ip_fragmented); |
| 79 |
|
| 80 |
// 17 |
| 81 |
traffic_data.set_ip_dont_fragment(packet.ip_dont_fragment); |
| 82 |
|
| 83 |
// 18 and 19 |
| 84 |
traffic_data.set_input_interface(packet.input_interface); |
| 85 |
traffic_data.set_output_interface(packet.output_interface); |
| 86 |
|
| 87 |
// 20 and 21 |
| 88 |
traffic_data.set_source_asn(packet.src_asn); |
| 89 |
traffic_data.set_destination_asn(packet.dst_asn); |
| 90 |
|
| 91 |
// 22 |
| 92 |
// In current version we support only IPv4 agent IP |
| 93 |
traffic_data.set_agent_address(&packet.agent_ipv4_address, sizeof(packet.agent_ipv4_address)); |
| 94 |
|
| 95 |
return true; |
| 96 |
} |