diff --git a/integration_test.go b/integration_test.go index 5dcd779..c2a574f 100644 --- a/integration_test.go +++ b/integration_test.go @@ -729,3 +729,31 @@ func TestGetDomainCPUStats(t *testing.T) { // return // } // } + +func TestListAllInterfaces(t *testing.T) { + conn, err := NewVirConnection("lxc:///") + if err != nil { + t.Error(err) + return + } + defer conn.CloseConnection() + ifaces, err := conn.ListAllInterfaces(0) + if err != nil { + t.Fatal(err) + } + lookingFor := "lo" + found := false + for _, iface := range ifaces { + name, err := iface.GetName() + if err != nil { + t.Fatal(err) + } + if name == lookingFor { + found = true + break + } + } + if found == false { + t.Fatalf("interface %s not found", lookingFor) + } +} diff --git a/libvirt.go b/libvirt.go index 9e404f8..9c86c5e 100644 --- a/libvirt.go +++ b/libvirt.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io/ioutil" + "reflect" "unsafe" ) @@ -617,3 +618,23 @@ func (c *VirConnection) LookupSecretByUsage(usageType int, usageID string) (VirS } return VirSecret{ptr: ptr}, nil } + +func (c *VirConnection) ListAllInterfaces(flags uint32) ([]VirInterface, error) { + var cList *C.virInterfacePtr + numIfaces := C.virConnectListAllInterfaces(c.ptr, (**C.virInterfacePtr)(&cList), C.uint(flags)) + if numIfaces == -1 { + return nil, errors.New(GetLastError()) + } + hdr := reflect.SliceHeader{ + Data: uintptr(unsafe.Pointer(cList)), + Len: int(numIfaces), + Cap: int(numIfaces), + } + var ifaces []VirInterface + slice := *(*[]C.virInterfacePtr)(unsafe.Pointer(&hdr)) + for _, ptr := range slice { + ifaces = append(ifaces, VirInterface{ptr}) + } + C.free(unsafe.Pointer(cList)) + return ifaces, nil +}