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

[libvirt] [PATCH 3/4] net-dhcp-leases: Private implementation inside network driver



By querying the driver for the path of the leases file for the given virtual
network and parsing it to retrieve info.

src/network/bridge_driver.c:
  * Implement networkGetDHCPLeases
  * Implement networkGetDHCPLeaseForMAC
  * Implement networkGetDHCPLeasesHelper

---
 src/network/bridge_driver.c | 222 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 222 insertions(+)

diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index 3a8be90..e5923eb 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -109,6 +109,35 @@ static int networkPlugBandwidth(virNetworkObjPtr net,
                                 virDomainNetDefPtr iface);
 static int networkUnplugBandwidth(virNetworkObjPtr net,
                                   virDomainNetDefPtr iface);
+/**
+ * VIR_NETWORK_DHCPLEASE_LENGTH_MAX:
+ *
+ * Macro providing the maximum length of an entry in the leases file
+ * Refer: http://lists.thekelleys.org.uk/pipermail/dnsmasq-discuss/2013q3/007402.html
+ */
+#define VIR_NETWORK_DHCPLEASE_LENGTH_MAX 2048
+
+/**
+ * VIR_NETWORK_DHCPLEASE_PARAMS:
+ *
+ * Macro providing the maximum number of parameters in an entry in
+ * the leases file
+ */
+#define VIR_NETWORK_DHCPLEASE_FIELDS 5
+
+static int networkGetDHCPLeases(virNetworkPtr network,
+                                virNetworkDHCPLeasesPtr **leases,
+                                unsigned int flags);
+
+static int networkGetDHCPLeaseForMAC(virNetworkPtr network,
+                                     const char *mac,
+                                     virNetworkDHCPLeasesPtr lease,
+                                     unsigned int flags);
+
+static int networkGetDHCPLeasesHelper(virNetworkPtr network,
+                                      const char *mac,
+                                      virNetworkDHCPLeasesPtr **leases,
+                                      unsigned int flags);
 
 static virNetworkDriverStatePtr driverState = NULL;
 
@@ -2980,6 +3009,197 @@ cleanup:
     return ret;
 }
 
+static int
+networkGetDHCPLeases(virNetworkPtr network,
+                     virNetworkDHCPLeasesPtr **leases,
+                     unsigned int flags)
+{
+    int rv = -1;
+    virNetworkDriverStatePtr driver = network->conn->networkPrivateData;
+    virNetworkObjPtr obj;
+
+    virCheckFlags(0, -1);
+
+    networkDriverLock(driver);
+    obj = virNetworkFindByName(&driver->networks, network->name);
+    networkDriverUnlock(driver);
+
+    if (virNetworkGetDHCPLeasesEnsureACL(network->conn, obj->def) < 0)
+        goto cleanup;
+
+    rv = networkGetDHCPLeasesHelper(network, NULL, leases, flags);
+
+cleanup:
+    if (obj)
+        virNetworkObjUnlock(obj);
+    return rv;
+}
+
+static int
+networkGetDHCPLeaseForMAC(virNetworkPtr network,
+                          const char *mac,
+                          virNetworkDHCPLeasesPtr lease,
+                          unsigned int flags)
+{
+    virNetworkDHCPLeasesPtr *leases = NULL;
+    int nleases = 0;
+    int rv = -1;
+    size_t i;
+
+    virNetworkDriverStatePtr driver = network->conn->networkPrivateData;
+    virNetworkObjPtr obj;
+
+    virCheckFlags(0, -1);
+
+    networkDriverLock(driver);
+    obj = virNetworkFindByName(&driver->networks, network->name);
+    networkDriverUnlock(driver);
+
+    if (virNetworkGetDHCPLeaseForMACEnsureACL(network->conn, obj->def) < 0)
+        goto cleanup;
+
+    if ((nleases = networkGetDHCPLeasesHelper(network, mac,
+                                              &leases, flags)) <= 0)
+        goto error;
+
+    if (nleases > 1) {
+        /* Hmm, can DHCP give more than one lease for same MAC? */
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Something is wrong. MAC address "
+                         "can't repeat in the leases"));
+        goto error;
+    }
+
+    lease->expirytime = leases[0]->expirytime;
+
+    if ((VIR_STRDUP(lease->mac, leases[0]->mac) < 0) ||
+        (VIR_STRDUP(lease->ipaddr, leases[0]->ipaddr) < 0) ||
+        (VIR_STRDUP(lease->hostname, leases[0]->hostname) < 0) ||
+        (VIR_STRDUP(lease->clientid, leases[0]->clientid) < 0))
+        goto error;
+
+    rv = nleases;
+
+cleanup:
+    if (obj)
+        virNetworkObjUnlock(obj);
+    return rv;
+
+error:
+    if (leases) {
+        for (i = 0; i < nleases; i++)
+            virNetworkDHCPLeaseFree(leases[i]);
+    }
+    VIR_FREE(leases);
+    goto cleanup;
+}
+
+static int
+networkGetDHCPLeasesHelper(virNetworkPtr network,
+                           const char *mac,
+                           virNetworkDHCPLeasesPtr **leases,
+                           unsigned int flags)
+{
+    char *leasefile;
+    char **leaseparams;
+    char dhcpentry[VIR_NETWORK_DHCPLEASE_LENGTH_MAX];
+    FILE *fp = NULL;
+    int rv = -1;
+
+    virNetworkDHCPLeasesPtr *leases_ret = NULL;
+    size_t nleases = 0;
+    size_t i = 0;
+
+
+    virCheckFlags(0, -1);
+
+    if (!network) {
+        virReportError(VIR_ERR_NO_NETWORK, "%s",
+                       _("no network with matching name"));
+        return -1;
+    }
+
+    /* Retrive leases file location */
+    leasefile = networkDnsmasqLeaseFileNameDefault(network->name);
+    if (!(fp = fopen(leasefile, "r"))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unable to open leases file: %s"), leasefile);
+        goto error;
+    }
+
+    while (fgets(dhcpentry, sizeof(dhcpentry), fp) != NULL) {
+        virNetworkDHCPLeasesPtr lease = NULL;
+
+        /* Remove newline */
+        dhcpentry[strlen(dhcpentry) - 1] = '\0';
+
+        /* split the lease line */
+        leaseparams = virStringSplit(dhcpentry, " ", VIR_NETWORK_DHCPLEASE_FIELDS);
+
+        if (virStringListLength(leaseparams) != VIR_NETWORK_DHCPLEASE_FIELDS) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Number of lease params aren't equal to: %d"),
+                           VIR_NETWORK_DHCPLEASE_FIELDS);
+            goto error;
+        }
+
+        if (mac && STRNEQ(mac, leaseparams[1]))
+            continue;
+
+        /* We don't know the total number of leases yet */
+        if (VIR_EXPAND_N(leases_ret, nleases, 1) < 0)
+            goto error;
+
+        if (VIR_ALLOC(leases_ret[nleases - 1]) < 0)
+            goto error;
+
+        lease = leases_ret[nleases - 1];
+
+        /* Convert expirytime here */
+        if (virStrToLong_ll(leaseparams[0], NULL, 10, &(lease->expirytime)) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Unable to convert lease expiry time to integer: %s"),
+                           leaseparams[0]);
+            goto error;
+        }
+
+        if ((VIR_STRDUP(lease->mac, leaseparams[1]) < 0) ||
+            (VIR_STRDUP(lease->ipaddr, leaseparams[2]) < 0) ||
+            (VIR_STRDUP(lease->hostname, leaseparams[3]) < 0) ||
+            (VIR_STRDUP(lease->clientid, leaseparams[4]) < 0))
+            goto error;
+
+        if (mac)
+            break;
+    }
+
+    if (mac && !leases_ret) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("no lease with matching MAC address: %s"), mac);
+        goto error;
+    }
+
+    if (leases_ret) {
+        /* trim the array to the final size */
+        ignore_value(VIR_REALLOC_N(leases_ret, nleases + 1));
+        *leases = leases_ret;
+        leases_ret = NULL;
+        rv = nleases;
+    }
+
+cleanup:
+    VIR_FORCE_FCLOSE(fp);
+    VIR_FREE(leasefile);
+    return rv;
+
+error:
+    if (leases_ret) {
+        for (i = 0; i < nleases; i++)
+            virNetworkDHCPLeaseFree(leases_ret[i]);
+    }
+    VIR_FREE(leases_ret);
+    goto cleanup;
+}
 
 static virNetworkDriver networkDriver = {
     "Network",
@@ -3004,6 +3224,8 @@ static virNetworkDriver networkDriver = {
     .networkSetAutostart = networkSetAutostart, /* 0.2.1 */
     .networkIsActive = networkIsActive, /* 0.7.3 */
     .networkIsPersistent = networkIsPersistent, /* 0.7.3 */
+    .networkGetDHCPLeases = networkGetDHCPLeases, /* 1.1.3 */
+    .networkGetDHCPLeaseForMAC = networkGetDHCPLeaseForMAC, /* 1.1.3 */
 };
 
 static virStateDriver networkStateDriver = {
-- 
1.7.11.7


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