Exemplo n.º 1
0
// OpenLive opens a device and returns a *Handle.
// It takes as arguments the name of the device ("eth0"), the maximum size to
// read for each packet (snaplen), whether to put the interface in promiscuous
// mode, and a timeout.
func OpenLive(device string, snaplen int32, promisc bool, timeout time.Duration) (handle *Handle, _ error) {
	var buf *C.char
	buf = (*C.char)(C.calloc(errorBufferSize, 1))
	defer C.free(unsafe.Pointer(buf))
	var pro C.int
	if promisc {
		pro = 1
	}

	dev := C.CString(device)
	defer C.free(unsafe.Pointer(dev))

	// This copies a bunch of the pcap_open_live implementation from pcap.c:
	cptr := C.pcap_create(dev, buf)
	if cptr == nil {
		return nil, errors.New(C.GoString(buf))
	}
	var status C.int
	if status = C.pcap_set_snaplen(cptr, C.int(snaplen)); status < 0 {
		goto fail
	} else if status = C.pcap_set_promisc(cptr, pro); status < 0 {
		goto fail
	} else if status = C.pcap_set_timeout(cptr, C.int(timeout/time.Millisecond)); status < 0 {
		goto fail
	}
	return newHandle(cptr), nil
fail:
	C.pcap_close(cptr)
	return nil, statusError(status)
}
Exemplo n.º 2
0
// Close closes the files associated with p and deallocates C resources.
func (p *Pcap) Close() {
	if p.cptr != nil {
		p.m.Lock()
		C.pcap_close(p.cptr)
		p.m.Unlock()
	}
}
Exemplo n.º 3
0
// Close closes the underlying pcap handle.
func (p *Handle) Close() {
	p.mu.Lock()
	if p.cptr == nil {
		return
	}
	C.pcap_close(p.cptr)
	p.cptr = nil
	p.mu.Unlock()
}
Exemplo n.º 4
0
func (p *Pcap) Close() {
	C.pcap_close(p.cptr)
}
Exemplo n.º 5
0
// CleanUp cleans up any stuff left over from a successful or failed building
// of a handle.
func (p *InactiveHandle) CleanUp() {
	if p.cptr != nil {
		C.pcap_close(p.cptr)
	}
}
Exemplo n.º 6
0
// Close closes the underlying pcap handle.
func (p *Handle) Close() {
	C.pcap_close(p.cptr)
}
Exemplo n.º 7
0
// Close the packet source.
func (h *Handle) Close() {
	C.pcap_close(h.pcap)
}
Exemplo n.º 8
0
func (p *Pcap) Close() error {
	// is there a possible error condition here?
	C.pcap_close(p.cptr)
	return nil
}
Exemplo n.º 9
0
func (p *Pcap) Close() {
	C.pcap_close(p.pcap)
}