util: add virNetDevGetName() function

Add a function getting the name of a network interface out of its index.
This commit is contained in:
Cédric Bosdonnat 2017-03-15 14:46:56 +01:00
parent 3ee35d7d6c
commit 5dd607059d
3 changed files with 22 additions and 0 deletions

View File

@ -1995,6 +1995,7 @@ virNetDevGetIndex;
virNetDevGetLinkInfo; virNetDevGetLinkInfo;
virNetDevGetMAC; virNetDevGetMAC;
virNetDevGetMTU; virNetDevGetMTU;
virNetDevGetName;
virNetDevGetOnline; virNetDevGetOnline;
virNetDevGetPhysicalFunction; virNetDevGetPhysicalFunction;
virNetDevGetPromiscuous; virNetDevGetPromiscuous;

View File

@ -899,6 +899,25 @@ virNetDevGetRcvAllMulti(const char *ifname,
return virNetDevGetIFFlag(ifname, VIR_IFF_ALLMULTI, receive); return virNetDevGetIFFlag(ifname, VIR_IFF_ALLMULTI, receive);
} }
char *virNetDevGetName(int ifindex)
{
char name[IFNAMSIZ];
char *ifname = NULL;
memset(&name, 0, sizeof(name));
if (!if_indextoname(ifindex, name)) {
virReportSystemError(errno,
_("Failed to convert interface index %d to a name"),
ifindex);
goto cleanup;
}
ignore_value(VIR_STRDUP(ifname, name));
cleanup:
return ifname;
}
/** /**
* virNetDevGetIndex: * virNetDevGetIndex:

View File

@ -157,6 +157,8 @@ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
int virNetDevSetName(const char *ifname, const char *newifname) int virNetDevSetName(const char *ifname, const char *newifname)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
char *virNetDevGetName(int ifindex)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
int virNetDevGetIndex(const char *ifname, int *ifindex) int virNetDevGetIndex(const char *ifname, int *ifindex)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;