../svn-commit.tmp
[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 #include <stdlib.h>
24 #include <string.h>
25
26 #include "ike_sa_id.h"
27
28 #include <types.h>
29 #include <utils/allocator.h>
30
31 typedef struct private_ike_sa_id_t private_ike_sa_id_t;
32
33 /**
34 * Private data of an ike_sa_id object
35 */
36 struct private_ike_sa_id_t {
37 /**
38 * Public part of a ike_sa_id object
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 /**
60 * implements ike_sa_id_t.set_responder_spi.
61 */
62 static void set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi)
63 {
64 this->responder_spi = responder_spi;
65 }
66
67 /**
68 * implements ike_sa_id_t.set_initiator_spi.
69 */
70 static void set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi)
71 {
72 this->initiator_spi = initiator_spi;
73 }
74
75 /**
76 * implements ike_sa_id_t.get_initiator_spi.
77 */
78 static u_int64_t get_initiator_spi (private_ike_sa_id_t *this)
79 {
80 return this->initiator_spi;
81 }
82
83 /**
84 * implements ike_sa_id_t.get_responder_spi.
85 */
86 static u_int64_t get_responder_spi (private_ike_sa_id_t *this)
87 {
88 return this->responder_spi;
89 }
90
91 /**
92 * implements ike_sa_id_t.get_responder_spi.
93 */
94 static bool equals (private_ike_sa_id_t *this, private_ike_sa_id_t *other)
95 {
96 if (other == NULL)
97 {
98 return FALSE;
99 }
100 if ((this->is_initiator_flag == other->is_initiator_flag) &&
101 (this->initiator_spi == other->initiator_spi) &&
102 (this->responder_spi == other->responder_spi))
103 {
104 /* private_ike_sa_id's are equal */
105 return TRUE;
106 }
107 else
108 {
109 /* private_ike_sa_id's are not equal */
110 return FALSE;
111 }
112 }
113
114 /**
115 * implements ike_sa_id_t.replace_values.
116 */
117 static void replace_values(private_ike_sa_id_t *this, private_ike_sa_id_t *other)
118 {
119 this->initiator_spi = other->initiator_spi;
120 this->responder_spi = other->responder_spi;
121 this->is_initiator_flag = other->is_initiator_flag;
122 }
123
124 /**
125 * implements ike_sa_id_t.is_initiator.
126 */
127 static bool is_initiator(private_ike_sa_id_t *this)
128 {
129 return this->is_initiator_flag;
130 }
131
132 /**
133 * implements ike_sa_id_t.switch_initiator.
134 */
135 static bool switch_initiator(private_ike_sa_id_t *this)
136 {
137 if (this->is_initiator_flag)
138 {
139 this->is_initiator_flag = FALSE;
140 }
141 else
142 {
143 this->is_initiator_flag = TRUE;
144 }
145 return this->is_initiator_flag;
146 }
147
148 /**
149 * implements ike_sa_id_t.clone.
150 */
151 static ike_sa_id_t* clone(private_ike_sa_id_t *this)
152 {
153 return ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator_flag);
154 }
155
156 /**
157 * implements ike_sa_id_t.clone.
158 */
159 static void destroy(private_ike_sa_id_t *this)
160 {
161 allocator_free(this);
162 }
163
164 /*
165 * Described in Header-File
166 */
167 ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiator_flag)
168 {
169 private_ike_sa_id_t *this = allocator_alloc_thing(private_ike_sa_id_t);
170
171 /* Public functions */
172 this->public.set_responder_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi;
173 this->public.set_initiator_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi;
174 this->public.get_responder_spi = (u_int64_t(*)(ike_sa_id_t*)) get_responder_spi;
175 this->public.get_initiator_spi = (u_int64_t(*)(ike_sa_id_t*)) get_initiator_spi;
176 this->public.equals = (bool(*)(ike_sa_id_t*,ike_sa_id_t*)) equals;
177 this->public.replace_values = (void(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values;
178
179 this->public.is_initiator = (bool(*)(ike_sa_id_t*)) is_initiator;
180 this->public.switch_initiator = (bool(*)(ike_sa_id_t*)) switch_initiator;
181
182 this->public.clone = (ike_sa_id_t*(*)(ike_sa_id_t*)) clone;
183 this->public.destroy = (void(*)(ike_sa_id_t*))destroy;
184
185 /* private data */
186 this->initiator_spi = initiator_spi;
187 this->responder_spi = responder_spi;
188 this->is_initiator_flag = is_initiator_flag;
189
190 return (&this->public);
191 }