Exemplo n.º 1
0
// NewLearner returns an IPMacLearner object bound to a particular interface.
func NewPCAP(dev string, file string) (*Pcap, error) {
	ret := &Pcap{}

	devC := C.CString(dev)
	fileC := C.CString(file)
	defer C.free(unsafe.Pointer(devC))
	defer C.free(unsafe.Pointer(fileC))

	handle := C.gopcapInit(devC)
	if handle == nil {
		return ret, fmt.Errorf("could not open device %v", dev)
	}
	ret.handle = unsafe.Pointer(handle)

	// start pcap
	dumperHandle := C.gopcapPrepare(handle, fileC)
	if dumperHandle == nil {
		C.gopcapClose(ret.handle, nil)
		return nil, fmt.Errorf("could not open output file %v", file)
	}
	ret.dumperHandle = unsafe.Pointer(dumperHandle)

	go C.gopcapCapture(handle, dumperHandle)

	return ret, nil
}
Exemplo n.º 2
0
// Stop searching for IP addresses.
func (p *Pcap) Close() {
	p.lock.Lock()
	defer p.lock.Unlock()

	C.gopcapClose(p.handle, p.dumperHandle)

	p.handle = nil
	p.dumperHandle = nil
}