Exemple #1
0
//Opens a pcap handle
func OpenActiveDevice(dev string, snaplen uint32, promisc bool, timeout_ms uint32, filter string) (*Pcap, error) {
	var cbuf *C.char
	cbuf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
	handle := new(Pcap)
	var cpromisc int32 = 0
	if promisc {
		cpromisc = 1
	}

	cdev := C.CString(dev)
	defer C.free(unsafe.Pointer(cdev))
	handle.cptr = C.pcap_open_live(cdev, C.int(snaplen), C.int(cpromisc), C.int(timeout_ms), cbuf)
	if handle.cptr == nil {
		return nil, errors.New(C.GoString(cbuf))
	}
	x := C.pcap_datalink(handle.cptr)
	handle.linkLayer = uint16(x)

	C.free(unsafe.Pointer(cbuf))
	if filter != "" {
		ret := int32(C.pcap_set_filter(handle.cptr, cdev, C.CString(filter), cbuf, ERRBUF_SIZE-1))
		if ret != 0 {
			C.free(unsafe.Pointer(handle.cptr))
			return nil, errors.New(C.GoString(cbuf))
		}
	}
	return handle, nil
}
Exemple #2
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.
//
// See the package documentation for important details regarding 'timeout'.
func OpenLive(device string, snaplen int32, promisc bool, timeout time.Duration) (handle *Handle, _ error) {
	buf := (*C.char)(C.calloc(errorBufferSize, 1))
	defer C.free(unsafe.Pointer(buf))
	var pro C.int
	if promisc {
		pro = 1
	}
	p := &Handle{}
	p.blockForever = timeout < 0
	p.device = device

	ifc, err := net.InterfaceByName(device)
	if err != nil {
		// The device wasn't found in the OS, but could be "any"
		// Set index to 0
		p.deviceIndex = 0
	} else {
		p.deviceIndex = ifc.Index
	}

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

	p.cptr = C.pcap_open_live(dev, C.int(snaplen), pro, timeoutMillis(timeout), buf)
	if p.cptr == nil {
		return nil, errors.New(C.GoString(buf))
	}
	return p, nil
}
Exemple #3
0
// Open creates a packet capture descriptor to look at packets on the network.
// Calling  C.pcap_open_live is the equivalent of calling:
//	C.pcap_create
//	C.pcap_set_snaplen
//	C.pcap_set_promisc
//	C.pcap_set_timeout
//	C.pcap_activate
//
// In that order.  So if you want to use custom values for any thing that has
// to be set before pcap is active you should use Create instead of Open or
// OpenLive.
func (p *Pcap) Open() error {
	buf := (*C.char)(C.calloc(C.PCAP_ERRBUF_SIZE, 1))
	defer C.free(unsafe.Pointer(buf))

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

	p.cptr = C.pcap_open_live(dev, C.int(p.Snaplen), C.int(p.Promisc),
		C.int(p.Timeout), buf)
	if p.cptr == nil {
		return errors.New(C.GoString(buf))
	}
	return nil
}
Exemple #4
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 int32
	if promisc {
		pro = 1
	}

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

	cptr := C.pcap_open_live(dev, C.int(snaplen), C.int(pro), C.int(timeout/time.Millisecond), buf)
	if cptr == nil {
		return nil, errors.New(C.GoString(buf))
	}
	return newHandle(cptr), nil
}
Exemple #5
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.
//
// See the package documentation for important details regarding 'timeout'.
func OpenLive(device string, snaplen int32, promisc bool, timeout time.Duration) (handle *Handle, _ error) {
	buf := (*C.char)(C.calloc(errorBufferSize, 1))
	defer C.free(unsafe.Pointer(buf))
	var pro C.int
	if promisc {
		pro = 1
	}
	p := &Handle{}
	p.blockForever = timeout < 0
	p.device = device

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

	p.cptr = C.pcap_open_live(dev, C.int(snaplen), pro, timeoutMillis(timeout), buf)
	if p.cptr == nil {
		return nil, errors.New(C.GoString(buf))
	}
	return p, nil
}
Exemple #6
0
func Openlive(device string, snaplen int32, promisc bool, timeout_ms int32) (handle *Pcap, err string) {
	var buf *C.char
	buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
	h := new(Pcap)
	var pro int32
	if promisc {
		pro = 1
	} else {
		pro = 0
	}
	h.cptr = C.pcap_open_live(C.CString(device), C.int(snaplen), C.int(pro), C.int(timeout_ms), buf)
	if nil == h.cptr {
		handle = nil
		err = C.GoString(buf)
	} else {
		handle = h
	}
	C.free(unsafe.Pointer(buf))
	return
}
Exemple #7
0
func Openlive(dev string, snaplen int32, promisc bool, timeout int32) (ret *Pcap, msg string) {
	cdev := C.CString(dev)
	defer C.free(unsafe.Pointer(cdev))

	var cpro C.int
	if promisc {
		cpro = 1
	}

	var buf [ERRBUF_SIZE]byte
	cbuf := (*C.char)(unsafe.Pointer(&buf[0]))
	cptr := C.pcap_open_live(cdev, C.int(snaplen), C.int(cpro), C.int(timeout), cbuf)
	clen := C.strlen(cbuf)
	msg = string(buf[:clen])
	if cptr != nil {
		ret = &Pcap{cptr: cptr}
	}

	return
}
Exemple #8
0
// OpenLive opens a device and returns a handler.
func OpenLive(device string, snaplen int32, promisc bool, timeout_ms int32) (handle *Pcap, err error) {
	var buf *C.char
	buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
	h := new(Pcap)
	var pro int32
	if promisc {
		pro = 1
	}

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

	h.cptr = C.pcap_open_live(dev, C.int(snaplen), C.int(pro), C.int(timeout_ms), buf)
	if nil == h.cptr {
		handle = nil
		err = &pcapError{C.GoString(buf)}
	} else {
		handle = h
	}

	handle.datalink = handle.Datalink()
	C.free(unsafe.Pointer(buf))
	return
}