when I get a pcap file from s3 client, I need to generate a packetSource of gopacket to read packets in it. But I only found OpenOfflineFile function in gopacket document, how could I do to generate packetSource with []byte(which is read from s3 file).
I have read source code of OpenOfflineFile function in gopacket, but I still get confused because I'm unfamiliar with uintptr, can I just generate a unitptr with []byte and then use it to generate packetSource.
func openOffline(file string) (handle *Handle, err error) {
err = LoadWinPCAP()
if err != nil {
return nil, err
}
buf := make([]byte, errorBufferSize)
f, err := syscall.BytePtrFromString(file)
if err != nil {
return nil, err
}
var cptr uintptr
if pcapOpenOfflineWithTstampPrecisionPtr == 0 {
cptr, _, _ = syscall.Syscall(pcapOpenOfflinePtr, 2, uintptr(unsafe.Pointer(f)), uintptr(unsafe.Pointer(&buf[0])), 0)
} else {
cptr, _, _ = syscall.Syscall(pcapOpenOfflineWithTstampPrecisionPtr, 3, uintptr(unsafe.Pointer(f)), uintptr(pcapTstampPrecisionNano), uintptr(unsafe.Pointer(&buf[0])))
}
if cptr == 0 {
return nil, errors.New(byteSliceToString(buf))
}
h := &Handle{cptr: pcapTPtr(cptr)}
return h, nil
}
Try
github.com/google/gopacket/pcapgoIf the file format is supported by the package
github.com/google/gopacket/pcapgo, considering using it because it makes it easy:Use
os.Pipewithgithub.com/google/gopacket/pcapIf the file format is not supported by
github.com/google/gopacket/pcapgoand we have to go withgithub.com/google/gopacket/pcap, a workaround is to create a pipe, and pass therfile topcap.OpenOfflineFile:Notes:
github.com/google/gopacket/pcapis a wrapper oflibpcap(orwinpcapornpcapon Windows). That's why it's a little complicated to work with[]byteorio.Reader.