- code cleaned up
[strongswan.git] / Source / 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 #include <utils/allocator.h>
27
28
29 typedef struct private_ike_sa_id_t private_ike_sa_id_t;
30
31 /**
32 * Private data of an ike_sa_id_t object.
33 */
34 struct private_ike_sa_id_t {
35 /**
36 * Public interface of ike_sa_id_t.
37 */
38 ike_sa_id_t public;
39
40 /**
41 * SPI of Initiator.
42 */
43 u_int64_t initiator_spi;
44
45 /**
46 * SPI of Responder.
47 */
48 u_int64_t responder_spi;
49
50 /**
51 * Role for specific IKE_SA.
52 */
53 bool is_initiator_flag;
54 };
55
56 /**
57 * Implementation of ike_sa_id_t.set_responder_spi.
58 */
59 static void set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi)
60 {
61 this->responder_spi = responder_spi;
62 }
63
64 /**
65 * Implementation of ike_sa_id_t.set_initiator_spi.
66 */
67 static void set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi)
68 {
69 this->initiator_spi = initiator_spi;
70 }
71
72 /**
73 * Implementation of ike_sa_id_t.get_initiator_spi.
74 */
75 static u_int64_t get_initiator_spi (private_ike_sa_id_t *this)
76 {
77 return this->initiator_spi;
78 }
79
80 /**
81 * Implementation of ike_sa_id_t.get_responder_spi.
82 */
83 static u_int64_t get_responder_spi (private_ike_sa_id_t *this)
84 {
85 return this->responder_spi;
86 }
87
88 /**
89 * Implementation of ike_sa_id_t.equals.
90 */
91 static bool equals (private_ike_sa_id_t *this, private_ike_sa_id_t *other)
92 {
93 if (other == NULL)
94 {
95 return FALSE;
96 }
97 if ((this->is_initiator_flag == other->is_initiator_flag) &&
98 (this->initiator_spi == other->initiator_spi) &&
99 (this->responder_spi == other->responder_spi))
100 {
101 /* private_ike_sa_id's are equal */
102 return TRUE;
103 }
104 else
105 {
106 /* private_ike_sa_id's are not equal */
107 return FALSE;
108 }
109 }
110
111 /**
112 * Implementation of ike_sa_id_t.replace_values.
113 */
114 static void replace_values(private_ike_sa_id_t *this, private_ike_sa_id_t *other)
115 {
116 this->initiator_spi = other->initiator_spi;
117 this->responder_spi = other->responder_spi;
118 this->is_initiator_flag = other->is_initiator_flag;
119 }
120
121 /**
122 * Implementation of ike_sa_id_t.is_initiator.
123 */
124 static bool is_initiator(private_ike_sa_id_t *this)
125 {
126 return this->is_initiator_flag;
127 }
128
129 /**
130 * Implementation of ike_sa_id_t.switch_initiator.
131 */
132 static bool switch_initiator(private_ike_sa_id_t *this)
133 {
134 if (this->is_initiator_flag)
135 {
136 this->is_initiator_flag = FALSE;
137 }
138 else
139 {
140 this->is_initiator_flag = TRUE;
141 }
142 return this->is_initiator_flag;
143 }
144
145 /**
146 * Implementation of ike_sa_id_t.clone.
147 */
148 static ike_sa_id_t* clone(private_ike_sa_id_t *this)
149 {
150 return ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator_flag);
151 }
152
153 /**
154 * Implementation of ike_sa_id_t.destroy.
155 */
156 static void destroy(private_ike_sa_id_t *this)
157 {
158 allocator_free(this);
159 }
160
161 /*
162 * Described in header.
163 */
164 ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiator_flag)
165 {
166 private_ike_sa_id_t *this = allocator_alloc_thing(private_ike_sa_id_t);
167
168 /* public functions */
169 this->public.set_responder_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi;
170 this->public.set_initiator_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi;
171 this->public.get_responder_spi = (u_int64_t(*)(ike_sa_id_t*)) get_responder_spi;
172 this->public.get_initiator_spi = (u_int64_t(*)(ike_sa_id_t*)) get_initiator_spi;
173 this->public.equals = (bool(*)(ike_sa_id_t*,ike_sa_id_t*)) equals;
174 this->public.replace_values = (void(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values;
175 this->public.is_initiator = (bool(*)(ike_sa_id_t*)) is_initiator;
176 this->public.switch_initiator = (bool(*)(ike_sa_id_t*)) switch_initiator;
177 this->public.clone = (ike_sa_id_t*(*)(ike_sa_id_t*)) clone;
178 this->public.destroy = (void(*)(ike_sa_id_t*))destroy;
179
180 /* private data */
181 this->initiator_spi = initiator_spi;
182 this->responder_spi = responder_spi;
183 this->is_initiator_flag = is_initiator_flag;
184
185 return (&this->public);
186 }