Files
weave/pcap.go
T
Matthias Radestock 7c4464c852 pcap handles are not thread safe
they contain an errbuf; there might be other issues too.
So we must use different handles in the sniffer and udplistener
goroutines.
2014-08-20 11:42:55 +01:00

60 lines
1.2 KiB
Go

package weave
import (
"code.google.com/p/gopacket/pcap"
)
type PcapIO struct {
handle *pcap.Handle
}
func NewPcapIO(ifName string, bufSz int) (pio PacketSourceSink, err error) {
pio, err = newPcapIO(ifName, true, 65535, bufSz)
return
}
func NewPcapO(ifName string) (po PacketSink, err error) {
po, err = newPcapIO(ifName, false, 0, 0)
return
}
func newPcapIO(ifName string, promisc bool, snaplen int, bufSz int) (handle *PcapIO, err error) {
inactive, err := pcap.NewInactiveHandle(ifName)
if err != nil {
return
}
defer inactive.CleanUp()
if inactive.SetPromisc(promisc) != nil {
return
}
if inactive.SetSnapLen(snaplen) != nil {
return
}
if inactive.SetTimeout(-1) != nil {
return
}
if inactive.SetImmediateMode(true) != nil {
return
}
if inactive.SetBufferSize(bufSz) != nil {
return
}
active, err := inactive.Activate()
if err != nil {
return
}
if active.SetDirection(pcap.DirectionIn) != nil {
return
}
return &PcapIO{handle: active}, nil
}
func (pi *PcapIO) ReadPacket() (data []byte, err error) {
data, _, err = pi.handle.ReadPacketData()
return
}
func (po *PcapIO) WritePacket(data []byte) error {
return po.handle.WritePacketData(data)
}