[PATCH] test_driver: implement virNetworkGetDHCPLeases

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

 



Return infinite time DHCP leases for fixed IPV4 addresses.

Signed-off-by: Ilias Stamatis <stamatis.iliass@xxxxxxxxx>
---
 src/test/test_driver.c | 118 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 118 insertions(+)

diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index 1aa79ce898..b7f8f6ecf2 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -3831,6 +3831,123 @@ testNetworkSetAutostart(virNetworkPtr net,
 }


+static int
+testNetworkGetDHCPLeases(virNetworkPtr net,
+                         const char *mac,
+                         virNetworkDHCPLeasePtr **leases,
+                         unsigned int flags)
+{
+    int ret = -1;
+    int ndomains = 0;
+    size_t i, j;
+    size_t nleases = 0;
+    bool need_results = !!leases;
+    char *hostname = NULL;
+    char mac_str[VIR_MAC_STRING_BUFLEN];
+    virMacAddr mac_addr;
+    virDomainObjPtr vm = NULL;
+    virDomainPtr *domains = NULL;
+    virDomainNetDefPtr inf = NULL;
+    virNetworkObjPtr obj = NULL;
+    virNetworkDefPtr obj_def = NULL;
+    virNetworkDHCPLeasePtr lease = NULL;
+    virNetworkDHCPLeasePtr *leases_ret = NULL;
+    testDriverPtr privconn = net->conn->privateData;
+
+    virCheckFlags(0, -1);
+
+    if (mac && virMacAddrParse(mac, &mac_addr) < 0) {
+        virReportError(VIR_ERR_INVALID_MAC, "%s", mac);
+        return -1;
+    }
+
+    if (!(obj = testNetworkObjFindByName(privconn, net->name)))
+        goto cleanup;
+    obj_def = virNetworkObjGetDef(obj);
+
+    ndomains = virDomainObjListExport(privconn->domains, net->conn, &domains,
+                                      NULL, VIR_CONNECT_LIST_DOMAINS_ACTIVE);
+    if (ndomains < 0)
+        goto cleanup;
+
+    for (i = 0; i < ndomains; i++) {
+        /* the following must be called before testDomObjFromDomain */
+        hostname = testDomainGetHostname(domains[i], 0);
+
+        if (!(vm = testDomObjFromDomain(domains[i])))
+            continue;
+
+        for (j = 0; j < vm->def->nnets; j++) {
+            inf = vm->def->nets[j];
+            if (STRNEQ(inf->data.network.name, obj_def->name))
+                continue;
+
+            virMacAddrFormat(&inf->mac, mac_str);
+            if (mac && virMacAddrCompare(mac, mac_str))
+                continue;
+
+            if (!need_results) {
+                nleases++;
+                continue;
+            }
+
+            if (VIR_ALLOC(lease) < 0)
+                goto error;
+
+            /* the lease is for infinite time */
+            lease->expirytime = 0;
+
+            if ((VIR_STRDUP(lease->mac, mac_str) < 0) ||
+                (VIR_STRDUP(lease->iface, obj_def->bridge) < 0) ||
+                (VIR_STRDUP(lease->hostname, hostname) < 0))
+                goto error;
+
+            lease->prefix = 24;
+            lease->type = VIR_IP_ADDR_TYPE_IPV4;
+            if (virAsprintf(&lease->ipaddr, "192.168.0.%zu", 1 + j) < 0)
+                goto error;
+
+            lease->iaid = lease->clientid = NULL;
+
+            if (VIR_INSERT_ELEMENT(leases_ret, nleases, nleases, lease) < 0)
+                goto error;
+        }
+
+        VIR_FREE(hostname);
+        virDomainObjEndAPI(&vm);
+    }
+
+    if (leases_ret) {
+        /* NULL terminated array */
+        ignore_value(VIR_REALLOC_N(leases_ret, nleases + 1));
+        VIR_STEAL_PTR(*leases, leases_ret);
+    }
+
+    ret = nleases;
+
+ cleanup:
+    for (i = 0; i < ndomains; i++)
+        virDomainFree(domains[i]);
+    VIR_FREE(domains);
+
+    VIR_FREE(hostname);
+    virNetworkObjEndAPI(&obj);
+    return ret;
+
+ error:
+    VIR_FREE(lease);
+
+    if (leases_ret) {
+        for (i = 0; i < nleases; i++)
+            virNetworkDHCPLeaseFree(leases_ret[i]);
+        VIR_FREE(leases_ret);
+    }
+
+    virDomainObjEndAPI(&vm);
+    goto cleanup;
+}
+
+
 /*
  * Physical host interface routines
  */
@@ -7114,6 +7231,7 @@ static virNetworkDriver testNetworkDriver = {
     .networkGetBridgeName = testNetworkGetBridgeName, /* 0.3.2 */
     .networkGetAutostart = testNetworkGetAutostart, /* 0.3.2 */
     .networkSetAutostart = testNetworkSetAutostart, /* 0.3.2 */
+    .networkGetDHCPLeases = testNetworkGetDHCPLeases, /* 5.5.0 */
     .networkIsActive = testNetworkIsActive, /* 0.7.3 */
     .networkIsPersistent = testNetworkIsPersistent, /* 0.7.3 */
 };
--
2.21.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