Skip to content

Commit 5881434

Browse files
committed
fix and count non tcp/udp packets
1 parent 186d513 commit 5881434

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

cmd/pktvisor/pktvisor.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ type StatSnapshot struct {
112112
Udp int64 `json:"udp"`
113113
In int64 `json:"in"`
114114
Out int64 `json:"out"`
115+
Other_L4 int64 `json:"other_l4"`
115116
Rates struct {
116117
Pps_in struct {
117118
P50 int64 `json:"p50"`
@@ -165,12 +166,14 @@ func updateHeader(v *gocui.View, rates *InstantRates, stats *StatSnapshot) {
165166
pcounts := stats.Packets
166167
// there may be some unknown
167168
inOutDiff := pcounts.Total - (pcounts.In + pcounts.Out)
168-
_, _ = fmt.Fprintf(v, "Pkts %d | UDP %d (%3.1f%%) | TCP %d (%3.1f%%) | IPv4 %d (%3.1f%%) | IPv6 %d (%3.1f%%) | In %d (%3.1f%%) | Out %d (%3.1f%%)\n",
169+
_, _ = fmt.Fprintf(v, "Pkts %d | UDP %d (%3.1f%%) | TCP %d (%3.1f%%) | Other %d (%3.1f%%) | IPv4 %d (%3.1f%%) | IPv6 %d (%3.1f%%) | In %d (%3.1f%%) | Out %d (%3.1f%%)\n",
169170
pcounts.Total,
170171
pcounts.Udp,
171172
(float64(pcounts.Udp)/float64(pcounts.Total))*100,
172173
pcounts.Tcp,
173174
(float64(pcounts.Tcp)/float64(pcounts.Total))*100,
175+
pcounts.Other_L4,
176+
(float64(pcounts.Other_L4)/float64(pcounts.Total))*100,
174177
pcounts.Ipv4,
175178
(float64(pcounts.Ipv4)/float64(pcounts.Total))*100,
176179
pcounts.Ipv6,

src/main.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,20 @@ static void onApplicationInterrupted(void *cookie)
9292
static void processRawPacket(pcpp::RawPacket *rawPacket, pktvisor::TcpDnsReassembly *tcpReassembly)
9393
{
9494

95-
pcpp::ProtocolType l3, l4;
95+
pcpp::ProtocolType l3(pcpp::UnknownProtocol), l4(pcpp::UnknownProtocol);
9696
pcpp::Packet packet(rawPacket);
97-
l3 = (packet.isPacketOfType(pcpp::IPv4)) ? pcpp::IPv4 : pcpp::IPv6;
98-
l4 = (packet.isPacketOfType(pcpp::UDP)) ? pcpp::UDP : pcpp::TCP;
97+
if (packet.isPacketOfType(pcpp::IPv4)) {
98+
l3 = pcpp::IPv4;
99+
}
100+
else if (packet.isPacketOfType(pcpp::IPv6)) {
101+
l3 = pcpp::IPv6;
102+
}
103+
if (packet.isPacketOfType(pcpp::UDP)) {
104+
l4 = pcpp::UDP;
105+
}
106+
else if (packet.isPacketOfType(pcpp::TCP)) {
107+
l4 = pcpp::TCP;
108+
}
99109
// determine packet direction by matching source/dest ips
100110
// note the direction may be indeterminate!
101111
pktvisor::Direction dir = pktvisor::unknown;

src/metrics.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void Metrics::merge(Metrics &other)
3333
_numPackets += other._numPackets;
3434
_numPackets_UDP += other._numPackets_UDP;
3535
_numPackets_TCP += other._numPackets_TCP;
36+
_numPackets_OtherL4 += other._numPackets_OtherL4;
3637
_numPackets_IPv6 += other._numPackets_IPv6;
3738
_numPackets_in += other._numPackets_in;
3839
_numPackets_out += other._numPackets_out;
@@ -269,6 +270,7 @@ void Metrics::newPacket(const pcpp::Packet &packet, pcpp::ProtocolType l3, pcpp:
269270
_numPackets_TCP++;
270271
break;
271272
default:
273+
_numPackets_OtherL4++;
272274
break;
273275
}
274276

@@ -425,6 +427,7 @@ void Metrics::toJSON(nlohmann::json &j, const std::string &key)
425427
j[key]["packets"]["total"] = _numPackets.load();
426428
j[key]["packets"]["udp"] = _numPackets_UDP.load();
427429
j[key]["packets"]["tcp"] = _numPackets_TCP.load();
430+
j[key]["packets"]["other_l4"] = _numPackets_OtherL4.load();
428431
j[key]["packets"]["ipv4"] = _numPackets - _numPackets_IPv6;
429432
j[key]["packets"]["ipv6"] = _numPackets_IPv6.load();
430433
j[key]["packets"]["in"] = _numPackets_in.load();

src/metrics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class Metrics
197197
std::atomic_uint64_t _numPackets = 0;
198198
std::atomic_uint64_t _numPackets_UDP = 0;
199199
std::atomic_uint64_t _numPackets_TCP = 0;
200+
std::atomic_uint64_t _numPackets_OtherL4 = 0;
200201
std::atomic_uint64_t _numPackets_IPv6 = 0;
201202
std::atomic_uint64_t _numPackets_in = 0;
202203
std::atomic_uint64_t _numPackets_out = 0;

0 commit comments

Comments
 (0)