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 package analyze
import ( import (
"fmt"
"git.darknebu.la/maride/pancap/output"
"git.darknebu.la/maride/pancap/protocol" "git.darknebu.la/maride/pancap/protocol"
"github.com/google/gopacket" "github.com/google/gopacket"
"log" "log"
) )
var (
// Store total amount and amount of visited packets
totalPackets int
processedPackets int
)
func Analyze(source *gopacket.PacketSource) error { func Analyze(source *gopacket.PacketSource) error {
// Loop over all packets now // Loop over all packets now
for { for {
@ -19,13 +27,23 @@ func Analyze(source *gopacket.PacketSource) error {
continue continue
} }
// Track if we didn't process a packet
processed := false
// Iterate over all possible protocols // Iterate over all possible protocols
for _, p := range protocol.Protocols { for _, p := range protocol.Protocols {
// Check if this protocol can handle this packet // Check if this protocol can handle this packet
if p.CanAnalyze(packet) { if p.CanAnalyze(packet) {
handleErr(p.Analyze(packet)) handleErr(p.Analyze(packet))
processed = true
} }
} }
// Raise statistics
totalPackets += 1
if processed {
processedPackets += 1
}
} }
return nil return nil
@ -33,6 +51,11 @@ func Analyze(source *gopacket.PacketSource) error {
// Prints all the summaries. // Prints all the summaries.
func PrintSummary() { 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 { for _, p := range protocol.Protocols {
p.PrintSummary() p.PrintSummary()
} }