removed deprecated iterator methods (has_next & current)
[strongswan.git] / src / charon / sa / transactions / delete_ike_sa.c
1 /**
2 * @file delete_ike_sa.c
3 *
4 * @brief Implementation of the delete_ike_sa transaction.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "delete_ike_sa.h"
24
25 #include <daemon.h>
26 #include <encoding/payloads/delete_payload.h>
27
28 typedef struct private_delete_ike_sa_t private_delete_ike_sa_t;
29
30 /**
31 * Private members of a delete_ike_sa_t object..
32 */
33 struct private_delete_ike_sa_t {
34
35 /**
36 * Public methods and transaction_t interface.
37 */
38 delete_ike_sa_t public;
39
40 /**
41 * Assigned IKE_SA.
42 */
43 ike_sa_t *ike_sa;
44
45 /**
46 * Message sent by our peer, if already generated
47 */
48 message_t *message;
49
50 /**
51 * Message ID this transaction uses
52 */
53 u_int32_t message_id;
54
55 /**
56 * Times we did send the request
57 */
58 u_int32_t requested;
59 };
60
61 /**
62 * Implementation of transaction_t.get_message_id.
63 */
64 static u_int32_t get_message_id(private_delete_ike_sa_t *this)
65 {
66 return this->message_id;
67 }
68
69 /**
70 * Implementation of transaction_t.requested.
71 */
72 static u_int32_t requested(private_delete_ike_sa_t *this)
73 {
74 return this->requested++;
75 }
76
77 /**
78 * Implementation of transaction_t.get_request.
79 */
80 static status_t get_request(private_delete_ike_sa_t *this, message_t **result)
81 {
82 message_t *request;
83 host_t *me, *other;
84 delete_payload_t *delete_payload;
85
86 /* check if we already have built a message (retransmission) */
87 if (this->message)
88 {
89 *result = this->message;
90 return SUCCESS;
91 }
92
93 me = this->ike_sa->get_my_host(this->ike_sa);
94 other = this->ike_sa->get_other_host(this->ike_sa);
95
96 /* build the request */
97 request = message_create();
98 request->set_source(request, me->clone(me));
99 request->set_destination(request, other->clone(other));
100 request->set_exchange_type(request, INFORMATIONAL);
101 request->set_request(request, TRUE);
102 this->message_id = this->ike_sa->get_next_message_id(this->ike_sa);
103 request->set_message_id(request, this->message_id);
104 request->set_ike_sa_id(request, this->ike_sa->get_id(this->ike_sa));
105 /* apply for caller */
106 *result = request;
107 /* store for retransmission */
108 this->message = request;
109
110 delete_payload = delete_payload_create(PROTO_IKE);
111 request->add_payload(request, (payload_t*)delete_payload);
112
113 /* transit to state SA_DELETING */
114 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
115
116 return SUCCESS;
117 }
118
119 /**
120 * Implementation of transaction_t.get_response.
121 */
122 static status_t get_response(private_delete_ike_sa_t *this, message_t *request,
123 message_t **result, transaction_t **next)
124 {
125 host_t *me, *other;
126 message_t *response;
127 iterator_t *payloads;
128 payload_t *payload;
129 delete_payload_t *delete_request = NULL;
130
131 /* check if we already have built a response (retransmission)
132 * this only happens in special simultanous transaction cases,
133 * as we delete the IKE_SA after the response is sent. */
134 if (this->message)
135 {
136 *result = this->message;
137 return SUCCESS;
138 }
139
140 me = this->ike_sa->get_my_host(this->ike_sa);
141 other = this->ike_sa->get_other_host(this->ike_sa);
142 this->message_id = request->get_message_id(request);
143
144 /* set up response */
145 response = message_create();
146 response->set_source(response, me->clone(me));
147 response->set_destination(response, other->clone(other));
148 response->set_exchange_type(response, INFORMATIONAL);
149 response->set_request(response, FALSE);
150 response->set_message_id(response, this->message_id);
151 response->set_ike_sa_id(response, this->ike_sa->get_id(this->ike_sa));
152 this->message = response;
153 *result = response;
154
155 /* check message type */
156 if (request->get_exchange_type(request) != INFORMATIONAL)
157 {
158 DBG1(SIG_DBG_IKE, "INFORMATIONAL response of invalid type, deleting IKE_SA");
159 return DESTROY_ME;
160 }
161
162 /* iterate over all payloads */
163 payloads = request->get_payload_iterator(request);
164 while (payloads->iterate(payloads, (void**)&payload))
165 {
166 switch (payload->get_type(payload))
167 {
168 case DELETE:
169 {
170 delete_request = (delete_payload_t *)payload;
171 break;
172 }
173 default:
174 {
175 DBG1(SIG_DBG_IKE, "ignoring payload %N",
176 payload_type_names, payload->get_type(payload));
177 break;
178 }
179 }
180 }
181 payloads->destroy(payloads);
182
183 if (delete_request &&
184 delete_request->get_protocol_id(delete_request) == PROTO_IKE)
185 {
186 DBG1(SIG_DBG_IKE, "DELETE request for IKE_SA received, deleting IKE_SA");
187 }
188 else
189 {
190 /* should not happen, as we preparsed this at transaction construction */
191 DBG1(SIG_DBG_IKE, "received a weird DELETE request for IKE_SA, deleting anyway");
192 }
193 if (this->ike_sa->get_state(this->ike_sa) == IKE_DELETING)
194 {
195 /* if we are already deleting an IKE_SA, we do not destroy. We wait
196 * until we get the response for our initiated delete. */
197 return SUCCESS;
198 }
199 this->ike_sa->set_state(this->ike_sa, IKE_DELETING);
200 return DESTROY_ME;
201 }
202
203
204 /**
205 * Implementation of transaction_t.conclude
206 */
207 static status_t conclude(private_delete_ike_sa_t *this, message_t *response,
208 transaction_t **transaction)
209 {
210 /* check message type */
211 if (response->get_exchange_type(response) != INFORMATIONAL)
212 {
213 DBG1(SIG_DBG_IKE, "INFORMATIONAL response of invalid type, deleting IKE_SA");
214 return DESTROY_ME;
215 }
216 /* this is only an acknowledge. We can't do anything here, but delete
217 * the IKE_SA. */
218 return DESTROY_ME;
219 }
220
221 /**
222 * implements transaction_t.destroy
223 */
224 static void destroy(private_delete_ike_sa_t *this)
225 {
226 DESTROY_IF(this->message);
227 free(this);
228 }
229
230 /*
231 * Described in header.
232 */
233 delete_ike_sa_t *delete_ike_sa_create(ike_sa_t *ike_sa)
234 {
235 private_delete_ike_sa_t *this = malloc_thing(private_delete_ike_sa_t);
236
237 /* transaction interface functions */
238 this->public.transaction.get_request = (status_t(*)(transaction_t*,message_t**))get_request;
239 this->public.transaction.get_response = (status_t(*)(transaction_t*,message_t*,message_t**,transaction_t**))get_response;
240 this->public.transaction.conclude = (status_t(*)(transaction_t*,message_t*,transaction_t**))conclude;
241 this->public.transaction.get_message_id = (u_int32_t(*)(transaction_t*))get_message_id;
242 this->public.transaction.requested = (u_int32_t(*)(transaction_t*))requested;
243 this->public.transaction.destroy = (void(*)(transaction_t*))destroy;
244
245 /* private data */
246 this->ike_sa = ike_sa;
247 this->message_id = 0;
248 this->message = NULL;
249 this->requested = 0;
250
251 return &this->public;
252 }