Example #1
0
func (h *Hypervisor) ListNetworks(flags uint) ([]*Network, error) {
	var cnetworks *C.virNetworkPtr
	result := C.virConnectListAllNetworks(h.cptr, &cnetworks, C.uint(flags))
	if result == -1 {
		return nil, GetLastError()
	}

	var networks = make([]*Network, result)
	p := (*[1 << 30]C.virNetworkPtr)(unsafe.Pointer(cnetworks))

	for i := 0; i < int(result); i++ {
		networks[i] = newNetwork(p[i])
	}
	defer C.free(unsafe.Pointer(cnetworks))

	return networks, nil
}
Example #2
0
func (c *VirConnection) ListAllNetworks(flags uint32) ([]VirNetwork, error) {
	var cList *C.virNetworkPtr
	numNets := C.virConnectListAllNetworks(c.ptr, (**C.virNetworkPtr)(&cList), C.uint(flags))
	if numNets == -1 {
		return nil, GetLastError()
	}
	hdr := reflect.SliceHeader{
		Data: uintptr(unsafe.Pointer(cList)),
		Len:  int(numNets),
		Cap:  int(numNets),
	}
	var nets []VirNetwork
	slice := *(*[]C.virNetworkPtr)(unsafe.Pointer(&hdr))
	for _, ptr := range slice {
		nets = append(nets, VirNetwork{ptr})
	}
	C.free(unsafe.Pointer(cList))
	return nets, nil
}