2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
18 #include "ike_auth_lifetime.h"
21 #include <encoding/payloads/notify_payload.h>
24 typedef struct private_ike_auth_lifetime_t private_ike_auth_lifetime_t
;
27 * Private members of a ike_auth_lifetime_t task.
29 struct private_ike_auth_lifetime_t
{
32 * Public methods and task_t interface.
34 ike_auth_lifetime_t
public;
43 * add the AUTH_LIFETIME notify to the message
45 static void add_auth_lifetime(private_ike_auth_lifetime_t
*this, message_t
*message
)
50 lifetime
= this->ike_sa
->get_statistic(this->ike_sa
, STAT_REAUTH_TIME
);
53 chunk
= chunk_from_thing(lifetime
);
54 *(u_int32_t
*)chunk
.ptr
= htonl(lifetime
);
55 message
->add_notify(message
, FALSE
, AUTH_LIFETIME
, chunk
);
60 * read notifys from message and evaluate them
62 static void process_payloads(private_ike_auth_lifetime_t
*this, message_t
*message
)
66 notify_payload_t
*notify
;
68 iterator
= message
->get_payload_iterator(message
);
69 while (iterator
->iterate(iterator
, (void**)&payload
))
71 if (payload
->get_type(payload
) == NOTIFY
)
73 notify
= (notify_payload_t
*)payload
;
74 switch (notify
->get_notify_type(notify
))
78 chunk_t data
= notify
->get_notification_data(notify
);
79 u_int32_t lifetime
= ntohl(*(u_int32_t
*)data
.ptr
);
80 this->ike_sa
->set_auth_lifetime(this->ike_sa
, lifetime
);
88 iterator
->destroy(iterator
);
92 * Implementation of task_t.process for initiator
94 static status_t
build_i(private_ike_auth_lifetime_t
*this, message_t
*message
)
96 if (message
->get_exchange_type(message
) == INFORMATIONAL
)
98 add_auth_lifetime(this, message
);
105 * Implementation of task_t.process for responder
107 static status_t
process_r(private_ike_auth_lifetime_t
*this, message_t
*message
)
109 if (message
->get_exchange_type(message
) == INFORMATIONAL
)
111 process_payloads(this, message
);
118 * Implementation of task_t.build for responder
120 static status_t
build_r(private_ike_auth_lifetime_t
*this, message_t
*message
)
122 if (message
->get_exchange_type(message
) == IKE_AUTH
&&
123 this->ike_sa
->get_state(this->ike_sa
) == IKE_ESTABLISHED
)
125 add_auth_lifetime(this, message
);
132 * Implementation of task_t.process for initiator
134 static status_t
process_i(private_ike_auth_lifetime_t
*this, message_t
*message
)
136 if (message
->get_exchange_type(message
) == IKE_AUTH
&&
137 this->ike_sa
->get_state(this->ike_sa
) == IKE_ESTABLISHED
)
139 process_payloads(this, message
);
146 * Implementation of task_t.get_type
148 static task_type_t
get_type(private_ike_auth_lifetime_t
*this)
150 return IKE_AUTH_LIFETIME
;
154 * Implementation of task_t.migrate
156 static void migrate(private_ike_auth_lifetime_t
*this, ike_sa_t
*ike_sa
)
158 this->ike_sa
= ike_sa
;
162 * Implementation of task_t.destroy
164 static void destroy(private_ike_auth_lifetime_t
*this)
170 * Described in header.
172 ike_auth_lifetime_t
*ike_auth_lifetime_create(ike_sa_t
*ike_sa
, bool initiator
)
174 private_ike_auth_lifetime_t
*this = malloc_thing(private_ike_auth_lifetime_t
);
176 this->public.task
.get_type
= (task_type_t(*)(task_t
*))get_type
;
177 this->public.task
.migrate
= (void(*)(task_t
*,ike_sa_t
*))migrate
;
178 this->public.task
.destroy
= (void(*)(task_t
*))destroy
;
182 this->public.task
.build
= (status_t(*)(task_t
*,message_t
*))build_i
;
183 this->public.task
.process
= (status_t(*)(task_t
*,message_t
*))process_i
;
187 this->public.task
.build
= (status_t(*)(task_t
*,message_t
*))build_r
;
188 this->public.task
.process
= (status_t(*)(task_t
*,message_t
*))process_r
;
191 this->ike_sa
= ike_sa
;
193 return &this->public;