some improvements in signaling code
[strongswan.git] / src / charon / sa / ike_sa_id.c
1 /**
2 * @file ike_sa_id.c
3 *
4 * @brief Implementation of ike_sa_id_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
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
25 #include "ike_sa_id.h"
26
27 #include <printf.h>
28 #include <stdio.h>
29
30
31 typedef struct private_ike_sa_id_t private_ike_sa_id_t;
32
33 /**
34 * Private data of an ike_sa_id_t object.
35 */
36 struct private_ike_sa_id_t {
37 /**
38 * Public interface of ike_sa_id_t.
39 */
40 ike_sa_id_t public;
41
42 /**
43 * SPI of Initiator.
44 */
45 u_int64_t initiator_spi;
46
47 /**
48 * SPI of Responder.
49 */
50 u_int64_t responder_spi;
51
52 /**
53 * Role for specific IKE_SA.
54 */
55 bool is_initiator_flag;
56 };
57
58 /**
59 * Implementation of ike_sa_id_t.set_responder_spi.
60 */
61 static void set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi)
62 {
63 this->responder_spi = responder_spi;
64 }
65
66 /**
67 * Implementation of ike_sa_id_t.set_initiator_spi.
68 */
69 static void set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi)
70 {
71 this->initiator_spi = initiator_spi;
72 }
73
74 /**
75 * Implementation of ike_sa_id_t.get_initiator_spi.
76 */
77 static u_int64_t get_initiator_spi (private_ike_sa_id_t *this)
78 {
79 return this->initiator_spi;
80 }
81
82 /**
83 * Implementation of ike_sa_id_t.get_responder_spi.
84 */
85 static u_int64_t get_responder_spi (private_ike_sa_id_t *this)
86 {
87 return this->responder_spi;
88 }
89
90 /**
91 * Implementation of ike_sa_id_t.equals.
92 */
93 static bool equals (private_ike_sa_id_t *this, private_ike_sa_id_t *other)
94 {
95 if (other == NULL)
96 {
97 return FALSE;
98 }
99 if ((this->is_initiator_flag == other->is_initiator_flag) &&
100 (this->initiator_spi == other->initiator_spi) &&
101 (this->responder_spi == other->responder_spi))
102 {
103 /* private_ike_sa_id's are equal */
104 return TRUE;
105 }
106 else
107 {
108 /* private_ike_sa_id's are not equal */
109 return FALSE;
110 }
111 }
112
113 /**
114 * Implementation of ike_sa_id_t.replace_values.
115 */
116 static void replace_values(private_ike_sa_id_t *this, private_ike_sa_id_t *other)
117 {
118 this->initiator_spi = other->initiator_spi;
119 this->responder_spi = other->responder_spi;
120 this->is_initiator_flag = other->is_initiator_flag;
121 }
122
123 /**
124 * Implementation of ike_sa_id_t.is_initiator.
125 */
126 static bool is_initiator(private_ike_sa_id_t *this)
127 {
128 return this->is_initiator_flag;
129 }
130
131 /**
132 * Implementation of ike_sa_id_t.switch_initiator.
133 */
134 static bool switch_initiator(private_ike_sa_id_t *this)
135 {
136 if (this->is_initiator_flag)
137 {
138 this->is_initiator_flag = FALSE;
139 }
140 else
141 {
142 this->is_initiator_flag = TRUE;
143 }
144 return this->is_initiator_flag;
145 }
146
147 /**
148 * Implementation of ike_sa_id_t.clone.
149 */
150 static ike_sa_id_t* clone(private_ike_sa_id_t *this)
151 {
152 return ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator_flag);
153 }
154
155 /**
156 * output handler in printf()
157 */
158 static int print(FILE *stream, const struct printf_info *info,
159 const void *const *args)
160 {
161 private_ike_sa_id_t *this = *((private_ike_sa_id_t**)(args[0]));
162
163 if (this == NULL)
164 {
165 return fprintf(stream, "(null)");
166 }
167 return fprintf(stream, "%llx:%llx[%c]",
168 this->initiator_spi, this->responder_spi,
169 this->is_initiator_flag ? 'i' : 'r');
170 }
171
172 /**
173 * arginfo handler in printf()
174 */
175 static int print_arginfo(const struct printf_info *info, size_t n, int *argtypes)
176 {
177 if (n > 0)
178 {
179 argtypes[0] = PA_POINTER;
180 }
181 return 1;
182 }
183
184 /**
185 * register printf() handlers
186 */
187 static void __attribute__ ((constructor))print_register()
188 {
189 register_printf_function(IKE_SA_ID_PRINTF_SPEC, print, print_arginfo);
190 }
191
192
193 /**
194 * Implementation of ike_sa_id_t.destroy.
195 */
196 static void destroy(private_ike_sa_id_t *this)
197 {
198 free(this);
199 }
200
201 /*
202 * Described in header.
203 */
204 ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiator_flag)
205 {
206 private_ike_sa_id_t *this = malloc_thing(private_ike_sa_id_t);
207
208 /* public functions */
209 this->public.set_responder_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi;
210 this->public.set_initiator_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi;
211 this->public.get_responder_spi = (u_int64_t(*)(ike_sa_id_t*)) get_responder_spi;
212 this->public.get_initiator_spi = (u_int64_t(*)(ike_sa_id_t*)) get_initiator_spi;
213 this->public.equals = (bool(*)(ike_sa_id_t*,ike_sa_id_t*)) equals;
214 this->public.replace_values = (void(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values;
215 this->public.is_initiator = (bool(*)(ike_sa_id_t*)) is_initiator;
216 this->public.switch_initiator = (bool(*)(ike_sa_id_t*)) switch_initiator;
217 this->public.clone = (ike_sa_id_t*(*)(ike_sa_id_t*)) clone;
218 this->public.destroy = (void(*)(ike_sa_id_t*))destroy;
219
220 /* private data */
221 this->initiator_spi = initiator_spi;
222 this->responder_spi = responder_spi;
223 this->is_initiator_flag = is_initiator_flag;
224
225 return &this->public;
226 }