vici: Add option to query leases of pools
authorTobias Brunner <tobias@strongswan.org>
Mon, 19 Oct 2015 13:35:51 +0000 (15:35 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 10 Nov 2015 09:43:25 +0000 (10:43 +0100)
We could later perhaps add filter parameters similar to those of the
`ipsec leases` command (pool name/virtual IP).

src/libcharon/plugins/vici/README.md
src/libcharon/plugins/vici/vici_attribute.c

index a0522b5..b9531d8 100644 (file)
@@ -526,12 +526,21 @@ Unloading fails for pools with leases currently online.
 
 List the currently loaded pools.
 
-       {} => {
+       {
+               leases = <set to yes to include leases>
+       } => {
                <pool name>* = {
                        base = <virtual IP pool base address>
                        size = <total number of addresses in the pool>
                        online = <number of leases online>
                        offline = <number of leases offline>
+                       leases = {
+                               <zero-based index>* = {
+                                       address = <IP address>
+                                       identity = <assigned identity>
+                                       status = <online|offline>
+                               }
+                       }
                }
        }
 
index f04bae7..9064d3d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014 Tobias Brunner
+ * Copyright (C) 2014-2015 Tobias Brunner
  * Hochschule fuer Technik Rapperswil
  *
  * Copyright (C) 2014 Martin Willi
@@ -662,9 +662,16 @@ CALLBACK(get_pools, vici_message_t*,
        vici_message_t *message)
 {
        vici_builder_t *builder;
-       enumerator_t *enumerator;
+       enumerator_t *enumerator, *leases;
        mem_pool_t *vips;
        pool_t *pool;
+       identification_t *uid;
+       host_t *lease;
+       bool list_leases, on;
+       char buf[32];
+       int i;
+
+       list_leases = message->get_bool(message, FALSE, "leases");
 
        builder = vici_builder_create();
 
@@ -681,6 +688,23 @@ CALLBACK(get_pools, vici_message_t*,
                builder->add_kv(builder, "online", "%u", vips->get_online(vips));
                builder->add_kv(builder, "offline", "%u", vips->get_offline(vips));
 
+               if (list_leases)
+               {
+                       i = 0;
+                       builder->begin_section(builder, "leases");
+                       leases = vips->create_lease_enumerator(vips);
+                       while (leases && leases->enumerate(leases, &uid, &lease, &on))
+                       {
+                               snprintf(buf, sizeof(buf), "%d", i++);
+                               builder->begin_section(builder, buf);
+                               builder->add_kv(builder, "address", "%H", lease);
+                               builder->add_kv(builder, "identity", "%Y", uid);
+                               builder->add_kv(builder, "status", on ? "online" : "offline");
+                               builder->end_section(builder);
+                       }
+                       leases->destroy(leases);
+                       builder->end_section(builder);
+               }
                builder->end_section(builder);
        }
        enumerator->destroy(enumerator);