Product SiteDocumentation Site

7.3. Retrieving Information About Interfaces

7.3.1. Enumerating Interfaces

Once you have a connection to a host (a virConnectPtr) you can learn the number of interfaces on the host with virConnectNumOfInterfaces and virConnectNumOfDefinedInterfaces, and a list of those interfaces' names with virConnectListInterfaces and virConnectListDefinedInterfaces ("defined" interfaces are those that have been defined, but are currently inactive). Each of these functions takes a connection object as its first argument; the list functions also take an argument pointing to a char* array for the result, and another giving the maximum number of entries to put in that array. All 4 functions return the number of interfaces found, or -1 if an error is encountered.
NB, error handling omitted for clarity
int numIfaces, i;
char *ifaceNames;

numIfaces = virConnectNumOfInterfaces(conn);
ifaceNames = malloc(numIfaces * sizeof(char*));
numIfaces = virConnectListInterfaces(conn, names, ct);

printf("Active host interfaces:\n");
for (i = 0; i < numIfaces; i++) {
    printf(" %s\n", ifaceNames[i]);
    free(ifaceNames[i]);
}
free(ifaceNames);
Example 7.5. Getting a list of active ("up") interfaces on a host

int numIfaces, i;
char *ifaceNames;

numIfaces = virConnectNumOfDefinedInterfaces(conn);
ifaceNames = malloc(numIfaces * sizeof(char*));
numIfaces = virConnectListDefinedInterfaces(conn, names, ct);

printf("Inactive host interfaces:\n");
for (i = 0; i < numIfaces; i++) {
    printf(" %s\n", ifaceNames[i]);
    free(ifaceNames[i]);
}
free(ifaceNames);
Example 7.6. Getting a list of inactive ("down") interfaces on a host