use more generic stats getter, introducing new stats
[strongswan.git] / src / charon / sa / tasks / ike_auth_lifetime.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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 * $Id$
16 */
17
18 #include "ike_auth_lifetime.h"
19
20 #include <daemon.h>
21 #include <encoding/payloads/notify_payload.h>
22
23
24 typedef struct private_ike_auth_lifetime_t private_ike_auth_lifetime_t;
25
26 /**
27 * Private members of a ike_auth_lifetime_t task.
28 */
29 struct private_ike_auth_lifetime_t {
30
31 /**
32 * Public methods and task_t interface.
33 */
34 ike_auth_lifetime_t public;
35
36 /**
37 * Assigned IKE_SA.
38 */
39 ike_sa_t *ike_sa;
40 };
41
42 /**
43 * add the AUTH_LIFETIME notify to the message
44 */
45 static void add_auth_lifetime(private_ike_auth_lifetime_t *this, message_t *message)
46 {
47 chunk_t chunk;
48 u_int32_t lifetime;
49
50 lifetime = this->ike_sa->get_statistic(this->ike_sa, STAT_REAUTH);
51 if (lifetime)
52 {
53 lifetime -= time(NULL);
54 chunk = chunk_from_thing(lifetime);
55 *(u_int32_t*)chunk.ptr = htonl(lifetime);
56 message->add_notify(message, FALSE, AUTH_LIFETIME, chunk);
57 }
58 }
59
60 /**
61 * read notifys from message and evaluate them
62 */
63 static void process_payloads(private_ike_auth_lifetime_t *this, message_t *message)
64 {
65 iterator_t *iterator;
66 payload_t *payload;
67 notify_payload_t *notify;
68
69 iterator = message->get_payload_iterator(message);
70 while (iterator->iterate(iterator, (void**)&payload))
71 {
72 if (payload->get_type(payload) == NOTIFY)
73 {
74 notify = (notify_payload_t*)payload;
75 switch (notify->get_notify_type(notify))
76 {
77 case AUTH_LIFETIME:
78 {
79 chunk_t data = notify->get_notification_data(notify);
80 u_int32_t lifetime = ntohl(*(u_int32_t*)data.ptr);
81 this->ike_sa->set_auth_lifetime(this->ike_sa, lifetime);
82 break;
83 }
84 default:
85 break;
86 }
87 }
88 }
89 iterator->destroy(iterator);
90 }
91
92 /**
93 * Implementation of task_t.process for initiator
94 */
95 static status_t build_i(private_ike_auth_lifetime_t *this, message_t *message)
96 {
97 if (message->get_exchange_type(message) == INFORMATIONAL)
98 {
99 add_auth_lifetime(this, message);
100 return SUCCESS;
101 }
102 return NEED_MORE;
103 }
104
105 /**
106 * Implementation of task_t.process for responder
107 */
108 static status_t process_r(private_ike_auth_lifetime_t *this, message_t *message)
109 {
110 if (message->get_exchange_type(message) == INFORMATIONAL)
111 {
112 process_payloads(this, message);
113 return SUCCESS;
114 }
115 return NEED_MORE;
116 }
117
118 /**
119 * Implementation of task_t.build for responder
120 */
121 static status_t build_r(private_ike_auth_lifetime_t *this, message_t *message)
122 {
123 if (message->get_exchange_type(message) == IKE_AUTH &&
124 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
125 {
126 add_auth_lifetime(this, message);
127 return SUCCESS;
128 }
129 return NEED_MORE;
130 }
131
132 /**
133 * Implementation of task_t.process for initiator
134 */
135 static status_t process_i(private_ike_auth_lifetime_t *this, message_t *message)
136 {
137 if (message->get_exchange_type(message) == IKE_AUTH &&
138 this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
139 {
140 process_payloads(this, message);
141 return SUCCESS;
142 }
143 return NEED_MORE;
144 }
145
146 /**
147 * Implementation of task_t.get_type
148 */
149 static task_type_t get_type(private_ike_auth_lifetime_t *this)
150 {
151 return IKE_AUTH_LIFETIME;
152 }
153
154 /**
155 * Implementation of task_t.migrate
156 */
157 static void migrate(private_ike_auth_lifetime_t *this, ike_sa_t *ike_sa)
158 {
159 this->ike_sa = ike_sa;
160 }
161
162 /**
163 * Implementation of task_t.destroy
164 */
165 static void destroy(private_ike_auth_lifetime_t *this)
166 {
167 free(this);
168 }
169
170 /*
171 * Described in header.
172 */
173 ike_auth_lifetime_t *ike_auth_lifetime_create(ike_sa_t *ike_sa, bool initiator)
174 {
175 private_ike_auth_lifetime_t *this = malloc_thing(private_ike_auth_lifetime_t);
176
177 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
178 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
179 this->public.task.destroy = (void(*)(task_t*))destroy;
180
181 if (initiator)
182 {
183 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
184 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
185 }
186 else
187 {
188 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
189 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
190 }
191
192 this->ike_sa = ike_sa;
193
194 return &this->public;
195 }
196