Skip to content

Commit 8eaa5fa

Browse files
committed
Top 10 ports - Adjust parser
1 parent 0db45b7 commit 8eaa5fa

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

include/ipfixprobe/parser-stats.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
#pragma once
2727

28+
#include "../../input/topPorts.hpp"
2829
#include <cstdint>
30+
#include <array>
2931

3032
namespace ipxp {
3133

@@ -46,6 +48,8 @@ struct ParserStats {
4648

4749
uint64_t seen_packets;
4850
uint64_t unknown_packets;
51+
52+
TopPorts<10> top_ports;
4953
};
5054

5155
} // namespace ipxp

input/input.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
*/
2525

2626
#include <ipfixprobe/input.hpp>
27+
#include <iterator>
28+
#include <string>
29+
#include <sstream>
30+
#include <numeric>
2731

2832
namespace ipxp {
2933

@@ -52,6 +56,17 @@ static telemetry::Content get_parser_stats_content(const ParserStats& parserStat
5256
dict["seen_packets"] = parserStats.seen_packets;
5357
dict["unknown_packets"] = parserStats.unknown_packets;
5458

59+
const auto& [ports, size] = parserStats.top_ports.get_top_ports();
60+
if (size == 0) {
61+
dict["top_10_ports"] = "";
62+
} else {
63+
std::string top_ports = std::to_string(ports[0].first) + ": " + std::to_string(ports[0].second);
64+
dict["top_10_ports"] = std::accumulate(ports.begin() + 1, ports.begin() + size, top_ports,
65+
[](std::string& acc, const std::pair<uint16_t, size_t>& portFrequency) {
66+
return acc + ", " + std::to_string(portFrequency.first) + ": " + std::to_string(portFrequency.second);
67+
});
68+
}
69+
5570
return dict;
5671
}
5772

input/parser.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
#include "headers.hpp"
3838
#include <ipfixprobe/packet.hpp>
3939

40+
#include <iterator>
41+
#include <string>
42+
#include <sstream>
43+
#include <numeric>
4044
namespace ipxp {
4145

4246
//#define DEBUG_PARSER
@@ -454,7 +458,7 @@ inline uint16_t parse_ipv6_hdr(const u_char *data_ptr, uint16_t data_len, Packet
454458
* \param [out] pkt Pointer to Packet structure where parsed fields will be stored.
455459
* \return Size of header in bytes.
456460
*/
457-
inline uint16_t parse_tcp_hdr(const u_char *data_ptr, uint16_t data_len, Packet *pkt)
461+
inline uint16_t parse_tcp_hdr(ParserStats& stats, const u_char *data_ptr, uint16_t data_len, Packet *pkt)
458462
{
459463
struct tcphdr *tcp = (struct tcphdr *) data_ptr;
460464
if (sizeof(struct tcphdr) > data_len) {
@@ -469,6 +473,9 @@ inline uint16_t parse_tcp_hdr(const u_char *data_ptr, uint16_t data_len, Packet
469473
pkt->tcp_flags = (uint8_t) *(data_ptr + 13) & 0xFF;
470474
pkt->tcp_window = ntohs(tcp->window);
471475

476+
stats.top_ports.insert(pkt->src_port);
477+
stats.top_ports.insert(pkt->dst_port);
478+
472479
DEBUG_MSG("TCP header:\n");
473480
DEBUG_MSG("\tSrc port:\t%u\n", ntohs(tcp->source));
474481
DEBUG_MSG("\tDest port:\t%u\n", ntohs(tcp->dest));
@@ -529,7 +536,7 @@ inline uint16_t parse_tcp_hdr(const u_char *data_ptr, uint16_t data_len, Packet
529536
* \param [out] pkt Pointer to Packet structure where parsed fields will be stored.
530537
* \return Size of header in bytes.
531538
*/
532-
inline uint16_t parse_udp_hdr(const u_char *data_ptr, uint16_t data_len, Packet *pkt)
539+
inline uint16_t parse_udp_hdr(ParserStats& stats, const u_char *data_ptr, uint16_t data_len, Packet *pkt)
533540
{
534541
struct udphdr *udp = (struct udphdr *) data_ptr;
535542
if (sizeof(struct udphdr) > data_len) {
@@ -539,6 +546,9 @@ inline uint16_t parse_udp_hdr(const u_char *data_ptr, uint16_t data_len, Packet
539546
pkt->src_port = ntohs(udp->source);
540547
pkt->dst_port = ntohs(udp->dest);
541548

549+
stats.top_ports.insert(pkt->src_port);
550+
stats.top_ports.insert(pkt->dst_port);
551+
542552
DEBUG_MSG("UDP header:\n");
543553
DEBUG_MSG("\tSrc port:\t%u\n", ntohs(udp->source));
544554
DEBUG_MSG("\tDest port:\t%u\n", ntohs(udp->dest));
@@ -727,10 +737,10 @@ void parse_packet(parser_opt_t *opt, ParserStats& stats, struct timeval ts, cons
727737

728738
l4_hdr_offset = data_offset;
729739
if (pkt->ip_proto == IPPROTO_TCP) {
730-
data_offset += parse_tcp_hdr(data + data_offset, caplen - data_offset, pkt);
740+
data_offset += parse_tcp_hdr(stats, data + data_offset, caplen - data_offset, pkt);
731741
stats.tcp_packets++;
732742
} else if (pkt->ip_proto == IPPROTO_UDP) {
733-
data_offset += parse_udp_hdr(data + data_offset, caplen - data_offset, pkt);
743+
data_offset += parse_udp_hdr(stats, data + data_offset, caplen - data_offset, pkt);
734744
stats.udp_packets++;
735745
}
736746
} catch (const char *err) {

0 commit comments

Comments
 (0)