You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When reading packet data from an activated inactive handle to read packets off the wire, a positive timeout seems to be blocking forever.
Consider this code segment:
package main
import (
"log""time""github.com/google/gopacket/pcap"
)
var (
device="eno1"snaplen=65535promisc=true
)
// A simple ZeroCopyReadPacketData() on a handle generated with gopacket.OpenLive()funcopenLive() {
handle, err:=pcap.OpenLive(device, int32(snaplen), promisc, time.Second*2)
iferr!=nil {
log.Fatal(err)
}
err=handle.SetBPFFilter("src host 1.2.3.4") // A random filter to make sure no data is receivediferr!=nil {
log.Fatal(err)
}
data, _, err:=handle.ZeroCopyReadPacketData()
iferr!=nil {
log.Fatal(err)
}
log.Println(data)
}
// A simple ZeroCopyReadPacketData() on a handle generated from an InactiveHandlefuncinactive() {
inactiveHandle, err:=pcap.NewInactiveHandle(device)
err=inactiveHandle.SetSnapLen(snaplen)
iferr!=nil {
log.Fatal(err)
}
err=inactiveHandle.SetPromisc(promisc)
iferr!=nil {
log.Fatal(err)
}
err=inactiveHandle.SetTimeout(time.Second*2)
iferr!=nil {
log.Fatal(err)
}
handle, err:=inactiveHandle.Activate()
iferr!=nil {
log.Fatal(err)
}
err=handle.SetBPFFilter("src host 1.2.3.4") // A random filter to make sure no data is receivediferr!=nil {
log.Fatal(err)
}
data, _, err:=handle.ZeroCopyReadPacketData()
iferr!=nil {
log.Fatal(err)
}
log.Println(data)
}
With this main function:
funcmain() {
log.Println("Started")
openLive()
}
Will work just fine - output:
2018/12/27 00:00:02 started
2018/12/27 00:00:05 Timeout Expired
Whereas with this main function, calling inactive() instead of openLive():
funcmain() {
log.Println("Started")
inactive()
}
Will just hang/block forever (until killed):
2018/12/27 00:00:02 started
The same thing will happen if we replace ZeroReadPacketData() with ReadPacketData()
I've set timeout in both functions to two seconds.
The text was updated successfully, but these errors were encountered:
Could verify the behaviour. The reason why openLive works is, that OpenLive sets pcap to nonblocking and the waiting is done with pcap_wait (I think this was done because of a performance regression). The same mechanism wasn't included in Activate (no idea why). strace shows that libpcap seems to want wo wait forever (poll([{fd=3, events=POLLIN}], 1, -1).
If I remove nonblocking from OpenLive it shows the same behaviour (adding to Activate removes it).
I'll investigate what the root cause is (=why libpcap doesn't want to wait 2 seconds).
When reading packet data from an activated inactive handle to read packets off the wire, a positive timeout seems to be blocking forever.
Consider this code segment:
With this main function:
Will work just fine - output:
Whereas with this main function, calling inactive() instead of openLive():
Will just hang/block forever (until killed):
The same thing will happen if we replace
ZeroReadPacketData()
withReadPacketData()
I've set timeout in both functions to two seconds.
The text was updated successfully, but these errors were encountered: