Skip to content

Instantly share code, notes, and snippets.

@fiorix
Created April 2, 2014 03:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiorix/9927296 to your computer and use it in GitHub Desktop.
Save fiorix/9927296 to your computer and use it in GitHub Desktop.
Reading sequential pcap payload in Go
func pcapReader(filename string) (io.ReadCloser, error) {
pr, pw, err := os.Pipe()
if err != nil {
return nil, err
}
handle, err := pcap.OpenOffline(filename)
if err != nil {
return nil, err
}
go func(h *pcap.Handle, w io.WriteCloser) {
packetSource := gopacket.NewPacketSource(h, h.LinkType())
for packet := range packetSource.Packets() {
app := packet.ApplicationLayer()
if app == nil {
panic("Packet capture has no payload")
}
if _, err = w.Write(app.Payload()); err != nil {
break // Silently ignore broken pipes.
}
}
w.Close()
h.Close()
}(handle, pw)
return pr, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment