Add statistics about how many packets were processed

This commit is contained in:
maride 2020-01-08 08:56:11 +01:00
parent 0051217bb5
commit 56b493283e

View File

@ -1,11 +1,19 @@
package analyze
import (
"fmt"
"git.darknebu.la/maride/pancap/output"
"git.darknebu.la/maride/pancap/protocol"
"github.com/google/gopacket"
"log"
)
var (
// Store total amount and amount of visited packets
totalPackets int
processedPackets int
)
func Analyze(source *gopacket.PacketSource) error {
// Loop over all packets now
for {
@ -19,13 +27,23 @@ func Analyze(source *gopacket.PacketSource) error {
continue
}
// Track if we didn't process a packet
processed := false
// Iterate over all possible protocols
for _, p := range protocol.Protocols {
// Check if this protocol can handle this packet
if p.CanAnalyze(packet) {
handleErr(p.Analyze(packet))
processed = true
}
}
// Raise statistics
totalPackets += 1
if processed {
processedPackets += 1
}
}
return nil
@ -33,6 +51,11 @@ func Analyze(source *gopacket.PacketSource) error {
// Prints all the summaries.
func PrintSummary() {
// First, print base information collected while analyzing
content := fmt.Sprintf("Processed %d out of %d packets (%d%%)", processedPackets, totalPackets, processedPackets*100/totalPackets)
output.PrintBlock("Overall statistics", content)
// Print summary of each protocol
for _, p := range protocol.Protocols {
p.PrintSummary()
}