- fixed compliation error using --enable-smartcard
[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 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23
24 #include "ike_sa_id.h"
25
26
27
28 typedef struct private_ike_sa_id_t private_ike_sa_id_t;
29
30 /**
31 * Private data of an ike_sa_id_t object.
32 */
33 struct private_ike_sa_id_t {
34 /**
35 * Public interface of ike_sa_id_t.
36 */
37 ike_sa_id_t public;
38
39 /**
40 * SPI of Initiator.
41 */
42 u_int64_t initiator_spi;
43
44 /**
45 * SPI of Responder.
46 */
47 u_int64_t responder_spi;
48
49 /**
50 * Role for specific IKE_SA.
51 */
52 bool is_initiator_flag;
53 };
54
55 /**
56 * Implementation of ike_sa_id_t.set_responder_spi.
57 */
58 static void set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi)
59 {
60 this->responder_spi = responder_spi;
61 }
62
63 /**
64 * Implementation of ike_sa_id_t.set_initiator_spi.
65 */
66 static void set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi)
67 {
68 this->initiator_spi = initiator_spi;
69 }
70
71 /**
72 * Implementation of ike_sa_id_t.get_initiator_spi.
73 */
74 static u_int64_t get_initiator_spi (private_ike_sa_id_t *this)
75 {
76 return this->initiator_spi;
77 }
78
79 /**
80 * Implementation of ike_sa_id_t.get_responder_spi.
81 */
82 static u_int64_t get_responder_spi (private_ike_sa_id_t *this)
83 {
84 return this->responder_spi;
85 }
86
87 /**
88 * Implementation of ike_sa_id_t.equals.
89 */
90 static bool equals (private_ike_sa_id_t *this, private_ike_sa_id_t *other)
91 {
92 if (other == NULL)
93 {
94 return FALSE;
95 }
96 if ((this->is_initiator_flag == other->is_initiator_flag) &&
97 (this->initiator_spi == other->initiator_spi) &&
98 (this->responder_spi == other->responder_spi))
99 {
100 /* private_ike_sa_id's are equal */
101 return TRUE;
102 }
103 else
104 {
105 /* private_ike_sa_id's are not equal */
106 return FALSE;
107 }
108 }
109
110 /**
111 * Implementation of ike_sa_id_t.replace_values.
112 */
113 static void replace_values(private_ike_sa_id_t *this, private_ike_sa_id_t *other)
114 {
115 this->initiator_spi = other->initiator_spi;
116 this->responder_spi = other->responder_spi;
117 this->is_initiator_flag = other->is_initiator_flag;
118 }
119
120 /**
121 * Implementation of ike_sa_id_t.is_initiator.
122 */
123 static bool is_initiator(private_ike_sa_id_t *this)
124 {
125 return this->is_initiator_flag;
126 }
127
128 /**
129 * Implementation of ike_sa_id_t.switch_initiator.
130 */
131 static bool switch_initiator(private_ike_sa_id_t *this)
132 {
133 if (this->is_initiator_flag)
134 {
135 this->is_initiator_flag = FALSE;
136 }
137 else
138 {
139 this->is_initiator_flag = TRUE;
140 }
141 return this->is_initiator_flag;
142 }
143
144 /**
145 * Implementation of ike_sa_id_t.clone.
146 */
147 static ike_sa_id_t* clone(private_ike_sa_id_t *this)
148 {
149 return ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator_flag);
150 }
151
152 /**
153 * Implementation of ike_sa_id_t.destroy.
154 */
155 static void destroy(private_ike_sa_id_t *this)
156 {
157 free(this);
158 }
159
160 /*
161 * Described in header.
162 */
163 ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiator_flag)
164 {
165 private_ike_sa_id_t *this = malloc_thing(private_ike_sa_id_t);
166
167 /* public functions */
168 this->public.set_responder_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi;
169 this->public.set_initiator_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi;
170 this->public.get_responder_spi = (u_int64_t(*)(ike_sa_id_t*)) get_responder_spi;
171 this->public.get_initiator_spi = (u_int64_t(*)(ike_sa_id_t*)) get_initiator_spi;
172 this->public.equals = (bool(*)(ike_sa_id_t*,ike_sa_id_t*)) equals;
173 this->public.replace_values = (void(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values;
174 this->public.is_initiator = (bool(*)(ike_sa_id_t*)) is_initiator;
175 this->public.switch_initiator = (bool(*)(ike_sa_id_t*)) switch_initiator;
176 this->public.clone = (ike_sa_id_t*(*)(ike_sa_id_t*)) clone;
177 this->public.destroy = (void(*)(ike_sa_id_t*))destroy;
178
179 /* private data */
180 this->initiator_spi = initiator_spi;
181 this->responder_spi = responder_spi;
182 this->is_initiator_flag = is_initiator_flag;
183
184 return (&this->public);
185 }