b0aa709714e68ab8a3fab063bafe47d5abf72332
[strongswan.git] / Source / charon / ike_sa.c
1 /**
2 * @file ike_sa.c
3 *
4 * @brief Class ike_sa_t. An object of this type is managed by an
5 * ike_sa_manager_t object and represents an IKE_SA
6 *
7 */
8
9 /*
10 * Copyright (C) 2005 Jan Hutter, Martin Willi
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include "ike_sa.h"
25
26 #include "types.h"
27 #include "utils/allocator.h"
28 #include "utils/linked_list.h"
29
30
31 /**
32 * States in which a IKE_SA can actually be
33 */
34 typedef enum ike_sa_state_e ike_sa_state_t;
35
36 enum ike_sa_state_e{
37
38 /**
39 * IKE_SA is is not in a state
40 */
41 NO_STATE,
42
43 /**
44 * A IKE_SA_INIT-message was sent: role initiator
45 */
46 IKE_SA_INIT_REQUESTED,
47
48 /**
49 * A IKE_SA_INIT-message was replied: role responder
50 */
51 IKE_SA_INIT_RESPONDED,
52
53 /**
54 * An IKE_AUTH-message was sent after a successful
55 * IKE_SA_INIT-exchange: role initiator
56 */
57 IKE_AUTH_REQUESTED,
58
59 /**
60 * An IKE_AUTH-message was replied: role responder.
61 * In this state, all the informations for an IKE_SA
62 * and one CHILD_SA are known.
63 */
64 IKE_SA_INITIALIZED
65 };
66
67
68 /**
69 * Private data of an message_t object
70 */
71 typedef struct private_ike_sa_s private_ike_sa_t;
72
73 struct private_ike_sa_s {
74
75 /**
76 * Public part of a ike_sa_t object
77 */
78 ike_sa_t public;
79
80
81 /* Private values */
82 /**
83 * Identifier for the current IKE_SA
84 */
85 ike_sa_id_t *ike_sa_id;
86
87 /**
88 * Linked List containing the child sa's of the current IKE_SA
89 */
90 linked_list_t *child_sas;
91
92 /**
93 * Current state of the IKE_SA
94 */
95 ike_sa_state_t current_state;
96 };
97
98 /**
99 * @brief implements function process_message of private_ike_sa_t
100 */
101 static status_t process_message (private_ike_sa_t *this, message_t *message)
102 {
103 /* @TODO Add Message Processing here */
104 return SUCCESS;
105 }
106
107 /**
108 * @brief implements function process_configuration of private_ike_sa_t
109 */
110 static status_t process_configuration (private_ike_sa_t *this,configuration_t *configuration)
111 {
112 /*
113 * @TODO Add configuration processing here
114 */
115 return SUCCESS;
116 }
117
118 /**
119 * @brief implements function private_ike_sa_t.get_id
120 */
121 static ike_sa_id_t* get_id(private_ike_sa_t *this)
122 {
123 return this->ike_sa_id;
124 }
125
126 /**
127 * @brief implements function destroy of private_ike_sa_t
128 */
129 static status_t destroy (private_ike_sa_t *this)
130 {
131 if (this == NULL)
132 {
133 return FAILED;
134 }
135
136 this->ike_sa_id->destroy(this->ike_sa_id);
137
138 this->child_sas->destroy(this->child_sas);
139
140 allocator_free(this);
141
142 return SUCCESS;
143 }
144
145 /*
146 * Described in Header
147 */
148 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
149 {
150 private_ike_sa_t *this = allocator_alloc_thing(private_ike_sa_t);
151 if (this == NULL)
152 {
153 return NULL;
154 }
155
156
157 /* Public functions */
158 this->public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
159 this->public.process_configuration = (status_t(*)(ike_sa_t*, configuration_t*)) process_configuration;
160 this->public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
161 this->public.destroy = (status_t(*)(ike_sa_t*))destroy;
162
163
164 /* initialize private fields */
165 if (ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id)) != SUCCESS)
166 {
167 allocator_free(this);
168 return NULL;
169 }
170
171 this->child_sas = linked_list_create();
172 if (this->child_sas == NULL)
173 {
174 this->ike_sa_id->destroy(this->ike_sa_id);
175 allocator_free(this);
176 return NULL;
177 }
178
179 /* at creation time, IKE_SA isn't in a specific state */
180 this->current_state = NO_STATE;
181
182 return (&this->public);
183 }