[PATCH 1/4] net-dhcp-leases: Implement the public APIs

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

 



Introduce 3 new APIs, virNetworkGetDHCPLeases, virNetworkGetDHCPLeaseForMAC
and virNetworkDHCPLeaseFree.

* virNetworkGetDHCPLeases: returns the dhcp leases information for a given
     virtual network. The information includes lease expirytime, MAC Address,
     IP Address, hostname and clientid.

* virNetworkGetDHCPLeaseForMAC: returns the dhcp lease information for a
     given virtual network and specified MAC Address.

* virNetworkDHCPLeaseFree: allows the upper layer application to free the
     network interface object conveniently.

There is no support for flags, so user is expected to pass 0 for
both the APIs.

include/libvirt/libvirt.h.in:
  * Define virNetworkGetDHCPLeases
  * Define virNetworkGetDHCPLeaseForMAC
  * Define virNetworkDHCPLeaseFree

python/generator.py:
  * Skip the auto-generation for virNetworkGetDHCPLeases
  * Skip the auto-generation for virNetworkGetDHCPLeaseForMAC
  * Skip the auto-generation for virNetworkDHCPLeaseFree

src/driver.h:
  * Define networkGetDHCPLeases
  * Define networkGetDHCPLeaseForMAC

src/libvirt.c:
  * Implement virNetworkGetDHCPLeases
  * Implement virNetworkGetDHCPLeaseForMAC
  * Implement virNetworkDHCPLeaseFree

src/libvirt_public.syms:
  * Export the new symbols

---
 include/libvirt/libvirt.h.in |  22 ++++++
 python/generator.py          |   3 +
 src/driver.h                 |  13 ++++
 src/libvirt.c                | 166 +++++++++++++++++++++++++++++++++++++++++++
 src/libvirt_public.syms      |   7 ++
 5 files changed, 211 insertions(+)

diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index a47e33c..3885baf 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -2800,6 +2800,28 @@ int                     virConnectNumOfDefinedInterfaces (virConnectPtr conn);
 int                     virConnectListDefinedInterfaces  (virConnectPtr conn,
                                                           char **const names,
                                                           int maxnames);
+
+typedef struct _virNetworkDHCPLeases virNetworkDHCPLeases;
+typedef virNetworkDHCPLeases *virNetworkDHCPLeasesPtr;
+struct _virNetworkDHCPLeases {
+    long long expirytime;
+    char *mac;
+    char *ipaddr;
+    char *hostname;
+    char *clientid;
+};
+
+void virNetworkDHCPLeaseFree(virNetworkDHCPLeasesPtr lease);
+
+int virNetworkGetDHCPLeases(virNetworkPtr network,
+                            virNetworkDHCPLeasesPtr **leases,
+                            unsigned int flags);
+
+int virNetworkGetDHCPLeaseForMAC(virNetworkPtr network,
+                                 const char *mac,
+                                 virNetworkDHCPLeasesPtr leases,
+                                 unsigned int flags);
+
 /*
  * virConnectListAllInterfaces:
  *
diff --git a/python/generator.py b/python/generator.py
index a91dde8..708657a 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -460,6 +460,8 @@ skip_impl = (
     'virNodeGetCPUMap',
     'virDomainMigrate3',
     'virDomainMigrateToURI3',
+    'virNetworkGetDHCPLeases',
+    'virNetworkGetDHCPLeaseForMAC',
 )
 
 lxc_skip_impl = (
@@ -560,6 +562,7 @@ skip_function = (
     "virTypedParamsGetString",
     "virTypedParamsGetUInt",
     "virTypedParamsGetULLong",
+    'virNetworkDHCPLeaseFree',
 )
 
 lxc_skip_function = (
diff --git a/src/driver.h b/src/driver.h
index be64333..4c835b2 100644
--- a/src/driver.h
+++ b/src/driver.h
@@ -1121,6 +1121,17 @@ typedef int
                                      int cookieinlen,
                                      unsigned int flags,
                                      int cancelled);
+typedef int
+(*virDrvNetworkGetDHCPLeases)(virNetworkPtr network,
+                              virNetworkDHCPLeasesPtr **leases,
+                              unsigned int flags);
+
+typedef int
+(*virDrvNetworkGetDHCPLeaseForMAC)(virNetworkPtr network,
+                                   const char *mac,
+                                   virNetworkDHCPLeasesPtr leases,
+                                   unsigned int flags);
+
 
 typedef struct _virDriver virDriver;
 typedef virDriver *virDriverPtr;
@@ -1451,6 +1462,8 @@ struct _virNetworkDriver {
     virDrvNetworkSetAutostart networkSetAutostart;
     virDrvNetworkIsActive networkIsActive;
     virDrvNetworkIsPersistent networkIsPersistent;
+    virDrvNetworkGetDHCPLeases networkGetDHCPLeases;
+    virDrvNetworkGetDHCPLeaseForMAC networkGetDHCPLeaseForMAC;
 };
 
 
diff --git a/src/libvirt.c b/src/libvirt.c
index 665b30b..63afdac 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -68,6 +68,7 @@
 #include "virstring.h"
 #include "virutil.h"
 #include "virtypedparam.h"
+#include "virmacaddr.h"
 
 #ifdef WITH_TEST
 # include "test/test_driver.h"
@@ -21961,3 +21962,168 @@ error:
     virDispatchError(dom->conn);
     return -1;
 }
+
+/**
+ * virNetworkGetDHCPLeases:
+ * @network: pointer to network object
+ * @leases: pointer to an array of pointers pointing to the obtained leases
+ * @flags: extra flags, not used yet, so callers should always pass 0
+ *
+ * The API returns lease info for all network interfaces connected to the
+ * given virtual network.
+ *
+ * Returns the number of leases found or -1 and sets @leases to NULL in case
+ * of error. On success, the array stored into @leases is guaranteed to have an
+ * extra allocated element set to NULL but not included in the return count,
+ * to make iteration easier. The caller is responsible for calling
+ * virNetworkDHCPLeaseFree on each array element, then calling free() on @leases.
+ *
+ * Example of usage:
+ *
+ * virNetworkDHCPLeasesPtr *leases = NULL;
+ * virNetworkPtr network = ... obtain a network pointer here ...;
+ * size_t i;
+ * int nleases;
+ * unsigned int flags = 0;
+ *
+ * nleases = virNetworkGetDHCPLeases(nleaseswork, &leases, flags);
+ * if (ret < 0)
+ *     error();
+ *
+ * ... do something with returned values, for example:
+ *
+ * for (i = 0; i < nleases; i++) {
+ *     virNetworkDHCPLeasesPtr lease = leases[i];
+ *     printf("Time(epoch): %lu, MAC address: %s,
+ *            IP address: %s, Hostname: %s, ClientID: %s\n",
+ *            expirytime, lease->mac,
+ *            lease->ipaddr, lease->hostname, lease->clientid);
+ * }
+ *
+ * if (leases) {
+ *     for (i = 0; i < nleases; i++)
+ *         virNetworkDHCPLeaseFree(leases[i]);
+ * }
+ * free(leases);
+ *
+ */
+int
+virNetworkGetDHCPLeases(virNetworkPtr network,
+                        virNetworkDHCPLeasesPtr **leases,
+                        unsigned int flags)
+{
+    virConnectPtr conn;
+    VIR_DEBUG("network=%p, leases=%p, flags=%x",
+               network, leases, flags);
+
+    virResetLastError();
+
+    virCheckNonNullArgGoto(network, error);
+
+    if (leases)
+        *leases = NULL;
+
+    if (!VIR_IS_CONNECTED_NETWORK(network)) {
+        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
+        virDispatchError(NULL);
+        return -1;
+    }
+
+    conn = network->conn;
+
+    if (conn->networkDriver && conn->networkDriver->networkGetDHCPLeases) {
+        int ret;
+        ret = conn->networkDriver->networkGetDHCPLeases(network,leases, flags);
+        if (ret < 0)
+            goto error;
+        return ret;
+    }
+
+    virLibConnError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
+
+error:
+    virDispatchError(network->conn);
+    return -1;
+}
+
+/**
+ * virNetworkGetDHCPLeaseForMAC:
+ * @network: pointer to network object
+ * @mac: MAC Address of an interface
+ * @lease: pointer to the obtained lease
+ * @flags: extra flags, not used yet, so callers should always pass 0
+ *
+ * The API returns the lease info of the interface which matches
+ * the given @mac
+ *
+ * Returns 1 on success, -1 otherwise. The caller is responsible for calling
+ * virNetworkDHCPLeaseFree to free the lease pointer @lease.
+ */
+int
+virNetworkGetDHCPLeaseForMAC(virNetworkPtr network,
+                             const char *mac,
+                             virNetworkDHCPLeasesPtr lease,
+                             unsigned int flags)
+{
+    virConnectPtr conn;
+    virMacAddr addr;
+
+    VIR_DEBUG("network=%p, mac=%s, lease=%p, flags=%x",
+               network, mac, lease, flags);
+
+    virResetLastError();
+
+    virCheckNonNullArgGoto(network, error);
+    virCheckNonNullArgGoto(mac, error);
+
+    if (!VIR_IS_CONNECTED_NETWORK(network)) {
+        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
+        virDispatchError(NULL);
+        return -1;
+    }
+
+    /* Validate the MAC address */
+    if (mac && virMacAddrParse(mac, &addr) < 0) {
+        virReportInvalidArg(mac,
+                            _("Given MAC Address doesn't comply "
+                              "with the standard (IEEE 802) format in %s"),
+                            __FUNCTION__);
+        goto error;
+    }
+
+    conn = network->conn;
+
+    if (conn->networkDriver &&
+        conn->networkDriver->networkGetDHCPLeaseForMAC) {
+        int ret;
+        ret = conn->networkDriver->networkGetDHCPLeaseForMAC(network, mac,
+                                                             lease, flags);
+        if (ret < 0)
+            goto error;
+        return ret;
+    }
+
+    virLibConnError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
+
+error:
+    virDispatchError(network->conn);
+    return -1;
+}
+
+/**
+ * virNetworkDHCPLeaseFree:
+ * @lease: pointer to a leases object
+ *
+ * Frees all the memory occupied by @lease.
+ */
+void
+virNetworkDHCPLeaseFree(virNetworkDHCPLeasesPtr lease)
+{
+    if (!lease)
+        return;
+    VIR_FREE(lease->mac);
+    VIR_FREE(lease->ipaddr);
+    VIR_FREE(lease->hostname);
+    VIR_FREE(lease->clientid);
+    VIR_FREE(lease);
+}
diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms
index bbdf78a..40b592f 100644
--- a/src/libvirt_public.syms
+++ b/src/libvirt_public.syms
@@ -634,4 +634,11 @@ LIBVIRT_1.1.1 {
         virDomainSetMemoryStatsPeriod;
 } LIBVIRT_1.1.0;
 
+LIBVIRT_1.1.3 {
+    global:
+       virNetworkGetDHCPLeases;
+       virNetworkGetDHCPLeaseForMAC;
+       virNetworkDHCPLeaseFree;
+} LIBVIRT_1.1.1;
+
 # .... define new API here using predicted next version number ....
-- 
1.7.11.7

--
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]