// 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) }
// 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() } }
// 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() }
func (p *Pcap) Close() { C.pcap_close(p.cptr) }
// 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) } }
// Close closes the underlying pcap handle. func (p *Handle) Close() { C.pcap_close(p.cptr) }
// Close the packet source. func (h *Handle) Close() { C.pcap_close(h.pcap) }
func (p *Pcap) Close() error { // is there a possible error condition here? C.pcap_close(p.cptr) return nil }
func (p *Pcap) Close() { C.pcap_close(p.pcap) }