google / gopacket

Provides packet processing capabilities for Go
BSD 3-Clause "New" or "Revised" License
6.28k stars 1.12k forks source link

Make packet sources interchangeable #383

Open jcrussell opened 6 years ago

jcrussell commented 6 years ago

Something like this might be convenient for swapping between different capture backends:

type PacketDataReader interface {
    PacketDataSource
}

type PacketDataReadCloser interface {
    PacketDataReader
    io.Closer
}

type ZeroCopyPacketDataReader interface {
    ZeroCopyPacketDataSource
}

type ZeroCopyPacketDataReadCloser interface {
    ZeroCopyPacketDataReader
    io.Closer
}

Names chosen to match io package.

It looks pcap/afpacket/pfring...Close don't have a return value though.

JustinAzoff commented 6 years ago

I was going to create a similar issue!

I think not only should there be a common packet source interface, packet sources should register themselves and there should be a method like https://golang.org/pkg/database/sql/#Open that can be used to load a packet source at runtime.

This way instead of needing to have variations like pcapdump and pfdump you could have a single tool that can take an interface specification. The only tricky part is things like af_packet and pfring can take options other than just interface, so you would want to be able to specify the interface like

driver=pcap interface=p1p1
driver=afpacket interface=p1p1 fanout=loadbalance id=12
driver=pfring interface=p1p1 cluster=12

In go it may look something like https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis

ideally it would be easy to specify the interface and any flags on the command line or in a configuration file. It could also be something like

--interface afpacket,p1p1,fanout=loadbalance,id=12

the specifics don't matter too much.