Avoid parsing retransmits we already responded to.
[strongswan.git] / src / libcharon / sa / task_manager_v1.c
1 /*
2 * Copyright (C) 2007-2011 Tobias Brunner
3 * Copyright (C) 2007-2011 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "task_manager_v1.h"
18
19 #include <math.h>
20
21 #include <daemon.h>
22 #include <sa/tasks/ike_vendor.h>
23 #include <sa/tasks/main_mode.h>
24 #include <sa/tasks/quick_mode.h>
25 #include <sa/tasks/xauth_request.h>
26 #include <sa/tasks/ike_vendor_v1.h>
27 #include <processing/jobs/retransmit_job.h>
28 #include <processing/jobs/delete_ike_sa_job.h>
29
30 typedef struct exchange_t exchange_t;
31
32 /**
33 * An exchange in the air, used do detect and handle retransmission
34 */
35 struct exchange_t {
36
37 /**
38 * Message ID used for this transaction
39 */
40 u_int32_t mid;
41
42 /**
43 * generated packet for retransmission
44 */
45 packet_t *packet;
46 };
47
48 typedef struct private_task_manager_t private_task_manager_t;
49
50 /**
51 * private data of the task manager
52 */
53 struct private_task_manager_t {
54
55 /**
56 * public functions
57 */
58 task_manager_v1_t public;
59
60 /**
61 * associated IKE_SA we are serving
62 */
63 ike_sa_t *ike_sa;
64
65 /**
66 * RNG to create message IDs
67 */
68 rng_t *rng;
69
70 /**
71 * Exchange we are currently handling as responder
72 */
73 struct {
74 /**
75 * Message ID of the exchange
76 */
77 u_int32_t mid;
78
79 /**
80 * Hash of a previously received message
81 */
82 u_int32_t hash;
83
84 /**
85 * packet for retransmission
86 */
87 packet_t *packet;
88
89 } responding;
90
91 /**
92 * Exchange we are currently handling as initiator
93 */
94 struct {
95 /**
96 * Message ID of the exchange
97 */
98 u_int32_t mid;
99
100 /**
101 * Hash of a previously received message
102 */
103 u_int32_t hash;
104
105 /**
106 * how many times we have retransmitted so far
107 */
108 u_int retransmitted;
109
110 /**
111 * packet for retransmission
112 */
113 packet_t *packet;
114
115 /**
116 * type of the initated exchange
117 */
118 exchange_type_t type;
119
120 } initiating;
121
122 /**
123 * List of queued tasks not yet in action
124 */
125 linked_list_t *queued_tasks;
126
127 /**
128 * List of active tasks, initiated by ourselve
129 */
130 linked_list_t *active_tasks;
131
132 /**
133 * List of tasks initiated by peer
134 */
135 linked_list_t *passive_tasks;
136
137 /**
138 * Number of times we retransmit messages before giving up
139 */
140 u_int retransmit_tries;
141
142 /**
143 * Retransmission timeout
144 */
145 double retransmit_timeout;
146
147 /**
148 * Base to calculate retransmission timeout
149 */
150 double retransmit_base;
151 };
152
153 /**
154 * flush all tasks in the task manager
155 */
156 static void flush(private_task_manager_t *this)
157 {
158 this->queued_tasks->destroy_offset(this->queued_tasks,
159 offsetof(task_t, destroy));
160 this->queued_tasks = linked_list_create();
161 this->passive_tasks->destroy_offset(this->passive_tasks,
162 offsetof(task_t, destroy));
163 this->passive_tasks = linked_list_create();
164 this->active_tasks->destroy_offset(this->active_tasks,
165 offsetof(task_t, destroy));
166 this->active_tasks = linked_list_create();
167 }
168
169 /**
170 * move a task of a specific type from the queue to the active list
171 */
172 static bool activate_task(private_task_manager_t *this, task_type_t type)
173 {
174 enumerator_t *enumerator;
175 task_t *task;
176 bool found = FALSE;
177
178 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
179 while (enumerator->enumerate(enumerator, (void**)&task))
180 {
181 if (task->get_type(task) == type)
182 {
183 DBG2(DBG_IKE, " activating %N task", task_type_names, type);
184 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
185 this->active_tasks->insert_last(this->active_tasks, task);
186 found = TRUE;
187 break;
188 }
189 }
190 enumerator->destroy(enumerator);
191 return found;
192 }
193
194 METHOD(task_manager_t, retransmit, status_t,
195 private_task_manager_t *this, u_int32_t message_id)
196 {
197 if (message_id == this->initiating.mid)
198 {
199 u_int32_t timeout;
200 packet_t *packet;
201 job_t *job;
202
203 if (this->initiating.retransmitted <= this->retransmit_tries)
204 {
205 timeout = (u_int32_t)(this->retransmit_timeout * 1000.0 *
206 pow(this->retransmit_base, this->initiating.retransmitted));
207 }
208 else
209 {
210 DBG1(DBG_IKE, "giving up after %d retransmits",
211 this->initiating.retransmitted - 1);
212 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
213 {
214 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
215 }
216 return DESTROY_ME;
217 }
218
219 if (this->initiating.retransmitted)
220 {
221 DBG1(DBG_IKE, "retransmit %d of request with message ID %d",
222 this->initiating.retransmitted, message_id);
223 }
224 packet = this->initiating.packet->clone(this->initiating.packet);
225 charon->sender->send(charon->sender, packet);
226
227 this->initiating.retransmitted++;
228 job = (job_t*)retransmit_job_create(this->initiating.mid,
229 this->ike_sa->get_id(this->ike_sa));
230 lib->scheduler->schedule_job_ms(lib->scheduler, job, timeout);
231 }
232 return SUCCESS;
233 }
234
235 void migrate_tasks(linked_list_t *from, linked_list_t *to)
236 {
237 enumerator_t *enumerator;
238 task_t *task;
239
240 enumerator = from->create_enumerator(from);
241 while(enumerator->enumerate(enumerator, (void**)&task))
242 {
243 DBG4(DBG_IKE, " Migrating %N task to new queue", task_type_names, task->get_type(task));
244 if(task->swap_initiator)
245 {
246 task->swap_initiator(task);
247 }
248 to->insert_last(to, task);
249 from->remove_at(from, enumerator);
250 }
251 enumerator->destroy(enumerator);
252 }
253
254 METHOD(task_manager_t, initiate, status_t,
255 private_task_manager_t *this)
256 {
257 enumerator_t *enumerator;
258 task_t *task;
259 message_t *message;
260 host_t *me, *other;
261 status_t status;
262 exchange_type_t exchange = EXCHANGE_TYPE_UNDEFINED;
263 bool new_mid = FALSE;
264
265 if (!this->rng)
266 {
267 DBG1(DBG_IKE, "no RNG supported");
268 return FAILED;
269 }
270
271 if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED)
272 {
273 DBG2(DBG_IKE, "delaying task initiation, %N exchange in progress",
274 exchange_type_names, this->initiating.type);
275 /* do not initiate if we already have a message in the air */
276 return SUCCESS;
277 }
278
279 if (this->active_tasks->get_count(this->active_tasks) == 0)
280 {
281 DBG2(DBG_IKE, "activating new tasks");
282 switch (this->ike_sa->get_state(this->ike_sa))
283 {
284 case IKE_CREATED:
285 activate_task(this, TASK_VENDOR_V1);
286 if (activate_task(this, TASK_MAIN_MODE))
287 {
288 exchange = ID_PROT;
289 }
290 break;
291 case IKE_ESTABLISHED:
292 if (activate_task(this, TASK_QUICK_MODE))
293 {
294 exchange = QUICK_MODE;
295 new_mid = TRUE;
296 break;
297 }
298 if (activate_task(this, TASK_XAUTH_REQUEST))
299 {
300 exchange = TRANSACTION;
301 new_mid = TRUE;
302 break;
303 }
304 break;
305 default:
306 break;
307 }
308 }
309 else
310 {
311 DBG2(DBG_IKE, "reinitiating already active tasks");
312 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
313 while (enumerator->enumerate(enumerator, (void**)&task))
314 {
315 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
316 switch (task->get_type(task))
317 {
318 case TASK_MAIN_MODE:
319 exchange = ID_PROT;
320 break;
321 case TASK_QUICK_MODE:
322 exchange = QUICK_MODE;
323 break;
324 case TASK_XAUTH_REQUEST:
325 exchange = TRANSACTION;
326 new_mid = TRUE;
327 break;
328 default:
329 continue;
330 }
331 break;
332 }
333 enumerator->destroy(enumerator);
334 }
335
336 if (exchange == EXCHANGE_TYPE_UNDEFINED)
337 {
338 DBG2(DBG_IKE, "nothing to initiate");
339 /* nothing to do yet... */
340 return SUCCESS;
341 }
342
343 me = this->ike_sa->get_my_host(this->ike_sa);
344 other = this->ike_sa->get_other_host(this->ike_sa);
345
346 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
347 if (new_mid)
348 {
349 this->rng->get_bytes(this->rng, sizeof(this->initiating.mid),
350 (void*)&this->initiating.mid);
351 }
352 message->set_message_id(message, this->initiating.mid);
353 message->set_source(message, me->clone(me));
354 message->set_destination(message, other->clone(other));
355 message->set_exchange_type(message, exchange);
356 this->initiating.type = exchange;
357 this->initiating.retransmitted = 0;
358
359 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
360 while (enumerator->enumerate(enumerator, (void*)&task))
361 {
362 switch (task->build(task, message))
363 {
364 case SUCCESS:
365 /* task completed, remove it */
366 this->active_tasks->remove_at(this->active_tasks, enumerator);
367 task->destroy(task);
368 break;
369 case MIGRATE:
370 /* task completed, remove it */
371 this->active_tasks->remove_at(this->active_tasks, enumerator);
372 task->destroy(task);
373 /* migrate the remaining active tasks to the passive queue */
374 migrate_tasks(this->active_tasks, this->passive_tasks);
375 break;
376 case NEED_MORE:
377 /* processed, but task needs another exchange */
378 break;
379 case FAILED:
380 default:
381 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
382 {
383 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
384 }
385 /* FALL */
386 case DESTROY_ME:
387 /* critical failure, destroy IKE_SA */
388 enumerator->destroy(enumerator);
389 message->destroy(message);
390 flush(this);
391 return DESTROY_ME;
392 }
393 }
394 enumerator->destroy(enumerator);
395
396 /* update exchange type if a task changed it */
397 this->initiating.type = message->get_exchange_type(message);
398
399 status = this->ike_sa->generate_message(this->ike_sa, message,
400 &this->initiating.packet);
401 if (status != SUCCESS)
402 {
403 /* message generation failed. There is nothing more to do than to
404 * close the SA */
405 message->destroy(message);
406 flush(this);
407 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
408 return DESTROY_ME;
409 }
410 message->destroy(message);
411
412 charon->sender->send(charon->sender,
413 this->initiating.packet->clone(this->initiating.packet));
414
415 return SUCCESS;
416 }
417
418 /**
419 * handle exchange collisions
420 */
421 static bool handle_collisions(private_task_manager_t *this, task_t *task)
422 {
423 return FALSE;
424 }
425
426 /**
427 * build a response depending on the "passive" task list
428 */
429 static status_t build_response(private_task_manager_t *this, message_t *request)
430 {
431 enumerator_t *enumerator;
432 task_t *task;
433 message_t *message;
434 host_t *me, *other;
435 bool delete = FALSE;
436 status_t status;
437 bool migrate = FALSE;
438
439 me = request->get_destination(request);
440 other = request->get_source(request);
441
442 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
443 message->set_exchange_type(message, request->get_exchange_type(request));
444 /* send response along the path the request came in */
445 message->set_source(message, me->clone(me));
446 message->set_destination(message, other->clone(other));
447 message->set_message_id(message, request->get_message_id(request));
448 message->set_request(message, FALSE);
449
450 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
451 while (enumerator->enumerate(enumerator, (void*)&task))
452 {
453 switch (task->build(task, message))
454 {
455 case MIGRATE:
456 migrate = TRUE;
457 /* FALL */
458 case SUCCESS:
459 /* task completed, remove it */
460 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
461 if (!handle_collisions(this, task))
462 {
463 task->destroy(task);
464 }
465 break;
466 case NEED_MORE:
467 /* processed, but task needs another exchange */
468 if (handle_collisions(this, task))
469 {
470 this->passive_tasks->remove_at(this->passive_tasks,
471 enumerator);
472 }
473 break;
474 case FAILED:
475 default:
476 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
477 /* FALL */
478 case DESTROY_ME:
479 /* destroy IKE_SA, but SEND response first */
480 delete = TRUE;
481 break;
482 }
483 if (delete)
484 {
485 break;
486 }
487 }
488 enumerator->destroy(enumerator);
489
490 /* message complete, send it */
491 DESTROY_IF(this->responding.packet);
492 this->responding.packet = NULL;
493 status = this->ike_sa->generate_message(this->ike_sa, message,
494 &this->responding.packet);
495 message->destroy(message);
496 if (status != SUCCESS)
497 {
498 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
499 return DESTROY_ME;
500 }
501
502 charon->sender->send(charon->sender,
503 this->responding.packet->clone(this->responding.packet));
504
505 if (migrate)
506 {
507 migrate_tasks(this->passive_tasks, this->queued_tasks);
508 /* Kick off the newly installed tasks */
509 initiate(this);
510 }
511
512 if (delete)
513 {
514 return DESTROY_ME;
515 }
516 return SUCCESS;
517 }
518
519 /**
520 * handle an incoming request message
521 */
522 static status_t process_request(private_task_manager_t *this,
523 message_t *message)
524 {
525 enumerator_t *enumerator;
526 task_t *task = NULL;
527
528 if (this->passive_tasks->get_count(this->passive_tasks) == 0)
529 { /* create tasks depending on request type, if not already some queued */
530 switch (message->get_exchange_type(message))
531 {
532 case ID_PROT:
533 task = (task_t *)ike_vendor_v1_create(this->ike_sa, FALSE);
534 this->passive_tasks->insert_last(this->passive_tasks, task);
535 task = (task_t *)main_mode_create(this->ike_sa, FALSE);
536 this->passive_tasks->insert_last(this->passive_tasks, task);
537 task = (task_t *)xauth_request_create(this->ike_sa, FALSE);
538 this->passive_tasks->insert_last(this->passive_tasks, task);
539 break;
540 case AGGRESSIVE:
541 /* TODO-IKEv1: agressive mode */
542 return FAILED;
543 case QUICK_MODE:
544 task = (task_t *)quick_mode_create(this->ike_sa, NULL,
545 NULL, NULL);
546 this->passive_tasks->insert_last(this->passive_tasks, task);
547 break;
548 case INFORMATIONAL_V1:
549 /* TODO-IKEv1: informational */
550 return FAILED;
551 default:
552 return FAILED;
553 }
554 }
555 /* let the tasks process the message */
556 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
557 while (enumerator->enumerate(enumerator, (void*)&task))
558 {
559 switch (task->process(task, message))
560 {
561 case SUCCESS:
562 /* task completed, remove it */
563 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
564 task->destroy(task);
565 enumerator->destroy(enumerator);
566 return SUCCESS;
567 case MIGRATE:
568 /* task completed, remove it */
569 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
570 task->destroy(task);
571 enumerator->destroy(enumerator);
572 /* migrate the remaining tasks */
573 migrate_tasks(this->passive_tasks, this->queued_tasks);
574 /* Kick off the newly installed tasks */
575 initiate(this);
576 return SUCCESS;
577 case NEED_MORE:
578 /* processed, but task needs at least another call to build() */
579 break;
580 case FAILED:
581 default:
582 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
583 /* FALL */
584 case DESTROY_ME:
585 /* critical failure, destroy IKE_SA */
586 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
587 enumerator->destroy(enumerator);
588 task->destroy(task);
589 return DESTROY_ME;
590 }
591 }
592 enumerator->destroy(enumerator);
593
594 return build_response(this, message);
595 }
596
597 /**
598 * handle an incoming response message
599 */
600 static status_t process_response(private_task_manager_t *this,
601 message_t *message)
602 {
603 enumerator_t *enumerator;
604 task_t *task;
605
606 if (message->get_exchange_type(message) != this->initiating.type)
607 {
608 DBG1(DBG_IKE, "received %N response, but expected %N",
609 exchange_type_names, message->get_exchange_type(message),
610 exchange_type_names, this->initiating.type);
611 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
612 return DESTROY_ME;
613 }
614
615 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
616 while (enumerator->enumerate(enumerator, (void*)&task))
617 {
618 switch (task->process(task, message))
619 {
620 case SUCCESS:
621 /* task completed, remove it */
622 this->active_tasks->remove_at(this->active_tasks, enumerator);
623 task->destroy(task);
624 break;
625 case NEED_MORE:
626 /* processed, but task needs another exchange */
627 break;
628 case FAILED:
629 default:
630 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
631 /* FALL */
632 case DESTROY_ME:
633 /* critical failure, destroy IKE_SA */
634 this->active_tasks->remove_at(this->active_tasks, enumerator);
635 enumerator->destroy(enumerator);
636 task->destroy(task);
637 return DESTROY_ME;
638 }
639 }
640 enumerator->destroy(enumerator);
641
642 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
643 this->initiating.packet->destroy(this->initiating.packet);
644 this->initiating.packet = NULL;
645
646 return initiate(this);
647 }
648
649 METHOD(task_manager_t, process_message, status_t,
650 private_task_manager_t *this, message_t *msg)
651 {
652 u_int32_t hash, mid;
653 host_t *me, *other;
654 status_t status;
655
656 /* TODO-IKEv1: update hosts more selectively */
657 me = msg->get_destination(msg);
658 other = msg->get_source(msg);
659 mid = msg->get_message_id(msg);
660
661 if ((mid && mid == this->initiating.mid) ||
662 (this->initiating.mid == 0 &&
663 this->active_tasks->get_count(this->active_tasks)))
664 {
665 status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa));
666 if (status != SUCCESS)
667 {
668 return status;
669 }
670 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
671 time_monotonic(NULL));
672 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
673 charon->bus->message(charon->bus, msg, FALSE);
674 if (process_response(this, msg) != SUCCESS)
675 {
676 flush(this);
677 return DESTROY_ME;
678 }
679 }
680 else
681 {
682 hash = chunk_hash(msg->get_packet_data(msg));
683 if (hash == this->responding.hash)
684 {
685 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
686 "retransmitting response", mid);
687 charon->sender->send(charon->sender,
688 this->responding.packet->clone(this->responding.packet));
689 return SUCCESS;
690 }
691 status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa));
692 if (status != SUCCESS)
693 {
694 return status;
695 }
696 /* if this IKE_SA is virgin, we check for a config */
697 if (this->ike_sa->get_ike_cfg(this->ike_sa) == NULL)
698 {
699 ike_sa_id_t *ike_sa_id;
700 ike_cfg_t *ike_cfg;
701 job_t *job;
702 ike_cfg = charon->backends->get_ike_cfg(charon->backends, me, other);
703 if (ike_cfg == NULL)
704 {
705 /* no config found for these hosts, destroy */
706 DBG1(DBG_IKE, "no IKE config found for %H...%H", me, other);
707 return DESTROY_ME;
708 }
709 this->ike_sa->set_ike_cfg(this->ike_sa, ike_cfg);
710 ike_cfg->destroy(ike_cfg);
711 /* add a timeout if peer does not establish it completely */
712 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
713 job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE);
714 lib->scheduler->schedule_job(lib->scheduler, job,
715 lib->settings->get_int(lib->settings,
716 "charon.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT));
717 }
718 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
719 time_monotonic(NULL));
720 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
721 charon->bus->message(charon->bus, msg, TRUE);
722 if (process_request(this, msg) != SUCCESS)
723 {
724 flush(this);
725 return DESTROY_ME;
726 }
727
728 this->responding.mid = mid;
729 this->responding.hash = hash;
730 }
731 return SUCCESS;
732 }
733
734 METHOD(task_manager_t, queue_task, void,
735 private_task_manager_t *this, task_t *task)
736 {
737 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
738 this->queued_tasks->insert_last(this->queued_tasks, task);
739 }
740
741 METHOD(task_manager_t, adopt_tasks, void,
742 private_task_manager_t *this, task_manager_t *other_public)
743 {
744 private_task_manager_t *other = (private_task_manager_t*)other_public;
745 task_t *task;
746
747 /* move queued tasks from other to this */
748 while (other->queued_tasks->remove_last(other->queued_tasks,
749 (void**)&task) == SUCCESS)
750 {
751 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
752 task->migrate(task, this->ike_sa);
753 this->queued_tasks->insert_first(this->queued_tasks, task);
754 }
755 }
756
757 METHOD(task_manager_t, busy, bool,
758 private_task_manager_t *this)
759 {
760 return (this->active_tasks->get_count(this->active_tasks) > 0);
761 }
762
763 METHOD(task_manager_t, incr_mid, void,
764 private_task_manager_t *this, bool initiate)
765 {
766 }
767
768 METHOD(task_manager_t, reset, void,
769 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
770 {
771 }
772
773 METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
774 private_task_manager_t *this, task_queue_t queue)
775 {
776 switch (queue)
777 {
778 case TASK_QUEUE_ACTIVE:
779 return this->active_tasks->create_enumerator(this->active_tasks);
780 case TASK_QUEUE_PASSIVE:
781 return this->passive_tasks->create_enumerator(this->passive_tasks);
782 case TASK_QUEUE_QUEUED:
783 return this->queued_tasks->create_enumerator(this->queued_tasks);
784 default:
785 return enumerator_create_empty();
786 }
787 }
788
789 METHOD(task_manager_t, destroy, void,
790 private_task_manager_t *this)
791 {
792 flush(this);
793
794 this->active_tasks->destroy(this->active_tasks);
795 this->queued_tasks->destroy(this->queued_tasks);
796 this->passive_tasks->destroy(this->passive_tasks);
797
798 DESTROY_IF(this->responding.packet);
799 DESTROY_IF(this->initiating.packet);
800 DESTROY_IF(this->rng);
801 free(this);
802 }
803
804 /*
805 * see header file
806 */
807 task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
808 {
809 private_task_manager_t *this;
810
811 INIT(this,
812 .public = {
813 .task_manager = {
814 .process_message = _process_message,
815 .queue_task = _queue_task,
816 .initiate = _initiate,
817 .retransmit = _retransmit,
818 .incr_mid = _incr_mid,
819 .reset = _reset,
820 .adopt_tasks = _adopt_tasks,
821 .busy = _busy,
822 .create_task_enumerator = _create_task_enumerator,
823 .destroy = _destroy,
824 },
825 },
826 .ike_sa = ike_sa,
827 .initiating.type = EXCHANGE_TYPE_UNDEFINED,
828 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
829 .queued_tasks = linked_list_create(),
830 .active_tasks = linked_list_create(),
831 .passive_tasks = linked_list_create(),
832 .retransmit_tries = lib->settings->get_int(lib->settings,
833 "charon.retransmit_tries", RETRANSMIT_TRIES),
834 .retransmit_timeout = lib->settings->get_double(lib->settings,
835 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT),
836 .retransmit_base = lib->settings->get_double(lib->settings,
837 "charon.retransmit_base", RETRANSMIT_BASE),
838 );
839
840 return &this->public;
841 }