better split up of library files "types.h" & "definitions.h"
[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 * register printf() handlers
174 */
175 static void __attribute__ ((constructor))print_register()
176 {
177 register_printf_function(PRINTF_IKE_SA_ID, print, arginfo_ptr);
178 }
179
180 /**
181 * Implementation of ike_sa_id_t.destroy.
182 */
183 static void destroy(private_ike_sa_id_t *this)
184 {
185 free(this);
186 }
187
188 /*
189 * Described in header.
190 */
191 ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiator_flag)
192 {
193 private_ike_sa_id_t *this = malloc_thing(private_ike_sa_id_t);
194
195 /* public functions */
196 this->public.set_responder_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi;
197 this->public.set_initiator_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi;
198 this->public.get_responder_spi = (u_int64_t(*)(ike_sa_id_t*)) get_responder_spi;
199 this->public.get_initiator_spi = (u_int64_t(*)(ike_sa_id_t*)) get_initiator_spi;
200 this->public.equals = (bool(*)(ike_sa_id_t*,ike_sa_id_t*)) equals;
201 this->public.replace_values = (void(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values;
202 this->public.is_initiator = (bool(*)(ike_sa_id_t*)) is_initiator;
203 this->public.switch_initiator = (bool(*)(ike_sa_id_t*)) switch_initiator;
204 this->public.clone = (ike_sa_id_t*(*)(ike_sa_id_t*)) clone;
205 this->public.destroy = (void(*)(ike_sa_id_t*))destroy;
206
207 /* private data */
208 this->initiator_spi = initiator_spi;
209 this->responder_spi = responder_spi;
210 this->is_initiator_flag = is_initiator_flag;
211
212 return &this->public;
213 }