Created
April 2, 2014 03:03
-
-
Save fiorix/9927296 to your computer and use it in GitHub Desktop.
Reading sequential pcap payload in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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