I want to capture packets on 4 interfaces devices eth0, eth1, eth2, and eth3 by using "github.com/google/gopacket/pcap" . The machine I am using have large number of traffic. Lets say it is in lacks per minute. I used goroutines to capture packets on each device parallely.
I get one of following error after some time (after 1 hour less or more) when I run my binary of code on centOS 7 machine.
1) runtime: pointer 0xc441198b80 to unallocated span idx=0x108cc span.base()=0xc441196000 span.limit=0xc441197ff0 span.state=3 fatal error: found bad pointer in Go heap (incorrect use of unsafe or cgo?)
2) Segmentation fault
3) fatal error: systemstack called from unexpected goroutine Segmentation fault
How I am using goroutines-
func main(){
done = make(chan bool)
var status bool = true
for _, interface_device := range util.ConfigDetails.InterfaceDeviceList{
go func(device string) {
status = CapturePackets(device)
fmt.Println("status is", status)
done <- true
}(interface_device)
}
// wait for all goroutines to complete before exiting
for _ = range util.ConfigDetails.InterfaceDeviceList {
<-done
}
}
func CapturePackets(device string) bool {
handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, pcap.BlockForever)
if err != nil {
util.Log.Errorf("%v",err)
}
defer handle.Close()
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
parser := gopacket.NewDecodingLayerParser(
layers.LayerTypeEthernet,
ðLayer,
&ipLayer,
&ip6Layer,
&tcpLayer,
&udpLayer,
&payload,
)
for packet := range packetSource.Packets() {
foundLayerTypes := []gopacket.LayerType{}
var ip_source,ip_dest net.IP
for _, layerType := range foundLayerTypes {
if layerType == layers.LayerTypeIPv4 {
ip_source = ipLayer.SrcIP
ip_dest = ipLayer.DstIP
}
if layerType == layers.LayerTypeIPv6 {
ip_source = ip6Layer.SrcIP
ip_dest = ip6Layer.DstIP
}
fmt.Println(ip_source)
fmt.Println(ip_dest)
}
}
return true
}
Please do help me.