Moving charon to libcharon.
[strongswan.git] / src / libcharon / processing / jobs / delete_ike_sa_job.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
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 "delete_ike_sa_job.h"
18
19 #include <daemon.h>
20
21 typedef struct private_delete_ike_sa_job_t private_delete_ike_sa_job_t;
22
23 /**
24 * Private data of an delete_ike_sa_job_t Object
25 */
26 struct private_delete_ike_sa_job_t {
27 /**
28 * public delete_ike_sa_job_t interface
29 */
30 delete_ike_sa_job_t public;
31
32 /**
33 * ID of the ike_sa to delete
34 */
35 ike_sa_id_t *ike_sa_id;
36
37 /**
38 * Should the IKE_SA be deleted if it is in ESTABLISHED state?
39 */
40 bool delete_if_established;
41 };
42
43
44 /**
45 * Implements job_t.destroy.
46 */
47 static void destroy(private_delete_ike_sa_job_t *this)
48 {
49 this->ike_sa_id->destroy(this->ike_sa_id);
50 free(this);
51 }
52
53 /**
54 * Implementation of job_t.execute.
55 */
56 static void execute(private_delete_ike_sa_job_t *this)
57 {
58 ike_sa_t *ike_sa;
59
60 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
61 this->ike_sa_id);
62 if (ike_sa)
63 {
64 if (ike_sa->get_state(ike_sa) == IKE_PASSIVE)
65 {
66 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
67 return destroy(this);
68 }
69 if (this->delete_if_established)
70 {
71 if (ike_sa->delete(ike_sa) == DESTROY_ME)
72 {
73 charon->ike_sa_manager->checkin_and_destroy(
74 charon->ike_sa_manager, ike_sa);
75 }
76 else
77 {
78 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
79 }
80 }
81 else
82 {
83 /* destroy only if not ESTABLISHED */
84 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
85 {
86 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
87 }
88 else
89 {
90 DBG1(DBG_JOB, "deleting half open IKE_SA after timeout");
91 charon->ike_sa_manager->checkin_and_destroy(
92 charon->ike_sa_manager, ike_sa);
93 }
94 }
95 }
96 destroy(this);
97 }
98
99 /*
100 * Described in header
101 */
102 delete_ike_sa_job_t *delete_ike_sa_job_create(ike_sa_id_t *ike_sa_id,
103 bool delete_if_established)
104 {
105 private_delete_ike_sa_job_t *this = malloc_thing(private_delete_ike_sa_job_t);
106
107 /* interface functions */
108 this->public.job_interface.execute = (void (*) (job_t *)) execute;
109 this->public.job_interface.destroy = (void (*)(job_t *)) destroy;;
110
111 /* private variables */
112 this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
113 this->delete_if_established = delete_if_established;
114
115 return &(this->public);
116 }