Product SiteDocumentation Site

7.3.3. Obtaining a virInterfacePtr for an Interface

Many operations require that you have a virInterfacePtr, but you may only have the name or MAC address of the interface. You can use virInterfaceLookupByName and virinterfaceLookupByMACString to get the virInterfacePtr in these cases.
virInterfacePtr iface;
const char *name = "eth0";

iface = virInterfaceLookupByName(name);

if (iface) {

    /* use the virInterfacePtr ... */

    virInterfaceFree(iface);
} else {
    printf("Interface '%s' not found.\n", name);
}
Example 7.7. Fetching the virInterfacePtr for a given interface name

virInterfacePtr iface;
const char *mac = "00:01:02:03:04:05";

iface = virInterfaceLookupByMACString(mac);

if (iface) {

    /* use the virInterfacePtr ... */

    virInterfaceFree(iface);
} else {
    printf("No interface found with MAC address '%s'.\n", mac);
}
Example 7.8. Fetching the virInterfacePtr for a given interface MAC Address

Note that, as shown in the examples, after you are finished using the virinterfacePtr, you must call virInterfaceFree to free up its resources (even if you undefined or destroyed the interface in the meantime). Another important detail: doing a lookup for a MAC address that has multiple matches will result in a NULL return and a VIR_ERR_MULTIPLE_INTERFACES error being raised. This limitation will be addressed in the near future with a new API function.