- fixed sizeof bug
[strongswan.git] / Source / charon / ike_sa_id.c
1 /**
2 * @file ike_sa_id.c
3 *
4 * @brief Class for identification of an IKE_SA
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 /**
32 * Private data of an ike_sa_id object
33 */
34 typedef struct private_ike_sa_id_s private_ike_sa_id_t;
35
36 struct private_ike_sa_id_s {
37 /**
38 * Public part of a ike_sa_id object
39 */
40 ike_sa_id_t public;
41
42
43 /* Private values */
44
45 /**
46 * SPI of Initiator
47 */
48 u_int64_t initiator_spi;
49
50 /**
51 * SPI of Responder
52 */
53 u_int64_t responder_spi;
54
55 /**
56 * Role for specific IKE_SA
57 */
58 bool is_initiator;
59
60 };
61
62
63 /**
64 * @brief implements function set_responder_spi of ike_sa_id_t
65 */
66 static status_t set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi)
67 {
68 this->responder_spi = responder_spi;
69 return SUCCESS;
70 }
71
72 static status_t set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi)
73 {
74 this->initiator_spi = initiator_spi;
75 return SUCCESS;
76 }
77
78 /**
79 * @brief implements function initiator_spi_is_set of ike_sa_id_t
80 */
81 static bool initiator_spi_is_set (private_ike_sa_id_t *this)
82 {
83 return (this->initiator_spi != 0);
84 }
85
86 /**
87 * @brief implements function responder_spi_is_set of ike_sa_id_t
88 */
89 static bool responder_spi_is_set (private_ike_sa_id_t *this)
90 {
91 return (this->responder_spi != 0);
92 }
93
94 /**
95 * @brief implements function equals of ike_sa_id_t
96 */
97 static status_t equals (private_ike_sa_id_t *this,private_ike_sa_id_t *other, bool *are_equal)
98 {
99 if ((this == NULL)||(other == NULL))
100 {
101 return FAILED;
102 }
103 if ((this->is_initiator == other->is_initiator) &&
104 (this->initiator_spi == other->initiator_spi) &&
105 (this->responder_spi == other->responder_spi))
106
107 {
108 /* private_ike_sa_id's are equal */
109 *are_equal = TRUE;
110 }
111 else
112 {
113 /* private_ike_sa_id's are not equal */
114 *are_equal = FALSE;
115 }
116
117 return SUCCESS;
118 }
119
120 /**
121 * @brief implements function replace_values of ike_sa_id_t
122 */
123 status_t replace_values (private_ike_sa_id_t *this, private_ike_sa_id_t *other)
124 {
125 if ((this == NULL) || (other == NULL))
126 {
127 return FAILED;
128 }
129
130 this->initiator_spi = other->initiator_spi;
131 this->responder_spi = other->responder_spi;
132 this->is_initiator = other->is_initiator;
133
134 return SUCCESS;
135 }
136
137 /**
138 * @brief implements function ike_sa_id_t.get_values
139 */
140 static status_t get_values(private_ike_sa_id_t *this, u_int64_t *initiator, u_int64_t *responder, bool *is_initiator)
141 {
142 memcpy(initiator, &(this->initiator_spi), sizeof(this->initiator_spi));
143 memcpy(responder, &(this->responder_spi), sizeof(this->responder_spi));
144
145 *is_initiator = this->is_initiator;
146
147 return SUCCESS;
148 }
149
150
151 /**
152 * @brief implements function clone of ike_sa_id_t
153 */
154 static status_t clone (private_ike_sa_id_t *this, ike_sa_id_t **clone_of_this)
155 {
156 *clone_of_this = ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator);
157
158 return (*clone_of_this == NULL) ? OUT_OF_RES : SUCCESS;
159 }
160
161 /**
162 * @brief implements function destroy of ike_sa_id_t
163 */
164 static status_t destroy (private_ike_sa_id_t *this)
165 {
166 allocator_free(this);
167 return SUCCESS;
168 }
169
170 /*
171 * Described in Header-File
172 */
173 ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiator)
174 {
175 private_ike_sa_id_t *this = allocator_alloc_thing(private_ike_sa_id_t);
176 if (this == NULL)
177 {
178 return NULL;
179 }
180
181 /* Public functions */
182 this->public.set_responder_spi = (status_t(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi;
183 this->public.set_initiator_spi = (status_t(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi;
184 this->public.responder_spi_is_set = (bool(*)(ike_sa_id_t*)) responder_spi_is_set;
185 this->public.initiator_spi_is_set = (bool(*)(ike_sa_id_t*)) initiator_spi_is_set;
186 this->public.equals = (status_t(*)(ike_sa_id_t*,ike_sa_id_t*,bool*)) equals;
187 this->public.replace_values = (status_t(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values;
188 this->public.get_values = (status_t(*)(ike_sa_id_t*,u_int64_t*,u_int64_t*,bool*)) get_values;
189 this->public.clone = (status_t(*)(ike_sa_id_t*,ike_sa_id_t**)) clone;
190 this->public.destroy = (status_t(*)(ike_sa_id_t*))destroy;
191
192 /* private data */
193 this->initiator_spi = initiator_spi;
194 this->responder_spi = responder_spi;
195 this->is_initiator = is_initiator;
196
197 return (&this->public);
198 }