On 03/15/2017 10:45 AM, Cédric Bosdonnat wrote:
Add a function getting the name of a network interface out of its
index.
---
src/libvirt_private.syms | 1 +
src/util/virnetdev.c | 19 +++++++++++++++++++
src/util/virnetdev.h | 2 ++
3 files changed, 22 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 1f25e42d8..0fe88c3fa 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1982,6 +1982,7 @@ virNetDevFeatureTypeToString;
virNetDevGetFeatures;
virNetDevGetIndex;
virNetDevGetLinkInfo;
+virNetDevGetName;
virNetDevGetMAC;
virNetDevGetMTU;
virNetDevGetOnline;
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index d12324878..91a5274aa 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -899,6 +899,25 @@ virNetDevGetRcvAllMulti(const char *ifname,
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;
+}
This is defined outside of any #ifdefs, which I *think* is okay, since
the function exists in both Linux and FreeBSD. I guess if anybody
complains then we can think about testing for the function at configure
time, but no sense in complicating configure.ac if we don't need to...
ACK.
(BTW, although I didn't have a need for this function when I suggested
it, just today I was writing some code that will need it! :-)
/**
* virNetDevGetIndex:
diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h
index 236cf83ef..01e9c5b95 100644
--- a/src/util/virnetdev.h
+++ b/src/util/virnetdev.h
@@ -157,6 +157,8 @@ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
int virNetDevSetName(const char *ifname, const char *newifname)
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)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;