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