[dbus PATCH v2] Implement GetDHCPLeases method for Network Interface

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Katerina Koukiou <kkoukiou@xxxxxxxxxx>
---
 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"/>
     </method>
+    <method name="GetDHCPLeases">
+      <annotation name="org.gtk.GDBus.DocString"
+        value="See https://libvirt.org/html/libvirt-libvirt-network.html#virNetworkGetDHCPLeases
+               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#virNetworkGetXMLDesc"/>
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);
+
+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;
+        }
+
+        g_variant_builder_add(&builder, "(stssssuss)",
+                              lease->iface, lease->expirytime,
+                              typeStr, lease->mac,
+                              lease->iaid ? lease->iaid : "" ,
+                              lease->ipaddr, lease->prefix,
+                              lease->hostname ? lease->hostname : "",
+                              lease->clientid ? lease->clientid : "");
+    }
+    res = g_variant_builder_end(&builder);
+
+    *outArgs = g_variant_new_tuple(&res, 1);
+}
+
 static void
 virtDBusNetworkGetXMLDesc(GVariant *inArgs,
                           GUnixFDList *inFDs G_GNUC_UNUSED,
@@ -342,6 +415,7 @@ static virtDBusGDBusPropertyTable virtDBusNetworkPropertyTable[] = {
 static virtDBusGDBusMethodTable virtDBusNetworkMethodTable[] = {
     { "Create", virtDBusNetworkCreate },
     { "Destroy", virtDBusNetworkDestroy },
+    { "GetDHCPLeases", virtDBusNetworkGetDHCPLeases },
     { "GetXMLDesc", virtDBusNetworkGetXMLDesc },
     { "Undefine", virtDBusNetworkUndefine },
     { "Update", virtDBusNetworkUpdate },
-- 
2.15.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux