list assigned leases using "ipsec leases"
[strongswan.git] / src / charon / plugins / stroke / stroke_list.c
index b0791ce..f9e6567 100644 (file)
@@ -1000,6 +1000,77 @@ static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
 }
 
 /**
 }
 
 /**
+ * Print leases of a single pool
+ */
+static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
+                                               host_t *address, u_int size, u_int online, u_int offline)
+{
+       enumerator_t *enumerator;
+       identification_t *id;
+       host_t *lease;
+       bool on;
+       int found = 0;
+       
+       fprintf(out, "Leases in pool '%s', usage: %lu/%lu, %lu online\n",
+                       pool, online + offline, size, online);
+       enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
+       while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
+       {
+               if (!address || address->ip_equals(address, lease))
+               {
+                       fprintf(out, "  %15H   %s   '%D'\n",
+                                       lease, on ? "online" : "offline", id);
+                       found++;
+               }
+       }
+       enumerator->destroy(enumerator);
+       if (!found)
+       {
+               fprintf(out, "  no matching leases found\n");
+       }
+}
+
+/**
+ * Implementation of stroke_list_t.leases
+ */
+static void leases(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
+{
+       enumerator_t *enumerator;
+       u_int size, offline, online;
+       host_t *address = NULL;
+       char *pool;
+       int found = 0;
+       
+       if (msg->leases.address)
+       {
+               address = host_create_from_string(msg->leases.address, 0);
+       }
+       
+       enumerator = this->attribute->create_pool_enumerator(this->attribute);
+       while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
+       {
+               if (!msg->leases.pool || streq(msg->leases.pool, pool))
+               {
+                       pool_leases(this, out, pool, address, size, online, offline);
+                       found++;
+               }
+       }
+       enumerator->destroy(enumerator);
+       if (!found)
+       {
+               if (msg->leases.pool)
+               {
+                       fprintf(out, "pool '%s' not found\n", msg->leases.pool);
+               }
+               else
+               {
+                       fprintf(out, "no pools found\n");
+               }
+       }
+       DESTROY_IF(address);
+}
+
+/**
  * Implementation of stroke_list_t.destroy
  */
 static void destroy(private_stroke_list_t *this)
  * Implementation of stroke_list_t.destroy
  */
 static void destroy(private_stroke_list_t *this)
@@ -1016,6 +1087,7 @@ stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
        
        this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
        this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
        
        this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
        this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
+       this->public.leases = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))leases;
        this->public.destroy = (void(*)(stroke_list_t*))destroy;
        
        this->uptime = time(NULL);
        this->public.destroy = (void(*)(stroke_list_t*))destroy;
        
        this->uptime = time(NULL);