2019-12-09 11:14:01 +00:00
|
|
|
package analyze
|
2019-11-26 22:06:57 +00:00
|
|
|
|
2019-11-26 22:55:18 +00:00
|
|
|
import (
|
2020-01-08 07:56:11 +00:00
|
|
|
"fmt"
|
|
|
|
"git.darknebu.la/maride/pancap/output"
|
2019-12-09 11:14:01 +00:00
|
|
|
"git.darknebu.la/maride/pancap/protocol"
|
2019-11-26 22:55:18 +00:00
|
|
|
"github.com/google/gopacket"
|
|
|
|
"log"
|
|
|
|
)
|
2019-11-26 22:06:57 +00:00
|
|
|
|
2020-01-08 07:56:11 +00:00
|
|
|
var (
|
|
|
|
// Store total amount and amount of visited packets
|
|
|
|
totalPackets int
|
|
|
|
processedPackets int
|
|
|
|
)
|
|
|
|
|
2019-11-26 22:06:57 +00:00
|
|
|
func Analyze(source *gopacket.PacketSource) error {
|
2019-11-26 22:55:18 +00:00
|
|
|
// Loop over all packets now
|
|
|
|
for {
|
|
|
|
packet, packetErr := source.NextPacket()
|
|
|
|
if packet == nil {
|
|
|
|
// We iterated over all packets, we're done here
|
|
|
|
break
|
|
|
|
} else if packetErr != nil {
|
|
|
|
// encountered some problem, report it
|
|
|
|
log.Printf("Encountered a problem with a packet: %s", packetErr.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-08 07:56:11 +00:00
|
|
|
// Track if we didn't process a packet
|
|
|
|
processed := false
|
|
|
|
|
2019-12-09 11:14:01 +00:00
|
|
|
// 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))
|
2020-01-08 07:56:11 +00:00
|
|
|
processed = true
|
2019-12-09 11:14:01 +00:00
|
|
|
}
|
2019-11-28 16:16:22 +00:00
|
|
|
}
|
2020-01-08 07:56:11 +00:00
|
|
|
|
|
|
|
// Raise statistics
|
|
|
|
totalPackets += 1
|
|
|
|
if processed {
|
|
|
|
processedPackets += 1
|
|
|
|
}
|
2019-11-26 22:55:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 22:06:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-26 22:55:18 +00:00
|
|
|
|
|
|
|
// Prints all the summaries.
|
2019-12-09 11:14:01 +00:00
|
|
|
func PrintSummary() {
|
2020-01-08 07:56:11 +00:00
|
|
|
// 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
|
2019-12-09 11:14:01 +00:00
|
|
|
for _, p := range protocol.Protocols {
|
|
|
|
p.PrintSummary()
|
|
|
|
}
|
2019-11-26 22:55:18 +00:00
|
|
|
}
|
2019-11-28 17:49:14 +00:00
|
|
|
|
|
|
|
// Handles an error, if err is not nil.
|
|
|
|
func handleErr(err error) {
|
|
|
|
// (hopefully) most calls to this function will contain a nil error, so we need to check if we really got an error
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Encountered error while examining packets, continuing anyway. Error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2019-12-09 11:14:01 +00:00
|
|
|
|