4 * @brief Implementation of ike_sa_id_t.
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
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>.
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
25 #include "ike_sa_id.h"
31 typedef struct private_ike_sa_id_t private_ike_sa_id_t
;
34 * Private data of an ike_sa_id_t object.
36 struct private_ike_sa_id_t
{
38 * Public interface of ike_sa_id_t.
45 u_int64_t initiator_spi
;
50 u_int64_t responder_spi
;
53 * Role for specific IKE_SA.
55 bool is_initiator_flag
;
59 * Implementation of ike_sa_id_t.set_responder_spi.
61 static void set_responder_spi (private_ike_sa_id_t
*this, u_int64_t responder_spi
)
63 this->responder_spi
= responder_spi
;
67 * Implementation of ike_sa_id_t.set_initiator_spi.
69 static void set_initiator_spi(private_ike_sa_id_t
*this, u_int64_t initiator_spi
)
71 this->initiator_spi
= initiator_spi
;
75 * Implementation of ike_sa_id_t.get_initiator_spi.
77 static u_int64_t
get_initiator_spi (private_ike_sa_id_t
*this)
79 return this->initiator_spi
;
83 * Implementation of ike_sa_id_t.get_responder_spi.
85 static u_int64_t
get_responder_spi (private_ike_sa_id_t
*this)
87 return this->responder_spi
;
91 * Implementation of ike_sa_id_t.equals.
93 static bool equals (private_ike_sa_id_t
*this, private_ike_sa_id_t
*other
)
99 if ((this->is_initiator_flag
== other
->is_initiator_flag
) &&
100 (this->initiator_spi
== other
->initiator_spi
) &&
101 (this->responder_spi
== other
->responder_spi
))
103 /* private_ike_sa_id's are equal */
108 /* private_ike_sa_id's are not equal */
114 * Implementation of ike_sa_id_t.replace_values.
116 static void replace_values(private_ike_sa_id_t
*this, private_ike_sa_id_t
*other
)
118 this->initiator_spi
= other
->initiator_spi
;
119 this->responder_spi
= other
->responder_spi
;
120 this->is_initiator_flag
= other
->is_initiator_flag
;
124 * Implementation of ike_sa_id_t.is_initiator.
126 static bool is_initiator(private_ike_sa_id_t
*this)
128 return this->is_initiator_flag
;
132 * Implementation of ike_sa_id_t.switch_initiator.
134 static bool switch_initiator(private_ike_sa_id_t
*this)
136 if (this->is_initiator_flag
)
138 this->is_initiator_flag
= FALSE
;
142 this->is_initiator_flag
= TRUE
;
144 return this->is_initiator_flag
;
148 * Implementation of ike_sa_id_t.clone.
150 static ike_sa_id_t
* clone(private_ike_sa_id_t
*this)
152 return ike_sa_id_create(this->initiator_spi
, this->responder_spi
, this->is_initiator_flag
);
156 * output handler in printf()
158 static int print(FILE *stream
, const struct printf_info
*info
,
159 const void *const *args
)
161 private_ike_sa_id_t
*this = *((private_ike_sa_id_t
**)(args
[0]));
165 return fprintf(stream
, "(null)");
167 return fprintf(stream
, "%llx:%llx[%c]",
168 this->initiator_spi
, this->responder_spi
,
169 this->is_initiator_flag ?
'i' : 'r');
173 * register printf() handlers
175 static void __attribute__ ((constructor
))print_register()
177 register_printf_function(PRINTF_IKE_SA_ID
, print
, arginfo_ptr
);
181 * Implementation of ike_sa_id_t.destroy.
183 static void destroy(private_ike_sa_id_t
*this)
189 * Described in header.
191 ike_sa_id_t
* ike_sa_id_create(u_int64_t initiator_spi
, u_int64_t responder_spi
, bool is_initiator_flag
)
193 private_ike_sa_id_t
*this = malloc_thing(private_ike_sa_id_t
);
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
;
208 this->initiator_spi
= initiator_spi
;
209 this->responder_spi
= responder_spi
;
210 this->is_initiator_flag
= is_initiator_flag
;
212 return &this->public;