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
// NewLearner returns an IPMacLearner object bound to a particular interface.
func NewPCAP(dev string, file string) (*Pcap, error) {
	ret := &Pcap{}
	p := C.CString(dev)
	handle := C.gopcapInit(p)
	C.free(unsafe.Pointer(p))
	if handle == nil {
		return ret, fmt.Errorf("could not open device %v", dev)
	}
	ret.handle = unsafe.Pointer(handle)

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

	go C.gopcapCapture(handle, dumperHandle)

	return ret, nil
}