pancap/ethernet/ethernet.go

61 lines
1.5 KiB
Go
Raw Normal View History

package ethernet
import (
2019-11-28 12:45:32 +00:00
"git.darknebu.la/maride/pancap/ethernet/arp"
2019-11-28 16:16:22 +00:00
"git.darknebu.la/maride/pancap/ethernet/dhcpv4"
2019-11-28 18:30:54 +00:00
"git.darknebu.la/maride/pancap/ethernet/dns"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"log"
)
func Analyze(source *gopacket.PacketSource) error {
// 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
}
2019-11-27 19:24:12 +00:00
if packet.Layer(layers.LayerTypeDNS) != nil {
// Handle DNS packet
handleErr(dns.ProcessDNSPacket(packet))
2019-11-27 19:24:12 +00:00
}
if packet.Layer(layers.LayerTypeARP) != nil {
// Handle ARP packet
handleErr(arp.ProcessARPPacket(packet))
}
2019-11-28 16:16:22 +00:00
if packet.Layer(layers.LayerTypeDHCPv4) != nil {
// Handle DHCP (v4) packet
handleErr(dhcpv4.HandleDHCPv4Packet(packet))
2019-11-28 16:16:22 +00:00
}
}
// After processing all packets, print summary
printSummary()
return nil
}
// Prints all the summaries.
func printSummary() {
2019-11-27 19:24:12 +00:00
arp.PrintARPSummary()
dns.PrintDNSSummary()
2019-11-28 16:16:22 +00:00
dhcpv4.PrintDHCPv4Summary()
}
// 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())
}
}