Example #1
0
func Findalldevs() (ifs []Interface, err error) {
	var buf *C.char
	buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
	defer C.free(unsafe.Pointer(buf))
	var alldevsp *C.pcap_if_t

	if -1 == C.pcap_findalldevs((**C.pcap_if_t)(&alldevsp), buf) {
		return nil, errors.New(C.GoString(buf))
	}
	defer C.pcap_freealldevs((*C.pcap_if_t)(alldevsp))
	dev := alldevsp
	var i uint32
	for i = 0; dev != nil; dev = (*C.pcap_if_t)(dev.next) {
		i++
	}
	ifs = make([]Interface, i)
	dev = alldevsp
	for j := uint32(0); dev != nil; dev = (*C.pcap_if_t)(dev.next) {
		var iface Interface
		iface.Name = C.GoString(dev.name)
		iface.Description = C.GoString(dev.description)
		iface.Addresses = findalladdresses(dev.addresses)
		// TODO: add more elements
		ifs[j] = iface
		j++
	}
	return
}
Example #2
0
func GetDeviceList() ([]string, error) {
	var ifs []string
	var buf *C.char
	buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
	defer C.free(unsafe.Pointer(buf))

	var devs *C.pcap_if_t
	if C.pcap_findalldevs((**C.pcap_if_t)(&devs), buf) == -1 {
		return nil, errors.New(C.GoString(buf))
	}
	defer C.pcap_freealldevs((*C.pcap_if_t)(devs))
	dev := devs
	var devcount uint32

	// figure out how many devices we have available
	for devcount = 0; dev != nil; dev = (*C.pcap_if_t)(dev.next) {
		devcount++
	}

	dev = devs
	for i := uint32(0); dev != nil; dev = (*C.pcap_if_t)(dev.next) {
		ifs = append(ifs, C.GoString(dev.name))
		i++
	}
	return ifs, nil
}
Example #3
0
func Findalldevs() (ifs []Interface, err os.Error) {
	var alldevsp *C.pcap_if_t
	var buf [ERRBUF_SIZE]byte
	cbuf := (*C.char)(unsafe.Pointer(&buf[0]))

	if C.pcap_findalldevs(&alldevsp, cbuf) == -1 {
		clen := C.strlen(cbuf)
		err = os.NewError(string(buf[:clen]))
		return
	}
	defer C.pcap_freealldevs(alldevsp)

	dev := (*C.struct_pcap_if)(alldevsp)
	i := 0
	for ; dev != nil; dev = dev.next {
		i++
	}

	ifs = make([]Interface, i)
	for ; dev != nil; dev = dev.next {
		ifs[i].Name = C.GoString(dev.name)
		ifs[i].Desc = C.GoString(dev.description)
		i++
	}

	return
}
Example #4
0
func Findalldevs() (ifs []Interface, err string) {
	var buf *C.char
	buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
	var alldevsp *C.struct_pcap_if

	if -1 == C.pcap_findalldevs((**C.pcap_if_t)(unsafe.Pointer(&alldevsp)), buf) {
		ifs = nil
		err = C.GoString(buf)
	} else {
		dev := alldevsp
		var i uint32
		for i = 0; dev != nil; dev = dev.next {
			i++
		}
		ifs = make([]Interface, i)
		dev = alldevsp
		for j := uint32(0); dev != nil; dev = dev.next {
			var iface Interface
			iface.Name = C.GoString(dev.name)
			iface.Description = C.GoString(dev.description)
			// TODO: add more elements
			ifs[j] = iface
			j++
		}
		C.pcap_freealldevs((*C.pcap_if_t)(alldevsp))
	}
	C.free(unsafe.Pointer(buf))
	return
}