On Wed, Apr 18, 2018 at 05:25:52PM +0200, Katerina Koukiou wrote:
Signed-off-by: Katerina Koukiou <kkoukiou(a)redhat.com>
---
data/org.libvirt.Network.xml | 9 ++++++
src/network.c | 74 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 83 insertions(+)
diff --git a/data/org.libvirt.Network.xml b/data/org.libvirt.Network.xml
index 9d522dc..cf05062 100644
--- a/data/org.libvirt.Network.xml
+++ b/data/org.libvirt.Network.xml
@@ -36,6 +36,15 @@
<annotation name="org.gtk.GDBus.DocString"
value="See
https://libvirt.org/html/libvirt-libvirt-network.html#virNetworkDestroy&q...
</method>
+ <method name="GetDHCPLeases">
+ <annotation name="org.gtk.GDBus.DocString"
+ value="See
https://libvirt.org/html/libvirt-libvirt-network.html#virNetworkGetDHCPLe...
+ Empty string can be used to pass a NULL as @mac argument.
+ Empty string will be returned in output for NULL variables."/>
+ <arg name="mac" type="s" direction="in"/>
+ <arg name="flags" type="u" direction="in"/>
+ <arg name="leases" type="a(stssssuss)"
direction="out"/>
+ </method>
<method name="GetXMLDesc">
<annotation name="org.gtk.GDBus.DocString"
value="See
https://libvirt.org/html/libvirt-libvirt-network.html#virNetworkGetXMLDes...
diff --git a/src/network.c b/src/network.c
index c507225..5d24af8 100644
--- a/src/network.c
+++ b/src/network.c
@@ -3,6 +3,12 @@
#include <libvirt/libvirt.h>
+VIRT_DBUS_ENUM_DECL(virtDBusNetworkIPAddr)
+VIRT_DBUS_ENUM_IMPL(virtDBusNetworkIPAddr,
+ VIR_IP_ADDR_TYPE_LAST,
+ "ipv4",
+ "ipv6")
+
VIRT_DBUS_ENUM_DECL(virtDBusNetworkUpdateCommand)
VIRT_DBUS_ENUM_IMPL(virtDBusNetworkUpdateCommand,
VIR_NETWORK_UPDATE_COMMAND_LAST,
@@ -29,6 +35,15 @@ VIRT_DBUS_ENUM_IMPL(virtDBusNetworkUpdateSection,
"dns-txt",
"dns-srv")
+static void
+virtDBusNetworkDHCPLeaseListFree(virNetworkDHCPLeasePtr *leases)
+{
+ for (gint i = 0; leases[i] != NULL; i += 1)
+ virNetworkDHCPLeaseFree(leases[i]);
+
+ g_free(leases);
+}
+
static virNetworkPtr
virtDBusNetworkGetVirNetwork(virtDBusConnect *connect,
const gchar *objectPath,
@@ -235,6 +250,64 @@ virtDBusNetworkDestroy(GVariant *inArgs G_GNUC_UNUSED,
virtDBusUtilSetLastVirtError(error);
}
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetworkDHCPLeasePtr, virtDBusNetworkDHCPLeaseListFree);
I would move this macro next to the free function.
+
+static void
+virtDBusNetworkGetDHCPLeases(GVariant *inArgs,
+ GUnixFDList *inFDs G_GNUC_UNUSED,
+ const gchar *objectPath,
+ gpointer userData,
+ GVariant **outArgs,
+ GUnixFDList **outFDs G_GNUC_UNUSED,
+ GError **error)
+{
+ virtDBusConnect *connect = userData;
+ g_autoptr(virNetwork) network = NULL;
+ const gchar *mac;
+ guint flags;
+ g_autoptr(virNetworkDHCPLeasePtr) leases = NULL;
+ gint nleases;
+ GVariantBuilder builder;
+ GVariant *res;
+
+ g_variant_get(inArgs, "(&su)", &mac, &flags);
+ if (g_str_equal(mac, ""))
+ mac = NULL;
+
+ network = virtDBusNetworkGetVirNetwork(connect, objectPath, error);
+ if (!network)
+ return;
+
+ nleases = virNetworkGetDHCPLeases(network, mac, &leases, flags);
+ if (nleases < 0)
+ return virtDBusUtilSetLastVirtError(error);
+
+ g_variant_builder_init(&builder, G_VARIANT_TYPE("a(stssssuss)"));
+ for (gint i = 0; i < nleases; i++) {
+ const gchar *typeStr;
+
+ virNetworkDHCPLeasePtr lease = leases[i];
+
+ typeStr = virtDBusNetworkIPAddrTypeToString(lease->type);
+ if (!typeStr) {
+ g_set_error(error, VIRT_DBUS_ERROR, VIRT_DBUS_ERROR_LIBVIRT,
+ "Can't format virIPAddrType '%d' to
string.", lease->type);
+ return;
+ }
In case we are formatting output from libvirt API to present it as
output of libvirt-dbus method we should just call 'continue' instead
of printing an error because libvirt can extend any enum in future
and this API would stop working with that libvirt version, however in
this case I don't expect new IPAddrType any time soon so we can
leave the error here.
Reviewed-by: Pavel Hrdina <phrdina(a)redhat.com>