Introduce 3 new APIs, virNetworkGetDHCPLeases, virNetworkGetDHCPLeasesForMAC
and virNetworkDHCPLeaseFree.
* virNetworkGetDHCPLeases: returns the dhcp leases information for a given
virtual network. The information includes lease expirytime, MAC Address,
IP Address (with type and prefix), hostname and clientid.
* virNetworkGetDHCPLeasesForMAC: returns the dhcp leases 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 virNetworkGetDHCPLeasesForMAC
* Define virNetworkDHCPLeaseFree
python/generator.py:
* Skip the auto-generation for virNetworkGetDHCPLeases
* Skip the auto-generation for virNetworkGetDHCPLeasesForMAC
* Skip the auto-generation for virNetworkDHCPLeaseFree
src/driver.h:
* Define networkGetDHCPLeases
* Define networkGetDHCPLeasesForMAC
src/libvirt.c:
* Implement virNetworkGetDHCPLeases
* Implement virNetworkGetDHCPLeasesForMAC
* Implement virNetworkDHCPLeaseFree
src/libvirt_public.syms:
* Export the new symbols
---
include/libvirt/libvirt.h.in | 33 +++++++++
python/generator.py | 3 +
src/driver.h | 13 ++++
src/libvirt.c | 173 +++++++++++++++++++++++++++++++++++++++++++
src/libvirt_public.syms | 7 ++
5 files changed, 229 insertions(+)
diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index a47e33c..d92a720 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -2800,6 +2800,39 @@ int virConnectNumOfDefinedInterfaces
(virConnectPtr conn);
int virConnectListDefinedInterfaces (virConnectPtr conn,
char **const names,
int maxnames);
+
+typedef enum {
+ VIR_IP_ADDR_TYPE_IPV4,
+ VIR_IP_ADDR_TYPE_IPV6,
+
+#ifdef VIR_ENUM_SENTINELS
+ VIR_IP_ADDR_TYPE_LAST
+#endif
+} virIPAddrType;
+
+typedef struct _virNetworkDHCPLeases virNetworkDHCPLeases;
+typedef virNetworkDHCPLeases *virNetworkDHCPLeasesPtr;
+struct _virNetworkDHCPLeases {
+ long long expirytime;
+ char *mac; /* MAC address */
+ char *ipaddr; /* IP address */
+ char *hostname; /* Hostname */
+ char *clientid; /* Client ID */
+ int type; /* virIPAddrType */
+ unsigned int prefix; /* IP address prefix */
+};
+
+void virNetworkDHCPLeaseFree(virNetworkDHCPLeasesPtr lease);
+
+int virNetworkGetDHCPLeases(virNetworkPtr network,
+ virNetworkDHCPLeasesPtr **leases,
+ unsigned int flags);
+
+int virNetworkGetDHCPLeasesForMAC(virNetworkPtr network,
+ const char *mac,
+ virNetworkDHCPLeasesPtr **leases,
+ unsigned int flags);
+
/*
* virConnectListAllInterfaces:
*
diff --git a/python/generator.py b/python/generator.py
index a91dde8..e76cbfc 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -460,6 +460,8 @@ skip_impl = (
'virNodeGetCPUMap',
'virDomainMigrate3',
'virDomainMigrateToURI3',
+ 'virNetworkGetDHCPLeases',
+ 'virNetworkGetDHCPLeasesForMAC',
)
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..698e0ca 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
+(*virDrvNetworkGetDHCPLeasesForMAC)(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;
+ virDrvNetworkGetDHCPLeasesForMAC networkGetDHCPLeasesForMAC;
};
diff --git a/src/libvirt.c b/src/libvirt.c
index a6fcab0..05d20e9 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"
@@ -21966,3 +21967,175 @@ 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(network, &leases, flags);
+ * if (nleases < 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",
+ * leas->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;
+}
+
+/**
+ * virNetworkGetDHCPLeasesForMAC:
+ * @network: pointer to network object
+ * @mac: MAC Address of an interface
+ * @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 the leases info of the interface which matches with the
+ * given @mac. There can be multiple leases for a single @mac because this
+ * API intends to support DHCPv6 too.
+ *
+ * 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.
+ */
+int
+virNetworkGetDHCPLeasesForMAC(virNetworkPtr network,
+ const char *mac,
+ virNetworkDHCPLeasesPtr **leases,
+ unsigned int flags)
+{
+ virConnectPtr conn;
+ virMacAddr addr;
+
+ VIR_DEBUG("network=%p, mac=%s, leases=%p, flags=%x",
+ network, mac, leases, flags);
+
+ virResetLastError();
+
+ virCheckNonNullArgGoto(network, error);
+ virCheckNonNullArgGoto(mac, error);
+
+ if (leases)
+ *leases = NULL;
+
+ 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->networkGetDHCPLeasesForMAC) {
+ int ret;
+ ret = conn->networkDriver->networkGetDHCPLeasesForMAC(network, mac,
+ leases, 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..e952869 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;
+ virNetworkGetDHCPLeasesForMAC;
+ virNetworkDHCPLeaseFree;
+} LIBVIRT_1.1.1;
+
# .... define new API here using predicted next version number ....
--
1.7.11.7