Add stroke CHILD_SA rekeying counter
[strongswan.git] / src / libcharon / plugins / stroke / stroke_counter.c
index db0c4d9..ab2882e 100644 (file)
 
 #include "stroke_counter.h"
 
+#include <threading/spinlock.h>
+
+ENUM(stroke_counter_type_names,
+       COUNTER_INIT_IKE_SA_REKEY, COUNTER_OUT_INFORMATIONAL_RSP,
+       "ikeInitRekey",
+       "ikeRspRekey",
+       "ikeChildSaRekey",
+       "ikeInInvalid",
+       "ikeInInvalidSpi",
+       "ikeInInitReq",
+       "ikeInInitRsp",
+       "ikeOutInitReq",
+       "ikeOutInitRsp",
+       "ikeInAuthReq",
+       "ikeInAuthRsp",
+       "ikeOutAuthReq",
+       "ikeOutAuthRsp",
+       "ikeInCrChildReq",
+       "ikeInCrChildRsp",
+       "ikeOutCrChildReq",
+       "ikeOutCrChildRsp",
+       "ikeInInfoReq",
+       "ikeInInfoRsp",
+       "ikeOutInfoReq",
+       "ikeOutInfoRsp",
+);
+
 typedef struct private_stroke_counter_t private_stroke_counter_t;
 
 /**
@@ -27,11 +54,55 @@ struct private_stroke_counter_t {
         */
        stroke_counter_t public;
 
+       /**
+        * Counter values
+        */
+       u_int64_t counter[COUNTER_MAX];
+
+       /**
+        * Lock for counter values
+        */
+       spinlock_t *lock;
 };
 
+METHOD(listener_t, ike_rekey, bool,
+       private_stroke_counter_t *this, ike_sa_t *old, ike_sa_t *new)
+{
+       stroke_counter_type_t type;
+       ike_sa_id_t *id;
+
+       id = new->get_id(new);
+       if (id->is_initiator(id))
+       {
+               type = COUNTER_INIT_IKE_SA_REKEY;
+       }
+       else
+       {
+               type = COUNTER_RESP_IKE_SA_REKEY;
+       }
+
+       this->lock->lock(this->lock);
+       this->counter[type]++;
+       this->lock->unlock(this->lock);
+
+       return TRUE;
+}
+
+METHOD(listener_t, child_rekey, bool,
+       private_stroke_counter_t *this, ike_sa_t *ike_sa,
+       child_sa_t *old, child_sa_t *new)
+{
+       this->lock->lock(this->lock);
+       this->counter[COUNTER_CHILD_SA_REKEY]++;
+       this->lock->unlock(this->lock);
+
+       return TRUE;
+}
+
 METHOD(stroke_counter_t, destroy, void,
        private_stroke_counter_t *this)
 {
+       this->lock->destroy(this->lock);
        free(this);
 }
 
@@ -44,8 +115,13 @@ stroke_counter_t *stroke_counter_create()
 
        INIT(this,
                .public = {
+                       .listener = {
+                               .ike_rekey = _ike_rekey,
+                               .child_rekey = _child_rekey,
+                       },
                        .destroy = _destroy,
                },
+               .lock = spinlock_create(),
        );
 
        return &this->public;