b46ca0d6c0008ea3ba97ec0518a50b8c67c8a24d
[strongswan.git] / src / libcharon / plugins / stroke / stroke_counter.c
1 /*
2 * Copyright (C) 2012 Martin Willi
3 * Copyright (C) 2012 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "stroke_counter.h"
17
18 #include <threading/spinlock.h>
19
20 ENUM(stroke_counter_type_names,
21 COUNTER_INIT_IKE_SA_REKEY, COUNTER_OUT_INFORMATIONAL_RSP,
22 "ikeInitRekey",
23 "ikeRspRekey",
24 "ikeChildSaRekey",
25 "ikeInInvalid",
26 "ikeInInvalidSpi",
27 "ikeInInitReq",
28 "ikeInInitRsp",
29 "ikeOutInitReq",
30 "ikeOutInitRsp",
31 "ikeInAuthReq",
32 "ikeInAuthRsp",
33 "ikeOutAuthReq",
34 "ikeOutAuthRsp",
35 "ikeInCrChildReq",
36 "ikeInCrChildRsp",
37 "ikeOutCrChildReq",
38 "ikeOutCrChildRsp",
39 "ikeInInfoReq",
40 "ikeInInfoRsp",
41 "ikeOutInfoReq",
42 "ikeOutInfoRsp",
43 );
44
45 typedef struct private_stroke_counter_t private_stroke_counter_t;
46
47 /**
48 * Private data of an stroke_counter_t object.
49 */
50 struct private_stroke_counter_t {
51
52 /**
53 * Public stroke_counter_t interface.
54 */
55 stroke_counter_t public;
56
57 /**
58 * Counter values
59 */
60 u_int64_t counter[COUNTER_MAX];
61
62 /**
63 * Lock for counter values
64 */
65 spinlock_t *lock;
66 };
67
68 METHOD(listener_t, ike_rekey, bool,
69 private_stroke_counter_t *this, ike_sa_t *old, ike_sa_t *new)
70 {
71 stroke_counter_type_t type;
72 ike_sa_id_t *id;
73
74 id = new->get_id(new);
75 if (id->is_initiator(id))
76 {
77 type = COUNTER_INIT_IKE_SA_REKEY;
78 }
79 else
80 {
81 type = COUNTER_RESP_IKE_SA_REKEY;
82 }
83
84 this->lock->lock(this->lock);
85 this->counter[type]++;
86 this->lock->unlock(this->lock);
87
88 return TRUE;
89 }
90
91 METHOD(stroke_counter_t, destroy, void,
92 private_stroke_counter_t *this)
93 {
94 this->lock->destroy(this->lock);
95 free(this);
96 }
97
98 /**
99 * See header
100 */
101 stroke_counter_t *stroke_counter_create()
102 {
103 private_stroke_counter_t *this;
104
105 INIT(this,
106 .public = {
107 .listener = {
108 .ike_rekey = _ike_rekey,
109 },
110 .destroy = _destroy,
111 },
112 .lock = spinlock_create(),
113 );
114
115 return &this->public;
116 }