DecodingLayerParser
Jean-Louis Huynen 2019-02-15 12:51:30 +01:00
parent 00bd0836d9
commit 003bb39786
1 changed files with 67 additions and 47 deletions

View File

@ -265,12 +265,7 @@ func main() {
} }
} }
var dec gopacket.Decoder source := gopacket.NewPacketSource(handle, handle.LinkType())
var ok bool
if dec, ok = gopacket.DecodersByLayerName["Ethernet"]; !ok {
log.Fatal("No eth decoder")
}
source := gopacket.NewPacketSource(handle, dec)
source.NoCopy = true source.NoCopy = true
Info("Starting to read packets\n") Info("Starting to read packets\n")
count := 0 count := 0
@ -294,12 +289,32 @@ func main() {
w.Add(1) w.Add(1)
go processCompletedSession(jobQ, &w) go processCompletedSession(jobQ, &w)
var eth layers.Ethernet
var ip4 layers.IPv4
var ip6 layers.IPv6
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet, &eth, &ip4, &ip6)
decoded := []gopacket.LayerType{}
for packet := range source.Packets() { for packet := range source.Packets() {
count++ count++
Debug("PACKET #%d\n", count) Debug("PACKET #%d\n", count)
data := packet.Data()
bytes += int64(len(data))
data := packet.Data()
if err := parser.DecodeLayers(data, &decoded); err != nil {
// Well it sures complaing about not knowing how to decode TCP
}
fmt.Printf("%s\n", ip4.SrcIP)
// fmt.Printf("%s", hex.Dump(decoded))
for _, layerType := range decoded {
switch layerType {
case layers.LayerTypeIPv6:
fmt.Println(" IP6 ", ip6.SrcIP, ip6.DstIP)
case layers.LayerTypeIPv4:
fmt.Println(" IP4 ", ip4.SrcIP, ip4.DstIP)
// defrag the IPv4 packet if required // defrag the IPv4 packet if required
if !*nodefrag { if !*nodefrag {
ip4Layer := packet.Layer(layers.LayerTypeIPv4) ip4Layer := packet.Layer(layers.LayerTypeIPv4)
@ -341,6 +356,11 @@ func main() {
assembler.AssembleWithContext(packet.NetworkLayer().NetworkFlow(), tcp, &c) assembler.AssembleWithContext(packet.NetworkLayer().NetworkFlow(), tcp, &c)
} }
}
}
bytes += int64(len(data))
var done bool var done bool
select { select {
case <-signalChan: case <-signalChan: